-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
108 lines (103 loc) · 3.01 KB
/
Copy pathindex.ts
File metadata and controls
108 lines (103 loc) · 3.01 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 OpenApiTag = {
name: string;
description?: string;
externalDocs?: {
description?: string;
url: string;
};
};
export type GenerateOpenApiDocumentOptions = {
title: string;
description?: string;
version: string;
baseUrl: string;
docsUrl?: string;
termsURL?: string;
tags?: (string | OpenApiTag)[];
securitySchemes?: OpenAPIV3.ComponentsObject["securitySchemes"];
contact?: {
email: string;
};
license?: {
name: string;
url: string;
};
externalDocs?: {
description: string;
url: string;
};
};
// 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,
termsOfService: opts.termsURL,
contact: opts.contact ? { email: opts.contact.email } : undefined,
license: opts.license
? {
name: opts.license.name,
url: opts.license.url
}
: undefined
},
servers: [
{
url: opts.baseUrl
}
],
paths: getOpenApiPathsObject(appRouter, Object.keys(securitySchemes)),
components: {
securitySchemes,
responses: {
error: errorResponseObject
}
},
tags: opts.tags?.map((tag) => (typeof tag === "string"
? { name: tag } // Legacy support for string[]
: {
name: tag.name,
description: tag.description,
externalDocs: tag.externalDocs
? {
description: tag.externalDocs.description,
url: tag.externalDocs.url
}
: undefined
})),
// eslint-disable-next-line no-nested-ternary
externalDocs: opts.externalDocs
? {
description: opts.externalDocs.description,
url: opts.externalDocs.url
}
: opts.docsUrl
? { url: opts.docsUrl }
: undefined
}
}