2021-08-29 22:30:54 -05:00
|
|
|
import { Applications } from '@logto/schemas';
|
2021-08-14 08:39:37 -05:00
|
|
|
import Router from 'koa-router';
|
2021-08-26 09:50:27 -05:00
|
|
|
import { object, string } from 'zod';
|
2021-08-29 22:30:54 -05:00
|
|
|
|
2021-08-14 08:39:37 -05:00
|
|
|
import koaGuard from '@/middleware/koa-guard';
|
2021-08-29 22:30:54 -05:00
|
|
|
import { generateOidcClientMetadata } from '@/oidc/utils';
|
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';
|
|
|
|
|
|
|
|
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({
|
2021-08-26 09:50:27 -05:00
|
|
|
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) => {
|
2021-08-26 09:50:27 -05:00
|
|
|
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(),
|
2021-08-26 09:50:27 -05:00
|
|
|
...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
|
|
|
}
|