-
Notifications
You must be signed in to change notification settings - Fork 208
/
OpenAPI.ts
205 lines (178 loc) · 5.1 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module RpcInterface
*/
import { RpcOperation } from "../core/RpcOperation";
import { WebAppRpcProtocol } from "./WebAppRpcProtocol";
/* eslint-disable deprecation/deprecation */
/** An OpenAPI 3.0 root document object.
* @internal
*/
export interface OpenAPIDocument {
openapi: "3.0.0";
info: OpenAPIInfo;
paths: OpenAPIPaths;
}
/** An OpenAPI 3.0 info object.
* @public
* @deprecated in 3.6. The RPC system will be significantly refactored (or replaced) in the future.
*/
export interface OpenAPIInfo {
title: string;
version: string;
}
/** An OpenAPI 3.0 paths object.
* @internal
*/
export interface OpenAPIPaths {
[index: string]: OpenAPIPathItem;
}
/** An OpenAPI 3.0 path item object.
* @internal
*/
export interface OpenAPIPathItem {
summary?: string;
get?: OpenAPIOperation;
put?: OpenAPIOperation;
post?: OpenAPIOperation;
delete?: OpenAPIOperation;
options?: OpenAPIOperation;
head?: OpenAPIOperation;
patch?: OpenAPIOperation;
trace?: OpenAPIOperation;
parameters?: OpenAPIParameter[];
}
/** An OpenAPI 3.0 operation object.
* @internal
*/
export interface OpenAPIOperation {
summary?: string;
operationId?: string;
parameters?: OpenAPIParameter[];
requestBody?: OpenAPIRequestBody;
responses: OpenAPIResponses;
}
/** An OpenAPI 3.0 content map.
* @internal
*/
export interface OpenAPIContentMap {
[index: string]: OpenAPIMediaType;
}
/** An OpenAPI 3.0 parameter object.
* @internal
*/
export interface OpenAPIParameter {
name: string;
in: "query" | "header" | "path" | "cookie";
description?: string;
required?: boolean;
allowEmptyValue?: boolean;
style?: "matrix" | "label" | "form" | "simple" | "spaceDelimited" | "pipeDelimited" | "deepObject";
explode?: boolean;
allowReserved?: boolean;
schema?: OpenAPISchema;
content?: OpenAPIContentMap;
}
/** An OpenAPI 3.0 media type object.
* @internal
*/
export interface OpenAPIMediaType {
schema?: OpenAPISchema;
}
/** An OpenAPI 3.0 schema object.
* @internal
*/
export interface OpenAPISchema {
type?: "boolean" | "object" | "array" | "number" | "string";
nullable?: boolean;
description?: string;
}
/** An OpenAPI 3.0 encoding object.
* @internal
*/
export interface OpenAPIEncoding {
contentType?: string;
style?: string;
explode?: boolean;
allowReserved?: boolean;
}
/** An OpenAPI 3.0 parameter object.
* @internal
*/
export interface OpenAPIRequestBody {
description?: string;
content: OpenAPIContentMap;
required?: boolean;
}
/** An OpenAPI 3.0 responses object.
* @internal
*/
export interface OpenAPIResponses {
default?: OpenAPIResponse;
"200"?: OpenAPIResponse;
"301"?: OpenAPIResponse;
"302"?: OpenAPIResponse;
"400"?: OpenAPIResponse;
"404"?: OpenAPIResponse;
"500"?: OpenAPIResponse;
}
/** An OpenAPI 3.0 response object.
* @internal
*/
export interface OpenAPIResponse {
description: string;
content?: { [index: string]: OpenAPIMediaType };
}
/** An OpenAPI-compatible description of an RPC protocol.
* @internal
*/
export class RpcOpenAPIDescription {
/** The protocol for this description. */
public readonly protocol: WebAppRpcProtocol;
/** The OpenAPI paths object for the protocol. */
public get paths(): OpenAPIPaths {
const paths: OpenAPIPaths = {};
this.protocol.configuration.interfaces().forEach((definition) => {
RpcOperation.forEach(definition, (operation) => {
const path = this.protocol.supplyPathForOperation(operation, undefined);
paths[path] = this.generateDescription(operation);
});
});
return paths;
}
/** An OpenAPI 3.0 (Swagger) description of the RESTful API that is exposed through the protocol. */
public get document(): OpenAPIDocument {
return {
openapi: "3.0.0",
info: this.protocol.info,
paths: this.paths,
};
}
/** Creates an OpenAPI description of an RPC protocol. */
public constructor(protocol: WebAppRpcProtocol) {
this.protocol = protocol;
}
/** Converts to JSON. */
public toJSON() {
return this.document;
}
private generateDescription(operation: RpcOperation): OpenAPIPathItem {
const requestContent: OpenAPIContentMap = { "application/json": { schema: { type: "array" } } };
const responseContent: OpenAPIContentMap = { "application/json": { schema: { type: "object" } } };
const description: OpenAPIPathItem = {};
description.head = {
requestBody: { content: requestContent, required: true },
responses: {
200: { description: "Success", content: responseContent },
default: { description: "Error", content: responseContent },
},
};
const parameters = this.protocol.supplyPathParametersForOperation(operation);
if (parameters.length)
description.parameters = parameters;
return description;
}
}