Skip to content

Commit

Permalink
chore(core,test): update tests and refactor getJwtCustomizer query
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Mar 6, 2024
1 parent 118de63 commit 754d425
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
29 changes: 19 additions & 10 deletions packages/core/src/queries/logto-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
type jwtCustomizerConfigGuard,
jwtCustomizerConfigGuard,
LogtoTenantConfigKey,
LogtoConfigs,
type AdminConsoleData,
Expand All @@ -12,7 +12,9 @@ import {
import { convertToIdentifiers } from '@logto/shared';
import type { CommonQueryMethods } 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);

Expand Down Expand Up @@ -67,14 +69,21 @@ export const createLogtoConfigQueries = (pool: CommonQueryMethods) => {
`
);

const getJwtCustomizer = async <T extends LogtoJwtTokenKey>(key: T) =>
pool.one<{ value: JwtCustomizerType[T] }>(
sql`
select ${fields.value}
from ${table}
where ${fields.key} = ${key}
`
);
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 {
getAdminConsoleConfig,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/routes/logto-config.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
"parameters": [
{
"in": "path",
"name": "tokenType",
"name": "tokenTypePath",
"description": "The token type to get the JWT customizer for."
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('admin console sign-in experience', () => {
);
expect(updatedClientCredentials).toMatchObject(newClientCredentialsJwtCustomizerPayload);
await expect(getJwtCustomizer('client-credentials')).resolves.toMatchObject(
clientCredentialsJwtCustomizerPayload
newClientCredentialsJwtCustomizerPayload
);
});
});

0 comments on commit 754d425

Please sign in to comment.