0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(core): refactor

This commit is contained in:
Darcy Ye 2024-03-06 19:42:10 +08:00
parent e327754008
commit 5d55776d60
No known key found for this signature in database
GPG key ID: B46F4C07EDEFC610
5 changed files with 31 additions and 6 deletions

View file

@ -35,6 +35,7 @@ const logtoConfigs: LogtoConfigLibrary = {
}),
getOidcConfigs: jest.fn(),
upsertJwtCustomizer: jest.fn(),
getJwtCustomizer: jest.fn(),
};
describe('getAccessToken()', () => {

View file

@ -1,4 +1,5 @@
import {
LogtoConfigs,
cloudApiIndicator,
cloudConnectionDataGuard,
logtoOidcConfigGuard,
@ -6,13 +7,16 @@ import {
jwtCustomizerConfigGuard,
} from '@logto/schemas';
import type { LogtoOidcConfigType, LogtoJwtTokenKey } from '@logto/schemas';
import { convertToIdentifiers } from '@logto/shared';
import chalk from 'chalk';
import { z, ZodError } from 'zod';
import RequestError from '#src/errors/RequestError/index.js';
import type Queries from '#src/tenants/Queries.js';
import { consoleLog } from '#src/utils/console.js';
export type LogtoConfigLibrary = ReturnType<typeof createLogtoConfigLibrary>;
const { table } = convertToIdentifiers(LogtoConfigs);
export const createLogtoConfigLibrary = ({
logtoConfigs: {
@ -77,5 +81,21 @@ export const createLogtoConfigLibrary = ({
};
};
return { getOidcConfigs, getCloudConnectionData, upsertJwtCustomizer };
const getJwtCustomizer = async <T extends LogtoJwtTokenKey>(key: T) => {
const { rows } = await getRowsByKeys([key]);
// If the record does not exist (`rows` is empty)
if (rows.length === 0) {
throw new RequestError({
code: 'entity.not_exists',
name: table,
id: key,
status: 404,
});
}
return z.object({ value: jwtCustomizerConfigGuard[key] }).parse(rows[0]);
};
return { getOidcConfigs, getCloudConnectionData, upsertJwtCustomizer, getJwtCustomizer };
};

View file

@ -58,6 +58,7 @@ const cloudConnection = createCloudConnectionLibrary({
}),
getOidcConfigs: jest.fn(),
upsertJwtCustomizer: jest.fn(),
getJwtCustomizer: jest.fn(),
});
const getLogtoConnectors = jest.spyOn(connectorLibrary, 'getLogtoConnectors');

View file

@ -54,7 +54,6 @@ const logtoConfigQueries = {
}),
updateOidcConfigsByKey: jest.fn(),
getRowsByKeys: jest.fn(async () => mockLogtoConfigRows),
getJwtCustomizer: jest.fn(),
};
const logtoConfigLibraries = {
@ -63,6 +62,7 @@ const logtoConfigLibraries = {
[LogtoOidcConfigKey.CookieKeys]: mockCookieKeys,
})),
upsertJwtCustomizer: jest.fn(),
getJwtCustomizer: jest.fn(),
};
const settingRoutes = await pickDefault(import('./logto-config.js'));
@ -268,7 +268,7 @@ describe('configs routes', () => {
});
it('GET /configs/jwt-customizer/:tokenType should return the record', async () => {
logtoConfigQueries.getJwtCustomizer.mockResolvedValueOnce(
logtoConfigLibraries.getJwtCustomizer.mockResolvedValueOnce(
mockJwtCustomizerConfigForAccessToken
);
const response = await routeRequester.get('/configs/jwt-customizer/access-token');

View file

@ -83,7 +83,7 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
) {
const { getAdminConsoleConfig, getRowsByKeys, updateAdminConsoleConfig, updateOidcConfigsByKey } =
queries.logtoConfigs;
const { getOidcConfigs, upsertJwtCustomizer } = logtoConfigs;
const { getOidcConfigs, upsertJwtCustomizer, getJwtCustomizer } = logtoConfigs;
router.get(
'/configs/admin-console',
@ -254,8 +254,11 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
const {
params: { tokenTypePath },
} = ctx.guard;
const { value } = await getJwtCustomizer(getLogtoJwtTokenKey(tokenTypePath));
const { value } = await getJwtCustomizer(
tokenTypePath === LogtoJwtTokenPath.AccessToken
? LogtoJwtTokenKey.AccessToken
: LogtoJwtTokenKey.ClientCredentials
);
ctx.body = value;
return next();
}