0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00
logto/packages/core/src/routes/application.ts

108 lines
2.6 KiB
TypeScript
Raw Normal View History

import { Applications } from '@logto/schemas';
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';
import koaPagination from '@/middleware/koa-pagination';
import { buildOidcClientMetadata } from '@/oidc/utils';
2021-08-26 11:33:13 -05:00
import {
deleteApplicationById,
findApplicationById,
2021-09-01 07:51:34 -05:00
findAllApplications,
2021-08-26 11:33:13 -05:00
insertApplication,
updateApplicationById,
findTotalNumberOfApplications,
2021-08-26 11:33:13 -05:00
} from '@/queries/application';
2021-08-17 11:24:00 -05:00
import { buildIdGenerator } from '@/utils/id';
2021-09-01 04:35:23 -05:00
import { AuthedRouter } from './types';
2021-08-17 11:24:00 -05:00
const applicationId = buildIdGenerator(21);
2021-08-14 08:39:37 -05:00
2021-09-01 04:35:23 -05:00
export default function applicationRoutes<T extends AuthedRouter>(router: T) {
router.get('/applications', koaPagination(), async (ctx, next) => {
const { limit, offset } = ctx.pagination;
const [{ count }, applications] = await Promise.all([
findTotalNumberOfApplications(),
findAllApplications(limit, offset),
]);
// Return totalCount to pagination middleware
ctx.pagination.totalCount = count;
ctx.body = applications;
2021-09-01 07:51:34 -05:00
return next();
});
2021-08-14 08:39:37 -05:00
router.post(
'/applications',
2021-08-14 08:39:37 -05:00
koaGuard({
body: Applications.createGuard
.omit({ id: true, createdAt: true })
.partial()
.merge(Applications.createGuard.pick({ name: true, type: true })),
2021-08-14 08:39:37 -05:00
}),
async (ctx, next) => {
const { oidcClientMetadata, ...rest } = ctx.guard.body;
2021-08-14 08:39:37 -05:00
ctx.body = await insertApplication({
id: applicationId(),
oidcClientMetadata: buildOidcClientMetadata(oidcClientMetadata),
...rest,
});
2022-01-27 06:26:34 -05:00
2021-08-14 08:39:37 -05:00
return next();
}
);
2021-08-23 11:11:25 -05:00
router.get(
'/applications/:id',
koaGuard({
params: object({ id: string().min(1) }),
}),
async (ctx, next) => {
const {
params: { id },
} = ctx.guard;
ctx.body = await findApplicationById(id);
2022-01-27 06:26:34 -05:00
return next();
}
);
2021-08-26 11:33:13 -05:00
router.patch(
'/applications/:id',
2021-08-26 11:33:13 -05:00
koaGuard({
params: object({ id: string().min(1) }),
body: Applications.createGuard.omit({ id: true, createdAt: true }).deepPartial(),
2021-08-26 11:33:13 -05:00
}),
async (ctx, next) => {
const {
params: { id },
body,
} = ctx.guard;
ctx.body = await updateApplicationById(id, {
...body,
});
2022-01-27 06:26:34 -05:00
2021-08-26 11:33:13 -05:00
return next();
}
);
2021-08-23 11:11:25 -05:00
router.delete(
'/applications/:id',
2021-08-23 11:11:25 -05:00
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 findApplicationById(id);
2021-08-23 11:11:25 -05:00
await deleteApplicationById(id);
ctx.status = 204;
2022-01-27 06:26:34 -05:00
2021-08-23 11:11:25 -05:00
return next();
}
);
2021-08-14 08:39:37 -05:00
}