forked from trpc/trpc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
51 lines (47 loc) · 1.23 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
48
49
50
51
import { OpenAPIV3 } from 'openapi-types';
import { OpenApiRouter } from '../types';
import { getOpenApiPathsObject } from './paths';
import { errorResponseObject } from './schema';
export const openApiVersion = '3.0.3';
export type GenerateOpenApiDocumentOptions = {
title: string;
description?: string;
version: string;
baseUrl: string;
docsUrl?: string;
tags?: string[];
securitySchemes?: OpenAPIV3.ComponentsObject['securitySchemes'];
};
export const generateOpenApiDocument = (
appRouter: OpenApiRouter,
opts: GenerateOpenApiDocumentOptions,
): OpenAPIV3.Document => {
const securitySchemes = opts.securitySchemes || {
Authorization: {
type: 'http',
scheme: 'bearer',
},
};
return {
openapi: openApiVersion,
info: {
title: opts.title,
description: opts.description,
version: opts.version,
},
servers: [
{
url: opts.baseUrl,
},
],
paths: getOpenApiPathsObject(appRouter, Object.keys(securitySchemes)),
components: {
securitySchemes,
responses: {
error: errorResponseObject,
},
},
tags: opts.tags?.map((tag) => ({ name: tag })),
externalDocs: opts.docsUrl ? { url: opts.docsUrl } : undefined,
};
};