@opra/client 0.27.0 → 0.28.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.
Files changed (83) hide show
  1. package/browser.js +400 -295
  2. package/cjs/constants.js +2 -1
  3. package/cjs/core/backend.js +9 -0
  4. package/cjs/core/client-base.js +18 -0
  5. package/cjs/http/fetch-backend.js +210 -0
  6. package/cjs/http/http-backend.js +15 -0
  7. package/cjs/http/http-client-base.js +64 -0
  8. package/cjs/http/http-client.js +19 -0
  9. package/cjs/{impl/collection-node.js → http/http-collection-node.js} +9 -9
  10. package/cjs/http/http-fetch-client.js +19 -0
  11. package/cjs/http/http-interceptor-handler.js +18 -0
  12. package/cjs/http/http-request-observable.js +135 -0
  13. package/cjs/{impl → http}/http-response.js +1 -0
  14. package/cjs/{impl → http}/http-service-base.js +1 -1
  15. package/cjs/{impl/singleton-node.js → http/http-singleton-node.js} +6 -6
  16. package/cjs/{interfaces/http-event.interface.js → http/interfaces/http-event.js} +9 -10
  17. package/cjs/index.js +19 -12
  18. package/cjs/types.js +1 -0
  19. package/esm/constants.js +1 -0
  20. package/esm/core/backend.js +5 -0
  21. package/esm/core/client-base.js +14 -0
  22. package/esm/http/fetch-backend.js +205 -0
  23. package/esm/http/http-backend.js +11 -0
  24. package/esm/http/http-client-base.js +60 -0
  25. package/esm/http/http-client.js +15 -0
  26. package/esm/{impl/collection-node.js → http/http-collection-node.js} +9 -9
  27. package/esm/http/http-fetch-client.js +15 -0
  28. package/esm/http/http-interceptor-handler.js +14 -0
  29. package/esm/http/http-request-observable.js +130 -0
  30. package/esm/{impl → http}/http-response.js +1 -0
  31. package/esm/{impl → http}/http-service-base.js +1 -1
  32. package/esm/{impl/singleton-node.js → http/http-singleton-node.js} +6 -6
  33. package/esm/{interfaces/http-event.interface.js → http/interfaces/http-event.js} +9 -10
  34. package/esm/index.js +17 -11
  35. package/esm/types.js +1 -0
  36. package/package.json +7 -7
  37. package/types/constants.d.ts +1 -0
  38. package/types/core/backend.d.ts +18 -0
  39. package/types/core/client-base.d.ts +21 -0
  40. package/types/http/fetch-backend.d.ts +42 -0
  41. package/types/http/http-backend.d.ts +27 -0
  42. package/types/http/http-client-base.d.ts +31 -0
  43. package/types/http/http-client.d.ts +12 -0
  44. package/types/{impl/collection-node.d.ts → http/http-collection-node.d.ts} +11 -11
  45. package/types/http/http-fetch-client.d.ts +12 -0
  46. package/types/http/http-interceptor-handler.d.ts +11 -0
  47. package/types/http/http-request-observable.d.ts +35 -0
  48. package/types/{impl → http}/http-response.d.ts +0 -1
  49. package/types/{impl → http}/http-service-base.d.ts +1 -1
  50. package/types/{impl/singleton-node.d.ts → http/http-singleton-node.d.ts} +8 -8
  51. package/types/http/interfaces/http-event.d.ts +108 -0
  52. package/types/http/interfaces/http-handler.d.ts +7 -0
  53. package/types/http/interfaces/http-interceptor.d.ts +8 -0
  54. package/types/index.d.ts +17 -11
  55. package/types/types.d.ts +0 -4
  56. package/cjs/client.js +0 -83
  57. package/cjs/enums/index.js +0 -4
  58. package/cjs/impl/http-request-observable.js +0 -248
  59. package/cjs/impl/http-request.js +0 -28
  60. package/cjs/interfaces/index.js +0 -5
  61. package/esm/client.js +0 -79
  62. package/esm/enums/index.js +0 -1
  63. package/esm/impl/http-request-observable.js +0 -243
  64. package/esm/impl/http-request.js +0 -24
  65. package/esm/interfaces/index.js +0 -2
  66. package/types/client.d.ts +0 -33
  67. package/types/enums/index.d.ts +0 -1
  68. package/types/impl/http-request-observable.d.ts +0 -46
  69. package/types/impl/http-request.d.ts +0 -71
  70. package/types/interfaces/client-context.interface.d.ts +0 -13
  71. package/types/interfaces/http-event.interface.d.ts +0 -88
  72. package/types/interfaces/http-request-defaults.interface.d.ts +0 -6
  73. package/types/interfaces/index.d.ts +0 -2
  74. /package/cjs/{client-error.js → core/client-error.js} +0 -0
  75. /package/cjs/{enums → http/enums}/http-observable-type.enum.js +0 -0
  76. /package/cjs/{interfaces/client-context.interface.js → http/interfaces/http-handler.js} +0 -0
  77. /package/cjs/{interfaces/http-request-defaults.interface.js → http/interfaces/http-interceptor.js} +0 -0
  78. /package/esm/{client-error.js → core/client-error.js} +0 -0
  79. /package/esm/{enums → http/enums}/http-observable-type.enum.js +0 -0
  80. /package/esm/{interfaces/client-context.interface.js → http/interfaces/http-handler.js} +0 -0
  81. /package/esm/{interfaces/http-request-defaults.interface.js → http/interfaces/http-interceptor.js} +0 -0
  82. /package/types/{client-error.d.ts → core/client-error.d.ts} +0 -0
  83. /package/types/{enums → http/enums}/http-observable-type.enum.d.ts +0 -0
package/cjs/constants.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.kContext = exports.kClient = void 0;
3
+ exports.kContext = exports.kBackend = exports.kClient = void 0;
4
4
  exports.kClient = Symbol.for('kClient');
5
+ exports.kBackend = Symbol.for('kBackend');
5
6
  exports.kContext = Symbol.for('kContext');
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Backend = void 0;
4
+ class Backend {
5
+ constructor(options) {
6
+ this.api = options?.api;
7
+ }
8
+ }
9
+ exports.Backend = Backend;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientBase = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ /**
6
+ *
7
+ * @class ClientBase
8
+ * @abstract
9
+ */
10
+ class ClientBase {
11
+ constructor(backend) {
12
+ Object.defineProperty(this, constants_js_1.kBackend, {
13
+ enumerable: false,
14
+ value: backend
15
+ });
16
+ }
17
+ }
18
+ exports.ClientBase = ClientBase;
@@ -0,0 +1,210 @@
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 rxjs_1 = require("rxjs");
7
+ const isReadableStreamLike_1 = require("rxjs/internal/util/isReadableStreamLike");
8
+ const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is"));
9
+ const common_1 = require("@opra/common");
10
+ const http_backend_js_1 = require("./http-backend.js");
11
+ const http_response_js_1 = require("./http-response.js");
12
+ const http_event_js_1 = require("./interfaces/http-event.js");
13
+ /**
14
+ *
15
+ * @class FetchBackend
16
+ */
17
+ class FetchBackend extends http_backend_js_1.HttpBackend {
18
+ constructor(serviceUrl, options) {
19
+ super(serviceUrl, options);
20
+ // Create deduped interceptor array
21
+ this.interceptors = Array.from(new Set([...(options?.interceptors || [])]));
22
+ this.defaults = {
23
+ ...options?.defaults,
24
+ headers: options?.defaults?.headers instanceof Headers
25
+ ? options?.defaults?.headers : new Headers(options?.defaults?.headers),
26
+ params: options?.defaults?.params instanceof URLSearchParams
27
+ ? options?.defaults?.params : new URLSearchParams(options?.defaults?.params)
28
+ };
29
+ }
30
+ handle(init) {
31
+ return new rxjs_1.Observable((subscriber) => {
32
+ (async () => {
33
+ let request = this.prepareRequest(init);
34
+ if (request.body && init.reportProgress) {
35
+ const stream = request.body;
36
+ const contentLength = request.headers.get('content-length') || '0';
37
+ const total = parseInt(contentLength, 10) || 0;
38
+ let loaded = 0;
39
+ const progressTrackingStream = new TransformStream({
40
+ transform(chunk, controller) {
41
+ controller.enqueue(chunk);
42
+ loaded += chunk.byteLength;
43
+ // Emit 'UploadProgress' event
44
+ subscriber.next({
45
+ type: http_event_js_1.HttpEventType.UploadProgress,
46
+ request,
47
+ total,
48
+ loaded
49
+ });
50
+ }
51
+ });
52
+ request = new Request(request.url, {
53
+ cache: request.cache,
54
+ credentials: request.credentials,
55
+ headers: request.headers,
56
+ integrity: request.integrity,
57
+ keepalive: request.keepalive,
58
+ method: request.method,
59
+ mode: request.mode,
60
+ redirect: request.redirect,
61
+ referrer: request.referrer,
62
+ referrerPolicy: request.referrerPolicy,
63
+ signal: request.signal,
64
+ body: stream.pipeThrough(progressTrackingStream),
65
+ window: init.window,
66
+ ...{
67
+ // undici library requires
68
+ duplex: 'half'
69
+ },
70
+ });
71
+ }
72
+ // Send request
73
+ const fetchPromise = this.send(request);
74
+ // Emit 'Sent' event
75
+ subscriber.next({
76
+ request,
77
+ type: http_event_js_1.HttpEventType.Sent,
78
+ });
79
+ const fetchResponse = await fetchPromise;
80
+ // Emit 'ResponseHeader' event
81
+ const headersResponse = this.createResponse({
82
+ url: fetchResponse.url,
83
+ headers: fetchResponse.headers,
84
+ status: fetchResponse.status,
85
+ statusText: fetchResponse.statusText,
86
+ hasBody: !!fetchResponse.body
87
+ });
88
+ subscriber.next({
89
+ request,
90
+ type: http_event_js_1.HttpEventType.ResponseHeader,
91
+ response: headersResponse
92
+ });
93
+ // Parse body
94
+ let body;
95
+ if (fetchResponse.body) {
96
+ if (init.reportProgress) {
97
+ const fetchBody = fetchResponse.body;
98
+ const contentLength = fetchResponse.headers.get('content-length') || '0';
99
+ const total = parseInt(contentLength, 10) || 0;
100
+ let loaded = 0;
101
+ const res = new Response(new ReadableStream({
102
+ async start(controller) {
103
+ const reader = fetchBody.getReader();
104
+ for (;;) {
105
+ const { done, value } = await reader.read();
106
+ if (done)
107
+ break;
108
+ loaded += value.byteLength;
109
+ controller.enqueue(value);
110
+ // Emit 'DownloadProgress' event
111
+ subscriber.next({
112
+ type: http_event_js_1.HttpEventType.DownloadProgress,
113
+ request,
114
+ total,
115
+ loaded
116
+ });
117
+ }
118
+ controller.close();
119
+ },
120
+ }));
121
+ body = await this.parseBody(res);
122
+ }
123
+ else {
124
+ body = await this.parseBody(fetchResponse);
125
+ }
126
+ }
127
+ const response = this.createResponse({
128
+ url: fetchResponse.url,
129
+ headers: fetchResponse.headers,
130
+ status: fetchResponse.status,
131
+ statusText: fetchResponse.statusText,
132
+ body,
133
+ });
134
+ // Emit 'Response' event
135
+ subscriber.next({
136
+ request,
137
+ type: http_event_js_1.HttpEventType.Response,
138
+ response
139
+ });
140
+ subscriber.complete();
141
+ })().catch(error => subscriber.error(error));
142
+ });
143
+ }
144
+ send(request) {
145
+ return fetch(request);
146
+ }
147
+ prepareRequest(init) {
148
+ const headers = init.headers || new Headers();
149
+ const requestInit = {
150
+ ...init,
151
+ headers
152
+ };
153
+ const body = requestInit.body;
154
+ if (body) {
155
+ let contentType;
156
+ if (typeof body === 'string' || typeof body === 'number' || typeof body === 'boolean') {
157
+ contentType = 'text/plain; charset="UTF-8"';
158
+ requestInit.body = new Blob([String(body)], { type: contentType });
159
+ headers.set('Content-Length', String(requestInit.body.size));
160
+ delete requestInit.duplex;
161
+ }
162
+ else if ((0, isReadableStreamLike_1.isReadableStreamLike)(body)) {
163
+ contentType = 'application/octet-stream';
164
+ requestInit.duplex = 'half'; // undici library requires "duplex" option to be set for streams
165
+ }
166
+ else if (Buffer.isBuffer(body)) {
167
+ contentType = 'application/octet-stream';
168
+ headers.set('Content-Length', String(body.length));
169
+ delete requestInit.duplex;
170
+ }
171
+ else if ((0, common_1.isBlob)(body)) {
172
+ contentType = body.type || 'application/octet-stream';
173
+ headers.set('Content-Length', String(body.size));
174
+ delete requestInit.duplex;
175
+ }
176
+ else {
177
+ contentType = 'application/json;charset="UTF-8"';
178
+ requestInit.body = new Blob([JSON.stringify(body)], { type: contentType });
179
+ headers.set('Content-Length', String(requestInit.body.size));
180
+ delete requestInit.duplex;
181
+ }
182
+ if (contentType && !headers.has('Content-Type'))
183
+ headers.set('Content-Type', contentType);
184
+ }
185
+ return new Request(requestInit.url.toString(), requestInit);
186
+ }
187
+ createResponse(init) {
188
+ return new http_response_js_1.HttpResponse(init);
189
+ }
190
+ async parseBody(fetchResponse) {
191
+ let body;
192
+ const contentType = (fetchResponse.headers.get('Content-Type') || '');
193
+ if (type_is_1.default.is(contentType, ['json', 'application/*+json'])) {
194
+ body = await fetchResponse.json();
195
+ if (typeof body === 'string')
196
+ body = JSON.parse(body);
197
+ }
198
+ else if (type_is_1.default.is(contentType, ['text']))
199
+ body = await fetchResponse.text();
200
+ else if (type_is_1.default.is(contentType, ['multipart']))
201
+ body = await fetchResponse.formData();
202
+ else {
203
+ const buf = await fetchResponse.arrayBuffer();
204
+ if (buf.byteLength)
205
+ body = buf;
206
+ }
207
+ return body;
208
+ }
209
+ }
210
+ exports.FetchBackend = FetchBackend;
@@ -0,0 +1,15 @@
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
+ this.serviceUrl = serviceUrl;
13
+ }
14
+ }
15
+ exports.HttpBackend = HttpBackend;
@@ -0,0 +1,64 @@
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 client_base_js_1 = require("../core/client-base.js");
7
+ const http_collection_node_js_1 = require("./http-collection-node.js");
8
+ const http_request_observable_js_1 = require("./http-request-observable.js");
9
+ const http_singleton_node_js_1 = require("./http-singleton-node.js");
10
+ /**
11
+ *
12
+ * @class OpraClientBase
13
+ * @abstract
14
+ */
15
+ class HttpClientBase extends client_base_js_1.ClientBase {
16
+ constructor(backend) {
17
+ super(backend);
18
+ }
19
+ get serviceUrl() {
20
+ return this[constants_js_1.kBackend].serviceUrl;
21
+ }
22
+ async getMetadata() {
23
+ let promise = this._metadataPromise;
24
+ if (promise)
25
+ return promise;
26
+ const request = new http_request_observable_js_1.HttpRequestObservable(this[constants_js_1.kBackend], {
27
+ method: 'GET',
28
+ url: '/',
29
+ headers: new Headers({ 'accept': 'application/json' })
30
+ });
31
+ this._metadataPromise = promise = request.getBody();
32
+ return await promise
33
+ .then(async (body) => {
34
+ if (!body)
35
+ throw new Error(`No response returned.`);
36
+ const api = await common_1.ApiDocumentFactory.createDocument(body);
37
+ this[constants_js_1.kBackend].api = api;
38
+ return api;
39
+ })
40
+ .catch((e) => {
41
+ e.message = 'Unable to fetch metadata from service url (' + this.serviceUrl + '). ' + e.message;
42
+ throw e;
43
+ })
44
+ .finally(() => delete this._metadataPromise);
45
+ }
46
+ collection(path) {
47
+ return new http_collection_node_js_1.HttpCollectionNode(this[constants_js_1.kBackend], path);
48
+ }
49
+ singleton(path) {
50
+ return new http_singleton_node_js_1.HttpSingletonNode(this[constants_js_1.kBackend], path);
51
+ }
52
+ action(path, params) {
53
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this[constants_js_1.kBackend], {
54
+ method: 'GET',
55
+ url: path
56
+ });
57
+ if (params) {
58
+ Object.keys(params).forEach(k => params[k] = String(params[k]));
59
+ observable.param(params);
60
+ }
61
+ return observable;
62
+ }
63
+ }
64
+ exports.HttpClientBase = HttpClientBase;
@@ -0,0 +1,19 @@
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;
@@ -8,12 +8,12 @@ const http_request_observable_js_1 = require("./http-request-observable.js");
8
8
  * @class HttpCollectionNode
9
9
  */
10
10
  class HttpCollectionNode {
11
- constructor(client, path) {
12
- this._client = client;
11
+ constructor(backend, path) {
12
+ this._backend = backend;
13
13
  this._path = path;
14
14
  }
15
15
  create(data, options) {
16
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
16
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
17
17
  method: 'POST',
18
18
  url: this._path,
19
19
  body: data
@@ -31,13 +31,13 @@ class HttpCollectionNode {
31
31
  throw new TypeError(`'id' argument must have a value`);
32
32
  const url = new common_1.OpraURL();
33
33
  url.join({ resource: this._path, key: id });
34
- return new http_request_observable_js_1.HttpRequestObservable(this._client, {
34
+ return new http_request_observable_js_1.HttpRequestObservable(this._backend, {
35
35
  method: 'DELETE',
36
36
  url
37
37
  });
38
38
  }
39
39
  deleteMany(options) {
40
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
40
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
41
41
  method: 'DELETE',
42
42
  url: this._path
43
43
  });
@@ -50,7 +50,7 @@ class HttpCollectionNode {
50
50
  throw new TypeError(`'id' argument must have a value`);
51
51
  const url = new common_1.OpraURL();
52
52
  url.join({ resource: this._path, key: id });
53
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
53
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
54
54
  method: 'GET',
55
55
  url
56
56
  });
@@ -63,7 +63,7 @@ class HttpCollectionNode {
63
63
  return observable;
64
64
  }
65
65
  findMany(options) {
66
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
66
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
67
67
  method: 'GET',
68
68
  url: this._path
69
69
  });
@@ -92,7 +92,7 @@ class HttpCollectionNode {
92
92
  throw new TypeError(`'id' argument must have a value`);
93
93
  const url = new common_1.OpraURL();
94
94
  url.join({ resource: this._path, key: id });
95
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
95
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
96
96
  method: 'PATCH',
97
97
  url,
98
98
  body: data
@@ -106,7 +106,7 @@ class HttpCollectionNode {
106
106
  return observable;
107
107
  }
108
108
  updateMany(data, options) {
109
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
109
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
110
110
  method: 'PATCH',
111
111
  url: this._path,
112
112
  body: data
@@ -0,0 +1,19 @@
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;
@@ -0,0 +1,18 @@
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
+ }
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpRequestObservable = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const rxjs_1 = require("rxjs");
6
+ const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is"));
7
+ const common_1 = require("@opra/common");
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 && event.type === http_event_js_1.HttpEventType.ResponseHeader) {
30
+ subscriber.next(event.response);
31
+ subscriber.complete();
32
+ return;
33
+ }
34
+ if (event.type === http_event_js_1.HttpEventType.Response) {
35
+ const { response } = event;
36
+ if (observe === http_observable_type_enum_js_1.HttpObserveType.Response) {
37
+ subscriber.next(response);
38
+ subscriber.complete();
39
+ return;
40
+ }
41
+ const isOpraResponse = type_is_1.default.is(event.response.contentType || '', ['application/opra+json']);
42
+ if (response.status >= 400 && response.status < 600) {
43
+ subscriber.error(new client_error_js_1.ClientError({
44
+ message: response.status + ' ' + response.statusText,
45
+ status: response.status,
46
+ issues: isOpraResponse ? response.body.errors : undefined
47
+ }));
48
+ subscriber.complete();
49
+ return;
50
+ }
51
+ subscriber.next(event.response.body);
52
+ subscriber.complete();
53
+ }
54
+ },
55
+ error(error) {
56
+ subscriber.error(error);
57
+ },
58
+ complete() {
59
+ subscriber.complete();
60
+ }
61
+ });
62
+ });
63
+ Object.defineProperty(this, constants_js_1.kBackend, {
64
+ enumerable: false,
65
+ value: backend
66
+ });
67
+ const url = new common_1.OpraURL(init?.url, backend.serviceUrl.toString());
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
+ url,
75
+ }
76
+ });
77
+ }
78
+ clone() {
79
+ return new HttpRequestObservable(this[constants_js_1.kBackend], this[constants_js_1.kContext]);
80
+ }
81
+ options(options) {
82
+ Object.assign(this[constants_js_1.kContext], options);
83
+ return this;
84
+ }
85
+ header(arg0, value) {
86
+ const target = this[constants_js_1.kContext].headers;
87
+ if (typeof arg0 === 'object') {
88
+ const h = arg0 instanceof Headers
89
+ ? arg0
90
+ : new Headers(arg0);
91
+ h.forEach((v, k) => {
92
+ if (k.toLowerCase() === 'set-cookie') {
93
+ target.append(k, v);
94
+ }
95
+ else
96
+ target.set(k, v);
97
+ });
98
+ return this;
99
+ }
100
+ if (value == null || value === '')
101
+ target.delete(arg0);
102
+ else
103
+ target.append(arg0, String(value));
104
+ return this;
105
+ }
106
+ param(arg0, value) {
107
+ const target = this[constants_js_1.kContext].url.searchParams;
108
+ if (typeof arg0 === 'object') {
109
+ const h = arg0 instanceof URLSearchParams
110
+ ? arg0
111
+ : new URLSearchParams(arg0);
112
+ h.forEach((v, k) => target.set(k, v));
113
+ return this;
114
+ }
115
+ if (value == null)
116
+ target.delete(arg0);
117
+ else
118
+ target.set(arg0, String(value));
119
+ return this;
120
+ }
121
+ observe(observe) {
122
+ if (observe === this[constants_js_1.kContext].observe)
123
+ return this;
124
+ const cloned = this.clone();
125
+ cloned[constants_js_1.kContext].observe = observe || http_observable_type_enum_js_1.HttpObserveType.Body;
126
+ return cloned;
127
+ }
128
+ getBody() {
129
+ return (0, rxjs_1.lastValueFrom)(this.observe(http_observable_type_enum_js_1.HttpObserveType.Body));
130
+ }
131
+ getResponse() {
132
+ return (0, rxjs_1.lastValueFrom)(this.observe(http_observable_type_enum_js_1.HttpObserveType.Response));
133
+ }
134
+ }
135
+ exports.HttpRequestObservable = HttpRequestObservable;
@@ -15,6 +15,7 @@ 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.contentType = (this.headers.get('content-type') || '').split(';')[0];
18
19
  }
19
20
  clone(update) {
20
21
  return new HttpResponse({ ...this, ...update });
@@ -6,7 +6,7 @@ const constants_js_1 = require("../constants.js");
6
6
  class HttpServiceBase {
7
7
  constructor(client) {
8
8
  this[_a] = {
9
- resource: {},
9
+ resources: {},
10
10
  node: null
11
11
  };
12
12
  this[constants_js_1.kClient] = client;
@@ -7,12 +7,12 @@ const http_request_observable_js_1 = require("./http-request-observable.js");
7
7
  * @class HttpSingletonNode
8
8
  */
9
9
  class HttpSingletonNode {
10
- constructor(client, path) {
11
- this._client = client;
10
+ constructor(backend, path) {
11
+ this._backend = backend;
12
12
  this._path = path;
13
13
  }
14
14
  create(data, options) {
15
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
15
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
16
16
  method: 'POST',
17
17
  url: this._path,
18
18
  body: data
@@ -26,13 +26,13 @@ class HttpSingletonNode {
26
26
  return observable;
27
27
  }
28
28
  delete() {
29
- return new http_request_observable_js_1.HttpRequestObservable(this._client, {
29
+ return new http_request_observable_js_1.HttpRequestObservable(this._backend, {
30
30
  method: 'DELETE',
31
31
  url: this._path
32
32
  });
33
33
  }
34
34
  get(options) {
35
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
35
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
36
36
  method: 'GET',
37
37
  url: this._path
38
38
  });
@@ -45,7 +45,7 @@ class HttpSingletonNode {
45
45
  return observable;
46
46
  }
47
47
  update(data, options) {
48
- const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
48
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._backend, {
49
49
  method: 'PATCH',
50
50
  url: this._path,
51
51
  body: data