api 4.5.0 → 5.0.0-beta.1
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/LICENSE +1 -1
- package/README.md +4 -5
- package/bin/api +2 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +91 -0
- package/dist/cache.d.ts +30 -0
- package/dist/cache.js +217 -0
- package/dist/cli/codegen/index.d.ts +4 -0
- package/dist/cli/codegen/index.js +23 -0
- package/dist/cli/codegen/language.d.ts +27 -0
- package/dist/cli/codegen/language.js +19 -0
- package/dist/cli/codegen/languages/typescript.d.ts +99 -0
- package/dist/cli/codegen/languages/typescript.js +762 -0
- package/dist/cli/commands/index.d.ts +4 -0
- package/dist/cli/commands/index.js +9 -0
- package/dist/cli/commands/install.d.ts +3 -0
- package/dist/cli/commands/install.js +230 -0
- package/dist/cli/lib/prompt.d.ts +9 -0
- package/dist/cli/lib/prompt.js +81 -0
- package/dist/cli/logger.d.ts +1 -0
- package/dist/cli/logger.js +16 -0
- package/dist/cli/storage.d.ts +105 -0
- package/dist/cli/storage.js +264 -0
- package/dist/core/getJSONSchemaDefaults.d.ts +15 -0
- package/dist/core/getJSONSchemaDefaults.js +62 -0
- package/dist/core/index.d.ts +32 -0
- package/dist/core/index.js +143 -0
- package/dist/core/parseResponse.d.ts +1 -0
- package/dist/core/parseResponse.js +65 -0
- package/dist/core/prepareAuth.d.ts +5 -0
- package/dist/core/prepareAuth.js +55 -0
- package/dist/core/prepareParams.d.ts +24 -0
- package/dist/core/prepareParams.js +351 -0
- package/dist/core/prepareServer.d.ts +13 -0
- package/dist/core/prepareServer.js +50 -0
- package/dist/fetcher.d.ts +53 -0
- package/dist/fetcher.js +149 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +276 -0
- package/dist/packageInfo.d.ts +2 -0
- package/dist/packageInfo.js +6 -0
- package/package.json +65 -26
- package/src/.sink.d.ts +1 -0
- package/src/bin.ts +20 -0
- package/src/cache.ts +212 -0
- package/src/cli/codegen/index.ts +31 -0
- package/src/cli/codegen/language.ts +47 -0
- package/src/cli/codegen/languages/typescript.ts +798 -0
- package/src/cli/commands/index.ts +5 -0
- package/src/cli/commands/install.ts +196 -0
- package/src/cli/lib/prompt.ts +29 -0
- package/src/cli/logger.ts +10 -0
- package/src/cli/storage.ts +297 -0
- package/src/core/getJSONSchemaDefaults.ts +74 -0
- package/src/core/index.ts +108 -0
- package/src/{lib/parseResponse.js → core/parseResponse.ts} +5 -7
- package/src/core/prepareAuth.ts +85 -0
- package/src/core/prepareParams.ts +338 -0
- package/src/{lib/prepareServer.js → core/prepareServer.ts} +13 -12
- package/src/fetcher.ts +126 -0
- package/src/index.ts +212 -0
- package/src/packageInfo.ts +3 -0
- package/src/typings.d.ts +3 -0
- package/tsconfig.json +24 -0
- package/src/cache.js +0 -214
- package/src/index.js +0 -177
- package/src/lib/getSchema.js +0 -34
- package/src/lib/index.js +0 -11
- package/src/lib/prepareAuth.js +0 -69
- package/src/lib/prepareParams.js +0 -198
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Operation } from 'oas';
|
|
3
|
+
/**
|
|
4
|
+
* With potentially supplied body and/or metadata we need to run through them against a given API
|
|
5
|
+
* operation to see what's what and prepare any available parameters to be used in an API request
|
|
6
|
+
* with `@readme/oas-to-har`.
|
|
7
|
+
*
|
|
8
|
+
* @param operation
|
|
9
|
+
* @param body
|
|
10
|
+
* @param metadata
|
|
11
|
+
*/
|
|
12
|
+
export default function prepareParams(operation: Operation, body?: unknown, metadata?: Record<string, unknown>): Promise<{
|
|
13
|
+
body?: any;
|
|
14
|
+
cookie?: Record<string, string | number | boolean>;
|
|
15
|
+
files?: Record<string, Buffer>;
|
|
16
|
+
formData?: any;
|
|
17
|
+
header?: Record<string, string | number | boolean>;
|
|
18
|
+
path?: Record<string, string | number | boolean>;
|
|
19
|
+
query?: Record<string, string | number | boolean>;
|
|
20
|
+
server?: {
|
|
21
|
+
selected: number;
|
|
22
|
+
variables: Record<string, string | number>;
|
|
23
|
+
};
|
|
24
|
+
}>;
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
exports.__esModule = true;
|
|
42
|
+
var lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
43
|
+
var promises_1 = __importDefault(require("fs/promises"));
|
|
44
|
+
var path_1 = __importDefault(require("path"));
|
|
45
|
+
var stream_1 = __importDefault(require("stream"));
|
|
46
|
+
var get_stream_1 = __importDefault(require("get-stream"));
|
|
47
|
+
var sync_1 = __importDefault(require("datauri/sync"));
|
|
48
|
+
var parser_1 = __importDefault(require("datauri/parser"));
|
|
49
|
+
var getJSONSchemaDefaults_1 = __importDefault(require("./getJSONSchemaDefaults"));
|
|
50
|
+
/**
|
|
51
|
+
* Extract all available parameters from an operations Parameter Object into a digestable array
|
|
52
|
+
* that we can use to apply to the request.
|
|
53
|
+
*
|
|
54
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#parameterObject}
|
|
55
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterObject}
|
|
56
|
+
* @param parameters
|
|
57
|
+
*/
|
|
58
|
+
function digestParameters(parameters) {
|
|
59
|
+
return parameters.reduce(function (prev, param) {
|
|
60
|
+
var _a;
|
|
61
|
+
if ('$ref' in param || 'allOf' in param || 'anyOf' in param || 'oneOf' in param) {
|
|
62
|
+
throw new Error("The OpenAPI document for this operation wasn't dereferenced before processing.");
|
|
63
|
+
}
|
|
64
|
+
else if (param.name in prev) {
|
|
65
|
+
throw new Error("The operation you are using has the same parameter, ".concat(param.name, ", spread across multiple entry points. We unfortunately can't handle this right now."));
|
|
66
|
+
}
|
|
67
|
+
return Object.assign(prev, (_a = {}, _a[param.name] = param, _a));
|
|
68
|
+
}, {});
|
|
69
|
+
}
|
|
70
|
+
// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_isempty
|
|
71
|
+
function isEmpty(obj) {
|
|
72
|
+
return [Object, Array].includes((obj || {}).constructor) && !Object.entries(obj || {}).length;
|
|
73
|
+
}
|
|
74
|
+
function isObject(thing) {
|
|
75
|
+
if (thing instanceof stream_1["default"].Readable) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return typeof thing === 'object' && thing !== null && !Array.isArray(thing);
|
|
79
|
+
}
|
|
80
|
+
function isPrimitive(obj) {
|
|
81
|
+
return typeof obj === null || typeof obj === 'number' || typeof obj === 'string';
|
|
82
|
+
}
|
|
83
|
+
function merge(src, target) {
|
|
84
|
+
if (Array.isArray(target)) {
|
|
85
|
+
// @todo we need to add support for merging array defaults with array body/metadata arguments
|
|
86
|
+
return target;
|
|
87
|
+
}
|
|
88
|
+
else if (!isObject(target)) {
|
|
89
|
+
return target;
|
|
90
|
+
}
|
|
91
|
+
return (0, lodash_merge_1["default"])(src, target);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Ingest a file path or readable stream into a common object that we can later use to process it
|
|
95
|
+
* into a parameters object for making an API request.
|
|
96
|
+
*
|
|
97
|
+
* @param paramName
|
|
98
|
+
* @param file
|
|
99
|
+
*/
|
|
100
|
+
function processFile(paramName, file) {
|
|
101
|
+
if (typeof file === 'string') {
|
|
102
|
+
// In order to support relative pathed files, we need to attempt to resolve them.
|
|
103
|
+
var resolvedFile_1 = path_1["default"].resolve(file);
|
|
104
|
+
return promises_1["default"]
|
|
105
|
+
.stat(resolvedFile_1)
|
|
106
|
+
.then(function () { return (0, sync_1["default"])(resolvedFile_1); })
|
|
107
|
+
.then(function (fileMetadata) {
|
|
108
|
+
var payloadFilename = encodeURIComponent(path_1["default"].basename(resolvedFile_1));
|
|
109
|
+
return {
|
|
110
|
+
paramName: paramName,
|
|
111
|
+
base64: fileMetadata.content.replace(';base64', ";name=".concat(payloadFilename, ";base64")),
|
|
112
|
+
filename: payloadFilename,
|
|
113
|
+
buffer: fileMetadata.buffer
|
|
114
|
+
};
|
|
115
|
+
})["catch"](function (err) {
|
|
116
|
+
if (err.code === 'ENOENT') {
|
|
117
|
+
// It's less than ideal for us to handle files that don't exist like this but because
|
|
118
|
+
// `file` is a string it might actually be the full text contents of the file and not
|
|
119
|
+
// actually a path.
|
|
120
|
+
//
|
|
121
|
+
// We also can't really regex to see if `file` *looks*` like a path because one should be
|
|
122
|
+
// able to pass in a relative `owlbert.png` (instead of `./owlbert.png`) and though that
|
|
123
|
+
// doesn't *look* like a path, it is one that should still work.
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
throw err;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
else if (file instanceof stream_1["default"].Readable) {
|
|
130
|
+
return get_stream_1["default"].buffer(file).then(function (buffer) {
|
|
131
|
+
var filePath = file.path;
|
|
132
|
+
var parser = new parser_1["default"]();
|
|
133
|
+
var base64 = parser.format(filePath, buffer).content;
|
|
134
|
+
var payloadFilename = encodeURIComponent(path_1["default"].basename(filePath));
|
|
135
|
+
return {
|
|
136
|
+
paramName: paramName,
|
|
137
|
+
base64: base64.replace(';base64', ";name=".concat(payloadFilename, ";base64")),
|
|
138
|
+
filename: payloadFilename,
|
|
139
|
+
buffer: buffer
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return Promise.reject(new TypeError(paramName
|
|
144
|
+
? "The data supplied for the `".concat(paramName, "` request body parameter is not a file handler that we support.")
|
|
145
|
+
: "The data supplied for the request body payload is not a file handler that we support."));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* With potentially supplied body and/or metadata we need to run through them against a given API
|
|
149
|
+
* operation to see what's what and prepare any available parameters to be used in an API request
|
|
150
|
+
* with `@readme/oas-to-har`.
|
|
151
|
+
*
|
|
152
|
+
* @param operation
|
|
153
|
+
* @param body
|
|
154
|
+
* @param metadata
|
|
155
|
+
*/
|
|
156
|
+
function prepareParams(operation, body, metadata) {
|
|
157
|
+
var _a, _b, _c, _d;
|
|
158
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
+
var metadataIntersected, digestedParameters, hasDigestedParams, jsonSchema, jsonSchemaDefaults, params, intersection, payloadJsonSchema, conversions_1;
|
|
160
|
+
return __generator(this, function (_e) {
|
|
161
|
+
switch (_e.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
metadataIntersected = false;
|
|
164
|
+
digestedParameters = digestParameters(operation.getParameters());
|
|
165
|
+
hasDigestedParams = !!Object.keys(digestedParameters).length;
|
|
166
|
+
jsonSchema = operation.getParametersAsJsonSchema();
|
|
167
|
+
if (!jsonSchema && (body !== undefined || metadata !== undefined)) {
|
|
168
|
+
throw new Error("You supplied metadata and/or body data for this operation but it doesn't have any documented parameters or request payloads. If you think this is an error please contact support for the API you're using.");
|
|
169
|
+
}
|
|
170
|
+
jsonSchemaDefaults = jsonSchema ? (0, getJSONSchemaDefaults_1["default"])(jsonSchema) : {};
|
|
171
|
+
params = jsonSchemaDefaults;
|
|
172
|
+
// If a body argument was supplied we need to do a bit of work to see if it's actually a body
|
|
173
|
+
// argument or metadata because the library lets you supply either a body, metadata, or body with
|
|
174
|
+
// metadata.
|
|
175
|
+
if (typeof body !== 'undefined') {
|
|
176
|
+
if (Array.isArray(body) || isPrimitive(body)) {
|
|
177
|
+
// If the body param is an array or a primitive then we know it's absolutely a body because
|
|
178
|
+
// metadata can only ever be undefined or an object.
|
|
179
|
+
params.body = merge(params.body, body);
|
|
180
|
+
}
|
|
181
|
+
else if (typeof metadata === 'undefined') {
|
|
182
|
+
// No metadata was explicitly provided so we need to analyze the body to determine if it's a
|
|
183
|
+
// body or should be actually be treated as metadata.
|
|
184
|
+
if (!hasDigestedParams) {
|
|
185
|
+
// If no parameters were able to be digested it's because this operation has none so then
|
|
186
|
+
// we just have to assume that what the user supplied was for a body and not metadata. This
|
|
187
|
+
// might lead to unwanted false positives if the API definition isn't accurate but short of
|
|
188
|
+
// throwing an error (one that the user would have no control over resolving anyways) there
|
|
189
|
+
// isn't anything we can do about it.
|
|
190
|
+
params.body = merge(params.body, body);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
intersection = Object.keys(body).filter(function (value) { return Object.keys(digestedParameters).includes(value); }).length;
|
|
194
|
+
if (intersection && intersection / Object.keys(body).length > 0.25) {
|
|
195
|
+
/* eslint-disable no-param-reassign */
|
|
196
|
+
// If more than 25% of the body intersects with the parameters that we've got on hand,
|
|
197
|
+
// then we should treat it as a metadata object and organize into parameters.
|
|
198
|
+
metadataIntersected = true;
|
|
199
|
+
metadata = merge(params.body, body);
|
|
200
|
+
body = undefined;
|
|
201
|
+
/* eslint-enable no-param-reassign */
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
// For all other cases, we should just treat the supplied body as a body.
|
|
205
|
+
params.body = merge(params.body, body);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
// Body and metadata were both supplied.
|
|
211
|
+
params.body = merge(params.body, body);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (!!operation.hasRequestBody()) return [3 /*break*/, 1];
|
|
215
|
+
// If this operation doesn't have any documented request body then we shouldn't be sending
|
|
216
|
+
// anything.
|
|
217
|
+
delete params.body;
|
|
218
|
+
return [3 /*break*/, 3];
|
|
219
|
+
case 1:
|
|
220
|
+
if (!('body' in params))
|
|
221
|
+
params.body = {};
|
|
222
|
+
payloadJsonSchema = jsonSchema.find(function (js) { return js.type === 'body'; });
|
|
223
|
+
if (!payloadJsonSchema) return [3 /*break*/, 3];
|
|
224
|
+
if (!params.files)
|
|
225
|
+
params.files = {};
|
|
226
|
+
conversions_1 = [];
|
|
227
|
+
// @todo add support for `type: array`, `oneOf` and `anyOf`
|
|
228
|
+
if ((_a = payloadJsonSchema.schema) === null || _a === void 0 ? void 0 : _a.properties) {
|
|
229
|
+
Object.entries((_b = payloadJsonSchema.schema) === null || _b === void 0 ? void 0 : _b.properties)
|
|
230
|
+
.filter(function (_a) {
|
|
231
|
+
var schema = _a[1];
|
|
232
|
+
return (schema === null || schema === void 0 ? void 0 : schema.format) === 'binary';
|
|
233
|
+
})
|
|
234
|
+
.filter(function (_a) {
|
|
235
|
+
var prop = _a[0];
|
|
236
|
+
return Object.keys(params.body).includes(prop);
|
|
237
|
+
})
|
|
238
|
+
.forEach(function (_a) {
|
|
239
|
+
var prop = _a[0];
|
|
240
|
+
conversions_1.push(processFile(prop, params.body[prop]));
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
else if (((_c = payloadJsonSchema.schema) === null || _c === void 0 ? void 0 : _c.type) === 'string') {
|
|
244
|
+
if (((_d = payloadJsonSchema.schema) === null || _d === void 0 ? void 0 : _d.format) === 'binary') {
|
|
245
|
+
conversions_1.push(processFile(undefined, params.body));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return [4 /*yield*/, Promise.all(conversions_1)
|
|
249
|
+
.then(function (fileMetadata) { return fileMetadata.filter(Boolean); })
|
|
250
|
+
.then(function (fm) {
|
|
251
|
+
fm.forEach(function (fileMetadata) {
|
|
252
|
+
if (!fileMetadata) {
|
|
253
|
+
// If we don't have any metadata here it's because the file we have is likely
|
|
254
|
+
// the full string content of the file so since we don't have any filenames to
|
|
255
|
+
// work with we shouldn't do any additional handling to the `body` or `files`
|
|
256
|
+
// parameters.
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (fileMetadata.paramName) {
|
|
260
|
+
params.body[fileMetadata.paramName] = fileMetadata.base64;
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
params.body = fileMetadata.base64;
|
|
264
|
+
}
|
|
265
|
+
params.files[fileMetadata.filename] = fileMetadata.buffer;
|
|
266
|
+
});
|
|
267
|
+
})];
|
|
268
|
+
case 2:
|
|
269
|
+
_e.sent();
|
|
270
|
+
_e.label = 3;
|
|
271
|
+
case 3:
|
|
272
|
+
// Form data should be placed within `formData` instead of `body` for it to properly get picked
|
|
273
|
+
// up by `fetch-har`.
|
|
274
|
+
if (operation.isFormUrlEncoded()) {
|
|
275
|
+
params.formData = merge(params.formData, params.body);
|
|
276
|
+
delete params.body;
|
|
277
|
+
}
|
|
278
|
+
// Only spend time trying to organize metadata into parameters if we were able to digest
|
|
279
|
+
// parameters out of the operation schema. If we couldn't digest anything, but metadata was
|
|
280
|
+
// supplied then we wouldn't know how to send it in the request!
|
|
281
|
+
if (hasDigestedParams) {
|
|
282
|
+
if (!('cookie' in params))
|
|
283
|
+
params.cookie = {};
|
|
284
|
+
if (!('header' in params))
|
|
285
|
+
params.header = {};
|
|
286
|
+
if (!('path' in params))
|
|
287
|
+
params.path = {};
|
|
288
|
+
if (!('query' in params))
|
|
289
|
+
params.query = {};
|
|
290
|
+
Object.entries(digestedParameters).forEach(function (_a) {
|
|
291
|
+
var paramName = _a[0], param = _a[1];
|
|
292
|
+
var value;
|
|
293
|
+
if (typeof metadata === 'object' && !isEmpty(metadata) && paramName in metadata) {
|
|
294
|
+
value = metadata[paramName];
|
|
295
|
+
}
|
|
296
|
+
if (value === undefined) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
/* eslint-disable no-param-reassign */
|
|
300
|
+
switch (param["in"]) {
|
|
301
|
+
case 'path':
|
|
302
|
+
params.path[paramName] = value;
|
|
303
|
+
delete metadata[paramName];
|
|
304
|
+
break;
|
|
305
|
+
case 'query':
|
|
306
|
+
params.query[paramName] = value;
|
|
307
|
+
delete metadata[paramName];
|
|
308
|
+
break;
|
|
309
|
+
case 'header':
|
|
310
|
+
params.header[paramName] = value;
|
|
311
|
+
delete metadata[paramName];
|
|
312
|
+
break;
|
|
313
|
+
case 'cookie':
|
|
314
|
+
params.cookie[paramName] = value;
|
|
315
|
+
delete metadata[paramName];
|
|
316
|
+
break;
|
|
317
|
+
default: // no-op
|
|
318
|
+
}
|
|
319
|
+
/* eslint-enable no-param-reassign */
|
|
320
|
+
// Because a user might have sent just a metadata object, we want to make sure that we filter
|
|
321
|
+
// out anything that they sent that is a parameter from also being sent as part of a form
|
|
322
|
+
// data payload for `x-www-form-urlencoded` requests.
|
|
323
|
+
if (metadataIntersected && operation.isFormUrlEncoded()) {
|
|
324
|
+
if (paramName in params.formData) {
|
|
325
|
+
delete params.formData[paramName];
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
// If there's any leftover metadata that hasn't been moved into form data for this request we
|
|
330
|
+
// need to move it or else it'll get tossed.
|
|
331
|
+
if (!isEmpty(metadata)) {
|
|
332
|
+
if (operation.isFormUrlEncoded()) {
|
|
333
|
+
params.formData = merge(params.formData, metadata);
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
// Any other remaining unused metadata will be unused because we don't know where to place
|
|
337
|
+
// it in the request.
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
['body', 'cookie', 'files', 'formData', 'header', 'path', 'query'].forEach(function (type) {
|
|
342
|
+
if (type in params && isEmpty(params[type])) {
|
|
343
|
+
delete params[type];
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
return [2 /*return*/, params];
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
exports["default"] = prepareParams;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type Oas from 'oas';
|
|
2
|
+
/**
|
|
3
|
+
* With an SDK server config and an instance of OAS we should extract and prepare the server and
|
|
4
|
+
* any server variables to be supplied to `@readme/oas-to-har`.
|
|
5
|
+
*
|
|
6
|
+
* @param spec
|
|
7
|
+
* @param url
|
|
8
|
+
* @param variables
|
|
9
|
+
*/
|
|
10
|
+
export default function prepareServer(spec: Oas, url: string, variables?: Record<string, string | number>): false | {
|
|
11
|
+
selected: number;
|
|
12
|
+
variables: Record<string, string | number>;
|
|
13
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
function stripTrailingSlash(url) {
|
|
4
|
+
if (url[url.length - 1] === '/') {
|
|
5
|
+
return url.slice(0, -1);
|
|
6
|
+
}
|
|
7
|
+
return url;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* With an SDK server config and an instance of OAS we should extract and prepare the server and
|
|
11
|
+
* any server variables to be supplied to `@readme/oas-to-har`.
|
|
12
|
+
*
|
|
13
|
+
* @param spec
|
|
14
|
+
* @param url
|
|
15
|
+
* @param variables
|
|
16
|
+
*/
|
|
17
|
+
function prepareServer(spec, url, variables) {
|
|
18
|
+
if (variables === void 0) { variables = {}; }
|
|
19
|
+
var serverIdx;
|
|
20
|
+
var sanitizedUrl = stripTrailingSlash(url);
|
|
21
|
+
(spec.api.servers || []).forEach(function (server, i) {
|
|
22
|
+
if (server.url === sanitizedUrl) {
|
|
23
|
+
serverIdx = i;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
// If we were able to find the passed in server in the OAS servers, we should use that! If we
|
|
27
|
+
// couldn't and server variables were passed in we should try our best to handle that, otherwise
|
|
28
|
+
// we should ignore the passed in server and use whever the default from the OAS is.
|
|
29
|
+
if (serverIdx) {
|
|
30
|
+
return {
|
|
31
|
+
selected: serverIdx,
|
|
32
|
+
variables: variables
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
else if (Object.keys(variables).length) {
|
|
36
|
+
// @todo we should run `oas.replaceUrl(url)` and pass that unto `@readme/oas-to-har`
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
var server = spec.splitVariables(url);
|
|
40
|
+
if (server) {
|
|
41
|
+
return {
|
|
42
|
+
selected: server.selected,
|
|
43
|
+
variables: server.variables
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// @todo we should pass `url` directly into `@readme/oas-to-har` as the base URL
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
exports["default"] = prepareServer;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { OASDocument } from 'oas/@types/rmoas.types';
|
|
2
|
+
import 'isomorphic-fetch';
|
|
3
|
+
export default class Fetcher {
|
|
4
|
+
uri: string | OASDocument;
|
|
5
|
+
/**
|
|
6
|
+
* @example @petstore/v1.0#n6kvf10vakpemvplx
|
|
7
|
+
* @example @petstore#n6kvf10vakpemvplx
|
|
8
|
+
*/
|
|
9
|
+
static registryUUIDRegex: RegExp;
|
|
10
|
+
constructor(uri: string | OASDocument);
|
|
11
|
+
static isAPIRegistryUUID(uri: string): boolean;
|
|
12
|
+
static getProjectPrefixFromRegistryUUID(uri: string): string;
|
|
13
|
+
load(): Promise<(Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
14
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
15
|
+
jsonSchemaDialect?: string;
|
|
16
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[];
|
|
17
|
+
} & Pick<{
|
|
18
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
19
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
20
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
21
|
+
}, "paths"> & Omit<Partial<{
|
|
22
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
23
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
24
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
25
|
+
}>, "paths"> & Record<string, unknown>) | (Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
26
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
27
|
+
jsonSchemaDialect?: string;
|
|
28
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[];
|
|
29
|
+
} & Pick<{
|
|
30
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
31
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
32
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
33
|
+
}, "webhooks"> & Omit<Partial<{
|
|
34
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
35
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
36
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
37
|
+
}>, "webhooks"> & Record<string, unknown>) | (Omit<Omit<import("openapi-types").OpenAPIV3.Document<{}>, "paths" | "components">, "paths" | "components" | "info" | "servers" | "webhooks" | "jsonSchemaDialect"> & {
|
|
38
|
+
info: import("openapi-types").OpenAPIV3_1.InfoObject;
|
|
39
|
+
jsonSchemaDialect?: string;
|
|
40
|
+
servers?: import("openapi-types").OpenAPIV3_1.ServerObject[];
|
|
41
|
+
} & Pick<{
|
|
42
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
43
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
44
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
45
|
+
}, "components"> & Omit<Partial<{
|
|
46
|
+
paths: import("openapi-types").OpenAPIV3_1.PathsObject<{}, {}>;
|
|
47
|
+
webhooks: Record<string, import("openapi-types").OpenAPIV3_1.ReferenceObject | import("openapi-types").OpenAPIV3_1.PathItemObject<{}>>;
|
|
48
|
+
components: import("openapi-types").OpenAPIV3_1.ComponentsObject;
|
|
49
|
+
}>, "components"> & Record<string, unknown>) | (import("openapi-types").OpenAPIV3.Document<{}> & Record<string, unknown>)>;
|
|
50
|
+
static getURL(url: string): Promise<any>;
|
|
51
|
+
static getFile(uri: string): Promise<any>;
|
|
52
|
+
static validate(json: any): Promise<import("openapi-types").OpenAPI.Document<{}>>;
|
|
53
|
+
}
|
package/dist/fetcher.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
exports.__esModule = true;
|
|
42
|
+
require("isomorphic-fetch");
|
|
43
|
+
var openapi_parser_1 = __importDefault(require("@readme/openapi-parser"));
|
|
44
|
+
var js_yaml_1 = __importDefault(require("js-yaml"));
|
|
45
|
+
var fs_1 = __importDefault(require("fs"));
|
|
46
|
+
var path_1 = __importDefault(require("path"));
|
|
47
|
+
var Fetcher = /** @class */ (function () {
|
|
48
|
+
function Fetcher(uri) {
|
|
49
|
+
if (typeof uri === 'string') {
|
|
50
|
+
// Resolve OpenAPI definition shorthand accessors from within the ReadMe API Registry.
|
|
51
|
+
this.uri = Fetcher.isAPIRegistryUUID(uri)
|
|
52
|
+
? uri.replace(Fetcher.registryUUIDRegex, 'https://dash.readme.com/api/v1/api-registry/$4')
|
|
53
|
+
: uri;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
this.uri = uri;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
Fetcher.isAPIRegistryUUID = function (uri) {
|
|
60
|
+
return Fetcher.registryUUIDRegex.test(uri);
|
|
61
|
+
};
|
|
62
|
+
Fetcher.getProjectPrefixFromRegistryUUID = function (uri) {
|
|
63
|
+
var matches = uri.match(Fetcher.registryUUIDRegex);
|
|
64
|
+
if (!matches) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
return matches.groups.project;
|
|
68
|
+
};
|
|
69
|
+
Fetcher.prototype.load = function () {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
71
|
+
return __generator(this, function (_a) {
|
|
72
|
+
if (typeof this.uri !== 'string') {
|
|
73
|
+
throw new TypeError("Something disastrous occurred and a non-string URI was supplied to the Fetcher library. This shouldn't have happened!");
|
|
74
|
+
}
|
|
75
|
+
return [2 /*return*/, Promise.resolve(this.uri)
|
|
76
|
+
.then(function (uri) {
|
|
77
|
+
var url;
|
|
78
|
+
try {
|
|
79
|
+
url = new URL(uri);
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
// If that try fails for whatever reason than the URI that we have isn't a real URL and
|
|
83
|
+
// we can safely attempt to look for it on the filesystem.
|
|
84
|
+
return Fetcher.getFile(uri);
|
|
85
|
+
}
|
|
86
|
+
return Fetcher.getURL(url.href);
|
|
87
|
+
})
|
|
88
|
+
.then(function (res) { return Fetcher.validate(res); })
|
|
89
|
+
.then(function (res) { return res; })];
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
Fetcher.getURL = function (url) {
|
|
94
|
+
// @todo maybe include our user-agent here to identify our request
|
|
95
|
+
return fetch(url).then(function (res) {
|
|
96
|
+
if (!res.ok) {
|
|
97
|
+
throw new Error("Unable to retrieve URL (".concat(url, "). Reason: ").concat(res.statusText));
|
|
98
|
+
}
|
|
99
|
+
if (res.headers.get('content-type') === 'application/yaml' || /\.(yaml|yml)/.test(url)) {
|
|
100
|
+
return res.text().then(function (text) {
|
|
101
|
+
return js_yaml_1["default"].load(text);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return res.json();
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
Fetcher.getFile = function (uri) {
|
|
108
|
+
// Support relative paths by resolving them against the cwd.
|
|
109
|
+
var file = path_1["default"].resolve(process.cwd(), uri);
|
|
110
|
+
if (!fs_1["default"].existsSync(file)) {
|
|
111
|
+
throw new Error("Sorry, we were unable to load an API definition from ".concat(file, ". Please either supply a URL or a path on your filesystem."));
|
|
112
|
+
}
|
|
113
|
+
return Promise.resolve(fs_1["default"].readFileSync(file, 'utf8')).then(function (res) {
|
|
114
|
+
if (/\.(yaml|yml)/.test(file)) {
|
|
115
|
+
return js_yaml_1["default"].load(res);
|
|
116
|
+
}
|
|
117
|
+
return JSON.parse(res);
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
Fetcher.validate = function (json) {
|
|
121
|
+
if (json.swagger) {
|
|
122
|
+
throw new Error('Sorry, this module only supports OpenAPI definitions.');
|
|
123
|
+
}
|
|
124
|
+
// The `validate` method handles dereferencing for us.
|
|
125
|
+
return openapi_parser_1["default"].validate(json, {
|
|
126
|
+
dereference: {
|
|
127
|
+
/**
|
|
128
|
+
* If circular `$refs` are ignored they'll remain in the API definition as `$ref: String`.
|
|
129
|
+
* This allows us to not only do easy circular reference detection but also stringify and
|
|
130
|
+
* save dereferenced API definitions back into the cache directory.
|
|
131
|
+
*/
|
|
132
|
+
circular: 'ignore'
|
|
133
|
+
}
|
|
134
|
+
})["catch"](function (err) {
|
|
135
|
+
if (/is not a valid openapi definition/i.test(err.message)) {
|
|
136
|
+
throw new Error("Sorry, that doesn't look like a valid OpenAPI definition.");
|
|
137
|
+
}
|
|
138
|
+
throw err;
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* @example @petstore/v1.0#n6kvf10vakpemvplx
|
|
143
|
+
* @example @petstore#n6kvf10vakpemvplx
|
|
144
|
+
*/
|
|
145
|
+
// eslint-disable-next-line unicorn/no-unsafe-regex
|
|
146
|
+
Fetcher.registryUUIDRegex = /^@(?<project>[a-zA-Z0-9-_]+)(\/?(?<version>.+))?#(?<uuid>[a-z0-9]+)$/;
|
|
147
|
+
return Fetcher;
|
|
148
|
+
}());
|
|
149
|
+
exports["default"] = Fetcher;
|