@readme/api-core 7.0.0-alpha.1 → 7.0.0-alpha.3
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 +8 -7
- 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,36 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
3
|
+
class FetchError extends Error {
|
|
4
|
+
/** HTTP Status */
|
|
5
|
+
status;
|
|
6
|
+
/** The content of the response. */
|
|
7
|
+
data;
|
|
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;
|
|
27
19
|
// We could fix this by updating our target to ES2015 but because we support exporting to CJS
|
|
28
20
|
// we can't.
|
|
29
21
|
//
|
|
30
22
|
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
31
|
-
Object.setPrototypeOf(
|
|
32
|
-
return _this;
|
|
23
|
+
Object.setPrototypeOf(this, FetchError.prototype);
|
|
33
24
|
}
|
|
34
|
-
|
|
35
|
-
}(Error));
|
|
25
|
+
}
|
|
36
26
|
exports.default = FetchError;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type Oas from 'oas';
|
|
2
|
-
import type
|
|
3
|
-
import type { HttpMethods } from 'oas/
|
|
2
|
+
import type Operation from 'oas/operation';
|
|
3
|
+
import type { HttpMethods } from 'oas/rmoas.types';
|
|
4
4
|
import getJSONSchemaDefaults from './lib/getJSONSchemaDefaults';
|
|
5
5
|
import parseResponse from './lib/parseResponse';
|
|
6
6
|
import prepareAuth from './lib/prepareAuth';
|
package/dist/index.js
CHANGED
|
@@ -1,164 +1,96 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
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;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
4
|
};
|
|
52
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
6
|
exports.prepareServer = exports.prepareParams = exports.prepareAuth = exports.parseResponse = exports.getJSONSchemaDefaults = void 0;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
7
|
+
const oas_to_har_1 = __importDefault(require("@readme/oas-to-har"));
|
|
8
|
+
const fetch_har_1 = __importDefault(require("fetch-har"));
|
|
9
|
+
const fetchError_1 = __importDefault(require("./errors/fetchError"));
|
|
10
|
+
const getJSONSchemaDefaults_1 = __importDefault(require("./lib/getJSONSchemaDefaults"));
|
|
58
11
|
exports.getJSONSchemaDefaults = getJSONSchemaDefaults_1.default;
|
|
59
|
-
|
|
12
|
+
const parseResponse_1 = __importDefault(require("./lib/parseResponse"));
|
|
60
13
|
exports.parseResponse = parseResponse_1.default;
|
|
61
|
-
|
|
14
|
+
const prepareAuth_1 = __importDefault(require("./lib/prepareAuth"));
|
|
62
15
|
exports.prepareAuth = prepareAuth_1.default;
|
|
63
|
-
|
|
16
|
+
const prepareParams_1 = __importDefault(require("./lib/prepareParams"));
|
|
64
17
|
exports.prepareParams = prepareParams_1.default;
|
|
65
|
-
|
|
18
|
+
const prepareServer_1 = __importDefault(require("./lib/prepareServer"));
|
|
66
19
|
exports.prepareServer = prepareServer_1.default;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
20
|
+
class APICore {
|
|
21
|
+
spec;
|
|
22
|
+
auth = [];
|
|
23
|
+
server = false;
|
|
24
|
+
config = {};
|
|
25
|
+
userAgent;
|
|
26
|
+
constructor(spec, userAgent) {
|
|
72
27
|
if (spec)
|
|
73
28
|
this.spec = spec;
|
|
74
29
|
if (userAgent)
|
|
75
30
|
this.userAgent = userAgent;
|
|
76
31
|
}
|
|
77
|
-
|
|
32
|
+
setSpec(spec) {
|
|
78
33
|
this.spec = spec;
|
|
79
|
-
}
|
|
80
|
-
|
|
34
|
+
}
|
|
35
|
+
setConfig(config) {
|
|
81
36
|
this.config = config;
|
|
82
37
|
return this;
|
|
83
|
-
}
|
|
84
|
-
|
|
38
|
+
}
|
|
39
|
+
setUserAgent(userAgent) {
|
|
85
40
|
this.userAgent = userAgent;
|
|
86
41
|
return this;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
var values = [];
|
|
90
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
91
|
-
values[_i] = arguments[_i];
|
|
92
|
-
}
|
|
42
|
+
}
|
|
43
|
+
setAuth(...values) {
|
|
93
44
|
this.auth = values;
|
|
94
45
|
return this;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.server = { url: url, variables: variables };
|
|
46
|
+
}
|
|
47
|
+
setServer(url, variables = {}) {
|
|
48
|
+
this.server = { url, variables };
|
|
99
49
|
return this;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
switch (_a.label) {
|
|
143
|
-
case 0: return [4 /*yield*/, (0, parseResponse_1.default)(res)];
|
|
144
|
-
case 1:
|
|
145
|
-
parsed = _a.sent();
|
|
146
|
-
if (res.status >= 400 && res.status <= 599) {
|
|
147
|
-
throw new fetchError_1.default(parsed.status, parsed.data, parsed.headers, parsed.res);
|
|
148
|
-
}
|
|
149
|
-
return [2 /*return*/, parsed];
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
}); })
|
|
153
|
-
.finally(function () {
|
|
154
|
-
if (_this.config.timeout) {
|
|
155
|
-
clearTimeout(timeoutSignal);
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
})];
|
|
50
|
+
}
|
|
51
|
+
async fetch(path, method, body, metadata) {
|
|
52
|
+
const operation = this.spec.operation(path, method);
|
|
53
|
+
return this.fetchOperation(operation, body, metadata);
|
|
54
|
+
}
|
|
55
|
+
async fetchOperation(operation, body, metadata) {
|
|
56
|
+
return (0, prepareParams_1.default)(operation, body, metadata).then(params => {
|
|
57
|
+
const data = { ...params };
|
|
58
|
+
// If `sdk.server()` has been issued data then we need to do some extra work to figure out
|
|
59
|
+
// how to use that supplied server, and also handle any server variables that were sent
|
|
60
|
+
// alongside it.
|
|
61
|
+
if (this.server) {
|
|
62
|
+
const preparedServer = (0, prepareServer_1.default)(this.spec, this.server.url, this.server.variables);
|
|
63
|
+
if (preparedServer) {
|
|
64
|
+
data.server = preparedServer;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// @ts-expect-error `this.auth` typing is off. FIXME
|
|
68
|
+
const har = (0, oas_to_har_1.default)(this.spec, operation, data, (0, prepareAuth_1.default)(this.auth, operation));
|
|
69
|
+
let timeoutSignal;
|
|
70
|
+
const init = {};
|
|
71
|
+
if (this.config.timeout) {
|
|
72
|
+
const controller = new AbortController();
|
|
73
|
+
timeoutSignal = setTimeout(() => controller.abort(), this.config.timeout);
|
|
74
|
+
init.signal = controller.signal;
|
|
75
|
+
}
|
|
76
|
+
return (0, fetch_har_1.default)(har, {
|
|
77
|
+
files: data.files || {},
|
|
78
|
+
init,
|
|
79
|
+
userAgent: this.userAgent,
|
|
80
|
+
})
|
|
81
|
+
.then(async (res) => {
|
|
82
|
+
const parsed = await (0, parseResponse_1.default)(res);
|
|
83
|
+
if (res.status >= 400 && res.status <= 599) {
|
|
84
|
+
throw new fetchError_1.default(parsed.status, parsed.data, parsed.headers, parsed.res);
|
|
85
|
+
}
|
|
86
|
+
return parsed;
|
|
87
|
+
})
|
|
88
|
+
.finally(() => {
|
|
89
|
+
if (this.config.timeout) {
|
|
90
|
+
clearTimeout(timeoutSignal);
|
|
91
|
+
}
|
|
159
92
|
});
|
|
160
93
|
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
}());
|
|
94
|
+
}
|
|
95
|
+
}
|
|
164
96
|
exports.default = APICore;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { SchemaWrapper } from 'oas/
|
|
1
|
+
import type { SchemaWrapper } from 'oas/operation/get-parameters-as-json-schema';
|
|
2
2
|
/**
|
|
3
3
|
* Run through a JSON Schema object and compose up an object containing default data for any schema
|
|
4
4
|
* property that is required and also has a defined default.
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
6
|
+
const json_schema_traverse_1 = __importDefault(require("json-schema-traverse"));
|
|
7
7
|
/**
|
|
8
8
|
* Run through a JSON Schema object and compose up an object containing default data for any schema
|
|
9
9
|
* property that is required and also has a defined default.
|
|
@@ -16,34 +16,32 @@ var json_schema_traverse_1 = __importDefault(require("json-schema-traverse"));
|
|
|
16
16
|
*/
|
|
17
17
|
function getJSONSchemaDefaults(jsonSchemas) {
|
|
18
18
|
return jsonSchemas
|
|
19
|
-
.map(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
var defaults = {};
|
|
23
|
-
(0, json_schema_traverse_1.default)(jsonSchema, function (schema, pointer, rootSchema, parentPointer, parentKeyword, parentSchema, indexProperty) {
|
|
19
|
+
.map(({ type: payloadType, schema: jsonSchema }) => {
|
|
20
|
+
const defaults = {};
|
|
21
|
+
(0, json_schema_traverse_1.default)(jsonSchema, (schema, pointer, rootSchema, parentPointer, parentKeyword, parentSchema, indexProperty) => {
|
|
24
22
|
if (!pointer.startsWith('/properties/')) {
|
|
25
23
|
return;
|
|
26
24
|
}
|
|
27
|
-
if (Array.isArray(parentSchema
|
|
25
|
+
if (Array.isArray(parentSchema?.required) && parentSchema?.required.includes(String(indexProperty))) {
|
|
28
26
|
if (schema.type === 'object' && indexProperty) {
|
|
29
27
|
defaults[indexProperty] = {};
|
|
30
28
|
}
|
|
31
|
-
|
|
29
|
+
let destination = defaults;
|
|
32
30
|
if (parentPointer) {
|
|
33
31
|
// To map nested objects correct we need to pick apart the parent pointer.
|
|
34
32
|
parentPointer
|
|
35
33
|
.replace(/\/properties/g, '')
|
|
36
34
|
.split('/')
|
|
37
|
-
.forEach(
|
|
35
|
+
.forEach((subSchema) => {
|
|
38
36
|
if (subSchema === '') {
|
|
39
37
|
return;
|
|
40
38
|
}
|
|
41
|
-
|
|
39
|
+
destination = destination?.[subSchema] || {};
|
|
42
40
|
});
|
|
43
41
|
}
|
|
44
42
|
if (schema.default !== undefined) {
|
|
45
43
|
if (indexProperty !== undefined) {
|
|
46
|
-
|
|
44
|
+
destination[indexProperty] = schema.default;
|
|
47
45
|
}
|
|
48
46
|
}
|
|
49
47
|
}
|
|
@@ -51,11 +49,11 @@ function getJSONSchemaDefaults(jsonSchemas) {
|
|
|
51
49
|
if (!Object.keys(defaults).length) {
|
|
52
50
|
return {};
|
|
53
51
|
}
|
|
54
|
-
return
|
|
52
|
+
return {
|
|
55
53
|
// @todo should we filter out empty and undefined objects from here with `remove-undefined-objects`?
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
[payloadType]: defaults,
|
|
55
|
+
};
|
|
58
56
|
})
|
|
59
|
-
.reduce(
|
|
57
|
+
.reduce((prev, next) => Object.assign(prev, next));
|
|
60
58
|
}
|
|
61
59
|
exports.default = getJSONSchemaDefaults;
|
|
@@ -1,71 +1,25 @@
|
|
|
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
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
return [2 /*return*/, {
|
|
62
|
-
data: data,
|
|
63
|
-
status: response.status,
|
|
64
|
-
headers: response.headers,
|
|
65
|
-
res: response,
|
|
66
|
-
}];
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
});
|
|
3
|
+
const utils_1 = require("oas/utils");
|
|
4
|
+
async function parseResponse(response) {
|
|
5
|
+
const contentType = response.headers.get('Content-Type');
|
|
6
|
+
const isJSON = contentType && (utils_1.matchesMimeType.json(contentType) || utils_1.matchesMimeType.wildcard(contentType));
|
|
7
|
+
const responseBody = await response.text();
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
let data = responseBody;
|
|
10
|
+
if (isJSON) {
|
|
11
|
+
try {
|
|
12
|
+
data = JSON.parse(responseBody);
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
// If our JSON parsing failed then we can just return plaintext instead.
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
data,
|
|
20
|
+
status: response.status,
|
|
21
|
+
headers: response.headers,
|
|
22
|
+
res: response,
|
|
23
|
+
};
|
|
70
24
|
}
|
|
71
25
|
exports.default = parseResponse;
|
package/dist/lib/prepareAuth.js
CHANGED
|
@@ -4,27 +4,27 @@ function prepareAuth(authKey, operation) {
|
|
|
4
4
|
if (authKey.length === 0) {
|
|
5
5
|
return {};
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const preparedAuth = {};
|
|
8
|
+
const security = operation.getSecurity();
|
|
9
9
|
if (security.length === 0) {
|
|
10
10
|
// If there's no auth configured on this operation, don't prepare anything (even if it was
|
|
11
11
|
// supplied by the user).
|
|
12
12
|
return {};
|
|
13
13
|
}
|
|
14
14
|
// Does this operation require multiple forms of auth?
|
|
15
|
-
if (security.every(
|
|
15
|
+
if (security.every(s => Object.keys(s).length > 1)) {
|
|
16
16
|
throw new Error("Sorry, this operation currently requires multiple forms of authentication which this library doesn't yet support.");
|
|
17
17
|
}
|
|
18
18
|
// Since we can only handle single auth security configurations, let's pull those out. This code
|
|
19
19
|
// is a bit opaque but `security` here may look like `[{ basic: [] }, { oauth2: [], basic: []}]`
|
|
20
20
|
// and are filtering it down to only single-auth requirements of `[{ basic: [] }]`.
|
|
21
|
-
|
|
22
|
-
.map(
|
|
21
|
+
const usableSecurity = security
|
|
22
|
+
.map(s => {
|
|
23
23
|
return Object.keys(s).length === 1 ? s : false;
|
|
24
24
|
})
|
|
25
25
|
.filter(Boolean);
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const usableSecuritySchemes = usableSecurity.map(s => Object.keys(s)).reduce((prev, next) => prev.concat(next), []);
|
|
27
|
+
const preparedSecurity = operation.prepareSecurity();
|
|
28
28
|
// If we have two auth tokens present let's look for Basic Auth in their configuration.
|
|
29
29
|
if (authKey.length >= 2) {
|
|
30
30
|
// If this operation doesn't support HTTP Basic auth but we have two tokens, that's a paddlin.
|
|
@@ -33,12 +33,12 @@ function prepareAuth(authKey, operation) {
|
|
|
33
33
|
}
|
|
34
34
|
// If we have two auth keys for Basic Auth but Basic isn't a usable security scheme (maybe it's
|
|
35
35
|
// part of an AND or auth configuration -- which we don't support) then we need to error out.
|
|
36
|
-
|
|
37
|
-
if (!
|
|
36
|
+
const schemes = preparedSecurity.Basic.filter(s => usableSecuritySchemes.includes(s._key));
|
|
37
|
+
if (!schemes.length) {
|
|
38
38
|
throw new Error('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.');
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
preparedAuth[
|
|
40
|
+
const scheme = schemes.shift();
|
|
41
|
+
preparedAuth[scheme._key] = {
|
|
42
42
|
user: authKey[0],
|
|
43
43
|
pass: authKey.length === 2 ? authKey[1] : '',
|
|
44
44
|
};
|
|
@@ -48,14 +48,11 @@ function prepareAuth(authKey, operation) {
|
|
|
48
48
|
// can pick the first usable security scheme available and try to use that. This might not always
|
|
49
49
|
// be the auth scheme that the user wants, but we don't have any other way for the user to tell
|
|
50
50
|
// us what they want with the current `sdk.auth()` API.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
.map(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
})
|
|
57
|
-
.reduce(function (prev, next) { return prev.concat(next); }, []);
|
|
58
|
-
var scheme = schemes.shift();
|
|
51
|
+
const usableScheme = usableSecuritySchemes[0];
|
|
52
|
+
const schemes = Object.entries(preparedSecurity)
|
|
53
|
+
.map(([, ps]) => ps.filter(s => usableScheme === s._key))
|
|
54
|
+
.reduce((prev, next) => prev.concat(next), []);
|
|
55
|
+
const scheme = schemes.shift();
|
|
59
56
|
switch (scheme.type) {
|
|
60
57
|
case 'http':
|
|
61
58
|
if (scheme.scheme === 'basic') {
|
|
@@ -77,7 +74,7 @@ function prepareAuth(authKey, operation) {
|
|
|
77
74
|
}
|
|
78
75
|
break;
|
|
79
76
|
default:
|
|
80
|
-
throw new Error(
|
|
77
|
+
throw new Error(`Sorry, this API currently uses a security scheme, ${scheme.type}, which this library doesn't yet support.`);
|
|
81
78
|
}
|
|
82
79
|
return preparedAuth;
|
|
83
80
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import type
|
|
2
|
+
import type Operation from 'oas/operation';
|
|
3
3
|
/**
|
|
4
4
|
* With potentially supplied body and/or metadata we need to run through them against a given API
|
|
5
5
|
* operation to see what's what and prepare any available parameters to be used in an API request
|