Skip to content

Commit

Permalink
feat(api): ⬆️ Use Toggl RequestUrl API when available.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcndt committed Feb 20, 2022
1 parent 77a6bb8 commit fb61130
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
80
],
"conventionalCommits.scopes": [
"reports"
"reports",
"api"
]
}
8 changes: 7 additions & 1 deletion lib/toggl/ApiManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { ISODate } from 'lib/reports/ReportQuery';
import { settingsStore } from 'lib/util/stores';
import moment from 'moment';
import TogglClient from 'toggl-client';
import { apiVersion } from 'obsidian';
import { checkVersion } from 'lib/util/checkVersion';

/** http headers used on every call to the Toggl API. */
const headers = {
Expand All @@ -26,7 +28,11 @@ export default class ApiManager {

/** Must be called after constructor and before use of the API. */
public async initialize(apiToken: string) {
this._api = TogglClient({ apiToken, headers });
this._api = TogglClient({
apiToken,
headers,
legacy: !checkVersion(apiVersion, 0, 13, 25)
});
try {
await this.testConnection();
} catch {
Expand Down
1 change: 1 addition & 0 deletions lib/toggl/toggl-client
Submodule toggl-client added at 0f5a96
31 changes: 31 additions & 0 deletions lib/util/checkVersion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { checkVersion } from './checkVersion';

describe('parse', () => {
it('passes on equal version', () => {
expect(checkVersion('1.2.3', 1, 2, 3)).toStrictEqual(true);
});

it('passes on greater patch version', () => {
expect(checkVersion('1.2.4', 1, 2, 3)).toStrictEqual(true);
});

it('passes on greater minor version', () => {
expect(checkVersion('1.3.4', 1, 2, 3)).toStrictEqual(true);
});

it('passes on greater major version', () => {
expect(checkVersion('2.3.4', 1, 2, 3)).toStrictEqual(true);
});

it('fails on lesser major version', () => {
expect(checkVersion('0.2.3', 1, 2, 3)).toStrictEqual(false);
});

it('fails on lesser minor version', () => {
expect(checkVersion('1.1.3', 1, 2, 3)).toStrictEqual(false);
});

it('fails on lesser patch version', () => {
expect(checkVersion('1.2.2', 1, 2, 3)).toStrictEqual(false);
});
});
23 changes: 23 additions & 0 deletions lib/util/checkVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param check version string to check. e.g. "0.13.21"
* @param major Minimum major version
* @param minor Minimum minor version
* @param patch Minimum patch version
* @return true if input string satisfies the major, minor and patch values (larger or equal than)
*/
export function checkVersion(
check: string,
major: number,
minor: number,
patch: number
): boolean {
const [checkMajor, checkMinor, checkPatch] = check.split('.');
if (
parseInt(checkMajor) >= major &&
parseInt(checkMinor) >= minor &&
parseInt(checkPatch) >= patch
) {
return true;
}
return false;
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"moment": "^2.29.1",
"supports-color": "^9.0.2",
"svelte-select": "^4.4.3",
"toggl-client": "git://github.com/mcndt/toggl-client.git"
"toggl-client": "git://github.com/mcndt/toggl-client.git#obsidian-request-api"
}
}

0 comments on commit fb61130

Please sign in to comment.