2021-08-30 11:30:54 +08:00
|
|
|
import { Applications } from '@logto/schemas';
|
2021-08-26 22:50:27 +08:00
|
|
|
import { object, string } from 'zod';
|
2021-08-30 11:30:54 +08:00
|
|
|
|
2021-08-14 21:39:37 +08:00
|
|
|
import koaGuard from '@/middleware/koa-guard';
|
2021-10-11 17:55:17 +08:00
|
|
|
import { buildOidcClientMetadata } from '@/oidc/utils';
|
2021-08-27 00:33:13 +08:00
|
|
|
import {
|
|
|
|
deleteApplicationById,
|
2021-09-01 18:23:53 +08:00
|
|
|
findApplicationById,
|
2021-09-01 20:51:34 +08:00
|
|
|
findAllApplications,
|
2021-08-27 00:33:13 +08:00
|
|
|
insertApplication,
|
|
|
|
updateApplicationById,
|
|
|
|
} from '@/queries/application';
|
2021-08-18 00:24:00 +08:00
|
|
|
import { buildIdGenerator } from '@/utils/id';
|
|
|
|
|
2021-09-01 17:35:23 +08:00
|
|
|
import { AuthedRouter } from './types';
|
|
|
|
|
2021-08-18 00:24:00 +08:00
|
|
|
const applicationId = buildIdGenerator(21);
|
2021-08-14 21:39:37 +08:00
|
|
|
|
2021-09-01 17:35:23 +08:00
|
|
|
export default function applicationRoutes<T extends AuthedRouter>(router: T) {
|
2021-09-01 20:51:34 +08:00
|
|
|
router.get('/applications', async (ctx, next) => {
|
|
|
|
ctx.body = await findAllApplications();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2021-08-14 21:39:37 +08:00
|
|
|
router.post(
|
|
|
|
'/application',
|
|
|
|
koaGuard({
|
2021-08-26 22:50:27 +08:00
|
|
|
body: Applications.guard
|
|
|
|
.omit({ id: true, createdAt: true })
|
|
|
|
.partial()
|
|
|
|
.merge(Applications.guard.pick({ name: true, type: true })),
|
2021-08-14 21:39:37 +08:00
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
2021-10-11 17:55:17 +08:00
|
|
|
const { name, type, oidcClientMetadata, ...rest } = ctx.guard.body;
|
2021-08-14 21:39:37 +08:00
|
|
|
|
2021-08-18 00:24:00 +08:00
|
|
|
ctx.body = await insertApplication({
|
|
|
|
id: applicationId(),
|
|
|
|
type,
|
|
|
|
name,
|
2021-10-11 17:55:17 +08:00
|
|
|
oidcClientMetadata: buildOidcClientMetadata(type, oidcClientMetadata),
|
2021-08-26 22:50:27 +08:00
|
|
|
...rest,
|
2021-08-18 00:24:00 +08:00
|
|
|
});
|
2021-08-14 21:39:37 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
2021-08-24 00:11:25 +08:00
|
|
|
|
2021-09-01 18:23:53 +08:00
|
|
|
router.get(
|
|
|
|
'/application/:id',
|
|
|
|
koaGuard({
|
|
|
|
params: object({ id: string().min(1) }),
|
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const {
|
|
|
|
params: { id },
|
|
|
|
} = ctx.guard;
|
|
|
|
|
|
|
|
ctx.body = await findApplicationById(id);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-08-27 00:33:13 +08:00
|
|
|
router.patch(
|
|
|
|
'/application/:id',
|
|
|
|
koaGuard({
|
|
|
|
params: object({ id: string().min(1) }),
|
|
|
|
body: Applications.guard.omit({ id: true, createdAt: true }).partial(),
|
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const {
|
|
|
|
params: { id },
|
|
|
|
body,
|
|
|
|
} = ctx.guard;
|
2021-10-11 17:55:17 +08:00
|
|
|
const application = await findApplicationById(id);
|
2021-08-27 00:33:13 +08:00
|
|
|
|
2021-10-11 17:55:17 +08:00
|
|
|
ctx.body = await updateApplicationById(id, {
|
|
|
|
...body,
|
|
|
|
oidcClientMetadata: buildOidcClientMetadata(body.type ?? application.type, {
|
|
|
|
...application.oidcClientMetadata,
|
|
|
|
...body.oidcClientMetadata,
|
|
|
|
}),
|
|
|
|
});
|
2021-08-27 00:33:13 +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
|
|
|
}
|