forked from trpc/trpc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
51 lines (48 loc) · 1.34 KB
/
index.ts
File metadata and controls
51 lines (48 loc) · 1.34 KB
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
48
49
50
51
import { OpenAPIObject, SecuritySchemeObject } from 'openapi3-ts/dist/oas31';
import { ZodOpenApiObject, ZodOpenApiPathsObject, createDocument } from 'zod-openapi';
import { OpenApiRouter } from '../types';
import { getOpenApiPathsObject } from './paths';
export type 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: {
...getOpenApiPathsObject(appRouter, Object.keys(securitySchemes)),
...(opts.paths ?? {}),
},
components: {
securitySchemes,
},
tags: opts.tags?.map((tag) => ({ name: tag })),
externalDocs: opts.docsUrl ? { url: opts.docsUrl } : undefined,
});
};