@mountainpass/waychaser 5.0.47 → 5.0.49
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/README.md +1 -1
- package/dist/augment-options.js +20 -57
- package/dist/augment-options.js.map +1 -1
- package/dist/expand-operation.js +18 -53
- package/dist/expand-operation.js.map +1 -1
- package/dist/handlers/hal/de-curie.js +6 -25
- package/dist/handlers/hal/de-curie.js.map +1 -1
- package/dist/handlers/hal/hal-handler.js +16 -65
- package/dist/handlers/hal/hal-handler.js.map +1 -1
- package/dist/handlers/hal/map-hal-link-to-operation.js +5 -30
- package/dist/handlers/hal/map-hal-link-to-operation.js.map +1 -1
- package/dist/handlers/link-header/link-header-handler.js +20 -63
- package/dist/handlers/link-header/link-header-handler.js.map +1 -1
- package/dist/handlers/location-header/location-header-handler.js +4 -7
- package/dist/handlers/location-header/location-header-handler.js.map +1 -1
- package/dist/handlers/siren/map-siren-action-to-operation.js +12 -53
- package/dist/handlers/siren/map-siren-action-to-operation.js.map +1 -1
- package/dist/handlers/siren/map-siren-link-to-operation.js +4 -29
- package/dist/handlers/siren/map-siren-link-to-operation.js.map +1 -1
- package/dist/handlers/siren/siren-action-handler.js +7 -32
- package/dist/handlers/siren/siren-action-handler.js.map +1 -1
- package/dist/handlers/siren/siren-handler.js +10 -39
- package/dist/handlers/siren/siren-handler.js.map +1 -1
- package/dist/handlers/siren/siren-link-handler.js +8 -43
- package/dist/handlers/siren/siren-link-handler.js.map +1 -1
- package/dist/operation-array.d.ts +1 -0
- package/dist/operation-array.js +27 -70
- package/dist/operation-array.js.map +1 -1
- package/dist/operation.js +128 -272
- package/dist/operation.js.map +1 -1
- package/dist/src/augment-options.d.ts +30 -0
- package/dist/src/expand-operation.d.ts +5 -0
- package/dist/src/handlers/hal/de-curie.d.ts +9 -0
- package/dist/src/handlers/hal/hal-handler.d.ts +6 -0
- package/dist/src/handlers/hal/map-hal-link-to-operation.d.ts +7 -0
- package/dist/src/handlers/link-header/link-header-handler.d.ts +7 -0
- package/dist/src/handlers/location-header/location-header-handler.d.ts +5 -0
- package/dist/src/handlers/siren/map-siren-action-to-operation.d.ts +6 -0
- package/dist/src/handlers/siren/map-siren-link-to-operation.d.ts +13 -0
- package/dist/src/handlers/siren/siren-action-handler.d.ts +5 -0
- package/dist/src/handlers/siren/siren-handler.d.ts +6 -0
- package/dist/src/handlers/siren/siren-link-handler.d.ts +6 -0
- package/dist/src/operation-array.d.ts +13 -0
- package/dist/src/operation.d.ts +24 -0
- package/dist/src/util/media-types.d.ts +5 -0
- package/dist/src/util/method-can-have-body.d.ts +4 -0
- package/dist/src/util/parse-accept.d.ts +6 -0
- package/dist/src/util/parse-operations.d.ts +14 -0
- package/dist/src/util/preferred-content-type.d.ts +6 -0
- package/dist/src/util/uri-template-lite.d.ts +5 -0
- package/dist/src/waychaser.d.ts +131 -0
- package/dist/util/media-types.js +2 -4
- package/dist/util/media-types.js.map +1 -1
- package/dist/util/method-can-have-body.js +1 -4
- package/dist/util/method-can-have-body.js.map +1 -1
- package/dist/util/parse-accept.js +17 -42
- package/dist/util/parse-accept.js.map +1 -1
- package/dist/util/parse-operations.js +22 -78
- package/dist/util/parse-operations.js.map +1 -1
- package/dist/util/preferred-content-type.js +4 -7
- package/dist/util/preferred-content-type.js.map +1 -1
- package/dist/util/uri-template-lite.js +6 -42
- package/dist/util/uri-template-lite.js.map +1 -1
- package/dist/waychaser.es.js +475 -922
- package/dist/waychaser.es.min.js +1 -1
- package/dist/waychaser.js +212 -480
- package/dist/waychaser.js.map +1 -1
- package/package.json +2 -2
package/dist/waychaser.js
CHANGED
|
@@ -1,204 +1,64 @@
|
|
|
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
|
-
var __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
39
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
-
function step(op) {
|
|
42
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
|
-
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;
|
|
45
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
-
switch (op[0]) {
|
|
47
|
-
case 0: case 1: t = op; break;
|
|
48
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
-
default:
|
|
52
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
-
if (t[2]) _.ops.pop();
|
|
57
|
-
_.trys.pop(); continue;
|
|
58
|
-
}
|
|
59
|
-
op = body.call(thisArg, _);
|
|
60
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
var __values = (this && this.__values) || function(o) {
|
|
65
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
66
|
-
if (m) return m.call(o);
|
|
67
|
-
if (o && typeof o.length === "number") return {
|
|
68
|
-
next: function () {
|
|
69
|
-
if (o && i >= o.length) o = void 0;
|
|
70
|
-
return { value: o && o[i++], done: !o };
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
74
|
-
};
|
|
75
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
76
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
77
|
-
if (!m) return o;
|
|
78
|
-
var i = m.call(o), r, ar = [], e;
|
|
79
|
-
try {
|
|
80
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
81
|
-
}
|
|
82
|
-
catch (error) { e = { error: error }; }
|
|
83
|
-
finally {
|
|
84
|
-
try {
|
|
85
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
86
|
-
}
|
|
87
|
-
finally { if (e) throw e.error; }
|
|
88
|
-
}
|
|
89
|
-
return ar;
|
|
90
|
-
};
|
|
91
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
92
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
93
|
-
if (ar || !(i in from)) {
|
|
94
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
95
|
-
ar[i] = from[i];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
99
|
-
};
|
|
100
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
101
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
102
|
-
};
|
|
103
|
-
var _a, _b;
|
|
104
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
105
|
-
exports.waychaser = exports.WayChaserProblem = exports.WayChaserResponse = void 0;
|
|
106
|
-
exports.isValidationSuccess = isValidationSuccess;
|
|
107
|
-
exports.sortHandlers = sortHandlers;
|
|
108
|
-
exports._waychaser = _waychaser;
|
|
109
1
|
/* global Response */
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
2
|
+
import { InvocableOperation } from './operation';
|
|
3
|
+
import { OperationArray } from './operation-array';
|
|
4
|
+
import { parseOperations } from './util/parse-operations';
|
|
5
|
+
import { URI } from './util/uri-template-lite';
|
|
6
|
+
import { flatten } from 'flat';
|
|
7
|
+
import { halHandler } from './handlers/hal/hal-handler';
|
|
8
|
+
import { linkHeaderHandler } from './handlers/link-header/link-header-handler';
|
|
9
|
+
import { sirenHandler } from './handlers/siren/siren-handler';
|
|
10
|
+
import MediaTypes from './util/media-types';
|
|
119
11
|
// import { fetch } from 'cross-fetch'
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
12
|
+
import { locationHeaderHandler } from './handlers/location-header/location-header-handler';
|
|
13
|
+
import { augmentOptions } from './augment-options';
|
|
14
|
+
import { ProblemDocument } from '@mountainpass/problem-document';
|
|
123
15
|
/**
|
|
124
16
|
* @param result
|
|
125
17
|
*/
|
|
126
|
-
function isValidationSuccess(result) {
|
|
18
|
+
export function isValidationSuccess(result) {
|
|
127
19
|
return result.success;
|
|
128
20
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
var e_1, _b, e_2, _c, e_3, _d;
|
|
132
|
-
var handlers = _a.handlers, defaultOptions = _a.defaultOptions, baseResponse = _a.baseResponse, content = _a.content, fullContent = _a.fullContent, anchor = _a.anchor, parentOperations = _a.parentOperations, parameters = _a.parameters;
|
|
21
|
+
export class WayChaserResponse {
|
|
22
|
+
constructor({ handlers, defaultOptions, baseResponse, content, fullContent, anchor, parentOperations, parameters }) {
|
|
133
23
|
this.response = baseResponse;
|
|
134
24
|
this.anchor = anchor;
|
|
135
25
|
this.parameters = parameters || {};
|
|
136
26
|
this.fullContent = fullContent || content;
|
|
137
27
|
this.content = content;
|
|
138
|
-
this.operations =
|
|
139
|
-
|
|
28
|
+
this.operations = OperationArray.create();
|
|
29
|
+
const thisContext = flatten({ this: content });
|
|
140
30
|
if (parentOperations && anchor && defaultOptions) {
|
|
141
31
|
this.allOperations = parentOperations;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
var operation = _f.value;
|
|
145
|
-
this.operations.push(new operation_1.InvocableOperation(operation, this, defaultOptions, thisContext));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
149
|
-
finally {
|
|
150
|
-
try {
|
|
151
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
152
|
-
}
|
|
153
|
-
finally { if (e_1) throw e_1.error; }
|
|
32
|
+
for (const operation of this.allOperations[anchor] || []) {
|
|
33
|
+
this.operations.push(new InvocableOperation(operation, this, defaultOptions, thisContext));
|
|
154
34
|
}
|
|
155
35
|
// not only do we need to go through the operations with a matching anchor
|
|
156
36
|
// we also need to go though anchors that could match this.anchor
|
|
157
|
-
for (
|
|
37
|
+
for (const key in this.allOperations) {
|
|
158
38
|
if (key !== '') {
|
|
159
39
|
// need to see if key could match this.anchor
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
40
|
+
const template = new URI.Template(key);
|
|
41
|
+
const parameters = template.match(this.anchor);
|
|
42
|
+
const expandedAnchor = template.expand(parameters);
|
|
163
43
|
if (expandedAnchor === this.anchor) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
var operation = _h.value;
|
|
168
|
-
this.operations.push(new operation_1.InvocableOperation(operation, this, expandedOptions, thisContext));
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
172
|
-
finally {
|
|
173
|
-
try {
|
|
174
|
-
if (_h && !_h.done && (_c = _g.return)) _c.call(_g);
|
|
175
|
-
}
|
|
176
|
-
finally { if (e_2) throw e_2.error; }
|
|
44
|
+
const expandedOptions = Object.assign({}, defaultOptions, { parameters: Object.assign(parameters, defaultOptions.parameters) });
|
|
45
|
+
for (const operation of this.allOperations[key]) {
|
|
46
|
+
this.operations.push(new InvocableOperation(operation, this, expandedOptions, thisContext));
|
|
177
47
|
}
|
|
178
48
|
}
|
|
179
49
|
}
|
|
180
50
|
}
|
|
181
51
|
}
|
|
182
52
|
else if (baseResponse && handlers && defaultOptions) {
|
|
183
|
-
this.allOperations =
|
|
184
|
-
baseResponse
|
|
185
|
-
content
|
|
186
|
-
handlers
|
|
53
|
+
this.allOperations = parseOperations({
|
|
54
|
+
baseResponse,
|
|
55
|
+
content,
|
|
56
|
+
handlers
|
|
187
57
|
});
|
|
188
|
-
this.operations =
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
var op = new operation_1.InvocableOperation(operation, this, defaultOptions, thisContext);
|
|
193
|
-
this.operations.push(op);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
197
|
-
finally {
|
|
198
|
-
try {
|
|
199
|
-
if (_k && !_k.done && (_d = _j.return)) _d.call(_j);
|
|
200
|
-
}
|
|
201
|
-
finally { if (e_3) throw e_3.error; }
|
|
58
|
+
this.operations = OperationArray.create();
|
|
59
|
+
for (const operation of this.allOperations[''] || []) {
|
|
60
|
+
const op = new InvocableOperation(operation, this, defaultOptions, thisContext);
|
|
61
|
+
this.operations.push(op);
|
|
202
62
|
}
|
|
203
63
|
}
|
|
204
64
|
}
|
|
@@ -210,161 +70,108 @@ var WayChaserResponse = /** @class */ (function () {
|
|
|
210
70
|
* @returns
|
|
211
71
|
* @throws {WayChaserProblem}
|
|
212
72
|
*/
|
|
213
|
-
|
|
214
|
-
if (content instanceof
|
|
73
|
+
static create(baseResponse, content, defaultOptions, mergedOptions) {
|
|
74
|
+
if (content instanceof ProblemDocument) {
|
|
215
75
|
throw new WayChaserProblem({
|
|
216
|
-
response: new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions
|
|
76
|
+
response: new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions, baseResponse, content, parameters: mergedOptions.parameters }),
|
|
217
77
|
problem: content,
|
|
218
78
|
});
|
|
219
79
|
}
|
|
220
80
|
else {
|
|
221
81
|
if (mergedOptions.validator) {
|
|
222
|
-
|
|
82
|
+
const validationResult = mergedOptions.validator(content);
|
|
223
83
|
if (isValidationSuccess(validationResult)) {
|
|
224
|
-
return new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions
|
|
84
|
+
return new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions, baseResponse, content: validationResult.content, parameters: mergedOptions.parameters });
|
|
225
85
|
}
|
|
226
86
|
else {
|
|
227
87
|
throw new WayChaserProblem({
|
|
228
|
-
response: new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions
|
|
88
|
+
response: new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions, baseResponse, content, parameters: mergedOptions.parameters }),
|
|
229
89
|
problem: validationResult.problem,
|
|
230
90
|
});
|
|
231
91
|
}
|
|
232
92
|
}
|
|
233
93
|
else {
|
|
234
|
-
return new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions
|
|
94
|
+
return new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions, baseResponse, content, parameters: mergedOptions.parameters });
|
|
235
95
|
}
|
|
236
96
|
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
enumerable: false,
|
|
243
|
-
configurable: true
|
|
244
|
-
});
|
|
245
|
-
WayChaserResponse.prototype.invoke = function (relationship, options) {
|
|
97
|
+
}
|
|
98
|
+
get ops() {
|
|
99
|
+
return this.operations;
|
|
100
|
+
}
|
|
101
|
+
invoke(relationship, options) {
|
|
246
102
|
return this.operations.invoke(relationship, options);
|
|
247
|
-
}
|
|
248
|
-
|
|
103
|
+
}
|
|
104
|
+
invokeAll(relationship, options) {
|
|
249
105
|
return this.operations.invokeAll(relationship, options);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
get: function () {
|
|
253
|
-
throw new Error('Not Implemented. Use `.content` instead');
|
|
254
|
-
},
|
|
255
|
-
enumerable: false,
|
|
256
|
-
configurable: true
|
|
257
|
-
});
|
|
258
|
-
Object.defineProperty(WayChaserResponse.prototype, "bodyUsed", {
|
|
259
|
-
get: function () {
|
|
260
|
-
return true;
|
|
261
|
-
},
|
|
262
|
-
enumerable: false,
|
|
263
|
-
configurable: true
|
|
264
|
-
});
|
|
265
|
-
Object.defineProperty(WayChaserResponse.prototype, "headers", {
|
|
266
|
-
get: function () {
|
|
267
|
-
var _a;
|
|
268
|
-
return (_a = this.response) === null || _a === void 0 ? void 0 : _a.headers;
|
|
269
|
-
},
|
|
270
|
-
enumerable: false,
|
|
271
|
-
configurable: true
|
|
272
|
-
});
|
|
273
|
-
Object.defineProperty(WayChaserResponse.prototype, "ok", {
|
|
274
|
-
get: function () {
|
|
275
|
-
return this.response ? this.response.ok : false;
|
|
276
|
-
},
|
|
277
|
-
enumerable: false,
|
|
278
|
-
configurable: true
|
|
279
|
-
});
|
|
280
|
-
Object.defineProperty(WayChaserResponse.prototype, "redirected", {
|
|
281
|
-
get: function () {
|
|
282
|
-
return this.response ? this.response.redirected : false;
|
|
283
|
-
},
|
|
284
|
-
enumerable: false,
|
|
285
|
-
configurable: true
|
|
286
|
-
});
|
|
287
|
-
Object.defineProperty(WayChaserResponse.prototype, "status", {
|
|
288
|
-
get: function () {
|
|
289
|
-
var _a;
|
|
290
|
-
return (_a = this.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
291
|
-
},
|
|
292
|
-
enumerable: false,
|
|
293
|
-
configurable: true
|
|
294
|
-
});
|
|
295
|
-
Object.defineProperty(WayChaserResponse.prototype, "statusText", {
|
|
296
|
-
get: function () {
|
|
297
|
-
var _a;
|
|
298
|
-
return (_a = this.response) === null || _a === void 0 ? void 0 : _a.statusText;
|
|
299
|
-
},
|
|
300
|
-
enumerable: false,
|
|
301
|
-
configurable: true
|
|
302
|
-
});
|
|
303
|
-
Object.defineProperty(WayChaserResponse.prototype, "type", {
|
|
304
|
-
get: function () {
|
|
305
|
-
var _a;
|
|
306
|
-
return (_a = this.response) === null || _a === void 0 ? void 0 : _a.type;
|
|
307
|
-
},
|
|
308
|
-
enumerable: false,
|
|
309
|
-
configurable: true
|
|
310
|
-
});
|
|
311
|
-
Object.defineProperty(WayChaserResponse.prototype, "url", {
|
|
312
|
-
get: function () {
|
|
313
|
-
var _a;
|
|
314
|
-
return (_a = this.response) === null || _a === void 0 ? void 0 : _a.url;
|
|
315
|
-
},
|
|
316
|
-
enumerable: false,
|
|
317
|
-
configurable: true
|
|
318
|
-
});
|
|
319
|
-
WayChaserResponse.prototype.arrayBuffer = function () {
|
|
106
|
+
}
|
|
107
|
+
get body() {
|
|
320
108
|
throw new Error('Not Implemented. Use `.content` instead');
|
|
321
|
-
}
|
|
322
|
-
|
|
109
|
+
}
|
|
110
|
+
get bodyUsed() {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
get headers() {
|
|
114
|
+
return this.response?.headers;
|
|
115
|
+
}
|
|
116
|
+
get ok() {
|
|
117
|
+
return this.response ? this.response.ok : false;
|
|
118
|
+
}
|
|
119
|
+
get redirected() {
|
|
120
|
+
return this.response ? this.response.redirected : false;
|
|
121
|
+
}
|
|
122
|
+
get status() {
|
|
123
|
+
return this.response?.status;
|
|
124
|
+
}
|
|
125
|
+
get statusText() {
|
|
126
|
+
return this.response?.statusText;
|
|
127
|
+
}
|
|
128
|
+
get type() {
|
|
129
|
+
return this.response?.type;
|
|
130
|
+
}
|
|
131
|
+
get url() {
|
|
132
|
+
return this.response?.url;
|
|
133
|
+
}
|
|
134
|
+
arrayBuffer() {
|
|
323
135
|
throw new Error('Not Implemented. Use `.content` instead');
|
|
324
|
-
}
|
|
325
|
-
|
|
136
|
+
}
|
|
137
|
+
blob() {
|
|
138
|
+
throw new Error('Not Implemented. Use `.content` instead');
|
|
139
|
+
}
|
|
140
|
+
clone() {
|
|
326
141
|
throw new Error('Not Implemented');
|
|
327
|
-
}
|
|
328
|
-
|
|
142
|
+
}
|
|
143
|
+
formData() {
|
|
329
144
|
throw new Error('Not Implemented. Use `.content` instead');
|
|
330
|
-
}
|
|
145
|
+
}
|
|
331
146
|
// async json() {
|
|
332
147
|
// throw new Error('Not Implemented. Use `.content` instead')
|
|
333
148
|
// }
|
|
334
|
-
|
|
149
|
+
text() {
|
|
335
150
|
throw new Error('Not Implemented. Use `.content` instead');
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
var WayChaserProblem = /** @class */ (function (_super) {
|
|
341
|
-
__extends(WayChaserProblem, _super);
|
|
342
|
-
function WayChaserProblem(_a) {
|
|
343
|
-
var response = _a.response, problem = _a.problem;
|
|
344
|
-
var _newTarget = this.constructor;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export class WayChaserProblem extends Error {
|
|
154
|
+
constructor({ response, problem }) {
|
|
345
155
|
// 'Error' breaks prototype chain here
|
|
346
|
-
|
|
156
|
+
super(problem.detail);
|
|
347
157
|
// restore prototype chain
|
|
348
|
-
|
|
158
|
+
const actualProto = new.target.prototype;
|
|
349
159
|
if (Object.setPrototypeOf) {
|
|
350
|
-
Object.setPrototypeOf(
|
|
160
|
+
Object.setPrototypeOf(this, actualProto);
|
|
351
161
|
}
|
|
352
162
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
353
163
|
else {
|
|
354
|
-
|
|
164
|
+
this.__proto__ = actualProto;
|
|
355
165
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
return _this;
|
|
166
|
+
this.problem = problem;
|
|
167
|
+
this.response = response;
|
|
359
168
|
}
|
|
360
|
-
|
|
361
|
-
}(Error));
|
|
362
|
-
exports.WayChaserProblem = WayChaserProblem;
|
|
169
|
+
}
|
|
363
170
|
/**
|
|
364
171
|
* @param handlers
|
|
365
172
|
*/
|
|
366
|
-
function sortHandlers(handlers) {
|
|
367
|
-
return handlers.sort(
|
|
173
|
+
export function sortHandlers(handlers) {
|
|
174
|
+
return handlers.sort((a, b) => {
|
|
368
175
|
// -ve if less
|
|
369
176
|
// e.g. a.q = 0.3, b.q = 0.4, then a.q - b.q = 0.3 - 0.4 = -0.1
|
|
370
177
|
return (b.q || 1) - (a.q || 1);
|
|
@@ -375,54 +182,33 @@ function sortHandlers(handlers) {
|
|
|
375
182
|
* @param handlers
|
|
376
183
|
*/
|
|
377
184
|
function allMediaRanges(handlers) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
if (handlerSpec.q) {
|
|
388
|
-
rval.push({ type: range.mediaType, q: handlerSpec.q * (range.q || 1) });
|
|
389
|
-
}
|
|
390
|
-
else {
|
|
391
|
-
rval.push({ type: range.mediaType, q: (range.q || 1) });
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
if (handlerSpec.q) {
|
|
396
|
-
rval.push({ type: range, q: handlerSpec.q });
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
rval.push({ type: range, q: 1 });
|
|
400
|
-
}
|
|
401
|
-
}
|
|
185
|
+
const rval = [];
|
|
186
|
+
for (const handlerSpec of handlers) {
|
|
187
|
+
for (const range of handlerSpec.mediaRanges) {
|
|
188
|
+
if (typeof range === 'object') {
|
|
189
|
+
if (handlerSpec.q) {
|
|
190
|
+
rval.push({ type: range.mediaType, q: handlerSpec.q * (range.q || 1) });
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
rval.push({ type: range.mediaType, q: (range.q || 1) });
|
|
402
194
|
}
|
|
403
195
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
196
|
+
else {
|
|
197
|
+
if (handlerSpec.q) {
|
|
198
|
+
rval.push({ type: range, q: handlerSpec.q });
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
rval.push({ type: range, q: 1 });
|
|
408
202
|
}
|
|
409
|
-
finally { if (e_5) throw e_5.error; }
|
|
410
203
|
}
|
|
411
204
|
}
|
|
412
205
|
}
|
|
413
|
-
|
|
414
|
-
finally {
|
|
415
|
-
try {
|
|
416
|
-
if (handlers_1_1 && !handlers_1_1.done && (_a = handlers_1.return)) _a.call(handlers_1);
|
|
417
|
-
}
|
|
418
|
-
finally { if (e_4) throw e_4.error; }
|
|
419
|
-
}
|
|
420
|
-
var sorted = rval.sort(function (a, b) {
|
|
206
|
+
const sorted = rval.sort((a, b) => {
|
|
421
207
|
return a.q - b.q;
|
|
422
208
|
});
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
209
|
+
const seen = new Set();
|
|
210
|
+
const filtered = sorted.filter(item => {
|
|
211
|
+
const k = item.type;
|
|
426
212
|
return seen.has(k) ? false : seen.add(k);
|
|
427
213
|
});
|
|
428
214
|
return filtered;
|
|
@@ -434,113 +220,73 @@ function allMediaRanges(handlers) {
|
|
|
434
220
|
* @returns {Promise<WayChaserResponse<Content>>}
|
|
435
221
|
* @throws {TypeError | AbortError | WayChaserProblem}
|
|
436
222
|
*/
|
|
437
|
-
function _waychaser(uriOrRequest, defaultOptions, options) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
baseResponse = _l.sent();
|
|
479
|
-
_l.label = 2;
|
|
480
|
-
case 2:
|
|
481
|
-
_l.trys.push([2, 4, , 5]);
|
|
482
|
-
return [4 /*yield*/, mergedOptions.contentParser(baseResponse)
|
|
483
|
-
// content is
|
|
484
|
-
// 1. unknown,
|
|
485
|
-
// 4. undefined
|
|
486
|
-
];
|
|
487
|
-
case 3:
|
|
488
|
-
content = _l.sent();
|
|
489
|
-
response = WayChaserResponse.create(baseResponse, content, defaultOptions, mergedOptions);
|
|
490
|
-
stop = false;
|
|
491
|
-
try {
|
|
492
|
-
for (_c = __values(updateOptions.postInterceptors), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
493
|
-
interceptor = _d.value;
|
|
494
|
-
interceptor(response, function () { return (stop = true); });
|
|
495
|
-
if (stop) {
|
|
496
|
-
break;
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
501
|
-
finally {
|
|
502
|
-
try {
|
|
503
|
-
if (_d && !_d.done && (_h = _c.return)) _h.call(_c);
|
|
504
|
-
}
|
|
505
|
-
finally { if (e_7) throw e_7.error; }
|
|
506
|
-
}
|
|
507
|
-
return [2 /*return*/, response];
|
|
508
|
-
case 4:
|
|
509
|
-
error_1 = _l.sent();
|
|
510
|
-
response = new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions: defaultOptions, baseResponse: baseResponse, content: error_1, parameters: mergedOptions.parameters });
|
|
511
|
-
wayChaserProblem = error_1 instanceof problem_document_1.ProblemDocument ? new WayChaserProblem({
|
|
512
|
-
response: response,
|
|
513
|
-
problem: error_1,
|
|
514
|
-
}) : new WayChaserProblem({
|
|
515
|
-
response: response,
|
|
516
|
-
problem: new problem_document_1.ProblemDocument({
|
|
517
|
-
type: "https://waychaser.io/unexected-error",
|
|
518
|
-
title: "An unexpected error occurred",
|
|
519
|
-
error: error_1
|
|
520
|
-
}),
|
|
521
|
-
});
|
|
522
|
-
stop = false;
|
|
523
|
-
try {
|
|
524
|
-
for (_e = __values(updateOptions.postErrorInterceptors), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
525
|
-
interceptor = _f.value;
|
|
526
|
-
interceptor(wayChaserProblem, function () { return (stop = true); });
|
|
527
|
-
if (stop) {
|
|
528
|
-
break;
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
533
|
-
finally {
|
|
534
|
-
try {
|
|
535
|
-
if (_f && !_f.done && (_j = _e.return)) _j.call(_e);
|
|
536
|
-
}
|
|
537
|
-
finally { if (e_8) throw e_8.error; }
|
|
538
|
-
}
|
|
539
|
-
throw wayChaserProblem;
|
|
540
|
-
case 5: return [2 /*return*/];
|
|
223
|
+
export async function _waychaser(uriOrRequest, defaultOptions, options) {
|
|
224
|
+
const mergedOptions = augmentOptions(defaultOptions, options);
|
|
225
|
+
const mergedHandlers = mergedOptions.defaultHandlers;
|
|
226
|
+
if (!mergedOptions.fetch) {
|
|
227
|
+
throw new Error('No global fetch and fetch not provided in options');
|
|
228
|
+
}
|
|
229
|
+
const defaultAcceptRanges = allMediaRanges(mergedHandlers);
|
|
230
|
+
const defaultAcceptStrings = defaultAcceptRanges.map(accept => {
|
|
231
|
+
return accept.q === 1 ? accept.type : `${accept.type};q=${accept.q}`;
|
|
232
|
+
});
|
|
233
|
+
const additionalAcceptHeader = options?.headers?.['accept']
|
|
234
|
+
? [options.headers['accept']]
|
|
235
|
+
: [];
|
|
236
|
+
const fullAcceptHeader = [
|
|
237
|
+
...additionalAcceptHeader,
|
|
238
|
+
...defaultAcceptStrings
|
|
239
|
+
].join(',');
|
|
240
|
+
const updateOptions = {
|
|
241
|
+
...mergedOptions,
|
|
242
|
+
headers: { ...mergedOptions.headers, accept: fullAcceptHeader }
|
|
243
|
+
};
|
|
244
|
+
let stop = false;
|
|
245
|
+
for (const interceptor of updateOptions.preInterceptors || []) {
|
|
246
|
+
interceptor(uriOrRequest, updateOptions, () => (stop = true));
|
|
247
|
+
if (stop) {
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const baseResponse = await mergedOptions.fetch(uriOrRequest.toString(), updateOptions);
|
|
252
|
+
try {
|
|
253
|
+
// TODO allow lazy loading of the content
|
|
254
|
+
const content = await mergedOptions.contentParser(baseResponse);
|
|
255
|
+
// content is
|
|
256
|
+
// 1. unknown,
|
|
257
|
+
// 4. undefined
|
|
258
|
+
const response = WayChaserResponse.create(baseResponse, content, defaultOptions, mergedOptions);
|
|
259
|
+
stop = false;
|
|
260
|
+
for (const interceptor of updateOptions.postInterceptors) {
|
|
261
|
+
interceptor(response, () => (stop = true));
|
|
262
|
+
if (stop) {
|
|
263
|
+
break;
|
|
541
264
|
}
|
|
265
|
+
}
|
|
266
|
+
return response;
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
const response = new WayChaserResponse({ handlers: mergedOptions.defaultHandlers, defaultOptions, baseResponse, content: error, parameters: mergedOptions.parameters });
|
|
270
|
+
const wayChaserProblem = error instanceof ProblemDocument ? new WayChaserProblem({
|
|
271
|
+
response,
|
|
272
|
+
problem: error,
|
|
273
|
+
}) : new WayChaserProblem({
|
|
274
|
+
response,
|
|
275
|
+
problem: new ProblemDocument({
|
|
276
|
+
type: "https://waychaser.io/unexected-error",
|
|
277
|
+
title: "An unexpected error occurred",
|
|
278
|
+
error
|
|
279
|
+
}),
|
|
542
280
|
});
|
|
543
|
-
|
|
281
|
+
stop = false;
|
|
282
|
+
for (const interceptor of updateOptions.postErrorInterceptors) {
|
|
283
|
+
interceptor(wayChaserProblem, () => (stop = true));
|
|
284
|
+
if (stop) {
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
throw wayChaserProblem;
|
|
289
|
+
}
|
|
544
290
|
}
|
|
545
291
|
/**
|
|
546
292
|
*
|
|
@@ -567,65 +313,53 @@ function tryParseJson(content, contentType) {
|
|
|
567
313
|
return JSON.parse(content);
|
|
568
314
|
}
|
|
569
315
|
catch (error) {
|
|
570
|
-
throw new
|
|
316
|
+
throw new ProblemDocument({
|
|
571
317
|
type: "https://waychaser.io/invalid-json",
|
|
572
318
|
title: "JSON response could not be parsed",
|
|
573
|
-
detail:
|
|
574
|
-
content
|
|
575
|
-
error: error
|
|
319
|
+
detail: `The response document with content type '${contentType}' could not be parsed as json`,
|
|
320
|
+
content, error
|
|
576
321
|
});
|
|
577
322
|
}
|
|
578
323
|
}
|
|
579
|
-
|
|
580
|
-
fetch: typeof globalThis === 'undefined' ?
|
|
324
|
+
const wayChaserDefaults = {
|
|
325
|
+
fetch: typeof globalThis === 'undefined' ? getGlobal()?.fetch : globalThis.fetch?.bind(globalThis),
|
|
581
326
|
handlers: [],
|
|
582
327
|
defaultHandlers: sortHandlers([
|
|
583
|
-
{ handler:
|
|
584
|
-
{ handler:
|
|
585
|
-
{ handler:
|
|
586
|
-
{ handler:
|
|
328
|
+
{ handler: locationHeaderHandler, mediaRanges: ['*/*'], q: 0.4 },
|
|
329
|
+
{ handler: linkHeaderHandler, mediaRanges: ['*/*'], q: 0.5 },
|
|
330
|
+
{ handler: halHandler, mediaRanges: [MediaTypes.HAL], q: 0.5 },
|
|
331
|
+
{ handler: sirenHandler, mediaRanges: [MediaTypes.SIREN], q: 0.5 }
|
|
587
332
|
]),
|
|
588
333
|
preInterceptors: [],
|
|
589
334
|
postInterceptors: [],
|
|
590
335
|
postErrorInterceptors: [],
|
|
591
|
-
contentParser:
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
jsonContent = tryParseJson(content, contentType);
|
|
605
|
-
if (contentType === 'application/problem+json') {
|
|
606
|
-
throw Object.assign(new problem_document_1.ProblemDocument(jsonContent), jsonContent);
|
|
607
|
-
}
|
|
608
|
-
else {
|
|
609
|
-
return [2 /*return*/, jsonContent];
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
_c.label = 2;
|
|
613
|
-
case 2: return [2 /*return*/];
|
|
336
|
+
contentParser: async (response) => {
|
|
337
|
+
if (response.headers.get('content-length') &&
|
|
338
|
+
response.headers.get('content-length') !== '0') {
|
|
339
|
+
const content = await response.text();
|
|
340
|
+
const contentType = response.headers.get('content-type')?.split(';')?.[0];
|
|
341
|
+
if (contentType?.endsWith('json')) {
|
|
342
|
+
const jsonContent = tryParseJson(content, contentType);
|
|
343
|
+
if (contentType === 'application/problem+json') {
|
|
344
|
+
throw Object.assign(new ProblemDocument(jsonContent), jsonContent);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
return jsonContent;
|
|
348
|
+
}
|
|
614
349
|
}
|
|
615
|
-
}
|
|
616
|
-
|
|
350
|
+
}
|
|
351
|
+
// else no content. Returning undefined
|
|
352
|
+
}
|
|
617
353
|
};
|
|
618
354
|
/**
|
|
619
355
|
* @param newDefaults
|
|
620
356
|
* @param oldDefaults
|
|
621
357
|
*/
|
|
622
358
|
function _defaults(newDefaults, oldDefaults) {
|
|
623
|
-
|
|
624
|
-
|
|
359
|
+
const mergedOptions = augmentOptions(oldDefaults, newDefaults);
|
|
360
|
+
const defaultedWaychaser = (uriOrRequest, options) => _waychaser(uriOrRequest, mergedOptions, options);
|
|
625
361
|
defaultedWaychaser.currentDefaults = mergedOptions;
|
|
626
|
-
defaultedWaychaser.defaults =
|
|
627
|
-
return _defaults(newDefaults, mergedOptions);
|
|
628
|
-
};
|
|
362
|
+
defaultedWaychaser.defaults = (newDefaults) => _defaults(newDefaults, mergedOptions);
|
|
629
363
|
return defaultedWaychaser;
|
|
630
364
|
}
|
|
631
365
|
/**
|
|
@@ -635,12 +369,10 @@ function _defaults(newDefaults, oldDefaults) {
|
|
|
635
369
|
* @returns WayChaserResponse<Content>
|
|
636
370
|
* @throws TypeError | AbortError | WayChaserResponseProblem | Error
|
|
637
371
|
*/
|
|
638
|
-
|
|
372
|
+
export const waychaser = Object.assign((uriOrRequest, options) => {
|
|
639
373
|
return _waychaser(uriOrRequest, wayChaserDefaults, options);
|
|
640
374
|
}, {
|
|
641
375
|
currentDefaults: wayChaserDefaults,
|
|
642
|
-
defaults:
|
|
643
|
-
return _defaults(newDefaults, wayChaserDefaults);
|
|
644
|
-
}
|
|
376
|
+
defaults: (newDefaults) => _defaults(newDefaults, wayChaserDefaults)
|
|
645
377
|
});
|
|
646
378
|
//# sourceMappingURL=waychaser.js.map
|