@opra/client 0.26.4 → 0.27.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 +30 -33
- package/cjs/client.js +1 -1
- package/cjs/constants.js +1 -5
- package/cjs/impl/http-request-observable.js +31 -29
- 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 +31 -30
- package/esm/impl/http-response.js +0 -1
- package/package.json +3 -3
- package/types/constants.d.ts +0 -4
- package/types/impl/collection-node.d.ts +8 -18
- package/types/impl/http-request-observable.d.ts +8 -7
- 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 "type-is";
|
|
27
24
|
import { isBlob, OpraURL as OpraURL2 } from "@opra/common";
|
|
28
25
|
|
|
29
26
|
// ../../build/client/esm/client-error.js
|
|
@@ -164,6 +161,7 @@ var HttpRequestObservable = class extends Observable {
|
|
|
164
161
|
return this;
|
|
165
162
|
}
|
|
166
163
|
observe(observe) {
|
|
164
|
+
observe = observe || HttpObserveType.Body;
|
|
167
165
|
return new Observable((subscriber) => {
|
|
168
166
|
this[kIntlObservable].subscribe((event) => {
|
|
169
167
|
if (observe === HttpObserveType.Events) {
|
|
@@ -175,22 +173,33 @@ var HttpRequestObservable = class extends Observable {
|
|
|
175
173
|
subscriber.complete();
|
|
176
174
|
return;
|
|
177
175
|
}
|
|
178
|
-
if (observe === HttpObserveType.Body && event.event === HttpEventType.Response) {
|
|
179
|
-
subscriber.next(event.response.body);
|
|
180
|
-
subscriber.complete();
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
176
|
if (event.event === HttpEventType.Response) {
|
|
184
|
-
|
|
177
|
+
const { response } = event;
|
|
178
|
+
const isOpraResponse = typeIs.is(event.response.contentType || "", ["application/opra+json"]);
|
|
179
|
+
if (observe === HttpObserveType.Response) {
|
|
180
|
+
subscriber.next(response);
|
|
181
|
+
subscriber.complete();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (response.status >= 400 && response.status < 600) {
|
|
185
|
+
subscriber.error(new ClientError({
|
|
186
|
+
message: response.status + " " + response.statusText,
|
|
187
|
+
status: response.status,
|
|
188
|
+
issues: isOpraResponse ? response.body.errors : void 0
|
|
189
|
+
}));
|
|
190
|
+
subscriber.complete();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
subscriber.next(event.response.body);
|
|
185
194
|
subscriber.complete();
|
|
186
195
|
}
|
|
187
196
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
188
197
|
});
|
|
189
198
|
}
|
|
190
199
|
toPromise() {
|
|
191
|
-
return this.
|
|
200
|
+
return this.getBody();
|
|
192
201
|
}
|
|
193
|
-
|
|
202
|
+
getBody() {
|
|
194
203
|
return lastValueFrom(this.observe(HttpObserveType.Body));
|
|
195
204
|
}
|
|
196
205
|
getResponse() {
|
|
@@ -211,12 +220,14 @@ var HttpRequestObservable = class extends Observable {
|
|
|
211
220
|
});
|
|
212
221
|
const url = new OpraURL2(request.url, clientContext.serviceUrl);
|
|
213
222
|
const fetchResponse = await clientContext.fetch(url.toString(), request);
|
|
223
|
+
const contentType = (fetchResponse.headers.get("content-type") || "").split(";")[0];
|
|
214
224
|
const headersResponse = clientContext.createResponse({
|
|
215
225
|
url: fetchResponse.url,
|
|
216
226
|
headers: fetchResponse.headers,
|
|
217
227
|
status: fetchResponse.status,
|
|
218
228
|
statusText: fetchResponse.statusText,
|
|
219
|
-
hasBody: !!fetchResponse.body
|
|
229
|
+
hasBody: !!fetchResponse.body,
|
|
230
|
+
contentType
|
|
220
231
|
});
|
|
221
232
|
subscriber.next({
|
|
222
233
|
request,
|
|
@@ -224,30 +235,17 @@ var HttpRequestObservable = class extends Observable {
|
|
|
224
235
|
response: headersResponse
|
|
225
236
|
});
|
|
226
237
|
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
238
|
const responseInit = {
|
|
238
239
|
url: fetchResponse.url,
|
|
239
240
|
headers: fetchResponse.headers,
|
|
240
241
|
status: fetchResponse.status,
|
|
241
242
|
statusText: fetchResponse.statusText,
|
|
243
|
+
contentType,
|
|
242
244
|
body
|
|
243
245
|
};
|
|
244
|
-
if (OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
|
|
245
|
-
responseInit.totalCount = body?.totalCount;
|
|
246
|
-
responseInit.affected = body?.affected;
|
|
247
|
-
}
|
|
248
246
|
const response = clientContext.createResponse(responseInit);
|
|
249
247
|
for (const interceptor of clientContext.responseInterceptors) {
|
|
250
|
-
await interceptor(response);
|
|
248
|
+
await interceptor(response, request);
|
|
251
249
|
}
|
|
252
250
|
subscriber.next({
|
|
253
251
|
request,
|
|
@@ -296,13 +294,13 @@ var HttpRequestObservable = class extends Observable {
|
|
|
296
294
|
async _parseBody(fetchResponse) {
|
|
297
295
|
let body;
|
|
298
296
|
const contentType = fetchResponse.headers.get("Content-Type") || "";
|
|
299
|
-
if (
|
|
297
|
+
if (typeIs.is(contentType, ["json", "application/*+json"])) {
|
|
300
298
|
body = await fetchResponse.json();
|
|
301
299
|
if (typeof body === "string")
|
|
302
300
|
body = JSON.parse(body);
|
|
303
|
-
} else if (
|
|
301
|
+
} else if (typeIs.is(contentType, ["text"]))
|
|
304
302
|
body = await fetchResponse.text();
|
|
305
|
-
else if (
|
|
303
|
+
else if (typeIs.is(contentType, ["multipart"]))
|
|
306
304
|
body = await fetchResponse.formData();
|
|
307
305
|
else {
|
|
308
306
|
const buf = await fetchResponse.arrayBuffer();
|
|
@@ -441,7 +439,6 @@ var HttpResponse = class _HttpResponse {
|
|
|
441
439
|
this.ok = this.status >= 200 && this.status < 300;
|
|
442
440
|
this.body = init?.body;
|
|
443
441
|
this.hasBody = init?.body != null || !!init?.hasBody;
|
|
444
|
-
this.totalCount = init?.totalCount;
|
|
445
442
|
}
|
|
446
443
|
clone(update) {
|
|
447
444
|
return new _HttpResponse({ ...this, ...update });
|
|
@@ -549,7 +546,7 @@ var OpraHttpClient = class {
|
|
|
549
546
|
url: "",
|
|
550
547
|
headers: { "accept": "application/json" }
|
|
551
548
|
});
|
|
552
|
-
this._metadataPromise = promise = controller.
|
|
549
|
+
this._metadataPromise = promise = controller.getBody();
|
|
553
550
|
return await promise.then(async (body) => {
|
|
554
551
|
if (!body)
|
|
555
552
|
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("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");
|
|
@@ -78,6 +80,7 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
78
80
|
return this;
|
|
79
81
|
}
|
|
80
82
|
observe(observe) {
|
|
83
|
+
observe = observe || index_js_1.HttpObserveType.Body;
|
|
81
84
|
return new rxjs_1.Observable((subscriber) => {
|
|
82
85
|
this[kIntlObservable].subscribe((event) => {
|
|
83
86
|
if (observe === index_js_1.HttpObserveType.Events) {
|
|
@@ -89,22 +92,33 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
89
92
|
subscriber.complete();
|
|
90
93
|
return;
|
|
91
94
|
}
|
|
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
95
|
if (event.event === index_js_2.HttpEventType.Response) {
|
|
98
|
-
|
|
96
|
+
const { response } = event;
|
|
97
|
+
const isOpraResponse = type_is_1.default.is(event.response.contentType || '', ['application/opra+json']);
|
|
98
|
+
if (observe === index_js_1.HttpObserveType.Response) {
|
|
99
|
+
subscriber.next(response);
|
|
100
|
+
subscriber.complete();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (response.status >= 400 && response.status < 600) {
|
|
104
|
+
subscriber.error(new client_error_js_1.ClientError({
|
|
105
|
+
message: response.status + ' ' + response.statusText,
|
|
106
|
+
status: response.status,
|
|
107
|
+
issues: isOpraResponse ? response.body.errors : undefined
|
|
108
|
+
}));
|
|
109
|
+
subscriber.complete();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
subscriber.next(event.response.body);
|
|
99
113
|
subscriber.complete();
|
|
100
114
|
}
|
|
101
115
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
102
116
|
});
|
|
103
117
|
}
|
|
104
118
|
toPromise() {
|
|
105
|
-
return this.
|
|
119
|
+
return this.getBody();
|
|
106
120
|
}
|
|
107
|
-
|
|
121
|
+
getBody() {
|
|
108
122
|
return (0, rxjs_1.lastValueFrom)(this.observe(index_js_1.HttpObserveType.Body));
|
|
109
123
|
}
|
|
110
124
|
getResponse() {
|
|
@@ -129,13 +143,15 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
129
143
|
// Send request
|
|
130
144
|
const url = new common_1.OpraURL(request.url, clientContext.serviceUrl);
|
|
131
145
|
const fetchResponse = await clientContext.fetch(url.toString(), request);
|
|
146
|
+
const contentType = (fetchResponse.headers.get('content-type') || '').split(';')[0];
|
|
132
147
|
// Emit 'response-header' event
|
|
133
148
|
const headersResponse = clientContext.createResponse({
|
|
134
149
|
url: fetchResponse.url,
|
|
135
150
|
headers: fetchResponse.headers,
|
|
136
151
|
status: fetchResponse.status,
|
|
137
152
|
statusText: fetchResponse.statusText,
|
|
138
|
-
hasBody: !!fetchResponse.body
|
|
153
|
+
hasBody: !!fetchResponse.body,
|
|
154
|
+
contentType
|
|
139
155
|
});
|
|
140
156
|
subscriber.next({
|
|
141
157
|
request,
|
|
@@ -146,33 +162,19 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
146
162
|
const body = fetchResponse.body
|
|
147
163
|
? await this._parseBody(fetchResponse)
|
|
148
164
|
: 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
165
|
// Create response
|
|
160
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
161
166
|
const responseInit = {
|
|
162
167
|
url: fetchResponse.url,
|
|
163
168
|
headers: fetchResponse.headers,
|
|
164
169
|
status: fetchResponse.status,
|
|
165
170
|
statusText: fetchResponse.statusText,
|
|
171
|
+
contentType,
|
|
166
172
|
body,
|
|
167
173
|
};
|
|
168
|
-
if (constants_js_1.OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
|
|
169
|
-
responseInit.totalCount = body?.totalCount;
|
|
170
|
-
responseInit.affected = body?.affected;
|
|
171
|
-
}
|
|
172
174
|
const response = clientContext.createResponse(responseInit);
|
|
173
175
|
// Call response Interceptors
|
|
174
176
|
for (const interceptor of clientContext.responseInterceptors) {
|
|
175
|
-
await interceptor(response);
|
|
177
|
+
await interceptor(response, request);
|
|
176
178
|
}
|
|
177
179
|
// Emit 'response' event
|
|
178
180
|
subscriber.next({
|
|
@@ -225,15 +227,15 @@ class HttpRequestObservable extends rxjs_1.Observable {
|
|
|
225
227
|
}
|
|
226
228
|
async _parseBody(fetchResponse) {
|
|
227
229
|
let body;
|
|
228
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
229
|
-
if (
|
|
230
|
+
const contentType = (fetchResponse.headers.get('Content-Type') || '');
|
|
231
|
+
if (type_is_1.default.is(contentType, ['json', 'application/*+json'])) {
|
|
230
232
|
body = await fetchResponse.json();
|
|
231
233
|
if (typeof body === 'string')
|
|
232
234
|
body = JSON.parse(body);
|
|
233
235
|
}
|
|
234
|
-
else if (
|
|
236
|
+
else if (type_is_1.default.is(contentType, ['text']))
|
|
235
237
|
body = await fetchResponse.text();
|
|
236
|
-
else if (
|
|
238
|
+
else if (type_is_1.default.is(contentType, ['multipart']))
|
|
237
239
|
body = await fetchResponse.formData();
|
|
238
240
|
else {
|
|
239
241
|
const buf = await fetchResponse.arrayBuffer();
|
|
@@ -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 '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';
|
|
@@ -75,6 +76,7 @@ export class HttpRequestObservable extends Observable {
|
|
|
75
76
|
return this;
|
|
76
77
|
}
|
|
77
78
|
observe(observe) {
|
|
79
|
+
observe = observe || HttpObserveType.Body;
|
|
78
80
|
return new Observable((subscriber) => {
|
|
79
81
|
this[kIntlObservable].subscribe((event) => {
|
|
80
82
|
if (observe === HttpObserveType.Events) {
|
|
@@ -86,22 +88,33 @@ export class HttpRequestObservable extends Observable {
|
|
|
86
88
|
subscriber.complete();
|
|
87
89
|
return;
|
|
88
90
|
}
|
|
89
|
-
if (observe === HttpObserveType.Body && event.event === HttpEventType.Response) {
|
|
90
|
-
subscriber.next(event.response.body);
|
|
91
|
-
subscriber.complete();
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
91
|
if (event.event === HttpEventType.Response) {
|
|
95
|
-
|
|
92
|
+
const { response } = event;
|
|
93
|
+
const isOpraResponse = typeIs.is(event.response.contentType || '', ['application/opra+json']);
|
|
94
|
+
if (observe === HttpObserveType.Response) {
|
|
95
|
+
subscriber.next(response);
|
|
96
|
+
subscriber.complete();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (response.status >= 400 && response.status < 600) {
|
|
100
|
+
subscriber.error(new ClientError({
|
|
101
|
+
message: response.status + ' ' + response.statusText,
|
|
102
|
+
status: response.status,
|
|
103
|
+
issues: isOpraResponse ? response.body.errors : undefined
|
|
104
|
+
}));
|
|
105
|
+
subscriber.complete();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
subscriber.next(event.response.body);
|
|
96
109
|
subscriber.complete();
|
|
97
110
|
}
|
|
98
111
|
}, (error) => subscriber.error(error), () => subscriber.complete());
|
|
99
112
|
});
|
|
100
113
|
}
|
|
101
114
|
toPromise() {
|
|
102
|
-
return this.
|
|
115
|
+
return this.getBody();
|
|
103
116
|
}
|
|
104
|
-
|
|
117
|
+
getBody() {
|
|
105
118
|
return lastValueFrom(this.observe(HttpObserveType.Body));
|
|
106
119
|
}
|
|
107
120
|
getResponse() {
|
|
@@ -126,13 +139,15 @@ export class HttpRequestObservable extends Observable {
|
|
|
126
139
|
// Send request
|
|
127
140
|
const url = new OpraURL(request.url, clientContext.serviceUrl);
|
|
128
141
|
const fetchResponse = await clientContext.fetch(url.toString(), request);
|
|
142
|
+
const contentType = (fetchResponse.headers.get('content-type') || '').split(';')[0];
|
|
129
143
|
// Emit 'response-header' event
|
|
130
144
|
const headersResponse = clientContext.createResponse({
|
|
131
145
|
url: fetchResponse.url,
|
|
132
146
|
headers: fetchResponse.headers,
|
|
133
147
|
status: fetchResponse.status,
|
|
134
148
|
statusText: fetchResponse.statusText,
|
|
135
|
-
hasBody: !!fetchResponse.body
|
|
149
|
+
hasBody: !!fetchResponse.body,
|
|
150
|
+
contentType
|
|
136
151
|
});
|
|
137
152
|
subscriber.next({
|
|
138
153
|
request,
|
|
@@ -143,33 +158,19 @@ export class HttpRequestObservable extends Observable {
|
|
|
143
158
|
const body = fetchResponse.body
|
|
144
159
|
? await this._parseBody(fetchResponse)
|
|
145
160
|
: 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
161
|
// Create response
|
|
157
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
158
162
|
const responseInit = {
|
|
159
163
|
url: fetchResponse.url,
|
|
160
164
|
headers: fetchResponse.headers,
|
|
161
165
|
status: fetchResponse.status,
|
|
162
166
|
statusText: fetchResponse.statusText,
|
|
167
|
+
contentType,
|
|
163
168
|
body,
|
|
164
169
|
};
|
|
165
|
-
if (OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
|
|
166
|
-
responseInit.totalCount = body?.totalCount;
|
|
167
|
-
responseInit.affected = body?.affected;
|
|
168
|
-
}
|
|
169
170
|
const response = clientContext.createResponse(responseInit);
|
|
170
171
|
// Call response Interceptors
|
|
171
172
|
for (const interceptor of clientContext.responseInterceptors) {
|
|
172
|
-
await interceptor(response);
|
|
173
|
+
await interceptor(response, request);
|
|
173
174
|
}
|
|
174
175
|
// Emit 'response' event
|
|
175
176
|
subscriber.next({
|
|
@@ -222,15 +223,15 @@ export class HttpRequestObservable extends Observable {
|
|
|
222
223
|
}
|
|
223
224
|
async _parseBody(fetchResponse) {
|
|
224
225
|
let body;
|
|
225
|
-
const contentType = fetchResponse.headers.get('Content-Type') || '';
|
|
226
|
-
if (
|
|
226
|
+
const contentType = (fetchResponse.headers.get('Content-Type') || '');
|
|
227
|
+
if (typeIs.is(contentType, ['json', 'application/*+json'])) {
|
|
227
228
|
body = await fetchResponse.json();
|
|
228
229
|
if (typeof body === 'string')
|
|
229
230
|
body = JSON.parse(body);
|
|
230
231
|
}
|
|
231
|
-
else if (
|
|
232
|
+
else if (typeIs.is(contentType, ['text']))
|
|
232
233
|
body = await fetchResponse.text();
|
|
233
|
-
else if (
|
|
234
|
+
else if (typeIs.is(contentType, ['multipart']))
|
|
234
235
|
body = await fetchResponse.formData();
|
|
235
236
|
else {
|
|
236
237
|
const buf = await fetchResponse.arrayBuffer();
|
|
@@ -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.0",
|
|
4
4
|
"description": "Opra Client package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@browsery/i18next": "^0.6.0",
|
|
39
39
|
"@browsery/stream": "^0.5.0",
|
|
40
40
|
"@browsery/util": "^0.4.0",
|
|
41
|
-
"@opra/common": "^0.
|
|
41
|
+
"@opra/common": "^0.27.0",
|
|
42
42
|
"accepts": "^1.3.8",
|
|
43
43
|
"buffer": "^6.0.3",
|
|
44
44
|
"cookie": "^0.5.0",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"path-browserify": "^1.0.1",
|
|
52
52
|
"process": "^0.11.10",
|
|
53
53
|
"putil-isplainobject": "^1.1.5",
|
|
54
|
-
"putil-merge": "^3.
|
|
54
|
+
"putil-merge": "^3.12.1",
|
|
55
55
|
"putil-promisify": "^1.10.1",
|
|
56
56
|
"putil-varhelpers": "^1.6.5",
|
|
57
57
|
"reflect-metadata": "^0.1.13",
|
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
|
}
|
|
@@ -21,7 +21,7 @@ declare const kIntlObservable: unique symbol;
|
|
|
21
21
|
/**
|
|
22
22
|
* @class HttpRequestObservable
|
|
23
23
|
*/
|
|
24
|
-
export declare class HttpRequestObservable<
|
|
24
|
+
export declare class HttpRequestObservable<TPayload = any, TResponseExt = {}> extends Observable<TPayload> {
|
|
25
25
|
[kClient]: OpraHttpClient;
|
|
26
26
|
[kIntlObservable]: Observable<HttpEvent>;
|
|
27
27
|
request: HttpRequest;
|
|
@@ -31,15 +31,16 @@ export declare class HttpRequestObservable<TBody = any, TResponseExt = {}> exten
|
|
|
31
31
|
header(name: string, value?: string | number | boolean | null): this;
|
|
32
32
|
param(params: URLSearchParamsInit): this;
|
|
33
33
|
param(name: string, value: any): this;
|
|
34
|
-
observe(
|
|
34
|
+
observe(): Observable<TPayload>;
|
|
35
|
+
observe(observe: HttpObserveType.Body): Observable<any>;
|
|
35
36
|
observe(observe: HttpObserveType.ResponseHeader): Observable<HttpResponse<void> & TResponseExt>;
|
|
36
|
-
observe(observe: HttpObserveType.Response): Observable<HttpResponse<
|
|
37
|
+
observe(observe: HttpObserveType.Response): Observable<HttpResponse<any> & TResponseExt>;
|
|
37
38
|
observe(observe: HttpObserveType.Events): Observable<HttpEvent>;
|
|
38
|
-
toPromise(): Promise<
|
|
39
|
-
|
|
40
|
-
getResponse(): Promise<HttpResponse<
|
|
39
|
+
toPromise(): Promise<TPayload>;
|
|
40
|
+
getBody(): Promise<TPayload>;
|
|
41
|
+
getResponse(): Promise<HttpResponse<TPayload> & TResponseExt>;
|
|
41
42
|
protected _send(): Observable<HttpEvent>;
|
|
42
43
|
protected _prepareRequest(): void;
|
|
43
|
-
protected _parseBody(fetchResponse: Response): Promise<
|
|
44
|
+
protected _parseBody(fetchResponse: Response): Promise<TPayload>;
|
|
44
45
|
}
|
|
45
46
|
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;
|