0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00
logto/packages/core/src/routes/application.ts

68 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-08-14 08:39:37 -05:00
import Router from 'koa-router';
import { object, string } from 'zod';
import { Applications } from '@logto/schemas';
2021-08-14 08:39:37 -05:00
import koaGuard from '@/middleware/koa-guard';
2021-08-26 11:33:13 -05:00
import {
deleteApplicationById,
insertApplication,
updateApplicationById,
} from '@/queries/application';
2021-08-17 11:24:00 -05:00
import { buildIdGenerator } from '@/utils/id';
import { generateOidcClientMetadata } from '@/oidc/utils';
const applicationId = buildIdGenerator(21);
2021-08-14 08:39:37 -05:00
2021-08-15 10:39:03 -05:00
export default function applicationRoutes<StateT, ContextT>(router: Router<StateT, ContextT>) {
2021-08-14 08:39:37 -05:00
router.post(
'/application',
koaGuard({
body: Applications.guard
.omit({ id: true, createdAt: true })
.partial()
.merge(Applications.guard.pick({ name: true, type: true })),
2021-08-14 08:39:37 -05:00
}),
async (ctx, next) => {
const { name, type, ...rest } = ctx.guard.body;
2021-08-14 08:39:37 -05:00
2021-08-17 11:24:00 -05:00
ctx.body = await insertApplication({
id: applicationId(),
type,
name,
oidcClientMetadata: generateOidcClientMetadata(),
...rest,
2021-08-17 11:24:00 -05:00
});
2021-08-14 08:39:37 -05:00
return next();
}
);
2021-08-23 11:11:25 -05:00
2021-08-26 11:33:13 -05:00
router.patch(
'/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) => {
const {
params: { id },
body,
} = ctx.guard;
ctx.body = await updateApplicationById(id, body);
return next();
}
);
2021-08-23 11:11:25 -05:00
router.delete(
'/application/:id',
koaGuard({ params: object({ id: string().min(1) }) }),
async (ctx, next) => {
const { id } = ctx.guard.params;
// Note: will need delete cascade when application is joint with other tables
await deleteApplicationById(id);
ctx.status = 204;
return next();
}
);
2021-08-14 08:39:37 -05:00
}