0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

chore(core,test): update tests and refactor getJwtCustomizer query

This commit is contained in:
Darcy Ye 2024-03-06 13:10:15 +08:00
parent 118de63049
commit 754d425a60
No known key found for this signature in database
GPG key ID: B46F4C07EDEFC610
3 changed files with 21 additions and 12 deletions

View file

@ -1,5 +1,5 @@
import { import {
type jwtCustomizerConfigGuard, jwtCustomizerConfigGuard,
LogtoTenantConfigKey, LogtoTenantConfigKey,
LogtoConfigs, LogtoConfigs,
type AdminConsoleData, type AdminConsoleData,
@ -12,7 +12,9 @@ import {
import { convertToIdentifiers } from '@logto/shared'; import { convertToIdentifiers } from '@logto/shared';
import type { CommonQueryMethods } from 'slonik'; import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik'; import { sql } from 'slonik';
import type { z } from 'zod'; import { z } from 'zod';
import RequestError from '#src/errors/RequestError/index.js';
const { table, fields } = convertToIdentifiers(LogtoConfigs); const { table, fields } = convertToIdentifiers(LogtoConfigs);
@ -67,14 +69,21 @@ export const createLogtoConfigQueries = (pool: CommonQueryMethods) => {
` `
); );
const getJwtCustomizer = async <T extends LogtoJwtTokenKey>(key: T) => const getJwtCustomizer = async <T extends LogtoJwtTokenKey>(key: T) => {
pool.one<{ value: JwtCustomizerType[T] }>( const { rows } = await getRowsByKeys([key]);
sql`
select ${fields.value} // If the record does not exist (`rows` is empty)
from ${table} if (rows.length === 0) {
where ${fields.key} = ${key} throw new RequestError({
` code: 'entity.not_exists',
); name: table,
id: key,
status: 404,
});
}
return z.object({ value: jwtCustomizerConfigGuard[key] }).parse(rows[0]);
};
return { return {
getAdminConsoleConfig, getAdminConsoleConfig,

View file

@ -156,7 +156,7 @@
"parameters": [ "parameters": [
{ {
"in": "path", "in": "path",
"name": "tokenType", "name": "tokenTypePath",
"description": "The token type to get the JWT customizer for." "description": "The token type to get the JWT customizer for."
} }
], ],

View file

@ -184,7 +184,7 @@ describe('admin console sign-in experience', () => {
); );
expect(updatedClientCredentials).toMatchObject(newClientCredentialsJwtCustomizerPayload); expect(updatedClientCredentials).toMatchObject(newClientCredentialsJwtCustomizerPayload);
await expect(getJwtCustomizer('client-credentials')).resolves.toMatchObject( await expect(getJwtCustomizer('client-credentials')).resolves.toMatchObject(
clientCredentialsJwtCustomizerPayload newClientCredentialsJwtCustomizerPayload
); );
}); });
}); });