@readme/api-core 7.0.0-alpha.2 → 7.0.0-alpha.4
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/chunk-L7DN22AA.js +25 -0
- package/dist/chunk-L7DN22AA.js.map +1 -0
- package/dist/chunk-NVFSP2R3.cjs +25 -0
- package/dist/chunk-NVFSP2R3.cjs.map +1 -0
- package/dist/errors/fetchError.cjs +8 -0
- package/dist/errors/fetchError.cjs.map +1 -0
- package/dist/errors/fetchError.d.cts +13 -0
- package/dist/errors/fetchError.d.ts +2 -1
- package/dist/errors/fetchError.js +7 -26
- package/dist/errors/fetchError.js.map +1 -0
- package/dist/index.cjs +516 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +46 -0
- package/dist/index.d.ts +10 -13
- package/dist/index.js +504 -85
- package/dist/index.js.map +1 -0
- package/package.json +26 -11
- package/src/errors/fetchError.ts +1 -3
- package/src/index.ts +2 -8
- package/src/lib/index.ts +7 -0
- package/src/lib/prepareParams.ts +3 -3
- package/tsconfig.json +3 -10
- package/tsup.config.ts +15 -0
- package/dist/lib/getJSONSchemaDefaults.d.ts +0 -14
- package/dist/lib/getJSONSchemaDefaults.js +0 -59
- package/dist/lib/parseResponse.d.ts +0 -6
- package/dist/lib/parseResponse.js +0 -25
- package/dist/lib/prepareAuth.d.ts +0 -5
- package/dist/lib/prepareAuth.js +0 -81
- package/dist/lib/prepareParams.d.ts +0 -21
- package/dist/lib/prepareParams.js +0 -378
- package/dist/lib/prepareServer.d.ts +0 -10
- package/dist/lib/prepareServer.js +0 -46
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/errors/fetchError.ts
|
|
2
|
+
var FetchError = class _FetchError extends Error {
|
|
3
|
+
/** HTTP Status */
|
|
4
|
+
status;
|
|
5
|
+
/** The content of the response. */
|
|
6
|
+
data;
|
|
7
|
+
/** The Headers of the response. */
|
|
8
|
+
headers;
|
|
9
|
+
/** The raw `Response` object. */
|
|
10
|
+
res;
|
|
11
|
+
constructor(status, data, headers, res) {
|
|
12
|
+
super(res.statusText);
|
|
13
|
+
this.name = "FetchError";
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.data = data;
|
|
16
|
+
this.headers = headers;
|
|
17
|
+
this.res = res;
|
|
18
|
+
Object.setPrototypeOf(this, _FetchError.prototype);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
FetchError
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=chunk-L7DN22AA.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors/fetchError.ts"],"sourcesContent":["export default class FetchError<Status = number, Data = unknown> extends Error {\n /** HTTP Status */\n status: Status;\n\n /** The content of the response. */\n data: Data;\n\n /** The Headers of the response. */\n headers: Headers;\n\n /** The raw `Response` object. */\n res: Response;\n\n constructor(status: Status, data: Data, headers: Headers, res: Response) {\n super(res.statusText);\n\n this.name = 'FetchError';\n this.status = status;\n this.data = data;\n this.headers = headers;\n this.res = res;\n\n // We could fix this by updating our target to ES2015 but because we support exporting to CJS\n // we can't.\n //\n // https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/\n Object.setPrototypeOf(this, FetchError.prototype);\n }\n}\n"],"mappings":";AAAA,IAAqB,aAArB,MAAqB,oBAAoD,MAAM;AAAA;AAAA,EAE7E;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA,EAEA,YAAY,QAAgB,MAAY,SAAkB,KAAe;AACvE,UAAM,IAAI,UAAU;AAEpB,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,MAAM;AAMX,WAAO,eAAe,MAAM,YAAW,SAAS;AAAA,EAClD;AACF;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/errors/fetchError.ts
|
|
2
|
+
var FetchError = class _FetchError extends Error {
|
|
3
|
+
/** HTTP Status */
|
|
4
|
+
|
|
5
|
+
/** The content of the response. */
|
|
6
|
+
|
|
7
|
+
/** The Headers of the response. */
|
|
8
|
+
|
|
9
|
+
/** The raw `Response` object. */
|
|
10
|
+
|
|
11
|
+
constructor(status, data, headers, res) {
|
|
12
|
+
super(res.statusText);
|
|
13
|
+
this.name = "FetchError";
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.data = data;
|
|
16
|
+
this.headers = headers;
|
|
17
|
+
this.res = res;
|
|
18
|
+
Object.setPrototypeOf(this, _FetchError.prototype);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
exports.FetchError = FetchError;
|
|
25
|
+
//# sourceMappingURL=chunk-NVFSP2R3.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors/fetchError.ts"],"names":[],"mappings":";AAAA,IAAqB,aAArB,MAAqB,oBAAoD,MAAM;AAAA;AAAA,EAE7E;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA,EAEA,YAAY,QAAgB,MAAY,SAAkB,KAAe;AACvE,UAAM,IAAI,UAAU;AAEpB,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,MAAM;AAMX,WAAO,eAAe,MAAM,YAAW,SAAS;AAAA,EAClD;AACF","sourcesContent":["export default class FetchError<Status = number, Data = unknown> extends Error {\n /** HTTP Status */\n status: Status;\n\n /** The content of the response. */\n data: Data;\n\n /** The Headers of the response. */\n headers: Headers;\n\n /** The raw `Response` object. */\n res: Response;\n\n constructor(status: Status, data: Data, headers: Headers, res: Response) {\n super(res.statusText);\n\n this.name = 'FetchError';\n this.status = status;\n this.data = data;\n this.headers = headers;\n this.res = res;\n\n // We could fix this by updating our target to ES2015 but because we support exporting to CJS\n // we can't.\n //\n // https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/\n Object.setPrototypeOf(this, FetchError.prototype);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare class FetchError<Status = number, Data = unknown> extends Error {
|
|
2
|
+
/** HTTP Status */
|
|
3
|
+
status: Status;
|
|
4
|
+
/** The content of the response. */
|
|
5
|
+
data: Data;
|
|
6
|
+
/** The Headers of the response. */
|
|
7
|
+
headers: Headers;
|
|
8
|
+
/** The raw `Response` object. */
|
|
9
|
+
res: Response;
|
|
10
|
+
constructor(status: Status, data: Data, headers: Headers, res: Response);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export = FetchError;
|
|
@@ -1,26 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/** The Headers of the response. */
|
|
9
|
-
headers;
|
|
10
|
-
/** The raw `Response` object. */
|
|
11
|
-
res;
|
|
12
|
-
constructor(status, data, headers, res) {
|
|
13
|
-
super(res.statusText);
|
|
14
|
-
this.name = 'FetchError';
|
|
15
|
-
this.status = status;
|
|
16
|
-
this.data = data;
|
|
17
|
-
this.headers = headers;
|
|
18
|
-
this.res = res;
|
|
19
|
-
// We could fix this by updating our target to ES2015 but because we support exporting to CJS
|
|
20
|
-
// we can't.
|
|
21
|
-
//
|
|
22
|
-
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
23
|
-
Object.setPrototypeOf(this, FetchError.prototype);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
exports.default = FetchError;
|
|
1
|
+
import {
|
|
2
|
+
FetchError
|
|
3
|
+
} from "../chunk-L7DN22AA.js";
|
|
4
|
+
export {
|
|
5
|
+
FetchError as default
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=fetchError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
|
|
2
|
+
|
|
3
|
+
var _chunkNVFSP2R3cjs = require('./chunk-NVFSP2R3.cjs');
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
var _oastohar = require('@readme/oas-to-har'); var _oastohar2 = _interopRequireDefault(_oastohar);
|
|
7
|
+
var _fetchhar = require('fetch-har'); var _fetchhar2 = _interopRequireDefault(_fetchhar);
|
|
8
|
+
|
|
9
|
+
// src/lib/getJSONSchemaDefaults.ts
|
|
10
|
+
var _jsonschematraverse = require('json-schema-traverse'); var _jsonschematraverse2 = _interopRequireDefault(_jsonschematraverse);
|
|
11
|
+
function getJSONSchemaDefaults(jsonSchemas) {
|
|
12
|
+
return jsonSchemas.map(({ type: payloadType, schema: jsonSchema }) => {
|
|
13
|
+
const defaults = {};
|
|
14
|
+
_jsonschematraverse2.default.call(void 0,
|
|
15
|
+
jsonSchema,
|
|
16
|
+
(schema, pointer, rootSchema, parentPointer, parentKeyword, parentSchema, indexProperty) => {
|
|
17
|
+
if (!pointer.startsWith("/properties/")) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(_optionalChain([parentSchema, 'optionalAccess', _ => _.required])) && _optionalChain([parentSchema, 'optionalAccess', _2 => _2.required, 'access', _3 => _3.includes, 'call', _4 => _4(String(indexProperty))])) {
|
|
21
|
+
if (schema.type === "object" && indexProperty) {
|
|
22
|
+
defaults[indexProperty] = {};
|
|
23
|
+
}
|
|
24
|
+
let destination = defaults;
|
|
25
|
+
if (parentPointer) {
|
|
26
|
+
parentPointer.replace(/\/properties/g, "").split("/").forEach((subSchema) => {
|
|
27
|
+
if (subSchema === "") {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
destination = _optionalChain([destination, 'optionalAccess', _5 => _5[subSchema]]) || {};
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (schema.default !== void 0) {
|
|
34
|
+
if (indexProperty !== void 0) {
|
|
35
|
+
destination[indexProperty] = schema.default;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
if (!Object.keys(defaults).length) {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
// @todo should we filter out empty and undefined objects from here with `remove-undefined-objects`?
|
|
46
|
+
[payloadType]: defaults
|
|
47
|
+
};
|
|
48
|
+
}).reduce((prev, next) => Object.assign(prev, next));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/lib/parseResponse.ts
|
|
52
|
+
var _utils = require('oas/utils');
|
|
53
|
+
async function parseResponse(response) {
|
|
54
|
+
const contentType = response.headers.get("Content-Type");
|
|
55
|
+
const isJSON = contentType && (_utils.matchesMimeType.json(contentType) || _utils.matchesMimeType.wildcard(contentType));
|
|
56
|
+
const responseBody = await response.text();
|
|
57
|
+
let data = responseBody;
|
|
58
|
+
if (isJSON) {
|
|
59
|
+
try {
|
|
60
|
+
data = JSON.parse(responseBody);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
data,
|
|
66
|
+
status: response.status,
|
|
67
|
+
headers: response.headers,
|
|
68
|
+
res: response
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/lib/prepareAuth.ts
|
|
73
|
+
function prepareAuth(authKey, operation) {
|
|
74
|
+
if (authKey.length === 0) {
|
|
75
|
+
return {};
|
|
76
|
+
}
|
|
77
|
+
const preparedAuth = {};
|
|
78
|
+
const security = operation.getSecurity();
|
|
79
|
+
if (security.length === 0) {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
if (security.every((s) => Object.keys(s).length > 1)) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
"Sorry, this operation currently requires multiple forms of authentication which this library doesn't yet support."
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const usableSecurity = security.map((s) => {
|
|
88
|
+
return Object.keys(s).length === 1 ? s : false;
|
|
89
|
+
}).filter(Boolean);
|
|
90
|
+
const usableSecuritySchemes = usableSecurity.map((s) => Object.keys(s)).reduce((prev, next) => prev.concat(next), []);
|
|
91
|
+
const preparedSecurity = operation.prepareSecurity();
|
|
92
|
+
if (authKey.length >= 2) {
|
|
93
|
+
if (!("Basic" in preparedSecurity)) {
|
|
94
|
+
throw new Error("Multiple auth tokens were supplied for this endpoint but only a single token is needed.");
|
|
95
|
+
}
|
|
96
|
+
const schemes2 = preparedSecurity.Basic.filter((s) => usableSecuritySchemes.includes(s._key));
|
|
97
|
+
if (!schemes2.length) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
"Credentials for Basic Authentication were supplied but this operation requires another form of auth in that case, which this library does not yet support. This operation does, however, allow supplying a single auth token."
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
const scheme2 = schemes2.shift();
|
|
103
|
+
preparedAuth[scheme2._key] = {
|
|
104
|
+
user: authKey[0],
|
|
105
|
+
pass: authKey.length === 2 ? authKey[1] : ""
|
|
106
|
+
};
|
|
107
|
+
return preparedAuth;
|
|
108
|
+
}
|
|
109
|
+
const usableScheme = usableSecuritySchemes[0];
|
|
110
|
+
const schemes = Object.entries(preparedSecurity).map(([, ps]) => ps.filter((s) => usableScheme === s._key)).reduce((prev, next) => prev.concat(next), []);
|
|
111
|
+
const scheme = schemes.shift();
|
|
112
|
+
switch (scheme.type) {
|
|
113
|
+
case "http":
|
|
114
|
+
if (scheme.scheme === "basic") {
|
|
115
|
+
preparedAuth[scheme._key] = {
|
|
116
|
+
user: authKey[0],
|
|
117
|
+
pass: authKey.length === 2 ? authKey[1] : ""
|
|
118
|
+
};
|
|
119
|
+
} else if (scheme.scheme === "bearer") {
|
|
120
|
+
preparedAuth[scheme._key] = authKey[0];
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
case "oauth2":
|
|
124
|
+
preparedAuth[scheme._key] = authKey[0];
|
|
125
|
+
break;
|
|
126
|
+
case "apiKey":
|
|
127
|
+
if (scheme.in === "query" || scheme.in === "header" || scheme.in === "cookie") {
|
|
128
|
+
preparedAuth[scheme._key] = authKey[0];
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
default:
|
|
132
|
+
throw new Error(
|
|
133
|
+
`Sorry, this API currently uses a security scheme, ${scheme.type}, which this library doesn't yet support.`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return preparedAuth;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// src/lib/prepareParams.ts
|
|
140
|
+
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
|
|
141
|
+
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
142
|
+
var _stream = require('stream'); var _stream2 = _interopRequireDefault(_stream);
|
|
143
|
+
var _caseless = require('caseless'); var _caseless2 = _interopRequireDefault(_caseless);
|
|
144
|
+
var _parserjs = require('datauri/parser.js'); var _parserjs2 = _interopRequireDefault(_parserjs);
|
|
145
|
+
var _syncjs = require('datauri/sync.js'); var _syncjs2 = _interopRequireDefault(_syncjs);
|
|
146
|
+
var _getstream = require('get-stream'); var _getstream2 = _interopRequireDefault(_getstream);
|
|
147
|
+
var _lodashmerge = require('lodash.merge'); var _lodashmerge2 = _interopRequireDefault(_lodashmerge);
|
|
148
|
+
var _removeundefinedobjects = require('remove-undefined-objects'); var _removeundefinedobjects2 = _interopRequireDefault(_removeundefinedobjects);
|
|
149
|
+
var specialHeaders = ["accept", "authorization"];
|
|
150
|
+
function digestParameters(parameters) {
|
|
151
|
+
return parameters.reduce((prev, param) => {
|
|
152
|
+
if ("$ref" in param || "allOf" in param || "anyOf" in param || "oneOf" in param) {
|
|
153
|
+
throw new Error("The OpenAPI document for this operation wasn't dereferenced before processing.");
|
|
154
|
+
} else if (param.name in prev) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`The operation you are using has the same parameter, ${param.name}, spread across multiple entry points. We unfortunately can't handle this right now.`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
return Object.assign(prev, { [param.name]: param });
|
|
160
|
+
}, {});
|
|
161
|
+
}
|
|
162
|
+
function isEmpty(obj) {
|
|
163
|
+
return [Object, Array].includes((obj || {}).constructor) && !Object.entries(obj || {}).length;
|
|
164
|
+
}
|
|
165
|
+
function isObject(thing) {
|
|
166
|
+
if (thing instanceof _stream2.default.Readable) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return typeof thing === "object" && thing !== null && !Array.isArray(thing);
|
|
170
|
+
}
|
|
171
|
+
function isPrimitive(obj) {
|
|
172
|
+
return obj === null || typeof obj === "number" || typeof obj === "string";
|
|
173
|
+
}
|
|
174
|
+
function merge(src, target) {
|
|
175
|
+
if (Array.isArray(target)) {
|
|
176
|
+
return target;
|
|
177
|
+
} else if (!isObject(target)) {
|
|
178
|
+
return target;
|
|
179
|
+
}
|
|
180
|
+
return _lodashmerge2.default.call(void 0, src, target);
|
|
181
|
+
}
|
|
182
|
+
function processFile(paramName, file) {
|
|
183
|
+
if (typeof file === "string") {
|
|
184
|
+
const resolvedFile = _path2.default.resolve(file);
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
_fs2.default.stat(resolvedFile, async (err) => {
|
|
187
|
+
if (err) {
|
|
188
|
+
if (err.code === "ENOENT") {
|
|
189
|
+
return resolve(void 0);
|
|
190
|
+
}
|
|
191
|
+
return reject(err);
|
|
192
|
+
}
|
|
193
|
+
const fileMetadata = await _syncjs2.default.call(void 0, resolvedFile);
|
|
194
|
+
const payloadFilename = encodeURIComponent(_path2.default.basename(resolvedFile));
|
|
195
|
+
return resolve({
|
|
196
|
+
paramName,
|
|
197
|
+
base64: _optionalChain([fileMetadata, 'optionalAccess', _6 => _6.content, 'optionalAccess', _7 => _7.replace, 'call', _8 => _8(";base64", `;name=${payloadFilename};base64`)]),
|
|
198
|
+
filename: payloadFilename,
|
|
199
|
+
buffer: fileMetadata.buffer
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
} else if (file instanceof _stream2.default.Readable) {
|
|
204
|
+
return _getstream2.default.buffer(file).then((buffer) => {
|
|
205
|
+
const filePath = file.path;
|
|
206
|
+
const parser = new (0, _parserjs2.default)();
|
|
207
|
+
const base64 = parser.format(filePath, buffer).content;
|
|
208
|
+
const payloadFilename = encodeURIComponent(_path2.default.basename(filePath));
|
|
209
|
+
return {
|
|
210
|
+
paramName,
|
|
211
|
+
base64: _optionalChain([base64, 'optionalAccess', _9 => _9.replace, 'call', _10 => _10(";base64", `;name=${payloadFilename};base64`)]),
|
|
212
|
+
filename: payloadFilename,
|
|
213
|
+
buffer
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return Promise.reject(
|
|
218
|
+
new TypeError(
|
|
219
|
+
paramName ? `The data supplied for the \`${paramName}\` request body parameter is not a file handler that we support.` : "The data supplied for the request body payload is not a file handler that we support."
|
|
220
|
+
)
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
async function prepareParams(operation, body, metadata) {
|
|
224
|
+
let metadataIntersected = false;
|
|
225
|
+
const digestedParameters = digestParameters(operation.getParameters());
|
|
226
|
+
const jsonSchema = operation.getParametersAsJSONSchema();
|
|
227
|
+
metadata = _removeundefinedobjects2.default.call(void 0, metadata);
|
|
228
|
+
if (!jsonSchema && (body !== void 0 || metadata !== void 0)) {
|
|
229
|
+
let throwNoParamsError = true;
|
|
230
|
+
if (body !== void 0) {
|
|
231
|
+
if (typeof body === "object" && body !== null && !Array.isArray(body)) {
|
|
232
|
+
if (Object.keys(body).length <= 2) {
|
|
233
|
+
const bodyParams = _caseless2.default.call(void 0, body);
|
|
234
|
+
if (specialHeaders.some((header) => bodyParams.has(header))) {
|
|
235
|
+
throwNoParamsError = false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (throwNoParamsError) {
|
|
241
|
+
throw new Error(
|
|
242
|
+
"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."
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const jsonSchemaDefaults = jsonSchema ? getJSONSchemaDefaults(jsonSchema) : {};
|
|
247
|
+
const params = jsonSchemaDefaults;
|
|
248
|
+
if (typeof body !== "undefined") {
|
|
249
|
+
if (Array.isArray(body) || isPrimitive(body)) {
|
|
250
|
+
params.body = merge(params.body, body);
|
|
251
|
+
} else if (typeof metadata === "undefined") {
|
|
252
|
+
const headerParams = _caseless2.default.call(void 0, {});
|
|
253
|
+
Object.entries(digestedParameters).forEach(([paramName, param]) => {
|
|
254
|
+
if (param.in === "header") {
|
|
255
|
+
headerParams.set(paramName, "");
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
specialHeaders.forEach((header) => {
|
|
259
|
+
if (!headerParams.has(header)) {
|
|
260
|
+
headerParams.set(header, "");
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
const intersection = Object.keys(body).filter((value) => {
|
|
264
|
+
if (Object.keys(digestedParameters).includes(value)) {
|
|
265
|
+
return true;
|
|
266
|
+
} else if (headerParams.has(value)) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
return false;
|
|
270
|
+
}).length;
|
|
271
|
+
if (intersection && intersection / Object.keys(body).length > 0.25) {
|
|
272
|
+
metadataIntersected = true;
|
|
273
|
+
metadata = merge(params.body, body);
|
|
274
|
+
body = void 0;
|
|
275
|
+
} else {
|
|
276
|
+
params.body = merge(params.body, body);
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
params.body = merge(params.body, body);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (!operation.hasRequestBody()) {
|
|
283
|
+
delete params.body;
|
|
284
|
+
} else {
|
|
285
|
+
if (!("body" in params))
|
|
286
|
+
params.body = {};
|
|
287
|
+
const payloadJsonSchema = jsonSchema.find((js) => js.type === "body");
|
|
288
|
+
if (payloadJsonSchema) {
|
|
289
|
+
if (!params.files)
|
|
290
|
+
params.files = {};
|
|
291
|
+
const conversions = [];
|
|
292
|
+
if (_optionalChain([payloadJsonSchema, 'access', _11 => _11.schema, 'optionalAccess', _12 => _12.properties])) {
|
|
293
|
+
Object.entries(_optionalChain([payloadJsonSchema, 'access', _13 => _13.schema, 'optionalAccess', _14 => _14.properties])).filter(([, schema]) => _optionalChain([schema, 'optionalAccess', _15 => _15.format]) === "binary").filter(([prop]) => Object.keys(params.body).includes(prop)).forEach(([prop]) => {
|
|
294
|
+
conversions.push(processFile(prop, params.body[prop]));
|
|
295
|
+
});
|
|
296
|
+
} else if (_optionalChain([payloadJsonSchema, 'access', _16 => _16.schema, 'optionalAccess', _17 => _17.type]) === "string") {
|
|
297
|
+
if (_optionalChain([payloadJsonSchema, 'access', _18 => _18.schema, 'optionalAccess', _19 => _19.format]) === "binary") {
|
|
298
|
+
conversions.push(processFile(void 0, params.body));
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
await Promise.all(conversions).then((fileMetadata) => fileMetadata.filter(Boolean)).then((fm) => {
|
|
302
|
+
fm.forEach((fileMetadata) => {
|
|
303
|
+
if (!fileMetadata) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (fileMetadata.paramName) {
|
|
307
|
+
params.body[fileMetadata.paramName] = fileMetadata.base64;
|
|
308
|
+
} else {
|
|
309
|
+
params.body = fileMetadata.base64;
|
|
310
|
+
}
|
|
311
|
+
if (fileMetadata.buffer && _optionalChain([params, 'optionalAccess', _20 => _20.files])) {
|
|
312
|
+
params.files[fileMetadata.filename] = fileMetadata.buffer;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (operation.isFormUrlEncoded()) {
|
|
319
|
+
params.formData = merge(params.formData, params.body);
|
|
320
|
+
delete params.body;
|
|
321
|
+
}
|
|
322
|
+
if (typeof metadata !== "undefined") {
|
|
323
|
+
if (!("cookie" in params))
|
|
324
|
+
params.cookie = {};
|
|
325
|
+
if (!("header" in params))
|
|
326
|
+
params.header = {};
|
|
327
|
+
if (!("path" in params))
|
|
328
|
+
params.path = {};
|
|
329
|
+
if (!("query" in params))
|
|
330
|
+
params.query = {};
|
|
331
|
+
Object.entries(digestedParameters).forEach(([paramName, param]) => {
|
|
332
|
+
let value;
|
|
333
|
+
let metadataHeaderParam;
|
|
334
|
+
if (typeof metadata === "object" && !isEmpty(metadata)) {
|
|
335
|
+
if (paramName in metadata) {
|
|
336
|
+
value = metadata[paramName];
|
|
337
|
+
metadataHeaderParam = paramName;
|
|
338
|
+
} else if (param.in === "header") {
|
|
339
|
+
metadataHeaderParam = Object.keys(metadata).find((k) => k.toLowerCase() === paramName.toLowerCase()) || "";
|
|
340
|
+
value = metadata[metadataHeaderParam];
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (value === void 0) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
switch (param.in) {
|
|
347
|
+
case "path":
|
|
348
|
+
params.path[paramName] = value;
|
|
349
|
+
if (_optionalChain([metadata, 'optionalAccess', _21 => _21[paramName]]))
|
|
350
|
+
delete metadata[paramName];
|
|
351
|
+
break;
|
|
352
|
+
case "query":
|
|
353
|
+
params.query[paramName] = value;
|
|
354
|
+
if (_optionalChain([metadata, 'optionalAccess', _22 => _22[paramName]]))
|
|
355
|
+
delete metadata[paramName];
|
|
356
|
+
break;
|
|
357
|
+
case "header":
|
|
358
|
+
params.header[paramName.toLowerCase()] = value;
|
|
359
|
+
if (metadataHeaderParam && _optionalChain([metadata, 'optionalAccess', _23 => _23[metadataHeaderParam]]))
|
|
360
|
+
delete metadata[metadataHeaderParam];
|
|
361
|
+
break;
|
|
362
|
+
case "cookie":
|
|
363
|
+
params.cookie[paramName] = value;
|
|
364
|
+
if (_optionalChain([metadata, 'optionalAccess', _24 => _24[paramName]]))
|
|
365
|
+
delete metadata[paramName];
|
|
366
|
+
break;
|
|
367
|
+
default:
|
|
368
|
+
}
|
|
369
|
+
if (metadataIntersected && operation.isFormUrlEncoded()) {
|
|
370
|
+
if (paramName in params.formData) {
|
|
371
|
+
delete params.formData[paramName];
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
if (!isEmpty(metadata)) {
|
|
376
|
+
if (typeof metadata === "object") {
|
|
377
|
+
specialHeaders.forEach((headerName) => {
|
|
378
|
+
const headerParam = Object.keys(metadata || {}).find((m) => m.toLowerCase() === headerName);
|
|
379
|
+
if (headerParam) {
|
|
380
|
+
if (typeof metadata === "object") {
|
|
381
|
+
if (typeof params.header === "object") {
|
|
382
|
+
params.header[headerName] = metadata[headerParam];
|
|
383
|
+
}
|
|
384
|
+
delete metadata[headerParam];
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
if (operation.isFormUrlEncoded()) {
|
|
390
|
+
params.formData = merge(params.formData, metadata);
|
|
391
|
+
} else {
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
["body", "cookie", "files", "formData", "header", "path", "query"].forEach((type) => {
|
|
396
|
+
if (type in params && isEmpty(params[type])) {
|
|
397
|
+
delete params[type];
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
return params;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// src/lib/prepareServer.ts
|
|
404
|
+
function stripTrailingSlash(url) {
|
|
405
|
+
if (url[url.length - 1] === "/") {
|
|
406
|
+
return url.slice(0, -1);
|
|
407
|
+
}
|
|
408
|
+
return url;
|
|
409
|
+
}
|
|
410
|
+
function prepareServer(spec, url, variables = {}) {
|
|
411
|
+
let serverIdx;
|
|
412
|
+
const sanitizedUrl = stripTrailingSlash(url);
|
|
413
|
+
(spec.api.servers || []).forEach((server, i) => {
|
|
414
|
+
if (server.url === sanitizedUrl) {
|
|
415
|
+
serverIdx = i;
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
if (serverIdx) {
|
|
419
|
+
return {
|
|
420
|
+
selected: serverIdx,
|
|
421
|
+
variables
|
|
422
|
+
};
|
|
423
|
+
} else if (Object.keys(variables).length) {
|
|
424
|
+
} else {
|
|
425
|
+
const server = spec.splitVariables(url);
|
|
426
|
+
if (server) {
|
|
427
|
+
return {
|
|
428
|
+
selected: server.selected,
|
|
429
|
+
variables: server.variables
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// src/index.ts
|
|
437
|
+
var APICore = (_class = class {
|
|
438
|
+
|
|
439
|
+
__init() {this.auth = []}
|
|
440
|
+
__init2() {this.server = false}
|
|
441
|
+
__init3() {this.config = {}}
|
|
442
|
+
|
|
443
|
+
constructor(spec, userAgent) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);
|
|
444
|
+
if (spec)
|
|
445
|
+
this.spec = spec;
|
|
446
|
+
if (userAgent)
|
|
447
|
+
this.userAgent = userAgent;
|
|
448
|
+
}
|
|
449
|
+
setSpec(spec) {
|
|
450
|
+
this.spec = spec;
|
|
451
|
+
}
|
|
452
|
+
setConfig(config) {
|
|
453
|
+
this.config = config;
|
|
454
|
+
return this;
|
|
455
|
+
}
|
|
456
|
+
setUserAgent(userAgent) {
|
|
457
|
+
this.userAgent = userAgent;
|
|
458
|
+
return this;
|
|
459
|
+
}
|
|
460
|
+
setAuth(...values) {
|
|
461
|
+
this.auth = values;
|
|
462
|
+
return this;
|
|
463
|
+
}
|
|
464
|
+
setServer(url, variables = {}) {
|
|
465
|
+
this.server = { url, variables };
|
|
466
|
+
return this;
|
|
467
|
+
}
|
|
468
|
+
async fetch(path2, method, body, metadata) {
|
|
469
|
+
const operation = this.spec.operation(path2, method);
|
|
470
|
+
return this.fetchOperation(operation, body, metadata);
|
|
471
|
+
}
|
|
472
|
+
async fetchOperation(operation, body, metadata) {
|
|
473
|
+
return prepareParams(operation, body, metadata).then((params) => {
|
|
474
|
+
const data = { ...params };
|
|
475
|
+
if (this.server) {
|
|
476
|
+
const preparedServer = prepareServer(this.spec, this.server.url, this.server.variables);
|
|
477
|
+
if (preparedServer) {
|
|
478
|
+
data.server = preparedServer;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const har = _oastohar2.default.call(void 0, this.spec, operation, data, prepareAuth(this.auth, operation));
|
|
482
|
+
let timeoutSignal;
|
|
483
|
+
const init = {};
|
|
484
|
+
if (this.config.timeout) {
|
|
485
|
+
const controller = new AbortController();
|
|
486
|
+
timeoutSignal = setTimeout(() => controller.abort(), this.config.timeout);
|
|
487
|
+
init.signal = controller.signal;
|
|
488
|
+
}
|
|
489
|
+
return _fetchhar2.default.call(void 0, har, {
|
|
490
|
+
files: data.files || {},
|
|
491
|
+
init,
|
|
492
|
+
userAgent: this.userAgent
|
|
493
|
+
}).then(async (res) => {
|
|
494
|
+
const parsed = await parseResponse(res);
|
|
495
|
+
if (res.status >= 400 && res.status <= 599) {
|
|
496
|
+
throw new (0, _chunkNVFSP2R3cjs.FetchError)(
|
|
497
|
+
parsed.status,
|
|
498
|
+
parsed.data,
|
|
499
|
+
parsed.headers,
|
|
500
|
+
parsed.res
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
return parsed;
|
|
504
|
+
}).finally(() => {
|
|
505
|
+
if (this.config.timeout) {
|
|
506
|
+
clearTimeout(timeoutSignal);
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
}, _class);
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
exports.default = APICore;
|
|
515
|
+
|
|
516
|
+
module.exports = exports.default//# sourceMappingURL=index.cjs.map
|