-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
60 lines (56 loc) · 1.94 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
52
53
54
55
56
57
58
59
60
import { OpenAPIV3 } from "openapi-types"
// Application Sectional || Define Imports
// =================================================================================================
// =================================================================================================
import { OpenApiRouter } from "../types"
import { getOpenApiPathsObject } from "./paths"
import { errorResponseObject } from "./schema"
export const openApiVersion = "3.0.3"
// Application Sectional || Define Export Type
// =================================================================================================
// =================================================================================================
export type GenerateOpenApiDocumentOptions = {
title: string;
description?: string;
version: string;
baseUrl: string;
docsUrl?: string;
tags?: string[];
securitySchemes?: OpenAPIV3.ComponentsObject["securitySchemes"];
};
// Application Sectional || Define Export Handler
// =================================================================================================
// =================================================================================================
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
}
}