@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.
@@ -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
- var FetchError = /** @class */ (function (_super) {
19
- __extends(FetchError, _super);
20
- function FetchError(status, data, headers, res) {
21
- var _this = _super.call(this, res.statusText) || this;
22
- _this.name = 'FetchError';
23
- _this.status = status;
24
- _this.data = data;
25
- _this.headers = headers;
26
- _this.res = res;
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(_this, FetchError.prototype);
32
- return _this;
23
+ Object.setPrototypeOf(this, FetchError.prototype);
33
24
  }
34
- return FetchError;
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 { Operation } from 'oas';
3
- import type { HttpMethods } from 'oas/dist/rmoas.types';
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
- var oas_to_har_1 = __importDefault(require("@readme/oas-to-har"));
55
- var fetch_har_1 = __importDefault(require("fetch-har"));
56
- var fetchError_1 = __importDefault(require("./errors/fetchError"));
57
- var getJSONSchemaDefaults_1 = __importDefault(require("./lib/getJSONSchemaDefaults"));
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
- var parseResponse_1 = __importDefault(require("./lib/parseResponse"));
12
+ const parseResponse_1 = __importDefault(require("./lib/parseResponse"));
60
13
  exports.parseResponse = parseResponse_1.default;
61
- var prepareAuth_1 = __importDefault(require("./lib/prepareAuth"));
14
+ const prepareAuth_1 = __importDefault(require("./lib/prepareAuth"));
62
15
  exports.prepareAuth = prepareAuth_1.default;
63
- var prepareParams_1 = __importDefault(require("./lib/prepareParams"));
16
+ const prepareParams_1 = __importDefault(require("./lib/prepareParams"));
64
17
  exports.prepareParams = prepareParams_1.default;
65
- var prepareServer_1 = __importDefault(require("./lib/prepareServer"));
18
+ const prepareServer_1 = __importDefault(require("./lib/prepareServer"));
66
19
  exports.prepareServer = prepareServer_1.default;
67
- var APICore = /** @class */ (function () {
68
- function APICore(spec, userAgent) {
69
- this.auth = [];
70
- this.server = false;
71
- this.config = {};
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
- APICore.prototype.setSpec = function (spec) {
32
+ setSpec(spec) {
78
33
  this.spec = spec;
79
- };
80
- APICore.prototype.setConfig = function (config) {
34
+ }
35
+ setConfig(config) {
81
36
  this.config = config;
82
37
  return this;
83
- };
84
- APICore.prototype.setUserAgent = function (userAgent) {
38
+ }
39
+ setUserAgent(userAgent) {
85
40
  this.userAgent = userAgent;
86
41
  return this;
87
- };
88
- APICore.prototype.setAuth = function () {
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
- APICore.prototype.setServer = function (url, variables) {
97
- if (variables === void 0) { variables = {}; }
98
- this.server = { url: url, variables: variables };
46
+ }
47
+ setServer(url, variables = {}) {
48
+ this.server = { url, variables };
99
49
  return this;
100
- };
101
- APICore.prototype.fetch = function (path, method, body, metadata) {
102
- return __awaiter(this, void 0, void 0, function () {
103
- var operation;
104
- return __generator(this, function (_a) {
105
- operation = this.spec.operation(path, method);
106
- return [2 /*return*/, this.fetchOperation(operation, body, metadata)];
107
- });
108
- });
109
- };
110
- APICore.prototype.fetchOperation = function (operation, body, metadata) {
111
- return __awaiter(this, void 0, void 0, function () {
112
- var _this = this;
113
- return __generator(this, function (_a) {
114
- return [2 /*return*/, (0, prepareParams_1.default)(operation, body, metadata).then(function (params) {
115
- var data = __assign({}, params);
116
- // If `sdk.server()` has been issued data then we need to do some extra work to figure out
117
- // how to use that supplied server, and also handle any server variables that were sent
118
- // alongside it.
119
- if (_this.server) {
120
- var preparedServer = (0, prepareServer_1.default)(_this.spec, _this.server.url, _this.server.variables);
121
- if (preparedServer) {
122
- data.server = preparedServer;
123
- }
124
- }
125
- // @ts-expect-error `this.auth` typing is off. FIXME
126
- var har = (0, oas_to_har_1.default)(_this.spec, operation, data, (0, prepareAuth_1.default)(_this.auth, operation));
127
- var timeoutSignal;
128
- var init = {};
129
- if (_this.config.timeout) {
130
- var controller_1 = new AbortController();
131
- timeoutSignal = setTimeout(function () { return controller_1.abort(); }, _this.config.timeout);
132
- init.signal = controller_1.signal;
133
- }
134
- return (0, fetch_har_1.default)(har, {
135
- files: data.files || {},
136
- init: init,
137
- userAgent: _this.userAgent,
138
- })
139
- .then(function (res) { return __awaiter(_this, void 0, void 0, function () {
140
- var parsed;
141
- return __generator(this, function (_a) {
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
- return APICore;
163
- }());
94
+ }
95
+ }
164
96
  exports.default = APICore;
@@ -1,4 +1,4 @@
1
- import type { SchemaWrapper } from 'oas/dist/operation/get-parameters-as-json-schema';
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
- var json_schema_traverse_1 = __importDefault(require("json-schema-traverse"));
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(function (_a) {
20
- var _b;
21
- var payloadType = _a.type, jsonSchema = _a.schema;
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 === null || parentSchema === void 0 ? void 0 : parentSchema.required) && (parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.required.includes(String(indexProperty)))) {
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
- var destination_1 = defaults;
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(function (subSchema) {
35
+ .forEach((subSchema) => {
38
36
  if (subSchema === '') {
39
37
  return;
40
38
  }
41
- destination_1 = (destination_1 === null || destination_1 === void 0 ? void 0 : destination_1[subSchema]) || {};
39
+ destination = destination?.[subSchema] || {};
42
40
  });
43
41
  }
44
42
  if (schema.default !== undefined) {
45
43
  if (indexProperty !== undefined) {
46
- destination_1[indexProperty] = schema.default;
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 _b = {},
52
+ return {
55
53
  // @todo should we filter out empty and undefined objects from here with `remove-undefined-objects`?
56
- _b[payloadType] = defaults,
57
- _b;
54
+ [payloadType]: defaults,
55
+ };
58
56
  })
59
- .reduce(function (prev, next) { return Object.assign(prev, next); });
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
- var oas_1 = require("oas");
40
- var matchesMimeType = oas_1.utils.matchesMimeType;
41
- function parseResponse(response) {
42
- return __awaiter(this, void 0, void 0, function () {
43
- var contentType, isJSON, responseBody, data;
44
- return __generator(this, function (_a) {
45
- switch (_a.label) {
46
- case 0:
47
- contentType = response.headers.get('Content-Type');
48
- isJSON = contentType && (matchesMimeType.json(contentType) || matchesMimeType.wildcard(contentType));
49
- return [4 /*yield*/, response.text()];
50
- case 1:
51
- responseBody = _a.sent();
52
- data = responseBody;
53
- if (isJSON) {
54
- try {
55
- data = JSON.parse(responseBody);
56
- }
57
- catch (e) {
58
- // If our JSON parsing failed then we can just return plaintext instead.
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;
@@ -1,4 +1,4 @@
1
- import type { Operation } from 'oas';
1
+ import type Operation from 'oas/operation';
2
2
  export default function prepareAuth(authKey: (number | string)[], operation: Operation): Record<string, string | number | {
3
3
  pass: string | number;
4
4
  user: string | number;
@@ -4,27 +4,27 @@ function prepareAuth(authKey, operation) {
4
4
  if (authKey.length === 0) {
5
5
  return {};
6
6
  }
7
- var preparedAuth = {};
8
- var security = operation.getSecurity();
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(function (s) { return Object.keys(s).length > 1; })) {
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
- var usableSecurity = security
22
- .map(function (s) {
21
+ const usableSecurity = security
22
+ .map(s => {
23
23
  return Object.keys(s).length === 1 ? s : false;
24
24
  })
25
25
  .filter(Boolean);
26
- var usableSecuritySchemes = usableSecurity.map(function (s) { return Object.keys(s); }).reduce(function (prev, next) { return prev.concat(next); }, []);
27
- var preparedSecurity = operation.prepareSecurity();
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
- var schemes_1 = preparedSecurity.Basic.filter(function (s) { return usableSecuritySchemes.includes(s._key); });
37
- if (!schemes_1.length) {
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
- var scheme_1 = schemes_1.shift();
41
- preparedAuth[scheme_1._key] = {
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
- var usableScheme = usableSecuritySchemes[0];
52
- var schemes = Object.entries(preparedSecurity)
53
- .map(function (_a) {
54
- var ps = _a[1];
55
- return ps.filter(function (s) { return usableScheme === s._key; });
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("Sorry, this API currently uses a security scheme, ".concat(scheme.type, ", which this library doesn't yet support."));
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 { Operation } from 'oas';
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