@opra/client 0.26.5 → 0.27.2
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 +40 -35
- package/cjs/client.js +1 -1
- package/cjs/constants.js +1 -5
- package/cjs/impl/http-request-observable.js +41 -30
- package/cjs/impl/http-request.js +0 -1
- package/cjs/impl/http-response.js +0 -1
- package/esm/client.js +1 -1
- package/esm/constants.js +0 -4
- package/esm/impl/http-request-observable.js +41 -31
- package/esm/impl/http-request.js +0 -1
- package/esm/impl/http-response.js +0 -1
- package/package.json +7 -7
- package/types/constants.d.ts +0 -4
- package/types/impl/collection-node.d.ts +8 -18
- package/types/impl/http-request-observable.d.ts +16 -14
- package/types/impl/http-response.d.ts +6 -5
- package/types/impl/singleton-node.d.ts +5 -5
- package/types/types.d.ts +2 -4
package/browser.js
CHANGED
|
@@ -10,10 +10,6 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
10
10
|
import { ApiDocumentFactory } from "@opra/common";
|
|
11
11
|
|
|
12
12
|
// ../../build/client/esm/constants.js
|
|
13
|
-
var OPRA_JSON_CONTENT_TYPE_PATTERN = /^application\/\bopra\+json\b/i;
|
|
14
|
-
var JSON_CONTENT_TYPE_PATTERN = /^application\/([\w-]+\+)?\bjson\b/i;
|
|
15
|
-
var TEXT_CONTENT_TYPE_PATTERN = /^text\/.*$/i;
|
|
16
|
-
var FORMDATA_CONTENT_TYPE_PATTERN = /^multipart\/\bform-data\b/i;
|
|
17
13
|
var kClient = Symbol.for("kClient");
|
|
18
14
|
var kContext = Symbol.for("kContext");
|
|
19
15
|
|
|
@@ -24,6 +20,7 @@ import { OpraURL as OpraURL3 } from "@opra/common";
|
|
|
24
20
|
// ../../build/client/esm/impl/http-request-observable.js
|
|
25
21
|
import { lastValueFrom, Observable } from "rxjs";
|
|
26
22
|
import { isReadableStreamLike } from "rxjs/internal/util/isReadableStreamLike";
|
|
23
|
+
import typeIs from "@browsery/type-is";
|
|
27
24
|
import { isBlob, OpraURL as OpraURL2 } from "@opra/common";
|
|
28
25
|
|
|
29
26
|
// ../../build/client/esm/client-error.js
|
|
@@ -79,7 +76,6 @@ var HttpRequest = class {
|
|
|
79
76
|
this.method = (init?.method || "GET").toUpperCase();
|
|
80
77
|
this.mode = init?.mode || "cors";
|
|
81
78
|
this.redirect = init?.redirect || "follow";
|
|
82
|
-
this.mode = init?.mode || "cors";
|
|
83
79
|
this.referrer = init?.referrer || "";
|
|
84
80
|
this.referrerPolicy = init?.referrerPolicy || "";
|
|
85
81
|
this.signal = init?.signal || new AbortController().signal;
|
|
@@ -116,6 +112,15 @@ var HttpRequestObservable = class extends Observable {
|
|
|
116
112
|
}
|
|
117
113
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
118
114
|
});
|
|
115
|
+
const url = new OpraURL2(init?.url);
|
|
116
|
+
Object.defineProperty(this, kContext, {
|
|
117
|
+
enumerable: false,
|
|
118
|
+
value: {
|
|
119
|
+
headers: new Headers(init?.headers),
|
|
120
|
+
params: url.searchParams,
|
|
121
|
+
requestInit: { ...init }
|
|
122
|
+
}
|
|
123
|
+
});
|
|
119
124
|
Object.defineProperty(this, kClient, {
|
|
120
125
|
enumerable: false,
|
|
121
126
|
value: client
|
|
@@ -164,6 +169,7 @@ var HttpRequestObservable = class extends Observable {
|
|
|
164
169
|
return this;
|
|
165
170
|
}
|
|
166
171
|
observe(observe) {
|
|
172
|
+
observe = observe || HttpObserveType.Body;
|
|
167
173
|
return new Observable((subscriber) => {
|
|
168
174
|
this[kIntlObservable].subscribe((event) => {
|
|
169
175
|
if (observe === HttpObserveType.Events) {
|
|
@@ -175,22 +181,33 @@ var HttpRequestObservable = class extends Observable {
|
|
|
175
181
|
subscriber.complete();
|
|
176
182
|
return;
|
|
177
183
|
}
|
|
178
|
-
if (observe === HttpObserveType.Body && event.event === HttpEventType.Response) {
|
|
179
|
-
subscriber.next(event.response.body);
|
|
180
|
-
subscriber.complete();
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
184
|
if (event.event === HttpEventType.Response) {
|
|
184
|
-
|
|
185
|
+
const { response } = event;
|
|
186
|
+
const isOpraResponse = typeIs.is(event.response.contentType || "", ["application/opra+json"]);
|
|
187
|
+
if (observe === HttpObserveType.Response) {
|
|
188
|
+
subscriber.next(response);
|
|
189
|
+
subscriber.complete();
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (response.status >= 400 && response.status < 600) {
|
|
193
|
+
subscriber.error(new ClientError({
|
|
194
|
+
message: response.status + " " + response.statusText,
|
|
195
|
+
status: response.status,
|
|
196
|
+
issues: isOpraResponse ? response.body.errors : void 0
|
|
197
|
+
}));
|
|
198
|
+
subscriber.complete();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
subscriber.next(event.response.body);
|
|
185
202
|
subscriber.complete();
|
|
186
203
|
}
|
|
187
204
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
188
205
|
});
|
|
189
206
|
}
|
|
190
207
|
toPromise() {
|
|
191
|
-
return this.
|
|
208
|
+
return this.getBody();
|
|
192
209
|
}
|
|
193
|
-
|
|
210
|
+
getBody() {
|
|
194
211
|
return lastValueFrom(this.observe(HttpObserveType.Body));
|
|
195
212
|
}
|
|
196
213
|
getResponse() {
|
|
@@ -211,12 +228,14 @@ var HttpRequestObservable = class extends Observable {
|
|
|
211
228
|
});
|
|
212
229
|
const url = new OpraURL2(request.url, clientContext.serviceUrl);
|
|
213
230
|
const fetchResponse = await clientContext.fetch(url.toString(), request);
|
|
231
|
+
const contentType = (fetchResponse.headers.get("content-type") || "").split(";")[0];
|
|
214
232
|
const headersResponse = clientContext.createResponse({
|
|
215
233
|
url: fetchResponse.url,
|
|
216
234
|
headers: fetchResponse.headers,
|
|
217
235
|
status: fetchResponse.status,
|
|
218
236
|
statusText: fetchResponse.statusText,
|
|
219
|
-
hasBody: !!fetchResponse.body
|
|
237
|
+
hasBody: !!fetchResponse.body,
|
|
238
|
+
contentType
|
|
220
239
|
});
|
|
221
240
|
subscriber.next({
|
|
222
241
|
request,
|
|
@@ -224,30 +243,17 @@ var HttpRequestObservable = class extends Observable {
|
|
|
224
243
|
response: headersResponse
|
|
225
244
|
});
|
|
226
245
|
const body = fetchResponse.body ? await this._parseBody(fetchResponse) : void 0;
|
|
227
|
-
if (fetchResponse.status >= 400 && fetchResponse.status <= 599) {
|
|
228
|
-
subscriber.error(new ClientError({
|
|
229
|
-
message: fetchResponse.status + " " + fetchResponse.statusText,
|
|
230
|
-
status: fetchResponse.status,
|
|
231
|
-
issues: body.errors
|
|
232
|
-
}));
|
|
233
|
-
subscriber.complete();
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
const contentType = fetchResponse.headers.get("Content-Type") || "";
|
|
237
246
|
const responseInit = {
|
|
238
247
|
url: fetchResponse.url,
|
|
239
248
|
headers: fetchResponse.headers,
|
|
240
249
|
status: fetchResponse.status,
|
|
241
250
|
statusText: fetchResponse.statusText,
|
|
251
|
+
contentType,
|
|
242
252
|
body
|
|
243
253
|
};
|
|
244
|
-
if (OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
|
|
245
|
-
responseInit.totalCount = body?.totalCount;
|
|
246
|
-
responseInit.affected = body?.affected;
|
|
247
|
-
}
|
|
248
254
|
const response = clientContext.createResponse(responseInit);
|
|
249
255
|
for (const interceptor of clientContext.responseInterceptors) {
|
|
250
|
-
await interceptor(response);
|
|
256
|
+
await interceptor(response, request);
|
|
251
257
|
}
|
|
252
258
|
subscriber.next({
|
|
253
259
|
request,
|
|
@@ -280,7 +286,7 @@ var HttpRequestObservable = class extends Observable {
|
|
|
280
286
|
} else if (isBlob(request.body)) {
|
|
281
287
|
contentType = request.body.type || "application/octet-stream";
|
|
282
288
|
body = request.body;
|
|
283
|
-
request.headers.set("Content-Size", String(request.body.
|
|
289
|
+
request.headers.set("Content-Size", String(request.body.size));
|
|
284
290
|
delete request.duplex;
|
|
285
291
|
} else {
|
|
286
292
|
contentType = "application/json";
|
|
@@ -296,13 +302,13 @@ var HttpRequestObservable = class extends Observable {
|
|
|
296
302
|
async _parseBody(fetchResponse) {
|
|
297
303
|
let body;
|
|
298
304
|
const contentType = fetchResponse.headers.get("Content-Type") || "";
|
|
299
|
-
if (
|
|
305
|
+
if (typeIs.is(contentType, ["json", "application/*+json"])) {
|
|
300
306
|
body = await fetchResponse.json();
|
|
301
307
|
if (typeof body === "string")
|
|
302
308
|
body = JSON.parse(body);
|
|
303
|
-
} else if (
|
|
309
|
+
} else if (typeIs.is(contentType, ["text"]))
|
|
304
310
|
body = await fetchResponse.text();
|
|
305
|
-
else if (
|
|
311
|
+
else if (typeIs.is(contentType, ["multipart"]))
|
|
306
312
|
body = await fetchResponse.formData();
|
|
307
313
|
else {
|
|
308
314
|
const buf = await fetchResponse.arrayBuffer();
|
|
@@ -441,7 +447,6 @@ var HttpResponse = class _HttpResponse {
|
|
|
441
447
|
this.ok = this.status >= 200 && this.status < 300;
|
|
442
448
|
this.body = init?.body;
|
|
443
449
|
this.hasBody = init?.body != null || !!init?.hasBody;
|
|
444
|
-
this.totalCount = init?.totalCount;
|
|
445
450
|
}
|
|
446
451
|
clone(update) {
|
|
447
452
|
return new _HttpResponse({ ...this, ...update });
|
|
@@ -549,7 +554,7 @@ var OpraHttpClient = class {
|
|
|
549
554
|
url: "",
|
|
550
555
|
headers: { "accept": "application/json" }
|
|
551
556
|
});
|
|
552
|
-
this._metadataPromise = promise = controller.
|
|
557
|
+
this._metadataPromise = promise = controller.getBody();
|
|
553
558
|
return await promise.then(async (body) => {
|
|
554
559
|
if (!body)
|
|
555
560
|
throw new Error(`No response returned.`);
|
package/cjs/client.js
CHANGED
|
@@ -47,7 +47,7 @@ class OpraHttpClient {
|
|
|
47
47
|
url: '',
|
|
48
48
|
headers: { 'accept': 'application/json' }
|
|
49
49
|
});
|
|
50
|
-
this._metadataPromise = promise = controller.
|
|
50
|
+
this._metadataPromise = promise = controller.getBody();
|
|
51
51
|
return await promise
|
|
52
52
|
.then(async (body) => {
|
|
53
53
|
if (!body)
|
package/cjs/constants.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.kContext = exports.kClient =
|
|
4
|
-
exports.OPRA_JSON_CONTENT_TYPE_PATTERN = /^application\/\bopra\+json\b/i;
|
|
5
|
-
exports.JSON_CONTENT_TYPE_PATTERN = /^application\/([\w-]+\+)?\bjson\b/i;
|
|
6
|
-
exports.TEXT_CONTENT_TYPE_PATTERN = /^text\/.*$/i;
|
|
7
|
-
exports.FORMDATA_CONTENT_TYPE_PATTERN = /^multipart\/\bform-data\b/i;
|
|
3
|
+
exports.kContext = exports.kClient = void 0;
|
|
8
4
|
exports.kClient = Symbol.for('kClient');
|
|
9
5
|
exports.kContext = Symbol.for('kContext');
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HttpRequestObservable = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const rxjs_1 = require("rxjs");
|
|
5
6
|
const isReadableStreamLike_1 = require("rxjs/internal/util/isReadableStreamLike");
|
|
7
|
+
const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is"));
|
|
6
8
|
const common_1 = require("@opra/common");
|
|
7
9
|
const client_error_js_1 = require("../client-error.js");
|
|
8
10
|
const constants_js_1 = require("../constants.js");
|
|
@@ -25,6 +27,15 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
25
27
|
}
|
|
26
28
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
27
29
|
});
|
|
30
|
+
const url = new common_1.OpraURL(init?.url);
|
|
31
|
+
Object.defineProperty(this, constants_js_1.kContext, {
|
|
32
|
+
enumerable: false,
|
|
33
|
+
value: {
|
|
34
|
+
headers: new Headers(init?.headers),
|
|
35
|
+
params: url.searchParams,
|
|
36
|
+
requestInit: { ...init }
|
|
37
|
+
}
|
|
38
|
+
});
|
|
28
39
|
Object.defineProperty(this, constants_js_1.kClient, {
|
|
29
40
|
enumerable: false,
|
|
30
41
|
value: client
|
|
@@ -78,6 +89,7 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
78
89
|
return this;
|
|
79
90
|
}
|
|
80
91
|
observe(observe) {
|
|
92
|
+
observe = observe || index_js_1.HttpObserveType.Body;
|
|
81
93
|
return new rxjs_1.Observable((subscriber) => {
|
|
82
94
|
this[kIntlObservable].subscribe((event) => {
|
|
83
95
|
if (observe === index_js_1.HttpObserveType.Events) {
|
|
@@ -89,22 +101,33 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
89
101
|
subscriber.complete();
|
|
90
102
|
return;
|
|
91
103
|
}
|
|
92
|
-
if (observe === index_js_1.HttpObserveType.Body && event.event === index_js_2.HttpEventType.Response) {
|
|
93
|
-
subscriber.next(event.response.body);
|
|
94
|
-
subscriber.complete();
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
104
|
if (event.event === index_js_2.HttpEventType.Response) {
|
|
98
|
-
|
|
105
|
+
const { response } = event;
|
|
106
|
+
const isOpraResponse = type_is_1.default.is(event.response.contentType || '', ['application/opra+json']);
|
|
107
|
+
if (observe === index_js_1.HttpObserveType.Response) {
|
|
108
|
+
subscriber.next(response);
|
|
109
|
+
subscriber.complete();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (response.status >= 400 && response.status < 600) {
|
|
113
|
+
subscriber.error(new client_error_js_1.ClientError({
|
|
114
|
+
message: response.status + ' ' + response.statusText,
|
|
115
|
+
status: response.status,
|
|
116
|
+
issues: isOpraResponse ? response.body.errors : undefined
|
|
117
|
+
}));
|
|
118
|
+
subscriber.complete();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
subscriber.next(event.response.body);
|
|
99
122
|
subscriber.complete();
|
|
100
123
|
}
|
|
101
124
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
102
125
|
});
|
|
103
126
|
}
|
|
104
127
|
toPromise() {
|
|
105
|
-
return this.
|
|
128
|
+
return this.getBody();
|
|
106
129
|
}
|
|
107
|
-
|
|
130
|
+
getBody() {
|
|
108
131
|
return (0, rxjs_1.lastValueFrom)(this.observe(index_js_1.HttpObserveType.Body));
|
|
109
132
|
}
|
|
110
133
|
getResponse() {
|
|
@@ -129,13 +152,15 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
129
152
|
// Send request
|
|
130
153
|
const url = new common_1.OpraURL(request.url, clientContext.serviceUrl);
|
|
131
154
|
const fetchResponse = await clientContext.fetch(url.toString(), request);
|
|
155
|
+
const contentType = (fetchResponse.headers.get('content-type') || '').split(';')[0];
|
|
132
156
|
// Emit 'response-header' event
|
|
133
157
|
const headersResponse = clientContext.createResponse({
|
|
134
158
|
url: fetchResponse.url,
|
|
135
159
|
headers: fetchResponse.headers,
|
|
136
160
|
status: fetchResponse.status,
|
|
137
161
|
statusText: fetchResponse.statusText,
|
|
138
|
-
hasBody: !!fetchResponse.body
|
|
162
|
+
hasBody: !!fetchResponse.body,
|
|
163
|
+
contentType
|
|
139
164
|
});
|
|
140
165
|
subscriber.next({
|
|
141
166
|
request,
|
|
@@ -146,33 +171,19 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
146
171
|
const body = fetchResponse.body
|
|
147
172
|
? await this._parseBody(fetchResponse)
|
|
148
173
|
: undefined;
|
|
149
|
-
// Handle errors
|
|
150
|
-
if (fetchResponse.status >= 400 && fetchResponse.status <= 599) {
|
|
151
|
-
subscriber.error(new client_error_js_1.ClientError({
|
|
152
|
-
message: fetchResponse.status + ' ' + fetchResponse.statusText,
|
|
153
|
-
status: fetchResponse.status,
|
|
154
|
-
issues: body.errors
|
|
155
|
-
}));
|
|
156
|
-
subscriber.complete();
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
174
|
// Create response
|
|
160
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
161
175
|
const responseInit = {
|
|
162
176
|
url: fetchResponse.url,
|
|
163
177
|
headers: fetchResponse.headers,
|
|
164
178
|
status: fetchResponse.status,
|
|
165
179
|
statusText: fetchResponse.statusText,
|
|
180
|
+
contentType,
|
|
166
181
|
body,
|
|
167
182
|
};
|
|
168
|
-
if (constants_js_1.OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
|
|
169
|
-
responseInit.totalCount = body?.totalCount;
|
|
170
|
-
responseInit.affected = body?.affected;
|
|
171
|
-
}
|
|
172
183
|
const response = clientContext.createResponse(responseInit);
|
|
173
184
|
// Call response Interceptors
|
|
174
185
|
for (const interceptor of clientContext.responseInterceptors) {
|
|
175
|
-
await interceptor(response);
|
|
186
|
+
await interceptor(response, request);
|
|
176
187
|
}
|
|
177
188
|
// Emit 'response' event
|
|
178
189
|
subscriber.next({
|
|
@@ -209,7 +220,7 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
209
220
|
else if ((0, common_1.isBlob)(request.body)) {
|
|
210
221
|
contentType = request.body.type || 'application/octet-stream';
|
|
211
222
|
body = request.body;
|
|
212
|
-
request.headers.set('Content-Size', String(request.body.
|
|
223
|
+
request.headers.set('Content-Size', String(request.body.size));
|
|
213
224
|
delete request.duplex;
|
|
214
225
|
}
|
|
215
226
|
else {
|
|
@@ -225,15 +236,15 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
225
236
|
}
|
|
226
237
|
async _parseBody(fetchResponse) {
|
|
227
238
|
let body;
|
|
228
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
229
|
-
if (
|
|
239
|
+
const contentType = (fetchResponse.headers.get('Content-Type') || '');
|
|
240
|
+
if (type_is_1.default.is(contentType, ['json', 'application/*+json'])) {
|
|
230
241
|
body = await fetchResponse.json();
|
|
231
242
|
if (typeof body === 'string')
|
|
232
243
|
body = JSON.parse(body);
|
|
233
244
|
}
|
|
234
|
-
else if (
|
|
245
|
+
else if (type_is_1.default.is(contentType, ['text']))
|
|
235
246
|
body = await fetchResponse.text();
|
|
236
|
-
else if (
|
|
247
|
+
else if (type_is_1.default.is(contentType, ['multipart']))
|
|
237
248
|
body = await fetchResponse.formData();
|
|
238
249
|
else {
|
|
239
250
|
const buf = await fetchResponse.arrayBuffer();
|
package/cjs/impl/http-request.js
CHANGED
|
@@ -16,7 +16,6 @@ class HttpRequest {
|
|
|
16
16
|
this.method = (init?.method || 'GET').toUpperCase();
|
|
17
17
|
this.mode = init?.mode || 'cors';
|
|
18
18
|
this.redirect = init?.redirect || 'follow';
|
|
19
|
-
this.mode = init?.mode || 'cors';
|
|
20
19
|
this.referrer = init?.referrer || '';
|
|
21
20
|
this.referrerPolicy = init?.referrerPolicy || '';
|
|
22
21
|
this.signal = init?.signal || new AbortController().signal;
|
|
@@ -15,7 +15,6 @@ class HttpResponse {
|
|
|
15
15
|
this.ok = this.status >= 200 && this.status < 300;
|
|
16
16
|
this.body = init?.body;
|
|
17
17
|
this.hasBody = init?.body != null || !!init?.hasBody;
|
|
18
|
-
this.totalCount = init?.totalCount;
|
|
19
18
|
}
|
|
20
19
|
clone(update) {
|
|
21
20
|
return new HttpResponse({ ...this, ...update });
|
package/esm/client.js
CHANGED
|
@@ -44,7 +44,7 @@ export class OpraHttpClient {
|
|
|
44
44
|
url: '',
|
|
45
45
|
headers: { 'accept': 'application/json' }
|
|
46
46
|
});
|
|
47
|
-
this._metadataPromise = promise = controller.
|
|
47
|
+
this._metadataPromise = promise = controller.getBody();
|
|
48
48
|
return await promise
|
|
49
49
|
.then(async (body) => {
|
|
50
50
|
if (!body)
|
package/esm/constants.js
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
export const OPRA_JSON_CONTENT_TYPE_PATTERN = /^application\/\bopra\+json\b/i;
|
|
2
|
-
export const JSON_CONTENT_TYPE_PATTERN = /^application\/([\w-]+\+)?\bjson\b/i;
|
|
3
|
-
export const TEXT_CONTENT_TYPE_PATTERN = /^text\/.*$/i;
|
|
4
|
-
export const FORMDATA_CONTENT_TYPE_PATTERN = /^multipart\/\bform-data\b/i;
|
|
5
1
|
export const kClient = Symbol.for('kClient');
|
|
6
2
|
export const kContext = Symbol.for('kContext');
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { lastValueFrom, Observable } from 'rxjs';
|
|
2
2
|
import { isReadableStreamLike } from 'rxjs/internal/util/isReadableStreamLike';
|
|
3
|
+
import typeIs from '@browsery/type-is';
|
|
3
4
|
import { isBlob, OpraURL } from '@opra/common';
|
|
4
5
|
import { ClientError } from '../client-error.js';
|
|
5
|
-
import {
|
|
6
|
+
import { kClient, kContext } from '../constants.js';
|
|
6
7
|
import { HttpObserveType } from '../enums/index.js';
|
|
7
8
|
import { HttpEventType } from '../interfaces/index.js';
|
|
8
9
|
import { HttpRequest } from './http-request.js';
|
|
@@ -22,6 +23,15 @@ export class HttpRequestObservable extends Observable {
|
|
|
22
23
|
}
|
|
23
24
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
24
25
|
});
|
|
26
|
+
const url = new OpraURL(init?.url);
|
|
27
|
+
Object.defineProperty(this, kContext, {
|
|
28
|
+
enumerable: false,
|
|
29
|
+
value: {
|
|
30
|
+
headers: new Headers(init?.headers),
|
|
31
|
+
params: url.searchParams,
|
|
32
|
+
requestInit: { ...init }
|
|
33
|
+
}
|
|
34
|
+
});
|
|
25
35
|
Object.defineProperty(this, kClient, {
|
|
26
36
|
enumerable: false,
|
|
27
37
|
value: client
|
|
@@ -75,6 +85,7 @@ export class HttpRequestObservable extends Observable {
|
|
|
75
85
|
return this;
|
|
76
86
|
}
|
|
77
87
|
observe(observe) {
|
|
88
|
+
observe = observe || HttpObserveType.Body;
|
|
78
89
|
return new Observable((subscriber) => {
|
|
79
90
|
this[kIntlObservable].subscribe((event) => {
|
|
80
91
|
if (observe === HttpObserveType.Events) {
|
|
@@ -86,22 +97,33 @@ export class HttpRequestObservable extends Observable {
|
|
|
86
97
|
subscriber.complete();
|
|
87
98
|
return;
|
|
88
99
|
}
|
|
89
|
-
if (observe === HttpObserveType.Body && event.event === HttpEventType.Response) {
|
|
90
|
-
subscriber.next(event.response.body);
|
|
91
|
-
subscriber.complete();
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
100
|
if (event.event === HttpEventType.Response) {
|
|
95
|
-
|
|
101
|
+
const { response } = event;
|
|
102
|
+
const isOpraResponse = typeIs.is(event.response.contentType || '', ['application/opra+json']);
|
|
103
|
+
if (observe === HttpObserveType.Response) {
|
|
104
|
+
subscriber.next(response);
|
|
105
|
+
subscriber.complete();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (response.status >= 400 && response.status < 600) {
|
|
109
|
+
subscriber.error(new ClientError({
|
|
110
|
+
message: response.status + ' ' + response.statusText,
|
|
111
|
+
status: response.status,
|
|
112
|
+
issues: isOpraResponse ? response.body.errors : undefined
|
|
113
|
+
}));
|
|
114
|
+
subscriber.complete();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
subscriber.next(event.response.body);
|
|
96
118
|
subscriber.complete();
|
|
97
119
|
}
|
|
98
120
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
99
121
|
});
|
|
100
122
|
}
|
|
101
123
|
toPromise() {
|
|
102
|
-
return this.
|
|
124
|
+
return this.getBody();
|
|
103
125
|
}
|
|
104
|
-
|
|
126
|
+
getBody() {
|
|
105
127
|
return lastValueFrom(this.observe(HttpObserveType.Body));
|
|
106
128
|
}
|
|
107
129
|
getResponse() {
|
|
@@ -126,13 +148,15 @@ export class HttpRequestObservable extends Observable {
|
|
|
126
148
|
// Send request
|
|
127
149
|
const url = new OpraURL(request.url, clientContext.serviceUrl);
|
|
128
150
|
const fetchResponse = await clientContext.fetch(url.toString(), request);
|
|
151
|
+
const contentType = (fetchResponse.headers.get('content-type') || '').split(';')[0];
|
|
129
152
|
// Emit 'response-header' event
|
|
130
153
|
const headersResponse = clientContext.createResponse({
|
|
131
154
|
url: fetchResponse.url,
|
|
132
155
|
headers: fetchResponse.headers,
|
|
133
156
|
status: fetchResponse.status,
|
|
134
157
|
statusText: fetchResponse.statusText,
|
|
135
|
-
hasBody: !!fetchResponse.body
|
|
158
|
+
hasBody: !!fetchResponse.body,
|
|
159
|
+
contentType
|
|
136
160
|
});
|
|
137
161
|
subscriber.next({
|
|
138
162
|
request,
|
|
@@ -143,33 +167,19 @@ export class HttpRequestObservable extends Observable {
|
|
|
143
167
|
const body = fetchResponse.body
|
|
144
168
|
? await this._parseBody(fetchResponse)
|
|
145
169
|
: undefined;
|
|
146
|
-
// Handle errors
|
|
147
|
-
if (fetchResponse.status >= 400 && fetchResponse.status <= 599) {
|
|
148
|
-
subscriber.error(new ClientError({
|
|
149
|
-
message: fetchResponse.status + ' ' + fetchResponse.statusText,
|
|
150
|
-
status: fetchResponse.status,
|
|
151
|
-
issues: body.errors
|
|
152
|
-
}));
|
|
153
|
-
subscriber.complete();
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
170
|
// Create response
|
|
157
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
158
171
|
const responseInit = {
|
|
159
172
|
url: fetchResponse.url,
|
|
160
173
|
headers: fetchResponse.headers,
|
|
161
174
|
status: fetchResponse.status,
|
|
162
175
|
statusText: fetchResponse.statusText,
|
|
176
|
+
contentType,
|
|
163
177
|
body,
|
|
164
178
|
};
|
|
165
|
-
if (OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
|
|
166
|
-
responseInit.totalCount = body?.totalCount;
|
|
167
|
-
responseInit.affected = body?.affected;
|
|
168
|
-
}
|
|
169
179
|
const response = clientContext.createResponse(responseInit);
|
|
170
180
|
// Call response Interceptors
|
|
171
181
|
for (const interceptor of clientContext.responseInterceptors) {
|
|
172
|
-
await interceptor(response);
|
|
182
|
+
await interceptor(response, request);
|
|
173
183
|
}
|
|
174
184
|
// Emit 'response' event
|
|
175
185
|
subscriber.next({
|
|
@@ -206,7 +216,7 @@ export class HttpRequestObservable extends Observable {
|
|
|
206
216
|
else if (isBlob(request.body)) {
|
|
207
217
|
contentType = request.body.type || 'application/octet-stream';
|
|
208
218
|
body = request.body;
|
|
209
|
-
request.headers.set('Content-Size', String(request.body.
|
|
219
|
+
request.headers.set('Content-Size', String(request.body.size));
|
|
210
220
|
delete request.duplex;
|
|
211
221
|
}
|
|
212
222
|
else {
|
|
@@ -222,15 +232,15 @@ export class HttpRequestObservable extends Observable {
|
|
|
222
232
|
}
|
|
223
233
|
async _parseBody(fetchResponse) {
|
|
224
234
|
let body;
|
|
225
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
226
|
-
if (
|
|
235
|
+
const contentType = (fetchResponse.headers.get('Content-Type') || '');
|
|
236
|
+
if (typeIs.is(contentType, ['json', 'application/*+json'])) {
|
|
227
237
|
body = await fetchResponse.json();
|
|
228
238
|
if (typeof body === 'string')
|
|
229
239
|
body = JSON.parse(body);
|
|
230
240
|
}
|
|
231
|
-
else if (
|
|
241
|
+
else if (typeIs.is(contentType, ['text']))
|
|
232
242
|
body = await fetchResponse.text();
|
|
233
|
-
else if (
|
|
243
|
+
else if (typeIs.is(contentType, ['multipart']))
|
|
234
244
|
body = await fetchResponse.formData();
|
|
235
245
|
else {
|
|
236
246
|
const buf = await fetchResponse.arrayBuffer();
|
package/esm/impl/http-request.js
CHANGED
|
@@ -13,7 +13,6 @@ export class HttpRequest {
|
|
|
13
13
|
this.method = (init?.method || 'GET').toUpperCase();
|
|
14
14
|
this.mode = init?.mode || 'cors';
|
|
15
15
|
this.redirect = init?.redirect || 'follow';
|
|
16
|
-
this.mode = init?.mode || 'cors';
|
|
17
16
|
this.referrer = init?.referrer || '';
|
|
18
17
|
this.referrerPolicy = init?.referrerPolicy || '';
|
|
19
18
|
this.signal = init?.signal || new AbortController().signal;
|
|
@@ -12,7 +12,6 @@ export class HttpResponse {
|
|
|
12
12
|
this.ok = this.status >= 200 && this.status < 300;
|
|
13
13
|
this.body = init?.body;
|
|
14
14
|
this.hasBody = init?.body != null || !!init?.hasBody;
|
|
15
|
-
this.totalCount = init?.totalCount;
|
|
16
15
|
}
|
|
17
16
|
clone(update) {
|
|
18
17
|
return new HttpResponse({ ...this, ...update });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.2",
|
|
4
4
|
"description": "Opra Client package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,12 +33,13 @@
|
|
|
33
33
|
"types": "./types/index.d.ts",
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@browsery/fs": "^0.4.0",
|
|
36
|
-
"@browsery/highland": "^0.
|
|
36
|
+
"@browsery/highland": "^0.6.0",
|
|
37
37
|
"@browsery/http-parser": "^0.4.0",
|
|
38
|
-
"@browsery/i18next": "^0.
|
|
39
|
-
"@browsery/stream": "^0.
|
|
38
|
+
"@browsery/i18next": "^0.7.2",
|
|
39
|
+
"@browsery/stream": "^0.6.0",
|
|
40
|
+
"@browsery/type-is": "^0.6.3",
|
|
40
41
|
"@browsery/util": "^0.4.0",
|
|
41
|
-
"@opra/common": "^0.
|
|
42
|
+
"@opra/common": "^0.27.2",
|
|
42
43
|
"accepts": "^1.3.8",
|
|
43
44
|
"buffer": "^6.0.3",
|
|
44
45
|
"cookie": "^0.5.0",
|
|
@@ -51,13 +52,12 @@
|
|
|
51
52
|
"path-browserify": "^1.0.1",
|
|
52
53
|
"process": "^0.11.10",
|
|
53
54
|
"putil-isplainobject": "^1.1.5",
|
|
54
|
-
"putil-merge": "^3.
|
|
55
|
+
"putil-merge": "^3.12.1",
|
|
55
56
|
"putil-promisify": "^1.10.1",
|
|
56
57
|
"putil-varhelpers": "^1.6.5",
|
|
57
58
|
"reflect-metadata": "^0.1.13",
|
|
58
59
|
"rxjs": "^7.8.1",
|
|
59
60
|
"ts-lib": "^0.0.5",
|
|
60
|
-
"type-is": "^1.6.18",
|
|
61
61
|
"uid": "^2.0.1"
|
|
62
62
|
},
|
|
63
63
|
"sideEffects": false,
|
package/types/constants.d.ts
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
export declare const OPRA_JSON_CONTENT_TYPE_PATTERN: RegExp;
|
|
2
|
-
export declare const JSON_CONTENT_TYPE_PATTERN: RegExp;
|
|
3
|
-
export declare const TEXT_CONTENT_TYPE_PATTERN: RegExp;
|
|
4
|
-
export declare const FORMDATA_CONTENT_TYPE_PATTERN: RegExp;
|
|
5
1
|
export declare const kClient: unique symbol;
|
|
6
2
|
export declare const kContext: unique symbol;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PartialInput } from '@opra/common';
|
|
2
|
-
import { OpraFilter } from '@opra/common';
|
|
2
|
+
import { OperationResult, OpraFilter } from '@opra/common';
|
|
3
3
|
import type { OpraHttpClient } from '../client.js';
|
|
4
4
|
import { HttpRequestObservable } from './http-request-observable.js';
|
|
5
5
|
/**
|
|
@@ -9,13 +9,13 @@ export declare class HttpCollectionNode<TType, TResponseExt = {}> {
|
|
|
9
9
|
protected _client: OpraHttpClient;
|
|
10
10
|
protected _path: string;
|
|
11
11
|
constructor(client: OpraHttpClient<any>, path: string);
|
|
12
|
-
create(data: PartialInput<TType>, options?: HttpCollectionNode.CreateOptions): HttpRequestObservable<TType
|
|
13
|
-
delete(id: any): HttpRequestObservable<never
|
|
14
|
-
deleteMany(options?: HttpCollectionNode.DeleteManyOptions): HttpRequestObservable<
|
|
15
|
-
get(id: any, options?: HttpCollectionNode.GetOptions): HttpRequestObservable<TType
|
|
16
|
-
findMany(options?: HttpCollectionNode.FindManyOptions): HttpRequestObservable<
|
|
17
|
-
update(id: any, data: PartialInput<TType>, options?: HttpCollectionNode.UpdateOptions): HttpRequestObservable<TType
|
|
18
|
-
updateMany(data: PartialInput<TType>, options?: HttpCollectionNode.UpdateManyOptions): HttpRequestObservable<
|
|
12
|
+
create(data: PartialInput<TType>, options?: HttpCollectionNode.CreateOptions): HttpRequestObservable<OperationResult<TType>, TResponseExt>;
|
|
13
|
+
delete(id: any): HttpRequestObservable<OperationResult<never>, TResponseExt>;
|
|
14
|
+
deleteMany(options?: HttpCollectionNode.DeleteManyOptions): HttpRequestObservable<OperationResult<never>, TResponseExt>;
|
|
15
|
+
get(id: any, options?: HttpCollectionNode.GetOptions): HttpRequestObservable<OperationResult<TType>, TResponseExt>;
|
|
16
|
+
findMany(options?: HttpCollectionNode.FindManyOptions): HttpRequestObservable<OperationResult<TType[]>, TResponseExt>;
|
|
17
|
+
update(id: any, data: PartialInput<TType>, options?: HttpCollectionNode.UpdateOptions): HttpRequestObservable<OperationResult<TType>, TResponseExt>;
|
|
18
|
+
updateMany(data: PartialInput<TType>, options?: HttpCollectionNode.UpdateManyOptions): HttpRequestObservable<OperationResult<never>, TResponseExt>;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* @namespace
|
|
@@ -53,14 +53,4 @@ export declare namespace HttpCollectionNode {
|
|
|
53
53
|
interface UpdateManyOptions {
|
|
54
54
|
filter?: string | OpraFilter.Expression;
|
|
55
55
|
}
|
|
56
|
-
interface DeleteManyBody {
|
|
57
|
-
affected: number;
|
|
58
|
-
}
|
|
59
|
-
interface UpdateManyBody {
|
|
60
|
-
affected: number;
|
|
61
|
-
}
|
|
62
|
-
interface FindManyBody<TType> {
|
|
63
|
-
totalCount?: number;
|
|
64
|
-
data: TType[];
|
|
65
|
-
}
|
|
66
56
|
}
|
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import type { OpraHttpClient } from '../client.js';
|
|
3
|
-
import { kClient } from '../constants.js';
|
|
3
|
+
import { kClient, kContext } from '../constants.js';
|
|
4
4
|
import { HttpObserveType } from '../enums/index.js';
|
|
5
5
|
import { HttpEvent } from '../interfaces/index.js';
|
|
6
|
-
import {
|
|
6
|
+
import { URLSearchParamsInit } from '../types.js';
|
|
7
7
|
import { HttpRequest } from './http-request.js';
|
|
8
8
|
import { HttpResponse } from './http-response.js';
|
|
9
9
|
/**
|
|
10
10
|
* @namespace HttpRequestObservable
|
|
11
11
|
*/
|
|
12
12
|
export declare namespace HttpRequestObservable {
|
|
13
|
-
interface Initiator extends HttpRequest.Initiator {
|
|
14
|
-
requestInterceptors?: RequestInterceptor[];
|
|
15
|
-
responseInterceptors?: ResponseInterceptor[];
|
|
16
|
-
}
|
|
17
13
|
interface HttpOptions extends Partial<Pick<HttpRequest, 'cache' | 'credentials' | 'destination' | 'integrity' | 'keepalive' | 'mode' | 'redirect' | 'referrer' | 'referrerPolicy'>> {
|
|
18
14
|
}
|
|
19
15
|
}
|
|
@@ -21,25 +17,31 @@ declare const kIntlObservable: unique symbol;
|
|
|
21
17
|
/**
|
|
22
18
|
* @class HttpRequestObservable
|
|
23
19
|
*/
|
|
24
|
-
export declare class HttpRequestObservable<
|
|
20
|
+
export declare class HttpRequestObservable<TPayload = any, TResponseExt = {}> extends Observable<TPayload> {
|
|
25
21
|
[kClient]: OpraHttpClient;
|
|
22
|
+
[kContext]: {
|
|
23
|
+
headers: Headers;
|
|
24
|
+
params: URLSearchParams;
|
|
25
|
+
requestInit: any;
|
|
26
|
+
};
|
|
26
27
|
[kIntlObservable]: Observable<HttpEvent>;
|
|
27
28
|
request: HttpRequest;
|
|
28
|
-
constructor(client: OpraHttpClient<any>, init?:
|
|
29
|
+
constructor(client: OpraHttpClient<any>, init?: HttpRequest.Initiator);
|
|
29
30
|
httpOptions(options: HttpRequestObservable.HttpOptions): this;
|
|
30
31
|
header(headers: HeadersInit): this;
|
|
31
32
|
header(name: string, value?: string | number | boolean | null): this;
|
|
32
33
|
param(params: URLSearchParamsInit): this;
|
|
33
34
|
param(name: string, value: any): this;
|
|
34
|
-
observe(
|
|
35
|
+
observe(): Observable<TPayload>;
|
|
36
|
+
observe(observe: HttpObserveType.Body): Observable<any>;
|
|
35
37
|
observe(observe: HttpObserveType.ResponseHeader): Observable<HttpResponse<void> & TResponseExt>;
|
|
36
|
-
observe(observe: HttpObserveType.Response): Observable<HttpResponse<
|
|
38
|
+
observe(observe: HttpObserveType.Response): Observable<HttpResponse<any> & TResponseExt>;
|
|
37
39
|
observe(observe: HttpObserveType.Events): Observable<HttpEvent>;
|
|
38
|
-
toPromise(): Promise<
|
|
39
|
-
|
|
40
|
-
getResponse(): Promise<HttpResponse<
|
|
40
|
+
toPromise(): Promise<TPayload>;
|
|
41
|
+
getBody(): Promise<TPayload>;
|
|
42
|
+
getResponse(): Promise<HttpResponse<TPayload> & TResponseExt>;
|
|
41
43
|
protected _send(): Observable<HttpEvent>;
|
|
42
44
|
protected _prepareRequest(): void;
|
|
43
|
-
protected _parseBody(fetchResponse: Response): Promise<
|
|
45
|
+
protected _parseBody(fetchResponse: Response): Promise<TPayload>;
|
|
44
46
|
}
|
|
45
47
|
export {};
|
|
@@ -6,8 +6,7 @@ export declare namespace HttpResponse {
|
|
|
6
6
|
url?: string;
|
|
7
7
|
body?: any;
|
|
8
8
|
hasBody?: boolean;
|
|
9
|
-
|
|
10
|
-
affected?: number;
|
|
9
|
+
contentType: string;
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
12
|
export declare class HttpResponse<TBody = any> {
|
|
@@ -34,9 +33,11 @@ export declare class HttpResponse<TBody = any> {
|
|
|
34
33
|
/**
|
|
35
34
|
* Body contents
|
|
36
35
|
*/
|
|
37
|
-
readonly body: TBody
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
readonly body: TBody;
|
|
37
|
+
/**
|
|
38
|
+
* Content-Type header value without encoding part
|
|
39
|
+
*/
|
|
40
|
+
readonly contentType: string;
|
|
40
41
|
/**
|
|
41
42
|
* Returns true if response has body to be received
|
|
42
43
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PartialInput } from '@opra/common';
|
|
1
|
+
import { OperationResult, PartialInput } from '@opra/common';
|
|
2
2
|
import type { OpraHttpClient } from '../client.js';
|
|
3
3
|
import { HttpRequestObservable } from './http-request-observable.js';
|
|
4
4
|
/**
|
|
@@ -8,10 +8,10 @@ export declare class HttpSingletonNode<TType, TResponseExt = {}> {
|
|
|
8
8
|
protected _client: OpraHttpClient;
|
|
9
9
|
protected _path: string;
|
|
10
10
|
constructor(client: OpraHttpClient<any>, path: string);
|
|
11
|
-
create(data: PartialInput<TType>, options?: HttpSingletonNode.CreateOptions): HttpRequestObservable<TType
|
|
12
|
-
delete(): HttpRequestObservable<never
|
|
13
|
-
get(options?: HttpSingletonNode.GetOptions): HttpRequestObservable<TType
|
|
14
|
-
update(data: PartialInput<TType>, options?: HttpSingletonNode.UpdateOptions): HttpRequestObservable<TType
|
|
11
|
+
create(data: PartialInput<TType>, options?: HttpSingletonNode.CreateOptions): HttpRequestObservable<OperationResult<TType>, TResponseExt>;
|
|
12
|
+
delete(): HttpRequestObservable<OperationResult<never>, TResponseExt>;
|
|
13
|
+
get(options?: HttpSingletonNode.GetOptions): HttpRequestObservable<OperationResult<TType>, TResponseExt>;
|
|
14
|
+
update(data: PartialInput<TType>, options?: HttpSingletonNode.UpdateOptions): HttpRequestObservable<OperationResult<TType>, TResponseExt>;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* @namespace HttpSingletonNode
|
package/types/types.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type { Observable } from 'rxjs';
|
|
2
|
-
import type { HttpObserveType } from './enums/http-observable-type.enum.js';
|
|
3
1
|
import type { HttpRequest } from './impl/http-request';
|
|
4
|
-
|
|
2
|
+
import type { HttpResponse } from './impl/http-response.js';
|
|
5
3
|
export type RequestInterceptor = (request: HttpRequest) => void | Promise<void>;
|
|
6
|
-
export type ResponseInterceptor = (response:
|
|
4
|
+
export type ResponseInterceptor = (response: HttpResponse, request: HttpRequest) => void | Promise<void>;
|
|
7
5
|
export type URLSearchParamsInit = string[][] | Record<string, string> | string | URLSearchParams;
|