diff --git a/packages/core/src/env-set/index.ts b/packages/core/src/env-set/index.ts index da291b052..d01e09a1c 100644 --- a/packages/core/src/env-set/index.ts +++ b/packages/core/src/env-set/index.ts @@ -5,7 +5,7 @@ import type { PostgreSql } from '@withtyped/postgres'; import type { QueryClient } from '@withtyped/server'; import type { DatabasePool } from 'slonik'; -import { getOidcConfigs } from '#src/libraries/logto-config.js'; +import { createLogtoConfigLibrary } from '#src/libraries/logto-config.js'; import { appendPath } from '#src/utils/url.js'; import { checkAlterationState } from './check-alteration-state.js'; @@ -108,7 +108,8 @@ class EnvSet { this.#pool = pool; this.#queryClient = createQueryClient(this.databaseUrl, this.isTest); - const [, oidcConfigs] = await Promise.all([checkAlterationState(pool), getOidcConfigs(pool)]); + const { getOidcConfigs } = createLogtoConfigLibrary(pool); + const [, oidcConfigs] = await Promise.all([checkAlterationState(pool), getOidcConfigs()]); this.#oidc = await loadOidcValues( appendPath(this.values.endpoint, '/oidc').toString(), oidcConfigs diff --git a/packages/core/src/libraries/__mocks__/logto-config.ts b/packages/core/src/libraries/__mocks__/logto-config.ts index e180d0440..9000aee9c 100644 --- a/packages/core/src/libraries/__mocks__/logto-config.ts +++ b/packages/core/src/libraries/__mocks__/logto-config.ts @@ -15,8 +15,10 @@ const { privateKey } = generateKeyPairSync('rsa', { }, }); -export const getOidcConfigs = async (): Promise => ({ +const getOidcConfigs = async (): Promise => ({ [LogtoOidcConfigKey.PrivateKeys]: [privateKey], [LogtoOidcConfigKey.CookieKeys]: ['LOGTOSEKRIT1'], [LogtoOidcConfigKey.RefreshTokenReuseInterval]: 3, }); + +export const createLogtoConfigLibrary = () => ({ getOidcConfigs }); diff --git a/packages/core/src/libraries/logto-config.ts b/packages/core/src/libraries/logto-config.ts index 70dcf56b9..cd56ccfa7 100644 --- a/packages/core/src/libraries/logto-config.ts +++ b/packages/core/src/libraries/logto-config.ts @@ -5,30 +5,34 @@ import chalk from 'chalk'; import type { CommonQueryMethods } from 'slonik'; import { z, ZodError } from 'zod'; -export const getOidcConfigs = async (pool: CommonQueryMethods): Promise => { - try { - const { rows } = await getRowsByKeys(pool, Object.values(LogtoOidcConfigKey)); +export const createLogtoConfigLibrary = (pool: CommonQueryMethods) => { + const getOidcConfigs = async (): Promise => { + try { + const { rows } = await getRowsByKeys(pool, Object.values(LogtoOidcConfigKey)); + + return z + .object(logtoOidcConfigGuard) + .parse(Object.fromEntries(rows.map(({ key, value }) => [key, value]))); + } catch (error: unknown) { + if (error instanceof ZodError) { + console.error( + error.issues + .map(({ message, path }) => `${message} at ${chalk.green(path.join('.'))}`) + .join('\n') + ); + } else { + console.error(error); + } - return z - .object(logtoOidcConfigGuard) - .parse(Object.fromEntries(rows.map(({ key, value }) => [key, value]))); - } catch (error: unknown) { - if (error instanceof ZodError) { console.error( - error.issues - .map(({ message, path }) => `${message} at ${chalk.green(path.join('.'))}`) - .join('\n') + `\n${chalk.red('[error]')} Failed to get OIDC configs from your Logto database.` + + ' Did you forget to seed your database?\n\n' + + ` Use ${chalk.green('npm run cli db seed')} to seed your Logto database;\n` + + ` Or use ${chalk.green('npm run cli db seed oidc')} to seed OIDC configs alone.\n` ); - } else { - console.error(error); + throw new Error('Failed to get configs'); } + }; - console.error( - `\n${chalk.red('[error]')} Failed to get OIDC configs from your Logto database.` + - ' Did you forget to seed your database?\n\n' + - ` Use ${chalk.green('npm run cli db seed')} to seed your Logto database;\n` + - ` Or use ${chalk.green('npm run cli db seed oidc')} to seed OIDC configs alone.\n` - ); - throw new Error('Failed to get configs'); - } + return { getOidcConfigs }; };