mirror of
https://github.com/logto-io/logto.git
synced 2025-01-13 21:30:30 -05:00
97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
import { Applications } from '@logto/schemas';
|
|
import { object, string } from 'zod';
|
|
|
|
import koaGuard from '@/middleware/koa-guard';
|
|
import { buildOidcClientMetadata } from '@/oidc/utils';
|
|
import {
|
|
deleteApplicationById,
|
|
findApplicationById,
|
|
findAllApplications,
|
|
insertApplication,
|
|
updateApplicationById,
|
|
} from '@/queries/application';
|
|
import { buildIdGenerator } from '@/utils/id';
|
|
|
|
import { AuthedRouter } from './types';
|
|
|
|
const applicationId = buildIdGenerator(21);
|
|
|
|
export default function applicationRoutes<T extends AuthedRouter>(router: T) {
|
|
router.get('/applications', async (ctx, next) => {
|
|
ctx.body = await findAllApplications();
|
|
return next();
|
|
});
|
|
|
|
router.post(
|
|
'/applications',
|
|
koaGuard({
|
|
body: Applications.guard
|
|
.omit({ id: true, createdAt: true })
|
|
.partial()
|
|
.merge(Applications.guard.pick({ name: true, type: true })),
|
|
}),
|
|
async (ctx, next) => {
|
|
const { name, type, oidcClientMetadata, ...rest } = ctx.guard.body;
|
|
|
|
ctx.body = await insertApplication({
|
|
id: applicationId(),
|
|
type,
|
|
name,
|
|
oidcClientMetadata: buildOidcClientMetadata(type, oidcClientMetadata),
|
|
...rest,
|
|
});
|
|
return next();
|
|
}
|
|
);
|
|
|
|
router.get(
|
|
'/applications/:id',
|
|
koaGuard({
|
|
params: object({ id: string().min(1) }),
|
|
}),
|
|
async (ctx, next) => {
|
|
const {
|
|
params: { id },
|
|
} = ctx.guard;
|
|
|
|
ctx.body = await findApplicationById(id);
|
|
return next();
|
|
}
|
|
);
|
|
|
|
router.patch(
|
|
'/applications/: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;
|
|
const application = await findApplicationById(id);
|
|
|
|
ctx.body = await updateApplicationById(id, {
|
|
...body,
|
|
oidcClientMetadata: buildOidcClientMetadata(body.type ?? application.type, {
|
|
...application.oidcClientMetadata,
|
|
...body.oidcClientMetadata,
|
|
}),
|
|
});
|
|
return next();
|
|
}
|
|
);
|
|
|
|
router.delete(
|
|
'/applications/: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();
|
|
}
|
|
);
|
|
}
|