oas 36.0.3 → 37.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -5
- package/dist/analyzer/index.cjs +83 -28
- package/dist/analyzer/index.cjs.map +1 -1
- package/dist/analyzer/index.d.cts +3 -10
- package/dist/analyzer/index.d.ts +3 -10
- package/dist/analyzer/index.js +81 -26
- package/dist/analyzer/index.js.map +1 -1
- package/dist/analyzer/types.d.cts +0 -1
- package/dist/analyzer/types.d.ts +0 -1
- package/dist/{chunk-BJCTM2ME.cjs → chunk-GS43VKJH.cjs} +319 -270
- package/dist/chunk-GS43VKJH.cjs.map +1 -0
- package/dist/{chunk-6MDVLJ3A.js → chunk-IEN4GZPF.js} +9 -23
- package/dist/chunk-IEN4GZPF.js.map +1 -0
- package/dist/{chunk-SCWW2SNX.cjs → chunk-UKD63LKG.cjs} +10 -24
- package/dist/chunk-UKD63LKG.cjs.map +1 -0
- package/dist/{chunk-RQZ2BPMU.js → chunk-WIVQX3DA.js} +198 -149
- package/dist/chunk-WIVQX3DA.js.map +1 -0
- package/dist/index.cjs +634 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -135
- package/dist/index.d.ts +38 -135
- package/dist/index.js +633 -6
- package/dist/index.js.map +1 -1
- package/dist/operation/index.cjs +3 -3
- package/dist/operation/index.js +2 -2
- package/dist/reducer/index.cjs +7 -7
- package/dist/reducer/index.js +1 -1
- package/dist/utils.cjs +2 -2
- package/dist/utils.js +1 -1
- package/package.json +1 -2
- package/dist/chunk-4WJUHXQH.js +0 -866
- package/dist/chunk-4WJUHXQH.js.map +0 -1
- package/dist/chunk-6MDVLJ3A.js.map +0 -1
- package/dist/chunk-BJCTM2ME.cjs.map +0 -1
- package/dist/chunk-LJWHPW4H.cjs +0 -866
- package/dist/chunk-LJWHPW4H.cjs.map +0 -1
- package/dist/chunk-RQZ2BPMU.js.map +0 -1
- package/dist/chunk-SCWW2SNX.cjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,637 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
Operation,
|
|
3
|
+
Webhook,
|
|
4
|
+
defaultVariablesFromServers,
|
|
5
|
+
filterPathMethods,
|
|
6
|
+
findTargetPath,
|
|
7
|
+
generatePathMatches,
|
|
8
|
+
getUserVariable,
|
|
9
|
+
normalizedURLFromServers,
|
|
10
|
+
splitUrlFromServers,
|
|
11
|
+
stripTrailingSlash,
|
|
12
|
+
transformURLIntoRegex,
|
|
13
|
+
variablesFromServers
|
|
14
|
+
} from "./chunk-WIVQX3DA.js";
|
|
15
|
+
import {
|
|
16
|
+
SERVER_VARIABLE_REGEX,
|
|
17
|
+
dereferenceRef,
|
|
18
|
+
supportedMethods
|
|
19
|
+
} from "./chunk-IEN4GZPF.js";
|
|
20
|
+
import {
|
|
21
|
+
CODE_SAMPLES,
|
|
22
|
+
HEADERS,
|
|
23
|
+
OAUTH_OPTIONS,
|
|
24
|
+
PARAMETER_ORDERING,
|
|
25
|
+
SAMPLES_LANGUAGES,
|
|
26
|
+
extensionDefaults,
|
|
27
|
+
getExtension,
|
|
28
|
+
hasRootExtension,
|
|
29
|
+
validateParameterOrdering
|
|
30
|
+
} from "./chunk-S27IGTVG.js";
|
|
31
|
+
import {
|
|
32
|
+
isOpenAPI31,
|
|
33
|
+
isRef
|
|
34
|
+
} from "./chunk-XG4HGNCN.js";
|
|
35
|
+
|
|
36
|
+
// src/lib/get-auth.ts
|
|
37
|
+
function getPrimitiveDefaultAuth(scheme) {
|
|
38
|
+
return typeof scheme["x-default"] === "number" || typeof scheme["x-default"] === "string" ? scheme["x-default"] : null;
|
|
39
|
+
}
|
|
40
|
+
function getKey(user, scheme) {
|
|
41
|
+
switch (scheme.type) {
|
|
42
|
+
case "oauth2":
|
|
43
|
+
return user[scheme._key] || user.apiKey || getPrimitiveDefaultAuth(scheme) || null;
|
|
44
|
+
case "apiKey":
|
|
45
|
+
return user[scheme._key] || user.apiKey || scheme["x-default"] || null;
|
|
46
|
+
case "http":
|
|
47
|
+
if (scheme.scheme === "basic") {
|
|
48
|
+
return user[scheme._key] || (user.user || user.pass ? { user: user.user || null, pass: user.pass || null } : scheme["x-default"]) || {
|
|
49
|
+
user: null,
|
|
50
|
+
pass: null
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (scheme.scheme === "bearer") {
|
|
54
|
+
return user[scheme._key] || user.apiKey || scheme["x-default"] || null;
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
default:
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function getByScheme(user, scheme = {}, selectedApp) {
|
|
62
|
+
if (user?.keys?.length) {
|
|
63
|
+
if (selectedApp) {
|
|
64
|
+
const userKey = user.keys.find((k) => k.name === selectedApp);
|
|
65
|
+
if (!userKey) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return getKey(userKey, scheme);
|
|
69
|
+
}
|
|
70
|
+
return getKey(user.keys[0], scheme);
|
|
71
|
+
}
|
|
72
|
+
return getKey(user, scheme);
|
|
73
|
+
}
|
|
74
|
+
function getAuth(api, user, selectedApp) {
|
|
75
|
+
return Object.keys(api?.components?.securitySchemes || {}).map((scheme) => {
|
|
76
|
+
const securityScheme = dereferenceRef(api.components?.securitySchemes?.[scheme], api);
|
|
77
|
+
if (!securityScheme || isRef(securityScheme)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
[scheme]: getByScheme(
|
|
82
|
+
user,
|
|
83
|
+
{
|
|
84
|
+
...securityScheme,
|
|
85
|
+
_key: scheme
|
|
86
|
+
},
|
|
87
|
+
selectedApp
|
|
88
|
+
)
|
|
89
|
+
};
|
|
90
|
+
}).filter((item) => item !== void 0).reduce((prev, next) => Object.assign(prev, next), {});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/index.ts
|
|
94
|
+
var Oas = class _Oas {
|
|
95
|
+
/**
|
|
96
|
+
* The current OpenAPI definition.
|
|
97
|
+
*/
|
|
98
|
+
api;
|
|
99
|
+
/**
|
|
100
|
+
* The current user that we should use when pulling auth tokens from security schemes.
|
|
101
|
+
*/
|
|
102
|
+
user;
|
|
103
|
+
/**
|
|
104
|
+
* @param oas An OpenAPI definition.
|
|
105
|
+
* @param user The information about a user that we should use when pulling auth tokens from
|
|
106
|
+
* security schemes.
|
|
107
|
+
*/
|
|
108
|
+
constructor(oas, user) {
|
|
109
|
+
if (typeof oas === "string") {
|
|
110
|
+
this.api = JSON.parse(oas) || {};
|
|
111
|
+
} else {
|
|
112
|
+
this.api = oas || {};
|
|
113
|
+
}
|
|
114
|
+
this.user = user || {};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* This will initialize a new instance of the `Oas` class. This method is useful if you're using
|
|
118
|
+
* Typescript and are attempting to supply an untyped JSON object into `Oas` as it will force-type
|
|
119
|
+
* that object to an `OASDocument` for you.
|
|
120
|
+
*
|
|
121
|
+
* @param oas An OpenAPI definition.
|
|
122
|
+
* @param user The information about a user that we should use when pulling auth tokens from
|
|
123
|
+
* security schemes.
|
|
124
|
+
*/
|
|
125
|
+
static init(oas, user) {
|
|
126
|
+
return new _Oas(oas, user);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Retrieve the OpenAPI version that this API definition is targeted for.
|
|
130
|
+
*/
|
|
131
|
+
getVersion() {
|
|
132
|
+
if (this.api.openapi) {
|
|
133
|
+
return this.api.openapi;
|
|
134
|
+
}
|
|
135
|
+
throw new Error("Unable to recognize what specification version this API definition conforms to.");
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Retrieve the current OpenAPI API Definition.
|
|
139
|
+
*
|
|
140
|
+
*/
|
|
141
|
+
getDefinition() {
|
|
142
|
+
return this.api;
|
|
143
|
+
}
|
|
144
|
+
url(selected = 0, variables) {
|
|
145
|
+
const url = normalizedURLFromServers(this.api.servers, selected);
|
|
146
|
+
return this.replaceUrl(url, variables || this.defaultVariables(selected)).trim();
|
|
147
|
+
}
|
|
148
|
+
variables(selected = 0) {
|
|
149
|
+
return variablesFromServers(this.api.servers, selected);
|
|
150
|
+
}
|
|
151
|
+
defaultVariables(selected = 0) {
|
|
152
|
+
return defaultVariablesFromServers(this.api.servers, selected, this.user);
|
|
153
|
+
}
|
|
154
|
+
splitUrl(selected = 0) {
|
|
155
|
+
return splitUrlFromServers(this.api.servers, selected);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* With a fully composed server URL, run through our list of known OAS servers and return back
|
|
159
|
+
* which server URL was selected along with any contained server variables split out.
|
|
160
|
+
*
|
|
161
|
+
* For example, if you have an OAS server URL of `https://{name}.example.com:{port}/{basePath}`,
|
|
162
|
+
* and pass in `https://buster.example.com:3000/pet` to this function, you'll get back the
|
|
163
|
+
* following:
|
|
164
|
+
*
|
|
165
|
+
* { selected: 0, variables: { name: 'buster', port: 3000, basePath: 'pet' } }
|
|
166
|
+
*
|
|
167
|
+
* Re-supplying this data to `oas.url()` should return the same URL you passed into this method.
|
|
168
|
+
*
|
|
169
|
+
* @param baseUrl A given URL to extract server variables out of.
|
|
170
|
+
*/
|
|
171
|
+
splitVariables(baseUrl) {
|
|
172
|
+
const matchedServer = (this.api.servers || []).map((server, i) => {
|
|
173
|
+
const rgx = transformURLIntoRegex(server.url);
|
|
174
|
+
const found = new RegExp(rgx).exec(baseUrl);
|
|
175
|
+
if (!found) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
const variables = {};
|
|
179
|
+
Array.from(server.url.matchAll(SERVER_VARIABLE_REGEX)).forEach((variable, y) => {
|
|
180
|
+
variables[variable[1]] = found[y + 1];
|
|
181
|
+
});
|
|
182
|
+
return {
|
|
183
|
+
selected: i,
|
|
184
|
+
variables
|
|
185
|
+
};
|
|
186
|
+
}).filter((item) => item !== false);
|
|
187
|
+
return matchedServer.length ? matchedServer[0] : false;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Replace templated variables with supplied data in a given URL.
|
|
191
|
+
*
|
|
192
|
+
* There are a couple ways that this will utilize variable data:
|
|
193
|
+
*
|
|
194
|
+
* - Supplying a `variables` object. If this is supplied, this data will always take priority.
|
|
195
|
+
* This incoming `variables` object can be two formats:
|
|
196
|
+
* `{ variableName: { default: 'value' } }` and `{ variableName: 'value' }`. If the former is
|
|
197
|
+
* present, that will take precedence over the latter.
|
|
198
|
+
* - If the supplied `variables` object is empty or does not match the current template name,
|
|
199
|
+
* we fallback to the data stored in `this.user` and attempt to match against that.
|
|
200
|
+
* See `getUserVariable` for some more information on how this data is pulled from `this.user`.
|
|
201
|
+
*
|
|
202
|
+
* If no variables supplied match up with the template name, the template name will instead be
|
|
203
|
+
* used as the variable data.
|
|
204
|
+
*
|
|
205
|
+
* @param url A URL to swap variables into.
|
|
206
|
+
* @param variables An object containing variables to swap into the URL.
|
|
207
|
+
*/
|
|
208
|
+
replaceUrl(url, variables = {}) {
|
|
209
|
+
return stripTrailingSlash(
|
|
210
|
+
url.replace(SERVER_VARIABLE_REGEX, (original, key) => {
|
|
211
|
+
if (key in variables) {
|
|
212
|
+
const data = variables[key];
|
|
213
|
+
if (typeof data === "object") {
|
|
214
|
+
if (!Array.isArray(data) && data !== null && "default" in data) {
|
|
215
|
+
return String(data.default);
|
|
216
|
+
}
|
|
217
|
+
} else {
|
|
218
|
+
return String(data);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const userVariable = getUserVariable(this.user, key);
|
|
222
|
+
if (userVariable) {
|
|
223
|
+
return String(userVariable);
|
|
224
|
+
}
|
|
225
|
+
return original;
|
|
226
|
+
})
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Retrieve an Operation of Webhook class instance for a given path and method.
|
|
231
|
+
*
|
|
232
|
+
* @param path Path to lookup and retrieve.
|
|
233
|
+
* @param method HTTP Method to retrieve on the path.
|
|
234
|
+
*/
|
|
235
|
+
operation(path, method, opts = {}) {
|
|
236
|
+
let operation = {
|
|
237
|
+
parameters: []
|
|
238
|
+
};
|
|
239
|
+
if (opts.isWebhook) {
|
|
240
|
+
if (isOpenAPI31(this.api)) {
|
|
241
|
+
const webhookPath = dereferenceRef(this.api?.webhooks?.[path], this.api);
|
|
242
|
+
if (webhookPath && !isRef(webhookPath)) {
|
|
243
|
+
if (webhookPath?.[method]) {
|
|
244
|
+
operation = webhookPath[method];
|
|
245
|
+
return new Webhook(this, path, method, operation);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (this?.api?.paths?.[path]) {
|
|
251
|
+
const pathItem = dereferenceRef(this.api.paths[path], this.api);
|
|
252
|
+
if (pathItem?.[method]) {
|
|
253
|
+
operation = dereferenceRef(pathItem[method], this.api);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return new Operation(this, path, method, operation);
|
|
257
|
+
}
|
|
258
|
+
findOperationMatches(url) {
|
|
259
|
+
const { origin, hostname } = new URL(url);
|
|
260
|
+
const originRegExp = new RegExp(origin, "i");
|
|
261
|
+
const { servers, paths } = this.api;
|
|
262
|
+
let pathName;
|
|
263
|
+
let targetServer;
|
|
264
|
+
let matchedServer;
|
|
265
|
+
if (!servers || !servers.length) {
|
|
266
|
+
matchedServer = {
|
|
267
|
+
url: "https://example.com"
|
|
268
|
+
};
|
|
269
|
+
} else {
|
|
270
|
+
matchedServer = servers.find((s) => originRegExp.exec(this.replaceUrl(s.url, s.variables || {})));
|
|
271
|
+
if (!matchedServer) {
|
|
272
|
+
const hostnameRegExp = new RegExp(hostname);
|
|
273
|
+
matchedServer = servers.find((s) => hostnameRegExp.exec(this.replaceUrl(s.url, s.variables || {})));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (matchedServer) {
|
|
277
|
+
targetServer = {
|
|
278
|
+
...matchedServer,
|
|
279
|
+
url: this.replaceUrl(matchedServer.url, matchedServer.variables || {})
|
|
280
|
+
};
|
|
281
|
+
[, pathName] = url.split(new RegExp(targetServer.url, "i"));
|
|
282
|
+
}
|
|
283
|
+
if (!matchedServer || !pathName) {
|
|
284
|
+
const matchedServerAndPath = (servers || []).map((server) => {
|
|
285
|
+
const rgx = transformURLIntoRegex(server.url);
|
|
286
|
+
const found = new RegExp(rgx).exec(url);
|
|
287
|
+
if (!found) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
matchedServer: server,
|
|
292
|
+
pathName: url.split(new RegExp(rgx)).slice(-1).pop()
|
|
293
|
+
};
|
|
294
|
+
}).filter((item) => item !== void 0);
|
|
295
|
+
if (!matchedServerAndPath.length) {
|
|
296
|
+
return void 0;
|
|
297
|
+
}
|
|
298
|
+
pathName = matchedServerAndPath[0].pathName;
|
|
299
|
+
targetServer = {
|
|
300
|
+
...matchedServerAndPath[0].matchedServer
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
if (pathName === void 0) return void 0;
|
|
304
|
+
if (pathName === "") pathName = "/";
|
|
305
|
+
if (!paths || !targetServer) return void 0;
|
|
306
|
+
const annotatedPaths = generatePathMatches(paths, pathName, targetServer.url);
|
|
307
|
+
if (!annotatedPaths.length) return void 0;
|
|
308
|
+
return annotatedPaths;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Discover an operation in an OAS from a fully-formed URL and HTTP method. Will return an object
|
|
312
|
+
* containing a `url` object and another one for `operation`. This differs from `getOperation()`
|
|
313
|
+
* in that it does not return an instance of the `Operation` class.
|
|
314
|
+
*
|
|
315
|
+
* @param url A full URL to look up.
|
|
316
|
+
* @param method The cooresponding HTTP method to look up.
|
|
317
|
+
*/
|
|
318
|
+
findOperation(url, method) {
|
|
319
|
+
const annotatedPaths = this.findOperationMatches(url);
|
|
320
|
+
if (!annotatedPaths) {
|
|
321
|
+
return void 0;
|
|
322
|
+
}
|
|
323
|
+
const matches = filterPathMethods(annotatedPaths, method);
|
|
324
|
+
if (!matches.length) return void 0;
|
|
325
|
+
return findTargetPath(matches);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Discover an operation in an OAS from a fully-formed URL without an HTTP method. Will return an
|
|
329
|
+
* object containing a `url` object and another one for `operation`.
|
|
330
|
+
*
|
|
331
|
+
* @param url A full URL to look up.
|
|
332
|
+
*/
|
|
333
|
+
findOperationWithoutMethod(url) {
|
|
334
|
+
const annotatedPaths = this.findOperationMatches(url);
|
|
335
|
+
if (!annotatedPaths) {
|
|
336
|
+
return void 0;
|
|
337
|
+
}
|
|
338
|
+
return findTargetPath(annotatedPaths);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Retrieve an operation in an OAS from a fully-formed URL and HTTP method. Differs from
|
|
342
|
+
* `findOperation` in that while this method will return an `Operation` instance,
|
|
343
|
+
* `findOperation()` does not.
|
|
344
|
+
*
|
|
345
|
+
* @param url A full URL to look up.
|
|
346
|
+
* @param method The cooresponding HTTP method to look up.
|
|
347
|
+
*/
|
|
348
|
+
getOperation(url, method) {
|
|
349
|
+
const op = this.findOperation(url, method);
|
|
350
|
+
if (op === void 0) {
|
|
351
|
+
return void 0;
|
|
352
|
+
}
|
|
353
|
+
return this.operation(op.url.nonNormalizedPath, method);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Retrieve an operation in an OAS by an `operationId`.
|
|
357
|
+
*
|
|
358
|
+
* If an operation does not have an `operationId` one will be generated in place, using the
|
|
359
|
+
* default behavior of `Operation.getOperationId()`, and then asserted against your query.
|
|
360
|
+
*
|
|
361
|
+
* Note that because `operationId`s are unique that uniqueness does include casing so the ID
|
|
362
|
+
* you are looking for will be asserted as an exact match.
|
|
363
|
+
*
|
|
364
|
+
* @see {Operation.getOperationId()}
|
|
365
|
+
* @param id The `operationId` to look up.
|
|
366
|
+
*/
|
|
367
|
+
getOperationById(id) {
|
|
368
|
+
let found;
|
|
369
|
+
Object.values(this.getPaths()).forEach((operations) => {
|
|
370
|
+
if (found) return;
|
|
371
|
+
found = Object.values(operations).find((operation) => operation.getOperationId() === id);
|
|
372
|
+
});
|
|
373
|
+
if (found) {
|
|
374
|
+
return found;
|
|
375
|
+
}
|
|
376
|
+
Object.entries(this.getWebhooks()).forEach(([, webhooks]) => {
|
|
377
|
+
if (found) return;
|
|
378
|
+
found = Object.values(webhooks).find((webhook) => webhook.getOperationId() === id);
|
|
379
|
+
});
|
|
380
|
+
return found;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* With an object of user information, retrieve the appropriate API auth keys from the current
|
|
384
|
+
* OAS definition.
|
|
385
|
+
*
|
|
386
|
+
* @see {@link https://docs.readme.com/docs/passing-data-to-jwt}
|
|
387
|
+
* @param user User
|
|
388
|
+
* @param selectedApp The user app to retrieve an auth key for.
|
|
389
|
+
*/
|
|
390
|
+
getAuth(user, selectedApp) {
|
|
391
|
+
if (!this.api?.components?.securitySchemes) {
|
|
392
|
+
return {};
|
|
393
|
+
}
|
|
394
|
+
return getAuth(this.api, user, selectedApp);
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Determine if a security scheme exists within the API definition.
|
|
398
|
+
*
|
|
399
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#security-scheme-object}
|
|
400
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#security-scheme-object}
|
|
401
|
+
* @param name The name of the security scheme to check for.
|
|
402
|
+
*/
|
|
403
|
+
hasSecurityScheme(name) {
|
|
404
|
+
return Boolean(this.api?.components?.securitySchemes?.[name]);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Retrieve a security scheme from the API definition.
|
|
408
|
+
*
|
|
409
|
+
* If the found security scheme is a `$ref` pointer it will be lazily dereferenced; if the scheme
|
|
410
|
+
* cannot be resolved after that process (eg. it's circular or is an invalid `$ref`) then
|
|
411
|
+
* `undefined` will be returned.
|
|
412
|
+
*
|
|
413
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#security-scheme-object}
|
|
414
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#security-scheme-object}
|
|
415
|
+
* @param name The name of the security scheme to retrieve.
|
|
416
|
+
*/
|
|
417
|
+
getSecurityScheme(name) {
|
|
418
|
+
if (!this.hasSecurityScheme(name)) {
|
|
419
|
+
return void 0;
|
|
420
|
+
}
|
|
421
|
+
let scheme = this.api?.components?.securitySchemes?.[name];
|
|
422
|
+
if (!scheme) return void 0;
|
|
423
|
+
if (isRef(scheme)) {
|
|
424
|
+
scheme = dereferenceRef(scheme, this.api);
|
|
425
|
+
if (!scheme || isRef(scheme)) return void 0;
|
|
426
|
+
}
|
|
427
|
+
return scheme;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Returns the `paths` object that exists in this API definition but with every `method` mapped
|
|
431
|
+
* to an instance of the `Operation` class.
|
|
432
|
+
*
|
|
433
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#openapi-object}
|
|
434
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#openapi-object}
|
|
435
|
+
*/
|
|
436
|
+
getPaths() {
|
|
437
|
+
const paths = {};
|
|
438
|
+
if (!this.api.paths) {
|
|
439
|
+
return paths;
|
|
440
|
+
}
|
|
441
|
+
Object.keys(this.api.paths).forEach((path) => {
|
|
442
|
+
if (path.startsWith("x-")) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
paths[path] = {};
|
|
446
|
+
let pathItem = this.api.paths[path];
|
|
447
|
+
if (!pathItem) {
|
|
448
|
+
return;
|
|
449
|
+
} else if (isRef(pathItem)) {
|
|
450
|
+
this.api.paths[path] = dereferenceRef(pathItem, this.api);
|
|
451
|
+
pathItem = this.api.paths[path];
|
|
452
|
+
if (!pathItem || isRef(pathItem)) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
Object.keys(pathItem).forEach((method) => {
|
|
457
|
+
if (!supportedMethods.includes(method)) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
paths[path][method] = this.operation(path, method);
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
return paths;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Returns the `webhooks` object that exists in this API definition but with every `method`
|
|
467
|
+
* mapped to an instance of the `Webhook` class.
|
|
468
|
+
*
|
|
469
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#openapi-object}
|
|
470
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#openapi-object}
|
|
471
|
+
*/
|
|
472
|
+
getWebhooks() {
|
|
473
|
+
const webhooks = {};
|
|
474
|
+
if (!isOpenAPI31(this.api) || !this.api.webhooks) {
|
|
475
|
+
return webhooks;
|
|
476
|
+
}
|
|
477
|
+
Object.keys(this.api.webhooks).forEach((id) => {
|
|
478
|
+
webhooks[id] = {};
|
|
479
|
+
const webhookPath = dereferenceRef(this.api.webhooks?.[id], this.api);
|
|
480
|
+
if (webhookPath) {
|
|
481
|
+
Object.keys(webhookPath).forEach((method) => {
|
|
482
|
+
if (!supportedMethods.includes(method)) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
webhooks[id][method] = this.operation(id, method, {
|
|
486
|
+
isWebhook: true
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
return webhooks;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Return an array of all tag names that exist on this API definition.
|
|
495
|
+
*
|
|
496
|
+
* If the API definition uses the `x-disable-tag-sorting` extension then tags will be returned in
|
|
497
|
+
* the order they're defined.
|
|
498
|
+
*
|
|
499
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#openapi-object}
|
|
500
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#openapi-object}
|
|
501
|
+
* @param setIfMissing If a tag is not present on an operation that operations path will be added
|
|
502
|
+
* into the list of tags returned.
|
|
503
|
+
*/
|
|
504
|
+
getTags(setIfMissing = false) {
|
|
505
|
+
const allTags = /* @__PURE__ */ new Set();
|
|
506
|
+
const oasTags = this.api.tags?.map((tag) => tag.name) || [];
|
|
507
|
+
const disableTagSorting = getExtension("disable-tag-sorting", this.api);
|
|
508
|
+
Object.entries(this.getPaths()).forEach(([path, operations]) => {
|
|
509
|
+
Object.values(operations).forEach((operation) => {
|
|
510
|
+
const tags = operation.getTags();
|
|
511
|
+
if (setIfMissing && !tags.length) {
|
|
512
|
+
allTags.add(path);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
tags.forEach((tag) => {
|
|
516
|
+
allTags.add(tag.name);
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
Object.entries(this.getWebhooks()).forEach(([path, webhooks]) => {
|
|
521
|
+
Object.values(webhooks).forEach((webhook) => {
|
|
522
|
+
const tags = webhook.getTags();
|
|
523
|
+
if (setIfMissing && !tags.length) {
|
|
524
|
+
allTags.add(path);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
tags.forEach((tag) => {
|
|
528
|
+
allTags.add(tag.name);
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
const endpointTags = [];
|
|
533
|
+
const tagsArray = [];
|
|
534
|
+
if (disableTagSorting) {
|
|
535
|
+
return Array.from(allTags);
|
|
536
|
+
}
|
|
537
|
+
Array.from(allTags).forEach((tag) => {
|
|
538
|
+
if (oasTags.includes(tag)) {
|
|
539
|
+
tagsArray.push(tag);
|
|
540
|
+
} else {
|
|
541
|
+
endpointTags.push(tag);
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
let sortedTags = tagsArray.toSorted((a, b) => {
|
|
545
|
+
return oasTags.indexOf(a) - oasTags.indexOf(b);
|
|
546
|
+
});
|
|
547
|
+
sortedTags = sortedTags.concat(endpointTags);
|
|
548
|
+
return sortedTags;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Determine if a given a custom specification extension exists within the API definition.
|
|
552
|
+
*
|
|
553
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specification-extensions}
|
|
554
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specification-extensions}
|
|
555
|
+
* @param extension Specification extension to lookup.
|
|
556
|
+
*/
|
|
557
|
+
hasExtension(extension) {
|
|
558
|
+
return hasRootExtension(extension, this.api);
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Retrieve a custom specification extension off of the API definition.
|
|
562
|
+
*
|
|
563
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specification-extensions}
|
|
564
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specification-extensions}
|
|
565
|
+
* @param extension Specification extension to lookup.
|
|
566
|
+
*/
|
|
567
|
+
getExtension(extension, operation) {
|
|
568
|
+
return getExtension(extension, this.api, operation);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Determine if a given OpenAPI custom extension is valid or not.
|
|
572
|
+
*
|
|
573
|
+
* @see {@link https://docs.readme.com/docs/openapi-extensions}
|
|
574
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specification-extensions}
|
|
575
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specification-extensions}
|
|
576
|
+
* @param extension Specification extension to validate.
|
|
577
|
+
* @throws
|
|
578
|
+
*/
|
|
579
|
+
validateExtension(extension) {
|
|
580
|
+
if (this.hasExtension("x-readme")) {
|
|
581
|
+
const data = this.getExtension("x-readme");
|
|
582
|
+
if (typeof data !== "object" || Array.isArray(data) || data === null) {
|
|
583
|
+
throw new TypeError('"x-readme" must be of type "Object"');
|
|
584
|
+
}
|
|
585
|
+
if (extension in data) {
|
|
586
|
+
if ([CODE_SAMPLES, HEADERS, PARAMETER_ORDERING, SAMPLES_LANGUAGES].includes(extension)) {
|
|
587
|
+
if (data[extension] !== void 0) {
|
|
588
|
+
if (!Array.isArray(data[extension])) {
|
|
589
|
+
throw new TypeError(`"x-readme.${extension}" must be of type "Array"`);
|
|
590
|
+
}
|
|
591
|
+
if (extension === PARAMETER_ORDERING) {
|
|
592
|
+
validateParameterOrdering(data[extension], `x-readme.${extension}`);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
} else if (extension === OAUTH_OPTIONS) {
|
|
596
|
+
if (typeof data[extension] !== "object") {
|
|
597
|
+
throw new TypeError(`"x-readme.${extension}" must be of type "Object"`);
|
|
598
|
+
}
|
|
599
|
+
} else if (typeof data[extension] !== "boolean") {
|
|
600
|
+
throw new TypeError(`"x-readme.${extension}" must be of type "Boolean"`);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
if (this.hasExtension(`x-${extension}`)) {
|
|
605
|
+
const data = this.getExtension(`x-${extension}`);
|
|
606
|
+
if ([CODE_SAMPLES, HEADERS, PARAMETER_ORDERING, SAMPLES_LANGUAGES].includes(extension)) {
|
|
607
|
+
if (!Array.isArray(data)) {
|
|
608
|
+
throw new TypeError(`"x-${extension}" must be of type "Array"`);
|
|
609
|
+
}
|
|
610
|
+
if (extension === PARAMETER_ORDERING) {
|
|
611
|
+
validateParameterOrdering(data, `x-${extension}`);
|
|
612
|
+
}
|
|
613
|
+
} else if (extension === OAUTH_OPTIONS) {
|
|
614
|
+
if (typeof data !== "object") {
|
|
615
|
+
throw new TypeError(`"x-${extension}" must be of type "Object"`);
|
|
616
|
+
}
|
|
617
|
+
} else if (typeof data !== "boolean") {
|
|
618
|
+
throw new TypeError(`"x-${extension}" must be of type "Boolean"`);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Validate all of our custom or known OpenAPI extensions, throwing exceptions when necessary.
|
|
624
|
+
*
|
|
625
|
+
* @see {@link https://docs.readme.com/docs/openapi-extensions}
|
|
626
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specification-extensions}
|
|
627
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specification-extensions}
|
|
628
|
+
*/
|
|
629
|
+
validateExtensions() {
|
|
630
|
+
Object.keys(extensionDefaults).forEach((extension) => {
|
|
631
|
+
this.validateExtension(extension);
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
};
|
|
8
635
|
export {
|
|
9
636
|
Oas as default
|
|
10
637
|
};
|