forked from LilyRose2798/trpc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
47 lines (44 loc) · 1.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { ZodOpenApiObject, ZodOpenApiPathsObject, createDocument } from 'zod-openapi';
import { type OpenAPIObject, OpenApiRouter, type SecuritySchemeObject } from '../types';
import { getOpenApiPathsObject, mergePaths } from './paths';
export interface GenerateOpenApiDocumentOptions {
title: string;
description?: string;
version: string;
openApiVersion?: ZodOpenApiObject['openapi'];
baseUrl: string;
docsUrl?: string;
tags?: string[];
securitySchemes?: Record<string, SecuritySchemeObject>;
paths?: ZodOpenApiPathsObject;
}
export const generateOpenApiDocument = (
appRouter: OpenApiRouter,
opts: GenerateOpenApiDocumentOptions,
): OpenAPIObject => {
const securitySchemes = opts.securitySchemes ?? {
Authorization: {
type: 'http',
scheme: 'bearer',
},
};
return createDocument({
openapi: opts.openApiVersion ?? '3.0.3',
info: {
title: opts.title,
description: opts.description,
version: opts.version,
},
servers: [
{
url: opts.baseUrl,
},
],
paths: mergePaths(getOpenApiPathsObject(appRouter, Object.keys(securitySchemes)), opts.paths),
components: {
securitySchemes,
},
tags: opts.tags?.map((tag) => ({ name: tag })),
externalDocs: opts.docsUrl ? { url: opts.docsUrl } : undefined,
});
};