Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from MahjongPantheon/release-1.0
Browse files Browse the repository at this point in the history
Add some versioning tools
  • Loading branch information
Oleg Klimenko authored Apr 5, 2017
2 parents b5a382b + e64bef4 commit 8f9b0ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tyr",
"version": "0.2.0",
"version": "1.0.0",
"license": "GPL v3",
"angular-cli": {},
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions src/app/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
// Where you want Tyr to store his data
'apiUrl': 'https://api.furiten.ru/',

// Do not change this unless you really know what are you doing
'apiVersion': [1, 0]
};
22 changes: 18 additions & 4 deletions src/app/services/riichiApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ import {
} from './formatters';
import { AppState } from '../primitives/appstate';
import 'rxjs/add/operator/toPromise';

const API_URL = 'https://api.furiten.ru/';
import config from '../config';

@Injectable()
export class RiichiApiService {
Expand Down Expand Up @@ -124,7 +123,8 @@ export class RiichiApiService {
private _jsonRpcRequest<RET_TYPE>(methodName: string, ...params: any[]): Promise<RET_TYPE> {
const commonHeaders = new Headers({
'Content-type': 'application/json',
'X-Auth-Token': this._authToken
'X-Auth-Token': this._authToken,
'X-Api-Version': config.apiVersion.map((v) => v.toString()).join('.')
});
const jsonRpcBody = {
jsonrpc: "2.0",
Expand All @@ -134,9 +134,10 @@ export class RiichiApiService {
};

return this.http
.post(API_URL, jsonRpcBody, { headers: commonHeaders })
.post(config.apiUrl, jsonRpcBody, { headers: commonHeaders })
.toPromise()
.then<RET_TYPE>((response) => {
this._checkCompatibility(response.headers.get('X-Api-Headers'));
const json = response.json();
if (json.error) {
if (isDevMode()) {
Expand All @@ -148,4 +149,17 @@ export class RiichiApiService {
return json.result; // TODO: runtime checks of object structure
});
}

private _checkCompatibility(versionString) {
const [major, minor] = versionString.split('.').map((v) => parseInt(v, 10));
const [localMajor, localMinor] = config.apiVersion;
if (major !== localMajor) {
console.error('API major version mismatch. Update your app or API instance!');
throw new Error('Critical: API major version mismatch');
}

if (minor > localMinor && isDevMode()) {
console.warn('API minor version mismatch. Consider updating if possible');
}
}
}

0 comments on commit 8f9b0ee

Please sign in to comment.