From 89ba8a1a0c2631a9eca48557a3a62ba6d35cf785 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Tue, 19 Mar 2024 00:22:41 +0800 Subject: [PATCH] chore: update cloud dependency --- packages/console/package.json | 2 +- packages/core/src/oidc/init.ts | 42 ++++++++++++++++++---------------- pnpm-lock.yaml | 4 ++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/console/package.json b/packages/console/package.json index 9664676ab..46bdd4dbe 100644 --- a/packages/console/package.json +++ b/packages/console/package.json @@ -28,7 +28,7 @@ "@fontsource/roboto-mono": "^5.0.0", "@jest/types": "^29.5.0", "@logto/app-insights": "workspace:^1.4.0", - "@logto/cloud": "0.2.5-d9576f9", + "@logto/cloud": "0.2.5-ceb63ed", "@logto/connector-kit": "workspace:^2.1.0", "@logto/core-kit": "workspace:^2.3.0", "@logto/language-kit": "workspace:^1.1.0", diff --git a/packages/core/src/oidc/init.ts b/packages/core/src/oidc/init.ts index ca0b950c5..08e23c25e 100644 --- a/packages/core/src/oidc/init.ts +++ b/packages/core/src/oidc/init.ts @@ -14,10 +14,9 @@ import { logtoCookieKey, type LogtoUiCookie, LogtoJwtTokenKey, - type JsonObject, + type CustomJwtFetcher, } from '@logto/schemas'; import { conditional, trySafe, tryThat } from '@silverhand/essentials'; -import { got } from 'got'; import i18next from 'i18next'; import koaBody from 'koa-body'; import Provider, { errors } from 'oidc-provider'; @@ -239,8 +238,7 @@ export default function initOidc( if (script) { // Wait for cloud API to be ready and we can use cloud connection client to request the API. - const accessToken = await cloudConnection.getAccessToken(); - const { endpoint: cloudApiEndpoint } = await cloudConnection.getCloudConnectionData(); + const client = await cloudConnection.getClient(); // We pass context to the cloud API only when it is a user's access token. const logtoUserInfo = conditional( @@ -248,23 +246,27 @@ export default function initOidc( token.accountId && (await libraries.jwtCustomizers.getUserContext(token.accountId)) ); - const result = + /** + * `token` and `context` can not be assigned to Record according to the type inference, + * use request body guard to ensure the type. + * + * Use direct type casting to avoid the type inference issue since if the type is not correct the client + * will throw an Zod type error, there is no need to implement the zod guard and error handling here. + */ + // eslint-disable-next-line no-restricted-syntax + const payload = { + script, + envVars, + token, + context: conditional(logtoUserInfo && { user: logtoUserInfo }), + } as unknown as CustomJwtFetcher; + return ( (await trySafe( - got - .post(`${cloudApiEndpoint}/services/custom-jwt`, { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - json: { - script, - envVars, - token, - ...conditional(logtoUserInfo && { context: { user: logtoUserInfo } }), - }, - }) - .json() - )) ?? {}; - return result; + client.post(`/api/services/custom-jwt`, { + body: payload, + }) + )) ?? {} + ); } }, extraClientMetadata: { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e741117b0..355a80889 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2919,8 +2919,8 @@ importers: specifier: workspace:^1.4.0 version: link:../app-insights '@logto/cloud': - specifier: 0.2.5-d9576f9 - version: 0.2.5-d9576f9(zod@3.22.4) + specifier: 0.2.5-ceb63ed + version: 0.2.5-ceb63ed(zod@3.22.4) '@logto/connector-kit': specifier: workspace:^2.1.0 version: link:../toolkit/connector-kit