@opra/client 1.20.0 → 1.22.0
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/browser.js +21 -0
- package/{esm/core → core}/backend.js +1 -0
- package/{esm/core → core}/client-error.js +3 -0
- package/{esm/http → http}/fetch-backend.js +2 -0
- package/{esm/http → http}/http-backend.js +2 -0
- package/{esm/http → http}/http-interceptor-handler.js +2 -0
- package/http/http-response.js +51 -0
- package/package.json +15 -31
- package/browser/index.cjs +0 -21
- package/browser/index.mjs +0 -21
- package/cjs/constants.js +0 -6
- package/cjs/core/backend.js +0 -9
- package/cjs/core/client-error.js +0 -17
- package/cjs/http/enums/http-observable-type.enum.js +0 -10
- package/cjs/http/fetch-backend.js +0 -233
- package/cjs/http/http-backend.js +0 -18
- package/cjs/http/http-client-base.js +0 -104
- package/cjs/http/http-client.js +0 -19
- package/cjs/http/http-fetch-client.js +0 -19
- package/cjs/http/http-interceptor-handler.js +0 -18
- package/cjs/http/http-request-observable.js +0 -137
- package/cjs/http/http-response.js +0 -27
- package/cjs/http/http-utils.js +0 -11
- package/cjs/http/interfaces/http-event.js +0 -34
- package/cjs/http/interfaces/http-handler.js +0 -2
- package/cjs/http/interfaces/http-interceptor.js +0 -2
- package/cjs/index.js +0 -21
- package/cjs/package.json +0 -3
- package/esm/batch-request.js +0 -90
- package/esm/http/http-response.js +0 -23
- package/esm/package.json +0 -3
- package/types/index.d.cts +0 -14
- /package/{types/batch-request.d.ts → batch-request.d.ts} +0 -0
- /package/{cjs/batch-request.js → batch-request.js} +0 -0
- /package/{types/constants.d.ts → constants.d.ts} +0 -0
- /package/{esm/constants.js → constants.js} +0 -0
- /package/{types/core → core}/backend.d.ts +0 -0
- /package/{types/core → core}/client-error.d.ts +0 -0
- /package/{types/http → http}/enums/http-observable-type.enum.d.ts +0 -0
- /package/{esm/http → http}/enums/http-observable-type.enum.js +0 -0
- /package/{types/http → http}/fetch-backend.d.ts +0 -0
- /package/{types/http → http}/http-backend.d.ts +0 -0
- /package/{types/http → http}/http-client-base.d.ts +0 -0
- /package/{esm/http → http}/http-client-base.js +0 -0
- /package/{types/http → http}/http-client.d.ts +0 -0
- /package/{esm/http → http}/http-client.js +0 -0
- /package/{types/http → http}/http-fetch-client.d.ts +0 -0
- /package/{esm/http → http}/http-fetch-client.js +0 -0
- /package/{types/http → http}/http-interceptor-handler.d.ts +0 -0
- /package/{types/http → http}/http-request-observable.d.ts +0 -0
- /package/{esm/http → http}/http-request-observable.js +0 -0
- /package/{types/http → http}/http-response.d.ts +0 -0
- /package/{types/http → http}/http-utils.d.ts +0 -0
- /package/{esm/http → http}/http-utils.js +0 -0
- /package/{types/http → http}/interfaces/http-event.d.ts +0 -0
- /package/{esm/http → http}/interfaces/http-event.js +0 -0
- /package/{types/http → http}/interfaces/http-handler.d.ts +0 -0
- /package/{esm/http → http}/interfaces/http-handler.js +0 -0
- /package/{types/http → http}/interfaces/http-interceptor.d.ts +0 -0
- /package/{esm/http → http}/interfaces/http-interceptor.js +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{esm/index.js → index.js} +0 -0
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FetchBackend = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
// /// <reference lib="dom" />
|
|
6
|
-
const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is"));
|
|
7
|
-
const objects_1 = require("@jsopen/objects");
|
|
8
|
-
const common_1 = require("@opra/common");
|
|
9
|
-
const rxjs_1 = require("rxjs");
|
|
10
|
-
const isReadableStreamLike_1 = require("rxjs/internal/util/isReadableStreamLike");
|
|
11
|
-
const http_backend_js_1 = require("./http-backend.js");
|
|
12
|
-
const http_response_js_1 = require("./http-response.js");
|
|
13
|
-
const http_event_js_1 = require("./interfaces/http-event.js");
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
* @class FetchBackend
|
|
17
|
-
*/
|
|
18
|
-
class FetchBackend extends http_backend_js_1.HttpBackend {
|
|
19
|
-
constructor(serviceUrl, options) {
|
|
20
|
-
super(serviceUrl, options);
|
|
21
|
-
// Create deduped interceptor array
|
|
22
|
-
this.interceptors = Array.from(new Set([...(options?.interceptors || [])]));
|
|
23
|
-
this.defaults = {
|
|
24
|
-
...options?.defaults,
|
|
25
|
-
headers: options?.defaults?.headers instanceof Headers
|
|
26
|
-
? options?.defaults?.headers
|
|
27
|
-
: new Headers(options?.defaults?.headers),
|
|
28
|
-
params: options?.defaults?.params instanceof URLSearchParams
|
|
29
|
-
? options?.defaults?.params
|
|
30
|
-
: new URLSearchParams(options?.defaults?.params),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
handle(init) {
|
|
34
|
-
return new rxjs_1.Observable(subscriber => {
|
|
35
|
-
(async () => {
|
|
36
|
-
let request = this.prepareRequest(init);
|
|
37
|
-
if (request.body && init.reportProgress) {
|
|
38
|
-
const stream = request.body;
|
|
39
|
-
const contentLength = request.headers.get('content-length') || '0';
|
|
40
|
-
const total = parseInt(contentLength, 10) || 0;
|
|
41
|
-
let loaded = 0;
|
|
42
|
-
const progressTrackingStream = new TransformStream({
|
|
43
|
-
transform(chunk, controller) {
|
|
44
|
-
controller.enqueue(chunk);
|
|
45
|
-
loaded += chunk.byteLength;
|
|
46
|
-
// Emit 'UploadProgress' event
|
|
47
|
-
subscriber.next({
|
|
48
|
-
type: http_event_js_1.HttpEventType.UploadProgress,
|
|
49
|
-
request,
|
|
50
|
-
total,
|
|
51
|
-
loaded,
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
request = new Request(request.url, {
|
|
56
|
-
cache: request.cache,
|
|
57
|
-
credentials: request.credentials,
|
|
58
|
-
headers: request.headers,
|
|
59
|
-
integrity: request.integrity,
|
|
60
|
-
keepalive: request.keepalive,
|
|
61
|
-
method: request.method,
|
|
62
|
-
mode: request.mode,
|
|
63
|
-
redirect: request.redirect,
|
|
64
|
-
referrer: request.referrer,
|
|
65
|
-
referrerPolicy: request.referrerPolicy,
|
|
66
|
-
signal: request.signal,
|
|
67
|
-
body: stream.pipeThrough(progressTrackingStream),
|
|
68
|
-
window: init.window,
|
|
69
|
-
...{
|
|
70
|
-
// undici library requires
|
|
71
|
-
duplex: 'half',
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
// Send request
|
|
76
|
-
const fetchPromise = this.send(request);
|
|
77
|
-
// Emit 'Sent' event
|
|
78
|
-
subscriber.next({
|
|
79
|
-
request,
|
|
80
|
-
type: http_event_js_1.HttpEventType.Sent,
|
|
81
|
-
});
|
|
82
|
-
const fetchResponse = await fetchPromise;
|
|
83
|
-
// Emit 'ResponseHeader' event
|
|
84
|
-
const headersResponse = this.createResponse({
|
|
85
|
-
url: fetchResponse.url,
|
|
86
|
-
headers: fetchResponse.headers,
|
|
87
|
-
status: fetchResponse.status,
|
|
88
|
-
statusText: fetchResponse.statusText,
|
|
89
|
-
hasBody: !!fetchResponse.body,
|
|
90
|
-
});
|
|
91
|
-
subscriber.next({
|
|
92
|
-
request,
|
|
93
|
-
type: http_event_js_1.HttpEventType.ResponseHeader,
|
|
94
|
-
response: headersResponse,
|
|
95
|
-
});
|
|
96
|
-
// Parse body
|
|
97
|
-
let body;
|
|
98
|
-
if (fetchResponse.body) {
|
|
99
|
-
if (init.reportProgress) {
|
|
100
|
-
const fetchBody = fetchResponse.body;
|
|
101
|
-
const contentLength = fetchResponse.headers.get('content-length') || '0';
|
|
102
|
-
const total = parseInt(contentLength, 10) || 0;
|
|
103
|
-
let loaded = 0;
|
|
104
|
-
const res = new Response(new ReadableStream({
|
|
105
|
-
async start(controller) {
|
|
106
|
-
const reader = fetchBody.getReader();
|
|
107
|
-
for (;;) {
|
|
108
|
-
const { done, value } = await reader.read();
|
|
109
|
-
if (done)
|
|
110
|
-
break;
|
|
111
|
-
loaded += value.byteLength;
|
|
112
|
-
controller.enqueue(value);
|
|
113
|
-
// Emit 'DownloadProgress' event
|
|
114
|
-
subscriber.next({
|
|
115
|
-
type: http_event_js_1.HttpEventType.DownloadProgress,
|
|
116
|
-
request,
|
|
117
|
-
total,
|
|
118
|
-
loaded,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
controller.close();
|
|
122
|
-
},
|
|
123
|
-
}));
|
|
124
|
-
body = await this.parseBody(res);
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
body = await this.parseBody(fetchResponse);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const response = this.createResponse({
|
|
131
|
-
url: fetchResponse.url,
|
|
132
|
-
headers: fetchResponse.headers,
|
|
133
|
-
status: fetchResponse.status,
|
|
134
|
-
statusText: fetchResponse.statusText,
|
|
135
|
-
body,
|
|
136
|
-
});
|
|
137
|
-
// Emit 'Response' event
|
|
138
|
-
subscriber.next({
|
|
139
|
-
request,
|
|
140
|
-
type: http_event_js_1.HttpEventType.Response,
|
|
141
|
-
response,
|
|
142
|
-
});
|
|
143
|
-
subscriber.complete();
|
|
144
|
-
})().catch(error => subscriber.error(error));
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
send(request) {
|
|
148
|
-
return fetch(request);
|
|
149
|
-
}
|
|
150
|
-
prepareRequest(init) {
|
|
151
|
-
const headers = init.headers || new Headers();
|
|
152
|
-
const requestInit = {
|
|
153
|
-
...init,
|
|
154
|
-
...(0, objects_1.omit)(this.defaults, ['headers', 'params']),
|
|
155
|
-
headers,
|
|
156
|
-
};
|
|
157
|
-
this.defaults.headers.forEach((val, key) => {
|
|
158
|
-
if (!headers.has(key))
|
|
159
|
-
headers.set(key, val);
|
|
160
|
-
});
|
|
161
|
-
const url = new URL(requestInit.url, this.serviceUrl);
|
|
162
|
-
if (this.defaults.params.size) {
|
|
163
|
-
this.defaults.params.forEach((val, key) => {
|
|
164
|
-
if (!url.searchParams.has(key))
|
|
165
|
-
url.searchParams.set(key, val);
|
|
166
|
-
});
|
|
167
|
-
requestInit.url = url.toString();
|
|
168
|
-
}
|
|
169
|
-
const body = requestInit.body;
|
|
170
|
-
if (body) {
|
|
171
|
-
let contentType = '';
|
|
172
|
-
if (typeof body === 'string' ||
|
|
173
|
-
typeof body === 'number' ||
|
|
174
|
-
typeof body === 'boolean') {
|
|
175
|
-
contentType = 'text/plain; charset="UTF-8"';
|
|
176
|
-
requestInit.body = new Blob([String(body)], { type: contentType });
|
|
177
|
-
headers.set('Content-Length', String(requestInit.body.size));
|
|
178
|
-
delete requestInit.duplex;
|
|
179
|
-
}
|
|
180
|
-
else if ((0, isReadableStreamLike_1.isReadableStreamLike)(body)) {
|
|
181
|
-
contentType = 'application/octet-stream';
|
|
182
|
-
requestInit.duplex = 'half'; // undici library requires "duplex" option to be set for streams
|
|
183
|
-
}
|
|
184
|
-
else if (Buffer.isBuffer(body)) {
|
|
185
|
-
contentType = 'application/octet-stream';
|
|
186
|
-
headers.set('Content-Length', String(body.length));
|
|
187
|
-
delete requestInit.duplex;
|
|
188
|
-
}
|
|
189
|
-
else if ((0, common_1.isBlob)(body)) {
|
|
190
|
-
contentType = body.type || 'application/octet-stream';
|
|
191
|
-
headers.set('Content-Length', String(body.size));
|
|
192
|
-
delete requestInit.duplex;
|
|
193
|
-
}
|
|
194
|
-
else if ((0, common_1.isFormData)(body)) {
|
|
195
|
-
delete requestInit.duplex;
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
contentType = 'application/json;charset="UTF-8"';
|
|
199
|
-
requestInit.body = new Blob([JSON.stringify(body)], {
|
|
200
|
-
type: contentType,
|
|
201
|
-
});
|
|
202
|
-
headers.set('Content-Length', String(requestInit.body.size));
|
|
203
|
-
delete requestInit.duplex;
|
|
204
|
-
}
|
|
205
|
-
if (contentType && !headers.has('Content-Type'))
|
|
206
|
-
headers.set('Content-Type', contentType);
|
|
207
|
-
}
|
|
208
|
-
return new Request(url.toString(), requestInit);
|
|
209
|
-
}
|
|
210
|
-
createResponse(init) {
|
|
211
|
-
return new http_response_js_1.HttpResponse(init);
|
|
212
|
-
}
|
|
213
|
-
async parseBody(fetchResponse) {
|
|
214
|
-
let body;
|
|
215
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
216
|
-
if (type_is_1.default.is(contentType, ['json', 'application/*+json'])) {
|
|
217
|
-
body = await fetchResponse.json();
|
|
218
|
-
if (typeof body === 'string')
|
|
219
|
-
body = JSON.parse(body);
|
|
220
|
-
}
|
|
221
|
-
else if (type_is_1.default.is(contentType, ['text']))
|
|
222
|
-
body = await fetchResponse.text();
|
|
223
|
-
else if (type_is_1.default.is(contentType, ['multipart']))
|
|
224
|
-
body = await fetchResponse.formData();
|
|
225
|
-
else {
|
|
226
|
-
const buf = await fetchResponse.arrayBuffer();
|
|
227
|
-
if (buf.byteLength)
|
|
228
|
-
body = buf;
|
|
229
|
-
}
|
|
230
|
-
return body;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
exports.FetchBackend = FetchBackend;
|
package/cjs/http/http-backend.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpBackend = void 0;
|
|
4
|
-
const backend_js_1 = require("../core/backend.js");
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
* @class HttpBackend
|
|
8
|
-
*/
|
|
9
|
-
class HttpBackend extends backend_js_1.Backend {
|
|
10
|
-
constructor(serviceUrl, options) {
|
|
11
|
-
super(options);
|
|
12
|
-
const u = new URL(serviceUrl);
|
|
13
|
-
this.serviceUrl = u.toString().split('?')[0].split('#')[0];
|
|
14
|
-
if (!this.serviceUrl.endsWith('/'))
|
|
15
|
-
this.serviceUrl += '/';
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.HttpBackend = HttpBackend;
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpClientBase = void 0;
|
|
4
|
-
const common_1 = require("@opra/common");
|
|
5
|
-
const constants_js_1 = require("../constants.js");
|
|
6
|
-
const http_request_observable_js_1 = require("./http-request-observable.js");
|
|
7
|
-
const SPLIT_BACKSLASH_PATTERN = /^(\/*)(.+)/;
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
* @class OpraClientBase
|
|
11
|
-
* @abstract
|
|
12
|
-
*/
|
|
13
|
-
class HttpClientBase {
|
|
14
|
-
constructor(backend) {
|
|
15
|
-
Object.defineProperty(this, constants_js_1.kBackend, {
|
|
16
|
-
enumerable: false,
|
|
17
|
-
value: backend,
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
get serviceUrl() {
|
|
21
|
-
return this[constants_js_1.kBackend].serviceUrl;
|
|
22
|
-
}
|
|
23
|
-
async fetchDocument(options) {
|
|
24
|
-
const documentMap = {};
|
|
25
|
-
const getDocument = async (documentId) => {
|
|
26
|
-
const req = this.request('$schema', {
|
|
27
|
-
headers: new Headers({ accept: 'application/json' }),
|
|
28
|
-
});
|
|
29
|
-
if (documentId)
|
|
30
|
-
req.param('id', documentId);
|
|
31
|
-
const body = await req.getBody().catch(e => {
|
|
32
|
-
e.message =
|
|
33
|
-
'Error fetching api schema from url (' +
|
|
34
|
-
this.serviceUrl +
|
|
35
|
-
').\n' +
|
|
36
|
-
e.message;
|
|
37
|
-
throw e;
|
|
38
|
-
});
|
|
39
|
-
if (body.references) {
|
|
40
|
-
const oldReferences = body.references;
|
|
41
|
-
body.references = {};
|
|
42
|
-
for (const [ns, obj] of Object.entries(oldReferences)) {
|
|
43
|
-
if (documentMap[obj.id] === null)
|
|
44
|
-
throw new Error('Circular reference detected');
|
|
45
|
-
documentMap[obj.id] = null;
|
|
46
|
-
const x = await getDocument(obj.id);
|
|
47
|
-
body.references[ns] = documentMap[obj.id] = x;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return body;
|
|
51
|
-
};
|
|
52
|
-
const body = await getDocument(options?.documentId);
|
|
53
|
-
return await common_1.ApiDocumentFactory.createDocument(body).catch(e => {
|
|
54
|
-
e.message = 'Error loading api document.\n' + e.message;
|
|
55
|
-
throw e;
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
request(path, options) {
|
|
59
|
-
/** Remove leading backslashes */
|
|
60
|
-
path = SPLIT_BACKSLASH_PATTERN.exec(path)?.[2] || '';
|
|
61
|
-
const observable = new http_request_observable_js_1.HttpRequestObservable(this[constants_js_1.kBackend], {
|
|
62
|
-
...options,
|
|
63
|
-
method: options?.method || 'GET',
|
|
64
|
-
url: new URL(path, this.serviceUrl),
|
|
65
|
-
});
|
|
66
|
-
if (options?.params)
|
|
67
|
-
observable.param(options.params);
|
|
68
|
-
return observable;
|
|
69
|
-
}
|
|
70
|
-
delete(path, options) {
|
|
71
|
-
return this.request(path, {
|
|
72
|
-
...options,
|
|
73
|
-
method: 'DELETE',
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
get(path, options) {
|
|
77
|
-
return this.request(path, {
|
|
78
|
-
...options,
|
|
79
|
-
method: 'GET',
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
patch(path, requestBody, options) {
|
|
83
|
-
return this.request(path, {
|
|
84
|
-
...options,
|
|
85
|
-
method: 'PATCH',
|
|
86
|
-
body: requestBody,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
post(path, requestBody, options) {
|
|
90
|
-
return this.request(path, {
|
|
91
|
-
...options,
|
|
92
|
-
method: 'POST',
|
|
93
|
-
body: requestBody,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
put(path, requestBody, options) {
|
|
97
|
-
return this.request(path, {
|
|
98
|
-
...options,
|
|
99
|
-
method: 'PUT',
|
|
100
|
-
body: requestBody,
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
exports.HttpClientBase = HttpClientBase;
|
package/cjs/http/http-client.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OpraHttpClient = void 0;
|
|
4
|
-
const constants_js_1 = require("../constants.js");
|
|
5
|
-
const fetch_backend_js_1 = require("./fetch-backend.js");
|
|
6
|
-
const http_client_base_js_1 = require("./http-client-base.js");
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @class OpraHttpClient
|
|
10
|
-
*/
|
|
11
|
-
class OpraHttpClient extends http_client_base_js_1.HttpClientBase {
|
|
12
|
-
constructor(serviceUrl, options) {
|
|
13
|
-
super(new fetch_backend_js_1.FetchBackend(serviceUrl, options));
|
|
14
|
-
}
|
|
15
|
-
get defaults() {
|
|
16
|
-
return this[constants_js_1.kBackend].defaults;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
exports.OpraHttpClient = OpraHttpClient;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpFetchClient = void 0;
|
|
4
|
-
const constants_js_1 = require("../constants.js");
|
|
5
|
-
const fetch_backend_js_1 = require("./fetch-backend.js");
|
|
6
|
-
const http_client_base_js_1 = require("./http-client-base.js");
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @class HttpFetchClient
|
|
10
|
-
*/
|
|
11
|
-
class HttpFetchClient extends http_client_base_js_1.HttpClientBase {
|
|
12
|
-
constructor(serviceUrl, options) {
|
|
13
|
-
super(new fetch_backend_js_1.FetchBackend(serviceUrl, options));
|
|
14
|
-
}
|
|
15
|
-
get defaults() {
|
|
16
|
-
return this[constants_js_1.kBackend].defaults;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
exports.HttpFetchClient = HttpFetchClient;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpInterceptorHandler = void 0;
|
|
4
|
-
class HttpInterceptorHandler {
|
|
5
|
-
constructor(interceptors, finalHandler) {
|
|
6
|
-
this.chain = interceptors.reduceRight((chainTailFn, interceptor) => (initialRequest, handler) => interceptor.intercept(initialRequest, {
|
|
7
|
-
handle: downstreamRequest => chainTailFn(downstreamRequest, handler),
|
|
8
|
-
}), chainEnd);
|
|
9
|
-
this.finalHandler = finalHandler;
|
|
10
|
-
}
|
|
11
|
-
handle(initialRequest) {
|
|
12
|
-
return this.chain(initialRequest, req => this.finalHandler.handle(req));
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.HttpInterceptorHandler = HttpInterceptorHandler;
|
|
16
|
-
function chainEnd(req, handler) {
|
|
17
|
-
return handler(req);
|
|
18
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpRequestObservable = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is"));
|
|
6
|
-
const common_1 = require("@opra/common");
|
|
7
|
-
const rxjs_1 = require("rxjs");
|
|
8
|
-
const constants_js_1 = require("../constants.js");
|
|
9
|
-
const client_error_js_1 = require("../core/client-error.js");
|
|
10
|
-
const http_observable_type_enum_js_1 = require("./enums/http-observable-type.enum.js");
|
|
11
|
-
const http_interceptor_handler_js_1 = require("./http-interceptor-handler.js");
|
|
12
|
-
const http_event_js_1 = require("./interfaces/http-event.js");
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* @class HttpRequestObservable
|
|
16
|
-
*/
|
|
17
|
-
class HttpRequestObservable extends rxjs_1.Observable {
|
|
18
|
-
constructor(backend, init) {
|
|
19
|
-
super(subscriber => {
|
|
20
|
-
const observe = this[constants_js_1.kContext].observe;
|
|
21
|
-
new http_interceptor_handler_js_1.HttpInterceptorHandler(backend.interceptors || [], this[constants_js_1.kBackend])
|
|
22
|
-
.handle(this[constants_js_1.kContext])
|
|
23
|
-
.subscribe({
|
|
24
|
-
next(event) {
|
|
25
|
-
if (observe === http_observable_type_enum_js_1.HttpObserveType.Events) {
|
|
26
|
-
subscriber.next(event);
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
if (observe === http_observable_type_enum_js_1.HttpObserveType.ResponseHeader &&
|
|
30
|
-
event.type === http_event_js_1.HttpEventType.ResponseHeader) {
|
|
31
|
-
subscriber.next(event.response);
|
|
32
|
-
subscriber.complete();
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
if (event.type === http_event_js_1.HttpEventType.Response) {
|
|
36
|
-
const { response } = event;
|
|
37
|
-
if (observe === http_observable_type_enum_js_1.HttpObserveType.Response) {
|
|
38
|
-
subscriber.next(response);
|
|
39
|
-
subscriber.complete();
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
const isOpraResponse = type_is_1.default.is(event.response.contentType || '', [common_1.MimeTypes.opra_response_json]);
|
|
43
|
-
if (response.status >= 400 && response.status < 600) {
|
|
44
|
-
subscriber.error(new client_error_js_1.ClientError({
|
|
45
|
-
message: response.status + ' ' + response.statusText,
|
|
46
|
-
status: response.status,
|
|
47
|
-
issues: isOpraResponse ? response.body.errors : undefined,
|
|
48
|
-
}));
|
|
49
|
-
subscriber.complete();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
subscriber.next(event.response.body);
|
|
53
|
-
subscriber.complete();
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
error(error) {
|
|
57
|
-
subscriber.error(error);
|
|
58
|
-
},
|
|
59
|
-
complete() {
|
|
60
|
-
subscriber.complete();
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
Object.defineProperty(this, constants_js_1.kBackend, {
|
|
65
|
-
enumerable: false,
|
|
66
|
-
value: backend,
|
|
67
|
-
});
|
|
68
|
-
Object.defineProperty(this, constants_js_1.kContext, {
|
|
69
|
-
enumerable: false,
|
|
70
|
-
value: {
|
|
71
|
-
...init,
|
|
72
|
-
observe: http_observable_type_enum_js_1.HttpObserveType.Body,
|
|
73
|
-
headers: new Headers(init?.headers),
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
clone() {
|
|
78
|
-
return new HttpRequestObservable(this[constants_js_1.kBackend], this[constants_js_1.kContext]);
|
|
79
|
-
}
|
|
80
|
-
options(options) {
|
|
81
|
-
Object.assign(this[constants_js_1.kContext], options);
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
header(arg0, value) {
|
|
85
|
-
const target = this[constants_js_1.kContext].headers;
|
|
86
|
-
if (typeof arg0 === 'object') {
|
|
87
|
-
const h = arg0 instanceof Headers ? arg0 : new Headers(arg0);
|
|
88
|
-
h.forEach((v, k) => {
|
|
89
|
-
if (k.toLowerCase() === 'set-cookie') {
|
|
90
|
-
target.append(k, v);
|
|
91
|
-
}
|
|
92
|
-
else
|
|
93
|
-
target.set(k, v);
|
|
94
|
-
});
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
|
-
if (value == null || value === '')
|
|
98
|
-
target.delete(arg0);
|
|
99
|
-
else
|
|
100
|
-
target.append(arg0, String(value));
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
param(arg0, value) {
|
|
104
|
-
if (value && typeof value === 'object') {
|
|
105
|
-
value = JSON.stringify(value);
|
|
106
|
-
}
|
|
107
|
-
const target = this[constants_js_1.kContext].url.searchParams;
|
|
108
|
-
if (typeof arg0 === 'object') {
|
|
109
|
-
if (typeof arg0.forEach === 'function') {
|
|
110
|
-
arg0.forEach((v, k) => target.set(String(k), String(v)));
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
Object.entries(arg0).forEach(entry => target.set(String(entry[0]), String(entry[1])));
|
|
114
|
-
}
|
|
115
|
-
return this;
|
|
116
|
-
}
|
|
117
|
-
if (value == null)
|
|
118
|
-
target.delete(arg0);
|
|
119
|
-
else
|
|
120
|
-
target.set(arg0, String(value));
|
|
121
|
-
return this;
|
|
122
|
-
}
|
|
123
|
-
observe(observe) {
|
|
124
|
-
if (observe === this[constants_js_1.kContext].observe)
|
|
125
|
-
return this;
|
|
126
|
-
const cloned = this.clone();
|
|
127
|
-
cloned[constants_js_1.kContext].observe = observe || http_observable_type_enum_js_1.HttpObserveType.Body;
|
|
128
|
-
return cloned;
|
|
129
|
-
}
|
|
130
|
-
getBody() {
|
|
131
|
-
return (0, rxjs_1.lastValueFrom)(this.observe(http_observable_type_enum_js_1.HttpObserveType.Body));
|
|
132
|
-
}
|
|
133
|
-
getResponse() {
|
|
134
|
-
return (0, rxjs_1.lastValueFrom)(this.observe(http_observable_type_enum_js_1.HttpObserveType.Response));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
exports.HttpRequestObservable = HttpRequestObservable;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/// <reference lib="dom" />
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.HttpResponse = void 0;
|
|
5
|
-
class HttpResponse {
|
|
6
|
-
constructor(init) {
|
|
7
|
-
/**
|
|
8
|
-
* Returns true if response has body to be received
|
|
9
|
-
*/
|
|
10
|
-
this.hasBody = false;
|
|
11
|
-
this.headers =
|
|
12
|
-
init?.headers instanceof Headers
|
|
13
|
-
? init?.headers
|
|
14
|
-
: new Headers(init?.headers);
|
|
15
|
-
this.status = init?.status || 200;
|
|
16
|
-
this.statusText = init?.statusText || 'OK';
|
|
17
|
-
this.url = init?.url || null;
|
|
18
|
-
this.ok = this.status >= 200 && this.status < 300;
|
|
19
|
-
this.body = init?.body;
|
|
20
|
-
this.hasBody = init?.body != null || !!init?.hasBody;
|
|
21
|
-
this.contentType = (this.headers.get('content-type') || '').split(';')[0];
|
|
22
|
-
}
|
|
23
|
-
clone(update) {
|
|
24
|
-
return new HttpResponse({ ...this, ...update });
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
exports.HttpResponse = HttpResponse;
|
package/cjs/http/http-utils.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.urlPath = urlPath;
|
|
4
|
-
function urlPath(strings, ...values) {
|
|
5
|
-
let str = '';
|
|
6
|
-
let i;
|
|
7
|
-
for (i = 0; i < strings.length; i++) {
|
|
8
|
-
str += strings[0] + encodeURIComponent(values[i]);
|
|
9
|
-
}
|
|
10
|
-
return str;
|
|
11
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpEventType = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Type enumeration for the different kinds of `HttpEvent`.
|
|
6
|
-
* @enum HttpEventType
|
|
7
|
-
*/
|
|
8
|
-
var HttpEventType;
|
|
9
|
-
(function (HttpEventType) {
|
|
10
|
-
/**
|
|
11
|
-
* The request was sent out over the wire.
|
|
12
|
-
*/
|
|
13
|
-
HttpEventType["Sent"] = "Sent";
|
|
14
|
-
/**
|
|
15
|
-
* A download or upload progress event was received.
|
|
16
|
-
*/
|
|
17
|
-
HttpEventType["UploadProgress"] = "UploadProgress";
|
|
18
|
-
/**
|
|
19
|
-
* The response status code and headers were received.
|
|
20
|
-
*/
|
|
21
|
-
HttpEventType["ResponseHeader"] = "ResponseHeader";
|
|
22
|
-
/**
|
|
23
|
-
* A download or upload progress event was received.
|
|
24
|
-
*/
|
|
25
|
-
HttpEventType["DownloadProgress"] = "DownloadProgress";
|
|
26
|
-
/**
|
|
27
|
-
* The full response including the body was received.
|
|
28
|
-
*/
|
|
29
|
-
HttpEventType["Response"] = "Response";
|
|
30
|
-
/**
|
|
31
|
-
* A custom event from an interceptor or a backend.
|
|
32
|
-
*/
|
|
33
|
-
HttpEventType["User"] = "User";
|
|
34
|
-
})(HttpEventType || (exports.HttpEventType = HttpEventType = {}));
|
package/cjs/index.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.kContext = exports.kClient = exports.kBackend = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
var constants_js_1 = require("./constants.js");
|
|
6
|
-
Object.defineProperty(exports, "kBackend", { enumerable: true, get: function () { return constants_js_1.kBackend; } });
|
|
7
|
-
Object.defineProperty(exports, "kClient", { enumerable: true, get: function () { return constants_js_1.kClient; } });
|
|
8
|
-
Object.defineProperty(exports, "kContext", { enumerable: true, get: function () { return constants_js_1.kContext; } });
|
|
9
|
-
tslib_1.__exportStar(require("./core/backend.js"), exports);
|
|
10
|
-
tslib_1.__exportStar(require("./core/client-error.js"), exports);
|
|
11
|
-
tslib_1.__exportStar(require("./http/enums/http-observable-type.enum.js"), exports);
|
|
12
|
-
tslib_1.__exportStar(require("./http/fetch-backend.js"), exports);
|
|
13
|
-
tslib_1.__exportStar(require("./http/http-backend.js"), exports);
|
|
14
|
-
tslib_1.__exportStar(require("./http/http-client.js"), exports);
|
|
15
|
-
tslib_1.__exportStar(require("./http/http-client-base.js"), exports);
|
|
16
|
-
tslib_1.__exportStar(require("./http/http-request-observable.js"), exports);
|
|
17
|
-
tslib_1.__exportStar(require("./http/http-response.js"), exports);
|
|
18
|
-
tslib_1.__exportStar(require("./http/http-utils.js"), exports);
|
|
19
|
-
tslib_1.__exportStar(require("./http/interfaces/http-event.js"), exports);
|
|
20
|
-
tslib_1.__exportStar(require("./http/interfaces/http-handler.js"), exports);
|
|
21
|
-
tslib_1.__exportStar(require("./http/interfaces/http-interceptor.js"), exports);
|
package/cjs/package.json
DELETED