-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi.ts
213 lines (197 loc) · 6.55 KB
/
openapi.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import type { OpenAPIV3_1 } from 'openapi-types';
import type { ApiRouteSchema, ApiSettingsSchema } from 'moleculer-web';
import type { AliasRouteSchema } from './moleculer-web.js';
import { OpenApiDefined, OptionalOrFalse, ReturnOrResolve, SubOptionalOrFalse } from './utils.js';
import type { ActionSchema, Context, ServiceSettingSchema } from 'moleculer';
import { OpenApiVersionsSupported } from '../constants.js';
import { Alias } from '../objects/Alias.js';
import { Mapper, RuleToSchemaFunction, SchemaToRules } from './converters/index.js';
import { RuleCustom } from 'fastest-validator';
export type OA_GENERATE_DOCS_INPUT = {
/**
* maybe a future option ?
* @hidden
*/
version?: OpenApiVersionsSupported;
};
export type OA_GENERATE_DOCS_OUTPUT = OpenAPIV3_1.Document;
/**
* describe an openapi response .
* by default it use default content type {@link OpenApiMixinSettings.defaultResponseContentType}
*/
export type actionOpenApiResponse = Omit<OpenAPIV3_1.ResponseObject, 'content'> & {
/**
* the content of the response
*/
content?: OpenAPIV3_1.MediaTypeObject;
/**
* an override of the content-type
*/
type?: string;
/**
* an override of the default status code
*
* @default 200
*/
statusCode?: number;
};
export type openApiTag = string | OpenAPIV3_1.TagObject | null;
export interface ActionOpenApi extends commonOpenApi {
/**
* can be used to fastly declare the 200 answer
* can be directly a {@link https://spec.openapis.org/oas/v3.1.0#media-type-object | Media Type Object} so it will reuse the default contentType
* or an object specifying the type and the media
*/
response?: OpenAPIV3_1.MediaTypeObject | actionOpenApiResponse;
/**
* allow to bypass the generation from params . Specify it yourself
*/
requestBody?: OpenAPIV3_1.RequestBodyObject;
/**
* allow to bypass the query generation from params . Specify it yourself
*/
queryParameters?: Array<Omit<OpenAPIV3_1.ParameterObject, 'in'>>;
/**
* allow to bypass the query generation from params . Specify it yourself
*/
pathParameters?: Array<Omit<OpenAPIV3_1.ParameterObject, 'in'>>;
/**
* allow to pass header or cookies parameters
*/
parameters?: Array<
Omit<OpenAPIV3_1.ParameterObject, 'in'> & { in: 'cookie' | 'header' } & (
| Required<Pick<OpenAPIV3_1.ParameterObject, 'schema'>>
| Required<Pick<OpenAPIV3_1.ParameterObject, 'content'>>
)
>;
/**
* add a description to this operation
*/
description?: string;
/**
* add an external documentation to this operation
*/
externalDocs?: OpenAPIV3_1.ExternalDocumentationObject;
/**
* add an operation id to this operation
*/
operationId?: string;
/**
* add a summary to this operation
*/
summary?: string;
/**
* add a list of servers to this operation
*/
servers?: Array<OpenAPIV3_1.ServerObject>;
/**
* specify the security needed to call this endpoint
*/
security?: Array<OpenAPIV3_1.SecurityRequirementObject>;
}
export interface commonOpenApi {
/**
* Allow to define tags of the {@link https://spec.openapis.org/oas/v3.1.0#operation-object|Operation}
* - Use a tagObject to define it ( follow by his name to use it )
* - use null to remove all tags added previously (by merge from other levels)
* - use a string to use a tag
*
* tags are unique in all the openApi, and identified by his name .
* Defining two times the same tag will merge them
*
* @example
* // setting tags to all children
* {
* tags: ['tags1', 'tags2']
* }
*
* @example
* // remove parents tags, and set tags to children
* {
* tags: [null, 'tags3', 'tags4']
* }
*
* @example
* // add tag with description, and use it on children
* {
* tags: [
* {
* name: "tags1";
* description: "this is the first example tag";
* externalDocs: "https://doc.example.com/tags/tags1";
* },
* 'tags1'
* ]
* }
*
*/
tags?: Array<openApiTag>;
components?: SubOptionalOrFalse<OpenAPIV3_1.ComponentsObject>;
/**
* specify all responses of the operation .
* Merged by levels
*/
responses?: OptionalOrFalse<OpenAPIV3_1.ResponsesObject>;
/**
* set this endpoint as deprecated
*/
deprecated?: boolean;
}
export type openApiServiceOpenApi = Omit<OpenAPIV3_1.Document, 'openapi' | keyof commonOpenApi> & commonOpenApi;
export interface ApiSettingsOpenApi extends commonOpenApi {
/**
* This configuration is under your responsibility.
* If assigned, ensure that the URL correctly points to your Moleculer-web API.
* This module will not modify the configuration (will not add the Moleculer-web root path).
* As such, it can accommodate any reverse proxy configuration.
*/
server?: OpenAPIV3_1.ServerObject;
}
export interface ApiSettingsSchemaOpenApi extends ApiSettingsSchema {
routes?: Array<ApiRouteSchema>;
openapi?: ApiSettingsOpenApi;
}
export interface AliasRouteOpenApi extends ActionOpenApi {}
/**
* moleculer-web AliasRouteSchema is a type, so not extendable
*
* @example
* //you can use this in your aliases
* {
* aliases: {
* 'POST go': {} as AliasRouteSchemaOpenApi
* }
* }
*/
export interface AliasRouteSchemaOpenApi extends AliasRouteSchema {
openapi?: AliasRouteOpenApi | false;
}
export interface ApiRouteOpenApi extends commonOpenApi {}
declare module 'moleculer' {
interface ActionSchema {
openapi?: ActionOpenApi | false;
}
interface ServiceSettingSchema {
openapi?: ActionOpenApi | false;
}
}
declare module 'moleculer-web' {
interface ApiRouteSchema {
openapi?: ApiRouteOpenApi | false;
}
}
export type definedApiRouteSchema = OpenApiDefined<ApiRouteSchema>;
export type definedServiceSettingSchema = OpenApiDefined<ServiceSettingSchema>;
export type definedActionSchema = OpenApiDefined<ActionSchema>;
export type definedAliasRouteSchemaOpenApi = OpenApiDefined<AliasRouteSchemaOpenApi>;
/**
* allow to filter aliases
*/
export type filterAliasesFn = (ctx: Context<OA_GENERATE_DOCS_INPUT>, aliases: Array<Alias>) => ReturnOrResolve<Array<Alias>>;
/**
* Allow to add custom mappers
*/
export type addMappersFn = (
getSchemaObjectFromRule: RuleToSchemaFunction,
getSchemaObjectFromSchema: SchemaToRules
) => ReturnOrResolve<Record<string, Mapper<RuleCustom>>>;