From fb611303b6ea59f9a76581a882942582f2c12c6e Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Sun, 20 Feb 2022 23:37:13 +0100 Subject: [PATCH] feat(api): :arrow_up: Use Toggl RequestUrl API when available. --- .vscode/settings.json | 3 ++- lib/toggl/ApiManager.ts | 8 +++++++- lib/toggl/toggl-client | 1 + lib/util/checkVersion.spec.ts | 31 +++++++++++++++++++++++++++++++ lib/util/checkVersion.ts | 23 +++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 7 files changed, 67 insertions(+), 5 deletions(-) create mode 160000 lib/toggl/toggl-client create mode 100644 lib/util/checkVersion.spec.ts create mode 100644 lib/util/checkVersion.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 9b206c5..2334280 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,7 @@ 80 ], "conventionalCommits.scopes": [ - "reports" + "reports", + "api" ] } \ No newline at end of file diff --git a/lib/toggl/ApiManager.ts b/lib/toggl/ApiManager.ts index ef7d30a..fa30e07 100644 --- a/lib/toggl/ApiManager.ts +++ b/lib/toggl/ApiManager.ts @@ -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 = { @@ -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 { diff --git a/lib/toggl/toggl-client b/lib/toggl/toggl-client new file mode 160000 index 0000000..0f5a960 --- /dev/null +++ b/lib/toggl/toggl-client @@ -0,0 +1 @@ +Subproject commit 0f5a9604aba06e7bb4428248f95b636ef796dae6 diff --git a/lib/util/checkVersion.spec.ts b/lib/util/checkVersion.spec.ts new file mode 100644 index 0000000..1bbda0e --- /dev/null +++ b/lib/util/checkVersion.spec.ts @@ -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); + }); +}); diff --git a/lib/util/checkVersion.ts b/lib/util/checkVersion.ts new file mode 100644 index 0000000..f1f0ed5 --- /dev/null +++ b/lib/util/checkVersion.ts @@ -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; +} diff --git a/package-lock.json b/package-lock.json index 6dfc269..c5a28c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "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" }, "devDependencies": { "@rollup/plugin-commonjs": "^18.0.0", @@ -11618,7 +11618,7 @@ "toggl-client": { "version": "git+ssh://git@github.com/mcndt/toggl-client.git#0f5a9604aba06e7bb4428248f95b636ef796dae6", "integrity": "sha512-klm9feL8YC+3aoNVFQnvizI1+OsnT/3fsDKo66ueWtSRYY50ZL54Kmezm4c9IAGiqnCvevuMr3zUoGokBZE1Wg==", - "from": "toggl-client@git://github.com/mcndt/toggl-client.git", + "from": "toggl-client@git://github.com/mcndt/toggl-client.git#obsidian-request-api", "requires": { "debug": "^4.3.1", "got": "^11.8.2" diff --git a/package.json b/package.json index c1d8e9e..6afac43 100644 --- a/package.json +++ b/package.json @@ -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" } }