2021-08-14 21:39:37 +08:00
|
|
|
import Router from 'koa-router';
|
|
|
|
import { nativeEnum, object, string } from 'zod';
|
|
|
|
import { ApplicationType } from '@logto/schemas';
|
|
|
|
import koaGuard from '@/middleware/koa-guard';
|
2021-08-24 00:11:25 +08:00
|
|
|
import { deleteApplicationById, insertApplication } from '@/queries/application';
|
2021-08-18 00:24:00 +08:00
|
|
|
import { buildIdGenerator } from '@/utils/id';
|
|
|
|
import { generateOidcClientMetadata } from '@/oidc/utils';
|
|
|
|
|
|
|
|
const applicationId = buildIdGenerator(21);
|
2021-08-14 21:39:37 +08:00
|
|
|
|
2021-08-15 23:39:03 +08:00
|
|
|
export default function applicationRoutes<StateT, ContextT>(router: Router<StateT, ContextT>) {
|
2021-08-14 21:39:37 +08:00
|
|
|
router.post(
|
|
|
|
'/application',
|
|
|
|
koaGuard({
|
|
|
|
body: object({
|
|
|
|
name: string().min(1),
|
|
|
|
type: nativeEnum(ApplicationType),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const { name, type } = ctx.guard.body;
|
|
|
|
|
2021-08-18 00:24:00 +08:00
|
|
|
ctx.body = await insertApplication({
|
|
|
|
id: applicationId(),
|
|
|
|
type,
|
|
|
|
name,
|
|
|
|
oidcClientMetadata: generateOidcClientMetadata(),
|
|
|
|
});
|
2021-08-14 21:39:37 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
2021-08-24 00:11:25 +08: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 21:39:37 +08:00
|
|
|
}
|