0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/core/src/routes/swagger.ts

148 lines
3.7 KiB
TypeScript
Raw Normal View History

import { toTitle } from '@silverhand/essentials';
import Router, { IMiddleware } from 'koa-router';
2021-07-23 23:10:54 +08:00
import { OpenAPIV3 } from 'openapi-types';
import { ZodObject, ZodOptional } from 'zod';
2021-08-30 11:30:54 +08:00
2021-07-23 23:10:54 +08:00
import { isGuardMiddleware, WithGuardConfig } from '@/middleware/koa-guard';
import { isPaginationMiddleware, fallbackDefaultPageSize } from '@/middleware/koa-pagination';
import assertThat from '@/utils/assert-that';
2021-07-23 23:10:54 +08:00
import { zodTypeToSwagger } from '@/utils/zod';
2021-09-01 17:35:23 +08:00
import { AnonymousRouter } from './types';
type RouteObject = {
path: string;
method: OpenAPIV3.HttpMethods;
operation: OpenAPIV3.OperationObject;
};
type MethodMap = {
[key in OpenAPIV3.HttpMethods]?: OpenAPIV3.OperationObject;
};
2022-01-27 19:26:34 +08:00
export const paginationParameters: OpenAPIV3.ParameterObject[] = [
{
name: 'page',
in: 'query',
required: false,
schema: {
type: 'integer',
minimum: 1,
default: 1,
},
},
{
name: 'page_size',
in: 'query',
required: false,
schema: {
type: 'integer',
minimum: 1,
default: fallbackDefaultPageSize,
},
},
];
// Parameter serialization: https://swagger.io/docs/specification/serialization
const buildParameters = (
zodParameters: unknown,
inWhere: 'path' | 'query'
): OpenAPIV3.ParameterObject[] => {
if (!zodParameters) {
return [];
}
assertThat(zodParameters instanceof ZodObject, 'swagger.not_supported_zod_type_for_params');
return Object.entries(zodParameters.shape).map(([key, value]) => ({
name: key,
in: inWhere,
required: !(value instanceof ZodOptional),
schema: zodTypeToSwagger(value),
}));
};
function buildTag(path: string) {
const root = path.split('/')[1];
if (root?.startsWith('.')) {
return root;
}
return toTitle(root ?? 'General');
}
const buildOperation = (stack: IMiddleware[], path: string): OpenAPIV3.OperationObject => {
const guard = stack.find((function_): function_ is WithGuardConfig<IMiddleware> =>
isGuardMiddleware(function_)
);
const hasPagination = stack.some((function_) => isPaginationMiddleware(function_));
const body = guard?.config.body;
const requestBody = body && {
required: true,
content: {
'application/json': {
schema: zodTypeToSwagger(body),
},
},
};
const pathParameters = buildParameters(guard?.config.params, 'path');
const queryParameters = [
...buildParameters(guard?.config.query, 'query'),
...(hasPagination ? paginationParameters : []),
];
2021-07-23 23:10:54 +08:00
return {
tags: [buildTag(path)],
parameters: [...pathParameters, ...queryParameters],
requestBody,
responses: {
'200': {
description: 'OK',
},
},
};
};
2021-07-23 23:10:54 +08:00
export default function swaggerRoutes<T extends AnonymousRouter, R extends Router<unknown, any>>(
router: T,
allRouters: R[]
) {
router.get('/swagger.json', async (ctx, next) => {
const routes = allRouters.flatMap<RouteObject>((router) =>
router.stack.flatMap<RouteObject>(({ path, stack, methods }) =>
methods
// There is no need to show the HEAD method.
.filter((method) => method !== 'HEAD')
.map((method) => ({
path: `/api${path}`,
method: method.toLowerCase() as OpenAPIV3.HttpMethods,
operation: buildOperation(stack, path),
}))
)
2021-07-23 23:10:54 +08:00
);
const pathMap = new Map<string, MethodMap>();
// Group routes by path
for (const { path, method, operation } of routes) {
pathMap.set(path, { ...pathMap.get(path), [method]: operation });
}
2021-07-23 23:10:54 +08:00
const document: OpenAPIV3.Document = {
openapi: '3.0.1',
info: {
title: 'Logto Core',
version: '0.1.0',
},
paths: Object.fromEntries(pathMap),
2021-07-23 23:10:54 +08:00
};
ctx.body = document;
return next();
});
2021-07-23 23:10:54 +08:00
}