diff --git a/packages/core/package.json b/packages/core/package.json index 33bf888df..7ad84e55e 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -41,7 +41,7 @@ "@logto/shared": "workspace:^2.0.0", "@logto/ui": "workspace:*", "@silverhand/essentials": "^2.5.0", - "@withtyped/client": "^0.7.19", + "@withtyped/client": "^0.7.21", "chalk": "^5.0.0", "clean-deep": "^3.4.0", "date-fns": "^2.29.3", @@ -81,7 +81,7 @@ "zod": "^3.20.2" }, "devDependencies": { - "@logto/cloud": "0.2.5-4d5e389", + "@logto/cloud": "0.2.5-33a6965", "@silverhand/eslint-config": "4.0.1", "@silverhand/ts-config": "4.0.0", "@types/debug": "^4.1.7", diff --git a/packages/core/src/libraries/cloud-connection.ts b/packages/core/src/libraries/cloud-connection.ts index 20238d739..966ef0d39 100644 --- a/packages/core/src/libraries/cloud-connection.ts +++ b/packages/core/src/libraries/cloud-connection.ts @@ -27,6 +27,7 @@ const accessTokenResponseGuard = z.object({ const scopes: string[] = []; const accessTokenExpirationMargin = 60; +/** The library for connecting to Logto Cloud service. */ export class CloudConnectionLibrary { private client?: Client; private accessTokenCache?: { expiresAt: number; accessToken: string }; @@ -44,6 +45,16 @@ export class CloudConnectionLibrary { }; }; + /** + * Get the access token for the Cloud service in the following steps: + * + * 1. If the access token is cached and not expired, return it. + * 2. Otherwise, get a new access token from the Cloud service via client + * credentials flow and cache it. + * 3. If the request fails, throw an error. + * + * @returns The access token for the Cloud service. + */ public getAccessToken = async (): Promise => { if (this.accessTokenCache) { const { expiresAt, accessToken } = this.accessTokenCache; @@ -82,6 +93,10 @@ export class CloudConnectionLibrary { return result.data.access_token; }; + /** + * Get a withtyped client for the Cloud service. It is typed with the router + * defined in @logto/cloud/routes. + */ public getClient = async (): Promise> => { if (!this.client) { const { endpoint } = await this.getCloudConnectionData(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 86242c7b8..19291346f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1587,7 +1587,7 @@ importers: version: 8.44.0 jest: specifier: ^29.5.0 - version: 29.5.0(@types/node@18.11.18) + version: 29.5.0(@types/node@18.11.18)(ts-node@10.9.1) jest-matcher-specific-error: specifier: ^1.0.0 version: 1.0.0 @@ -3161,8 +3161,8 @@ importers: specifier: ^2.5.0 version: 2.5.0 '@withtyped/client': - specifier: ^0.7.19 - version: 0.7.19(zod@3.20.2) + specifier: ^0.7.21 + version: 0.7.21(zod@3.20.2) chalk: specifier: ^5.0.0 version: 5.1.2 @@ -3276,8 +3276,8 @@ importers: version: 3.20.2 devDependencies: '@logto/cloud': - specifier: 0.2.5-4d5e389 - version: 0.2.5-4d5e389(zod@3.20.2) + specifier: 0.2.5-33a6965 + version: 0.2.5-33a6965(zod@3.20.2) '@silverhand/eslint-config': specifier: 4.0.1 version: 4.0.1(eslint@8.44.0)(prettier@3.0.0)(typescript@5.0.2) @@ -6873,48 +6873,6 @@ packages: slash: 3.0.0 dev: true - /@jest/core@29.5.0: - resolution: {integrity: sha512-28UzQc7ulUrOQw1IsN/kv1QES3q2kkbl/wGslyhAclqZ/8cMdB5M68BffkIdSJgKBUt50d3hbwJ92XESlE7LiQ==} - engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} - peerDependencies: - node-notifier: ^8.0.1 || ^9.0.0 || ^10.0.0 - peerDependenciesMeta: - node-notifier: - optional: true - dependencies: - '@jest/console': 29.5.0 - '@jest/reporters': 29.5.0 - '@jest/test-result': 29.5.0 - '@jest/transform': 29.5.0 - '@jest/types': 29.5.0 - '@types/node': 18.11.18 - ansi-escapes: 4.3.2 - chalk: 4.1.2 - ci-info: 3.8.0 - exit: 0.1.2 - graceful-fs: 4.2.11 - jest-changed-files: 29.5.0 - jest-config: 29.5.0(@types/node@18.11.18) - jest-haste-map: 29.5.0 - jest-message-util: 29.5.0 - jest-regex-util: 29.4.3 - jest-resolve: 29.5.0 - jest-resolve-dependencies: 29.5.0 - jest-runner: 29.5.0 - jest-runtime: 29.5.0 - jest-snapshot: 29.5.0 - jest-util: 29.5.0 - jest-validate: 29.5.0 - jest-watcher: 29.5.0 - micromatch: 4.0.5 - pretty-format: 29.5.0 - slash: 3.0.0 - strip-ansi: 6.0.1 - transitivePeerDependencies: - - supports-color - - ts-node - dev: true - /@jest/core@29.5.0(ts-node@10.9.1): resolution: {integrity: sha512-28UzQc7ulUrOQw1IsN/kv1QES3q2kkbl/wGslyhAclqZ/8cMdB5M68BffkIdSJgKBUt50d3hbwJ92XESlE7LiQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -7359,12 +7317,12 @@ packages: - zod dev: true - /@logto/cloud@0.2.5-4d5e389(zod@3.20.2): - resolution: {integrity: sha512-vRJZGc0WvjE1rFJ0DNLaOHkhpe4TMdui/pvcTwGb/bDKzw/NM+4HtUoZj1a1DZ8Qqn24ex1WkTjxY8XGd3EruQ==} + /@logto/cloud@0.2.5-33a6965(zod@3.20.2): + resolution: {integrity: sha512-Jawe/nWJmjBDDhNfcxOsQfWsLaUifV5az1mNwn23xwVM/6L/YAkwOj+dWi12lqQ+Vk6MLUPzycXvZLV28tVxQg==} engines: {node: ^18.12.0} dependencies: '@silverhand/essentials': 2.7.0 - '@withtyped/server': 0.12.7(zod@3.20.2) + '@withtyped/server': 0.12.8(zod@3.20.2) transitivePeerDependencies: - zod dev: true @@ -10074,15 +10032,6 @@ packages: eslint-visitor-keys: 3.4.1 dev: true - /@withtyped/client@0.7.19(zod@3.20.2): - resolution: {integrity: sha512-cHmqCIsEMonrE9kDbu8YAPWWPHmy6imUGbUWQ2PGaPLTJagbgH3ABsxgnLiM0lm0LeuZFd3BHh+JYKRDUmS8cw==} - dependencies: - '@withtyped/server': 0.12.7(zod@3.20.2) - '@withtyped/shared': 0.2.2 - transitivePeerDependencies: - - zod - dev: false - /@withtyped/client@0.7.21(zod@3.20.2): resolution: {integrity: sha512-N9dvH5nqIwaT7YxaIm83RRQf9AEjxwJ4ugJviZJSxtWy8zLul2/odEMc6epieylFVa6CcLg82yJmRSlqPtJiTw==} dependencies: @@ -10090,16 +10039,6 @@ packages: '@withtyped/shared': 0.2.2 transitivePeerDependencies: - zod - dev: true - - /@withtyped/server@0.12.7(zod@3.20.2): - resolution: {integrity: sha512-NNT78ZZmSZiEosxI3iW/kVx1KEG5vetvpEXNl0Gy58OlOnI8l/7h8Q//JZJ268xWOKyaNI4KrngTRtL5uvZu9Q==} - peerDependencies: - zod: ^3.19.1 - dependencies: - '@silverhand/essentials': 2.7.0 - '@withtyped/shared': 0.2.2 - zod: 3.20.2 /@withtyped/server@0.12.8(zod@3.20.2): resolution: {integrity: sha512-fv9feTOKJhtlaoYM/Kbs2gSTcIXlmu4OMUFwGmK5jqdbVNIOkDBIPxtcC5ZEwevWFgOcd5OqBW+FvbjiaF27Fw==} @@ -11564,17 +11503,6 @@ packages: /dayjs@1.11.6: resolution: {integrity: sha512-zZbY5giJAinCG+7AGaw0wIhNZ6J8AhWuSXKvuc1KAyMiRsvGQWqh4L+MomvhdAYjN+lqvVCMq1I41e3YHvXkyQ==} - /debug@3.2.7: - resolution: {integrity: sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==} - peerDependencies: - supports-color: '*' - peerDependenciesMeta: - supports-color: - optional: true - dependencies: - ms: 2.1.3 - dev: true - /debug@3.2.7(supports-color@5.5.0): resolution: {integrity: sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==} peerDependencies: @@ -12126,7 +12054,7 @@ packages: /eslint-import-resolver-node@0.3.7: resolution: {integrity: sha512-gozW2blMLJCeFpBwugLTGyvVjNoeo1knonXAcatC6bjPBZitotxdWf7Gimr25N4c0AAOo4eOUfaG82IJPDpqCA==} dependencies: - debug: 3.2.7 + debug: 3.2.7(supports-color@5.5.0) is-core-module: 2.12.1 resolve: 1.22.2 transitivePeerDependencies: @@ -12179,7 +12107,7 @@ packages: optional: true dependencies: '@typescript-eslint/parser': 5.61.0(eslint@8.44.0)(typescript@5.0.2) - debug: 3.2.7 + debug: 3.2.7(supports-color@5.5.0) eslint: 8.44.0 eslint-import-resolver-node: 0.3.7 eslint-import-resolver-typescript: 3.5.5(@typescript-eslint/parser@5.61.0)(eslint-plugin-import@2.27.5)(eslint@8.44.0) @@ -12231,7 +12159,7 @@ packages: array-includes: 3.1.6 array.prototype.flat: 1.3.1 array.prototype.flatmap: 1.3.1 - debug: 3.2.7 + debug: 3.2.7(supports-color@5.5.0) doctrine: 2.1.0 eslint: 8.44.0 eslint-import-resolver-node: 0.3.7 @@ -14267,34 +14195,6 @@ packages: - supports-color dev: true - /jest-cli@29.5.0(@types/node@18.11.18): - resolution: {integrity: sha512-L1KcP1l4HtfwdxXNFCL5bmUbLQiKrakMUriBEcc1Vfz6gx31ORKdreuWvmQVBit+1ss9NNR3yxjwfwzZNdQXJw==} - engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} - hasBin: true - peerDependencies: - node-notifier: ^8.0.1 || ^9.0.0 || ^10.0.0 - peerDependenciesMeta: - node-notifier: - optional: true - dependencies: - '@jest/core': 29.5.0 - '@jest/test-result': 29.5.0 - '@jest/types': 29.5.0 - chalk: 4.1.2 - exit: 0.1.2 - graceful-fs: 4.2.11 - import-local: 3.1.0 - jest-config: 29.5.0(@types/node@18.11.18) - jest-util: 29.5.0 - jest-validate: 29.5.0 - prompts: 2.4.2 - yargs: 17.7.2 - transitivePeerDependencies: - - '@types/node' - - supports-color - - ts-node - dev: true - /jest-cli@29.5.0(@types/node@18.11.18)(ts-node@10.9.1): resolution: {integrity: sha512-L1KcP1l4HtfwdxXNFCL5bmUbLQiKrakMUriBEcc1Vfz6gx31ORKdreuWvmQVBit+1ss9NNR3yxjwfwzZNdQXJw==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -14323,45 +14223,6 @@ packages: - ts-node dev: true - /jest-config@29.5.0(@types/node@18.11.18): - resolution: {integrity: sha512-kvDUKBnNJPNBmFFOhDbm59iu1Fii1Q6SxyhXfvylq3UTHbg6o7j/g8k2dZyXWLvfdKB1vAPxNZnMgtKJcmu3kA==} - engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} - peerDependencies: - '@types/node': '*' - ts-node: '>=9.0.0' - peerDependenciesMeta: - '@types/node': - optional: true - ts-node: - optional: true - dependencies: - '@babel/core': 7.20.2 - '@jest/test-sequencer': 29.5.0 - '@jest/types': 29.5.0 - '@types/node': 18.11.18 - babel-jest: 29.5.0(@babel/core@7.20.2) - chalk: 4.1.2 - ci-info: 3.8.0 - deepmerge: 4.3.1 - glob: 7.2.3 - graceful-fs: 4.2.11 - jest-circus: 29.5.0 - jest-environment-node: 29.5.0 - jest-get-type: 29.4.3 - jest-regex-util: 29.4.3 - jest-resolve: 29.5.0 - jest-runner: 29.5.0 - jest-util: 29.5.0 - jest-validate: 29.5.0 - micromatch: 4.0.5 - parse-json: 5.2.0 - pretty-format: 29.5.0 - slash: 3.0.0 - strip-json-comments: 3.1.1 - transitivePeerDependencies: - - supports-color - dev: true - /jest-config@29.5.0(@types/node@18.11.18)(ts-node@10.9.1): resolution: {integrity: sha512-kvDUKBnNJPNBmFFOhDbm59iu1Fii1Q6SxyhXfvylq3UTHbg6o7j/g8k2dZyXWLvfdKB1vAPxNZnMgtKJcmu3kA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -14820,26 +14681,6 @@ packages: supports-color: 8.1.1 dev: true - /jest@29.5.0(@types/node@18.11.18): - resolution: {integrity: sha512-juMg3he2uru1QoXX078zTa7pO85QyB9xajZc6bU+d9yEGwrKX6+vGmJQ3UdVZsvTEUARIdObzH68QItim6OSSQ==} - engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} - hasBin: true - peerDependencies: - node-notifier: ^8.0.1 || ^9.0.0 || ^10.0.0 - peerDependenciesMeta: - node-notifier: - optional: true - dependencies: - '@jest/core': 29.5.0 - '@jest/types': 29.5.0 - import-local: 3.1.0 - jest-cli: 29.5.0(@types/node@18.11.18) - transitivePeerDependencies: - - '@types/node' - - supports-color - - ts-node - dev: true - /jest@29.5.0(@types/node@18.11.18)(ts-node@10.9.1): resolution: {integrity: sha512-juMg3he2uru1QoXX078zTa7pO85QyB9xajZc6bU+d9yEGwrKX6+vGmJQ3UdVZsvTEUARIdObzH68QItim6OSSQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}