@readme/api-core 7.0.0-alpha.1 → 7.0.0-alpha.2
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/dist/errors/fetchError.js +18 -28
- package/dist/index.d.ts +2 -2
- package/dist/index.js +69 -137
- package/dist/lib/getJSONSchemaDefaults.d.ts +1 -1
- package/dist/lib/getJSONSchemaDefaults.js +13 -15
- package/dist/lib/parseResponse.js +21 -67
- package/dist/lib/prepareAuth.d.ts +1 -1
- package/dist/lib/prepareAuth.js +17 -20
- package/dist/lib/prepareParams.d.ts +1 -1
- package/dist/lib/prepareParams.js +293 -355
- package/dist/lib/prepareServer.js +6 -7
- package/package.json +7 -6
- package/src/index.ts +2 -2
- package/src/lib/getJSONSchemaDefaults.ts +2 -2
- package/src/lib/parseResponse.ts +1 -3
- package/src/lib/prepareAuth.ts +2 -2
- package/src/lib/prepareParams.ts +2 -2
- package/tsconfig.json +2 -0
|
@@ -1,57 +1,21 @@
|
|
|
1
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 (g && (g = 0, op[0] && (_ = 0)), _) 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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
6
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const node_stream_1 = __importDefault(require("node:stream"));
|
|
9
|
+
const caseless_1 = __importDefault(require("caseless"));
|
|
10
|
+
const parser_1 = __importDefault(require("datauri/parser"));
|
|
11
|
+
const sync_1 = __importDefault(require("datauri/sync"));
|
|
12
|
+
const get_stream_1 = __importDefault(require("get-stream"));
|
|
13
|
+
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
14
|
+
const remove_undefined_objects_1 = __importDefault(require("remove-undefined-objects"));
|
|
15
|
+
const getJSONSchemaDefaults_1 = __importDefault(require("./getJSONSchemaDefaults"));
|
|
52
16
|
// These headers are normally only defined by the OpenAPI definition but we allow the user to
|
|
53
17
|
// manually supply them in their `metadata` parameter if they wish.
|
|
54
|
-
|
|
18
|
+
const specialHeaders = ['accept', 'authorization'];
|
|
55
19
|
/**
|
|
56
20
|
* Extract all available parameters from an operations Parameter Object into a digestable array
|
|
57
21
|
* that we can use to apply to the request.
|
|
@@ -60,15 +24,14 @@ var specialHeaders = ['accept', 'authorization'];
|
|
|
60
24
|
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterObject}
|
|
61
25
|
*/
|
|
62
26
|
function digestParameters(parameters) {
|
|
63
|
-
return parameters.reduce(
|
|
64
|
-
var _a;
|
|
27
|
+
return parameters.reduce((prev, param) => {
|
|
65
28
|
if ('$ref' in param || 'allOf' in param || 'anyOf' in param || 'oneOf' in param) {
|
|
66
29
|
throw new Error("The OpenAPI document for this operation wasn't dereferenced before processing.");
|
|
67
30
|
}
|
|
68
31
|
else if (param.name in prev) {
|
|
69
|
-
throw new Error(
|
|
32
|
+
throw new Error(`The operation you are using has the same parameter, ${param.name}, spread across multiple entry points. We unfortunately can't handle this right now.`);
|
|
70
33
|
}
|
|
71
|
-
return Object.assign(prev,
|
|
34
|
+
return Object.assign(prev, { [param.name]: param });
|
|
72
35
|
}, {});
|
|
73
36
|
}
|
|
74
37
|
// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_isempty
|
|
@@ -101,61 +64,51 @@ function merge(src, target) {
|
|
|
101
64
|
*
|
|
102
65
|
*/
|
|
103
66
|
function processFile(paramName, file) {
|
|
104
|
-
var _this = this;
|
|
105
67
|
if (typeof file === 'string') {
|
|
106
68
|
// In order to support relative pathed files, we need to attempt to resolve them.
|
|
107
|
-
|
|
108
|
-
return new Promise(
|
|
109
|
-
node_fs_1.default.stat(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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 [2 /*return*/, resolve(undefined)];
|
|
125
|
-
}
|
|
126
|
-
return [2 /*return*/, reject(err)];
|
|
127
|
-
}
|
|
128
|
-
return [4 /*yield*/, (0, sync_1.default)(resolvedFile_1)];
|
|
129
|
-
case 1:
|
|
130
|
-
fileMetadata = _b.sent();
|
|
131
|
-
payloadFilename = encodeURIComponent(node_path_1.default.basename(resolvedFile_1));
|
|
132
|
-
return [2 /*return*/, resolve({
|
|
133
|
-
paramName: paramName,
|
|
134
|
-
base64: (_a = fileMetadata === null || fileMetadata === void 0 ? void 0 : fileMetadata.content) === null || _a === void 0 ? void 0 : _a.replace(';base64', ";name=".concat(payloadFilename, ";base64")),
|
|
135
|
-
filename: payloadFilename,
|
|
136
|
-
buffer: fileMetadata.buffer,
|
|
137
|
-
})];
|
|
69
|
+
const resolvedFile = node_path_1.default.resolve(file);
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
node_fs_1.default.stat(resolvedFile, async (err) => {
|
|
72
|
+
if (err) {
|
|
73
|
+
if (err.code === 'ENOENT') {
|
|
74
|
+
// It's less than ideal for us to handle files that don't exist like this but because
|
|
75
|
+
// `file` is a string it might actually be the full text contents of the file and not
|
|
76
|
+
// actually a path.
|
|
77
|
+
//
|
|
78
|
+
// We also can't really regex to see if `file` *looks*` like a path because one should be
|
|
79
|
+
// able to pass in a relative `owlbert.png` (instead of `./owlbert.png`) and though that
|
|
80
|
+
// doesn't *look* like a path, it is one that should still work.
|
|
81
|
+
return resolve(undefined);
|
|
138
82
|
}
|
|
83
|
+
return reject(err);
|
|
84
|
+
}
|
|
85
|
+
const fileMetadata = await (0, sync_1.default)(resolvedFile);
|
|
86
|
+
const payloadFilename = encodeURIComponent(node_path_1.default.basename(resolvedFile));
|
|
87
|
+
return resolve({
|
|
88
|
+
paramName,
|
|
89
|
+
base64: fileMetadata?.content?.replace(';base64', `;name=${payloadFilename};base64`),
|
|
90
|
+
filename: payloadFilename,
|
|
91
|
+
buffer: fileMetadata.buffer,
|
|
139
92
|
});
|
|
140
|
-
});
|
|
93
|
+
});
|
|
141
94
|
});
|
|
142
95
|
}
|
|
143
96
|
else if (file instanceof node_stream_1.default.Readable) {
|
|
144
|
-
return get_stream_1.default.buffer(file).then(
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
97
|
+
return get_stream_1.default.buffer(file).then(buffer => {
|
|
98
|
+
const filePath = file.path;
|
|
99
|
+
const parser = new parser_1.default();
|
|
100
|
+
const base64 = parser.format(filePath, buffer).content;
|
|
101
|
+
const payloadFilename = encodeURIComponent(node_path_1.default.basename(filePath));
|
|
149
102
|
return {
|
|
150
|
-
paramName
|
|
151
|
-
base64: base64
|
|
103
|
+
paramName,
|
|
104
|
+
base64: base64?.replace(';base64', `;name=${payloadFilename};base64`),
|
|
152
105
|
filename: payloadFilename,
|
|
153
|
-
buffer
|
|
106
|
+
buffer,
|
|
154
107
|
};
|
|
155
108
|
});
|
|
156
109
|
}
|
|
157
110
|
return Promise.reject(new TypeError(paramName
|
|
158
|
-
?
|
|
111
|
+
? `The data supplied for the \`${paramName}\` request body parameter is not a file handler that we support.`
|
|
159
112
|
: 'The data supplied for the request body payload is not a file handler that we support.'));
|
|
160
113
|
}
|
|
161
114
|
/**
|
|
@@ -164,277 +117,262 @@ function processFile(paramName, file) {
|
|
|
164
117
|
* with `@readme/oas-to-har`.
|
|
165
118
|
*
|
|
166
119
|
*/
|
|
167
|
-
function prepareParams(operation, body, metadata) {
|
|
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
|
-
if (body !== undefined) {
|
|
196
|
-
if (typeof body === 'object' && body !== null && !Array.isArray(body)) {
|
|
197
|
-
if (Object.keys(body).length <= 2) {
|
|
198
|
-
bodyParams_1 = (0, caseless_1.default)(body);
|
|
199
|
-
if (specialHeaders.some(function (header) { return bodyParams_1.has(header); })) {
|
|
200
|
-
throwNoParamsError = false;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
if (throwNoParamsError) {
|
|
206
|
-
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.");
|
|
207
|
-
}
|
|
120
|
+
async function prepareParams(operation, body, metadata) {
|
|
121
|
+
let metadataIntersected = false;
|
|
122
|
+
const digestedParameters = digestParameters(operation.getParameters());
|
|
123
|
+
const jsonSchema = operation.getParametersAsJSONSchema();
|
|
124
|
+
/**
|
|
125
|
+
* It might be common for somebody to run `sdk.findPetsByStatus({ status: 'available' }, {})`, in
|
|
126
|
+
* which case we want to filter out the second (metadata) parameter and treat the first parameter
|
|
127
|
+
* as the metadata instead. If we don't do this, their supplied `status` metadata will be treated
|
|
128
|
+
* as a body parameter, and because there's no `status` body parameter, and no supplied metadata
|
|
129
|
+
* (because it's an empty object), the request won't send a payload.
|
|
130
|
+
*
|
|
131
|
+
* @see {@link https://github.com/readmeio/api/issues/449}
|
|
132
|
+
*/
|
|
133
|
+
// eslint-disable-next-line no-param-reassign
|
|
134
|
+
metadata = (0, remove_undefined_objects_1.default)(metadata);
|
|
135
|
+
if (!jsonSchema && (body !== undefined || metadata !== undefined)) {
|
|
136
|
+
let throwNoParamsError = true;
|
|
137
|
+
// If this operation doesn't have any parameters for us to transform to JSON Schema but they've
|
|
138
|
+
// sent us either an `Accept` or `Authorization` header (or both) we should let them do that.
|
|
139
|
+
// We should, however, only do this check for the `body` parameter as if they've sent this
|
|
140
|
+
// request both `body` and `metadata` we can reject it outright as the operation won't have any
|
|
141
|
+
// body data.
|
|
142
|
+
if (body !== undefined) {
|
|
143
|
+
if (typeof body === 'object' && body !== null && !Array.isArray(body)) {
|
|
144
|
+
if (Object.keys(body).length <= 2) {
|
|
145
|
+
const bodyParams = (0, caseless_1.default)(body);
|
|
146
|
+
if (specialHeaders.some(header => bodyParams.has(header))) {
|
|
147
|
+
throwNoParamsError = false;
|
|
208
148
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (throwNoParamsError) {
|
|
153
|
+
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.");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const jsonSchemaDefaults = jsonSchema ? (0, getJSONSchemaDefaults_1.default)(jsonSchema) : {};
|
|
157
|
+
const params = jsonSchemaDefaults;
|
|
158
|
+
// If a body argument was supplied we need to do a bit of work to see if it's actually a body
|
|
159
|
+
// argument or metadata because the library lets you supply either a body, metadata, or body with
|
|
160
|
+
// metadata.
|
|
161
|
+
if (typeof body !== 'undefined') {
|
|
162
|
+
if (Array.isArray(body) || isPrimitive(body)) {
|
|
163
|
+
// If the body param is an array or a primitive then we know it's absolutely a body because
|
|
164
|
+
// metadata can only ever be undefined or an object.
|
|
165
|
+
params.body = merge(params.body, body);
|
|
166
|
+
}
|
|
167
|
+
else if (typeof metadata === 'undefined') {
|
|
168
|
+
// No metadata was explicitly provided so we need to analyze the body to determine if it's a
|
|
169
|
+
// body or should be actually be treated as metadata.
|
|
170
|
+
const headerParams = (0, caseless_1.default)({});
|
|
171
|
+
Object.entries(digestedParameters).forEach(([paramName, param]) => {
|
|
172
|
+
// Headers are sent case-insensitive so we need to make sure that we're properly
|
|
173
|
+
// matching them when detecting what our incoming payload looks like.
|
|
174
|
+
if (param.in === 'header') {
|
|
175
|
+
headerParams.set(paramName, '');
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
// `Accept` and `Authorization` headers can't be defined as normal parameters but we should
|
|
179
|
+
// always allow the user to supply them if they wish.
|
|
180
|
+
specialHeaders.forEach(header => {
|
|
181
|
+
if (!headerParams.has(header)) {
|
|
182
|
+
headerParams.set(header, '');
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
const intersection = Object.keys(body).filter(value => {
|
|
186
|
+
if (Object.keys(digestedParameters).includes(value)) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
else if (headerParams.has(value)) {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
}).length;
|
|
194
|
+
// If more than 25% of the body intersects with the parameters that we've got on hand, then
|
|
195
|
+
// we should treat it as a metadata object and organize into parameters.
|
|
196
|
+
if (intersection && intersection / Object.keys(body).length > 0.25) {
|
|
197
|
+
/* eslint-disable no-param-reassign */
|
|
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
|
+
else {
|
|
209
|
+
// Body and metadata were both supplied.
|
|
210
|
+
params.body = merge(params.body, body);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (!operation.hasRequestBody()) {
|
|
214
|
+
// If this operation doesn't have any documented request body then we shouldn't be sending
|
|
215
|
+
// anything.
|
|
216
|
+
delete params.body;
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
if (!('body' in params))
|
|
220
|
+
params.body = {};
|
|
221
|
+
// We need to retrieve the request body for this operation to search for any `binary` format
|
|
222
|
+
// data that the user wants to send so we know what we need to prepare for the final API
|
|
223
|
+
// request.
|
|
224
|
+
const payloadJsonSchema = jsonSchema.find(js => js.type === 'body');
|
|
225
|
+
if (payloadJsonSchema) {
|
|
226
|
+
if (!params.files)
|
|
227
|
+
params.files = {};
|
|
228
|
+
const conversions = [];
|
|
229
|
+
// @todo add support for `type: array`, `oneOf` and `anyOf`
|
|
230
|
+
if (payloadJsonSchema.schema?.properties) {
|
|
231
|
+
Object.entries(payloadJsonSchema.schema?.properties)
|
|
232
|
+
.filter(([, schema]) => schema?.format === 'binary')
|
|
233
|
+
.filter(([prop]) => Object.keys(params.body).includes(prop))
|
|
234
|
+
.forEach(([prop]) => {
|
|
235
|
+
conversions.push(processFile(prop, params.body[prop]));
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
else if (payloadJsonSchema.schema?.type === 'string') {
|
|
239
|
+
if (payloadJsonSchema.schema?.format === 'binary') {
|
|
240
|
+
conversions.push(processFile(undefined, params.body));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
await Promise.all(conversions)
|
|
244
|
+
.then(fileMetadata => fileMetadata.filter(Boolean))
|
|
245
|
+
.then(fm => {
|
|
246
|
+
fm.forEach(fileMetadata => {
|
|
247
|
+
if (!fileMetadata) {
|
|
248
|
+
// If we don't have any metadata here it's because the file we have is likely
|
|
249
|
+
// the full string content of the file so since we don't have any filenames to
|
|
250
|
+
// work with we shouldn't do any additional handling to the `body` or `files`
|
|
251
|
+
// parameters.
|
|
252
|
+
return;
|
|
264
253
|
}
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
// anything.
|
|
268
|
-
delete params.body;
|
|
269
|
-
return [3 /*break*/, 3];
|
|
270
|
-
case 1:
|
|
271
|
-
if (!('body' in params))
|
|
272
|
-
params.body = {};
|
|
273
|
-
payloadJsonSchema = jsonSchema.find(function (js) { return js.type === 'body'; });
|
|
274
|
-
if (!payloadJsonSchema) return [3 /*break*/, 3];
|
|
275
|
-
if (!params.files)
|
|
276
|
-
params.files = {};
|
|
277
|
-
conversions_1 = [];
|
|
278
|
-
// @todo add support for `type: array`, `oneOf` and `anyOf`
|
|
279
|
-
if ((_a = payloadJsonSchema.schema) === null || _a === void 0 ? void 0 : _a.properties) {
|
|
280
|
-
Object.entries((_b = payloadJsonSchema.schema) === null || _b === void 0 ? void 0 : _b.properties)
|
|
281
|
-
.filter(function (_a) {
|
|
282
|
-
var schema = _a[1];
|
|
283
|
-
return (schema === null || schema === void 0 ? void 0 : schema.format) === 'binary';
|
|
284
|
-
})
|
|
285
|
-
.filter(function (_a) {
|
|
286
|
-
var prop = _a[0];
|
|
287
|
-
return Object.keys(params.body).includes(prop);
|
|
288
|
-
})
|
|
289
|
-
.forEach(function (_a) {
|
|
290
|
-
var prop = _a[0];
|
|
291
|
-
conversions_1.push(processFile(prop, params.body[prop]));
|
|
292
|
-
});
|
|
254
|
+
if (fileMetadata.paramName) {
|
|
255
|
+
params.body[fileMetadata.paramName] = fileMetadata.base64;
|
|
293
256
|
}
|
|
294
|
-
else
|
|
295
|
-
|
|
296
|
-
conversions_1.push(processFile(undefined, params.body));
|
|
297
|
-
}
|
|
257
|
+
else {
|
|
258
|
+
params.body = fileMetadata.base64;
|
|
298
259
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
.then(function (fm) {
|
|
302
|
-
fm.forEach(function (fileMetadata) {
|
|
303
|
-
if (!fileMetadata) {
|
|
304
|
-
// If we don't have any metadata here it's because the file we have is likely
|
|
305
|
-
// the full string content of the file so since we don't have any filenames to
|
|
306
|
-
// work with we shouldn't do any additional handling to the `body` or `files`
|
|
307
|
-
// parameters.
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
if (fileMetadata.paramName) {
|
|
311
|
-
params.body[fileMetadata.paramName] = fileMetadata.base64;
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
params.body = fileMetadata.base64;
|
|
315
|
-
}
|
|
316
|
-
if (fileMetadata.buffer && (params === null || params === void 0 ? void 0 : params.files)) {
|
|
317
|
-
params.files[fileMetadata.filename] = fileMetadata.buffer;
|
|
318
|
-
}
|
|
319
|
-
});
|
|
320
|
-
})];
|
|
321
|
-
case 2:
|
|
322
|
-
_e.sent();
|
|
323
|
-
_e.label = 3;
|
|
324
|
-
case 3:
|
|
325
|
-
// Form data should be placed within `formData` instead of `body` for it to properly get picked
|
|
326
|
-
// up by `fetch-har`.
|
|
327
|
-
if (operation.isFormUrlEncoded()) {
|
|
328
|
-
params.formData = merge(params.formData, params.body);
|
|
329
|
-
delete params.body;
|
|
260
|
+
if (fileMetadata.buffer && params?.files) {
|
|
261
|
+
params.files[fileMetadata.filename] = fileMetadata.buffer;
|
|
330
262
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
if (
|
|
422
|
-
params.
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
// Any other remaining unused metadata will be unused because we don't know where to place
|
|
426
|
-
// it in the request.
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// Form data should be placed within `formData` instead of `body` for it to properly get picked
|
|
268
|
+
// up by `fetch-har`.
|
|
269
|
+
if (operation.isFormUrlEncoded()) {
|
|
270
|
+
params.formData = merge(params.formData, params.body);
|
|
271
|
+
delete params.body;
|
|
272
|
+
}
|
|
273
|
+
// Only spend time trying to organize metadata into parameters if we were able to digest
|
|
274
|
+
// parameters out of the operation schema. If we couldn't digest anything, but metadata was
|
|
275
|
+
// supplied then we wouldn't know how to send it in the request!
|
|
276
|
+
if (typeof metadata !== 'undefined') {
|
|
277
|
+
if (!('cookie' in params))
|
|
278
|
+
params.cookie = {};
|
|
279
|
+
if (!('header' in params))
|
|
280
|
+
params.header = {};
|
|
281
|
+
if (!('path' in params))
|
|
282
|
+
params.path = {};
|
|
283
|
+
if (!('query' in params))
|
|
284
|
+
params.query = {};
|
|
285
|
+
Object.entries(digestedParameters).forEach(([paramName, param]) => {
|
|
286
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
287
|
+
let value;
|
|
288
|
+
let metadataHeaderParam;
|
|
289
|
+
if (typeof metadata === 'object' && !isEmpty(metadata)) {
|
|
290
|
+
if (paramName in metadata) {
|
|
291
|
+
value = metadata[paramName];
|
|
292
|
+
metadataHeaderParam = paramName;
|
|
293
|
+
}
|
|
294
|
+
else if (param.in === 'header') {
|
|
295
|
+
// Headers are sent case-insensitive so we need to make sure that we're properly
|
|
296
|
+
// matching them when detecting what our incoming payload looks like.
|
|
297
|
+
metadataHeaderParam = Object.keys(metadata).find(k => k.toLowerCase() === paramName.toLowerCase()) || '';
|
|
298
|
+
value = metadata[metadataHeaderParam];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (value === undefined) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
/* eslint-disable no-param-reassign */
|
|
305
|
+
switch (param.in) {
|
|
306
|
+
case 'path':
|
|
307
|
+
params.path[paramName] = value;
|
|
308
|
+
if (metadata?.[paramName])
|
|
309
|
+
delete metadata[paramName];
|
|
310
|
+
break;
|
|
311
|
+
case 'query':
|
|
312
|
+
params.query[paramName] = value;
|
|
313
|
+
if (metadata?.[paramName])
|
|
314
|
+
delete metadata[paramName];
|
|
315
|
+
break;
|
|
316
|
+
case 'header':
|
|
317
|
+
params.header[paramName.toLowerCase()] = value;
|
|
318
|
+
if (metadataHeaderParam && metadata?.[metadataHeaderParam])
|
|
319
|
+
delete metadata[metadataHeaderParam];
|
|
320
|
+
break;
|
|
321
|
+
case 'cookie':
|
|
322
|
+
params.cookie[paramName] = value;
|
|
323
|
+
if (metadata?.[paramName])
|
|
324
|
+
delete metadata[paramName];
|
|
325
|
+
break;
|
|
326
|
+
default: // no-op
|
|
327
|
+
}
|
|
328
|
+
/* eslint-enable no-param-reassign */
|
|
329
|
+
// Because a user might have sent just a metadata object, we want to make sure that we filter
|
|
330
|
+
// out anything that they sent that is a parameter from also being sent as part of a form
|
|
331
|
+
// data payload for `x-www-form-urlencoded` requests.
|
|
332
|
+
if (metadataIntersected && operation.isFormUrlEncoded()) {
|
|
333
|
+
if (paramName in params.formData) {
|
|
334
|
+
delete params.formData[paramName];
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
// If there's any leftover metadata that hasn't been moved into form data for this request we
|
|
339
|
+
// need to move it or else it'll get tossed.
|
|
340
|
+
if (!isEmpty(metadata)) {
|
|
341
|
+
if (typeof metadata === 'object') {
|
|
342
|
+
// If the user supplied an `accept` or `authorization` header themselves we should allow it
|
|
343
|
+
// through. Normally these headers are automatically handled by `@readme/oas-to-har` but in
|
|
344
|
+
// the event that maybe the user wants to return XML for an API that normally returns JSON
|
|
345
|
+
// or specify a custom auth header (maybe we can't handle their auth case right) this is the
|
|
346
|
+
// only way with this library that they can do that.
|
|
347
|
+
specialHeaders.forEach(headerName => {
|
|
348
|
+
const headerParam = Object.keys(metadata || {}).find(m => m.toLowerCase() === headerName);
|
|
349
|
+
if (headerParam) {
|
|
350
|
+
// this if-statement below is a typeguard
|
|
351
|
+
if (typeof metadata === 'object') {
|
|
352
|
+
// this if-statement below is a typeguard
|
|
353
|
+
if (typeof params.header === 'object') {
|
|
354
|
+
params.header[headerName] = metadata[headerParam];
|
|
427
355
|
}
|
|
356
|
+
// eslint-disable-next-line no-param-reassign
|
|
357
|
+
delete metadata[headerParam];
|
|
428
358
|
}
|
|
429
359
|
}
|
|
430
|
-
|
|
431
|
-
if (type in params && isEmpty(params[type])) {
|
|
432
|
-
delete params[type];
|
|
433
|
-
}
|
|
434
|
-
});
|
|
435
|
-
return [2 /*return*/, params];
|
|
360
|
+
});
|
|
436
361
|
}
|
|
437
|
-
|
|
362
|
+
if (operation.isFormUrlEncoded()) {
|
|
363
|
+
params.formData = merge(params.formData, metadata);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
// Any other remaining unused metadata will be unused because we don't know where to place
|
|
367
|
+
// it in the request.
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
['body', 'cookie', 'files', 'formData', 'header', 'path', 'query'].forEach((type) => {
|
|
372
|
+
if (type in params && isEmpty(params[type])) {
|
|
373
|
+
delete params[type];
|
|
374
|
+
}
|
|
438
375
|
});
|
|
376
|
+
return params;
|
|
439
377
|
}
|
|
440
378
|
exports.default = prepareParams;
|