0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-24 22:41:28 -05:00

refactor(core): fulfill correct applicationType when needed (#126)

* refactor(core): fulfill correct `applicationType` when needed

* refactor(core): use enum for application type

* refactor(core): do not filter undefined automatically
This commit is contained in:
Gao Sun 2021-10-11 17:55:17 +08:00 committed by GitHub
parent 0b23a5a793
commit 9aa4caa826
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 8 deletions

View file

@ -12,7 +12,10 @@ describe('buildUpdateWhere()', () => {
);
const updateWhere = buildUpdateWhere(pool, Users);
await expect(
updateWhere({ set: { username: '123' }, where: { id: 'foo', username: '456' } })
updateWhere({
set: { username: '123' },
where: { id: 'foo', username: '456' },
})
).resolves.toBe(undefined);
});
@ -32,6 +35,19 @@ describe('buildUpdateWhere()', () => {
).resolves.toStrictEqual(user);
});
it('throws an error when `undefined` found in values', async () => {
const pool = createTestPool(
'update "users"\nset "username"=$1\nwhere "id"=$2 and "username"=$3'
);
const updateWhere = buildUpdateWhere(pool, Users);
await expect(
updateWhere({
set: { username: '123', id: undefined },
where: { id: 'foo', username: '456' },
})
).rejects.toMatchError(new TypeError("Cannot read property 'toString' of undefined"));
});
it('throws `entity.not_exists_with_id` error with `undefined` when `returning` is true', async () => {
const pool = createTestPool('update "users"\nset "username"=$1\nwhere "id"=$2\nreturning *');
const updateWhere = buildUpdateWhere(pool, Users, true);

View file

@ -1,6 +1,14 @@
import { OidcClientMetadata } from '@logto/schemas';
import { ApplicationType, OidcClientMetadata } from '@logto/schemas';
export const generateOidcClientMetadata = (): OidcClientMetadata => ({
const getApplicationTypeString = (type: ApplicationType) =>
type === ApplicationType.Native ? 'native' : 'web';
export const buildOidcClientMetadata = (
type: ApplicationType,
metadata?: OidcClientMetadata
): OidcClientMetadata => ({
redirectUris: [],
postLogoutRedirectUris: [],
...metadata,
applicationType: getApplicationTypeString(type),
});

View file

@ -2,7 +2,7 @@ import { Applications } from '@logto/schemas';
import { object, string } from 'zod';
import koaGuard from '@/middleware/koa-guard';
import { generateOidcClientMetadata } from '@/oidc/utils';
import { buildOidcClientMetadata } from '@/oidc/utils';
import {
deleteApplicationById,
findApplicationById,
@ -31,13 +31,13 @@ export default function applicationRoutes<T extends AuthedRouter>(router: T) {
.merge(Applications.guard.pick({ name: true, type: true })),
}),
async (ctx, next) => {
const { name, type, ...rest } = ctx.guard.body;
const { name, type, oidcClientMetadata, ...rest } = ctx.guard.body;
ctx.body = await insertApplication({
id: applicationId(),
type,
name,
oidcClientMetadata: generateOidcClientMetadata(),
oidcClientMetadata: buildOidcClientMetadata(type, oidcClientMetadata),
...rest,
});
return next();
@ -63,7 +63,6 @@ export default function applicationRoutes<T extends AuthedRouter>(router: T) {
'/application/:id',
koaGuard({
params: object({ id: string().min(1) }),
// Consider `.deepPartial()` if OIDC client metadata bloats
body: Applications.guard.omit({ id: true, createdAt: true }).partial(),
}),
async (ctx, next) => {
@ -71,8 +70,15 @@ export default function applicationRoutes<T extends AuthedRouter>(router: T) {
params: { id },
body,
} = ctx.guard;
const application = await findApplicationById(id);
ctx.body = await updateApplicationById(id, body);
ctx.body = await updateApplicationById(id, {
...body,
oidcClientMetadata: buildOidcClientMetadata(body.type ?? application.type, {
...application.oidcClientMetadata,
...body.oidcClientMetadata,
}),
});
return next();
}
);

View file

@ -15,6 +15,7 @@ export const oidcModelInstancePayloadGuard = z
export type OidcModelInstancePayload = z.infer<typeof oidcModelInstancePayloadGuard>;
export const oidcClientMetadataGuard = z.object({
applicationType: z.enum(['web', 'native']),
redirectUris: z.string().array(),
postLogoutRedirectUris: z.string().array(),
});