@opra/client 0.27.2 → 0.28.1

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 -303
  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 +3 -3
  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 -257
  59. package/cjs/impl/http-request.js +0 -27
  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 -252
  64. package/esm/impl/http-request.js +0 -23
  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 -47
  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/browser.js CHANGED
@@ -6,24 +6,35 @@
6
6
  var __defProp = Object.defineProperty;
7
7
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
8
 
9
- // ../../build/client/esm/client.js
10
- import { ApiDocumentFactory } from "@opra/common";
9
+ // ../../build/client/esm/core/backend.js
10
+ var Backend = class {
11
+ static {
12
+ __name(this, "Backend");
13
+ }
14
+ constructor(options) {
15
+ this.api = options?.api;
16
+ }
17
+ };
11
18
 
12
19
  // ../../build/client/esm/constants.js
13
20
  var kClient = Symbol.for("kClient");
21
+ var kBackend = Symbol.for("kBackend");
14
22
  var kContext = Symbol.for("kContext");
15
23
 
16
- // ../../build/client/esm/impl/collection-node.js
17
- import { toArrayDef } from "putil-varhelpers";
18
- import { OpraURL as OpraURL3 } from "@opra/common";
19
-
20
- // ../../build/client/esm/impl/http-request-observable.js
21
- import { lastValueFrom, Observable } from "rxjs";
22
- import { isReadableStreamLike } from "rxjs/internal/util/isReadableStreamLike";
23
- import typeIs from "@browsery/type-is";
24
- import { isBlob, OpraURL as OpraURL2 } from "@opra/common";
24
+ // ../../build/client/esm/core/client-base.js
25
+ var ClientBase = class {
26
+ static {
27
+ __name(this, "ClientBase");
28
+ }
29
+ constructor(backend) {
30
+ Object.defineProperty(this, kBackend, {
31
+ enumerable: false,
32
+ value: backend
33
+ });
34
+ }
35
+ };
25
36
 
26
- // ../../build/client/esm/client-error.js
37
+ // ../../build/client/esm/core/client-error.js
27
38
  var ClientError = class extends Error {
28
39
  static {
29
40
  __name(this, "ClientError");
@@ -41,265 +52,218 @@ var ClientError = class extends Error {
41
52
  }
42
53
  };
43
54
 
44
- // ../../build/client/esm/enums/http-observable-type.enum.js
45
- var HttpObserveType;
46
- (function(HttpObserveType2) {
47
- HttpObserveType2["ResponseHeader"] = "response-header";
48
- HttpObserveType2["Response"] = "response";
49
- HttpObserveType2["Body"] = "body";
50
- HttpObserveType2["Events"] = "events";
51
- })(HttpObserveType || (HttpObserveType = {}));
52
-
53
- // ../../build/client/esm/interfaces/http-event.interface.js
54
- var HttpEventType;
55
- (function(HttpEventType2) {
56
- HttpEventType2["Sent"] = "sent";
57
- HttpEventType2["UploadProgress"] = "upload-progress";
58
- HttpEventType2["ResponseHeader"] = "response-header";
59
- HttpEventType2["DownloadProgress"] = "download-progress";
60
- HttpEventType2["Response"] = "response";
61
- HttpEventType2["Custom"] = "custom";
62
- })(HttpEventType || (HttpEventType = {}));
55
+ // ../../build/client/esm/http/fetch-backend.js
56
+ import { Observable } from "rxjs";
57
+ import { isReadableStreamLike } from "rxjs/internal/util/isReadableStreamLike";
58
+ import typeIs from "@browsery/type-is";
59
+ import { isBlob } from "@opra/common";
63
60
 
64
- // ../../build/client/esm/impl/http-request.js
65
- import { OpraURL } from "@opra/common";
66
- var HttpRequest = class {
61
+ // ../../build/client/esm/http/http-backend.js
62
+ var HttpBackend = class extends Backend {
67
63
  static {
68
- __name(this, "HttpRequest");
64
+ __name(this, "HttpBackend");
69
65
  }
70
- constructor(init) {
71
- this.cache = init?.cache || "default";
72
- this.credentials = init?.credentials || "same-origin";
73
- this.destination = init?.destination || "";
74
- this.integrity = init?.integrity || "";
75
- this.keepalive = init?.keepalive ?? false;
76
- this.method = (init?.method || "GET").toUpperCase();
77
- this.mode = init?.mode || "cors";
78
- this.redirect = init?.redirect || "follow";
79
- this.referrer = init?.referrer || "";
80
- this.referrerPolicy = init?.referrerPolicy || "";
81
- this.signal = init?.signal || new AbortController().signal;
82
- this.body = init?.body;
83
- this.url = init?.url instanceof OpraURL ? init.url : new OpraURL(init?.url);
84
- this.headers = init?.headers instanceof Headers ? init.headers : new Headers(init?.headers);
66
+ constructor(serviceUrl, options) {
67
+ super(options);
68
+ this.serviceUrl = serviceUrl;
85
69
  }
86
70
  };
87
71
 
88
- // ../../build/client/esm/impl/http-request-observable.js
89
- var directCopyProperties = [
90
- "cache",
91
- "credentials",
92
- "destination",
93
- "headers",
94
- "integrity",
95
- "keepalive",
96
- "mode",
97
- "redirect",
98
- "referrer",
99
- "referrerPolicy"
100
- ];
101
- var kIntlObservable = Symbol.for("kIntlObservable");
102
- var HttpRequestObservable = class extends Observable {
72
+ // ../../build/client/esm/http/http-response.js
73
+ var HttpResponse = class _HttpResponse {
103
74
  static {
104
- __name(this, "HttpRequestObservable");
105
- }
106
- constructor(client, init) {
107
- super((subscriber) => {
108
- this[kIntlObservable].subscribe((event) => {
109
- if (event.event === HttpEventType.Response) {
110
- subscriber.next(event.response.body);
111
- subscriber.complete();
112
- }
113
- }, (error) => subscriber.error(error), () => subscriber.complete());
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
- });
124
- Object.defineProperty(this, kClient, {
125
- enumerable: false,
126
- value: client
127
- });
128
- this.request = new HttpRequest(init);
129
- if (init?.headers)
130
- this.header(init.headers);
131
- this[kIntlObservable] = this._send();
132
- }
133
- httpOptions(options) {
134
- directCopyProperties.forEach((k) => {
135
- if (options[k] !== void 0)
136
- this.request[k] = options[k];
137
- });
138
- return this;
139
- }
140
- header(arg0, value) {
141
- const headers = this.request.headers;
142
- if (typeof arg0 === "object") {
143
- const h = arg0 instanceof Headers ? arg0 : new Headers(arg0);
144
- h.forEach((v, k) => {
145
- if (k.toLowerCase() === "set-cookie") {
146
- headers.append(k, v);
147
- } else
148
- headers.set(k, v);
149
- });
150
- return this;
151
- }
152
- if (value == null)
153
- headers.delete(arg0);
154
- else
155
- headers.append(arg0, String(value));
156
- return this;
157
- }
158
- param(arg0, value) {
159
- const params = this.request.url.searchParams;
160
- if (typeof arg0 === "object") {
161
- const h = arg0 instanceof URLSearchParams ? arg0 : new URLSearchParams(arg0);
162
- h.forEach((v, k) => params.set(k, v));
163
- return this;
164
- }
165
- if (value == null)
166
- params.delete(arg0);
167
- else
168
- params.set(arg0, String(value));
169
- return this;
75
+ __name(this, "HttpResponse");
170
76
  }
171
- observe(observe) {
172
- observe = observe || HttpObserveType.Body;
173
- return new Observable((subscriber) => {
174
- this[kIntlObservable].subscribe((event) => {
175
- if (observe === HttpObserveType.Events) {
176
- subscriber.next(event);
177
- return;
178
- }
179
- if (observe === HttpObserveType.ResponseHeader && event.event === HttpEventType.ResponseHeader) {
180
- subscriber.next(event.response);
181
- subscriber.complete();
182
- return;
183
- }
184
- if (event.event === HttpEventType.Response) {
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);
202
- subscriber.complete();
203
- }
204
- }, (error) => subscriber.error(error), () => subscriber.complete());
205
- });
77
+ constructor(init) {
78
+ this.hasBody = false;
79
+ this.headers = init?.headers instanceof Headers ? init?.headers : new Headers(init?.headers);
80
+ this.status = init?.status || 200;
81
+ this.statusText = init?.statusText || "OK";
82
+ this.url = init?.url || null;
83
+ this.ok = this.status >= 200 && this.status < 300;
84
+ this.body = init?.body;
85
+ this.hasBody = init?.body != null || !!init?.hasBody;
86
+ this.contentType = (this.headers.get("content-type") || "").split(";")[0];
206
87
  }
207
- toPromise() {
208
- return this.getBody();
88
+ clone(update) {
89
+ return new _HttpResponse({ ...this, ...update });
209
90
  }
210
- getBody() {
211
- return lastValueFrom(this.observe(HttpObserveType.Body));
91
+ };
92
+
93
+ // ../../build/client/esm/http/interfaces/http-event.js
94
+ var HttpEventType;
95
+ (function(HttpEventType2) {
96
+ HttpEventType2["Sent"] = "Sent";
97
+ HttpEventType2["UploadProgress"] = "UploadProgress";
98
+ HttpEventType2["ResponseHeader"] = "ResponseHeader";
99
+ HttpEventType2["DownloadProgress"] = "DownloadProgress";
100
+ HttpEventType2["Response"] = "Response";
101
+ HttpEventType2["User"] = "User";
102
+ })(HttpEventType || (HttpEventType = {}));
103
+
104
+ // ../../build/client/esm/http/fetch-backend.js
105
+ var FetchBackend = class extends HttpBackend {
106
+ static {
107
+ __name(this, "FetchBackend");
212
108
  }
213
- getResponse() {
214
- return lastValueFrom(this.observe(HttpObserveType.Response));
109
+ constructor(serviceUrl, options) {
110
+ super(serviceUrl, options);
111
+ this.interceptors = Array.from(/* @__PURE__ */ new Set([...options?.interceptors || []]));
112
+ this.defaults = {
113
+ ...options?.defaults,
114
+ headers: options?.defaults?.headers instanceof Headers ? options?.defaults?.headers : new Headers(options?.defaults?.headers),
115
+ params: options?.defaults?.params instanceof URLSearchParams ? options?.defaults?.params : new URLSearchParams(options?.defaults?.params)
116
+ };
215
117
  }
216
- _send() {
217
- const request = this.request;
218
- const clientContext = this[kClient][kContext];
118
+ handle(init) {
219
119
  return new Observable((subscriber) => {
220
120
  (async () => {
221
- this._prepareRequest();
222
- for (const interceptor of clientContext.requestInterceptors) {
223
- await interceptor(request);
121
+ let request = this.prepareRequest(init);
122
+ if (request.body && init.reportProgress) {
123
+ const stream = request.body;
124
+ const contentLength = request.headers.get("content-length") || "0";
125
+ const total = parseInt(contentLength, 10) || 0;
126
+ let loaded = 0;
127
+ const progressTrackingStream = new TransformStream({
128
+ transform(chunk, controller) {
129
+ controller.enqueue(chunk);
130
+ loaded += chunk.byteLength;
131
+ subscriber.next({
132
+ type: HttpEventType.UploadProgress,
133
+ request,
134
+ total,
135
+ loaded
136
+ });
137
+ }
138
+ });
139
+ request = new Request(request.url, {
140
+ cache: request.cache,
141
+ credentials: request.credentials,
142
+ headers: request.headers,
143
+ integrity: request.integrity,
144
+ keepalive: request.keepalive,
145
+ method: request.method,
146
+ mode: request.mode,
147
+ redirect: request.redirect,
148
+ referrer: request.referrer,
149
+ referrerPolicy: request.referrerPolicy,
150
+ signal: request.signal,
151
+ body: stream.pipeThrough(progressTrackingStream),
152
+ window: init.window,
153
+ ...{
154
+ // undici library requires
155
+ duplex: "half"
156
+ }
157
+ });
224
158
  }
159
+ const fetchPromise = this.send(request);
225
160
  subscriber.next({
226
161
  request,
227
- event: HttpEventType.Sent
162
+ type: HttpEventType.Sent
228
163
  });
229
- const url = new OpraURL2(request.url, clientContext.serviceUrl);
230
- const fetchResponse = await clientContext.fetch(url.toString(), request);
231
- const contentType = (fetchResponse.headers.get("content-type") || "").split(";")[0];
232
- const headersResponse = clientContext.createResponse({
164
+ const fetchResponse = await fetchPromise;
165
+ const headersResponse = this.createResponse({
233
166
  url: fetchResponse.url,
234
167
  headers: fetchResponse.headers,
235
168
  status: fetchResponse.status,
236
169
  statusText: fetchResponse.statusText,
237
- hasBody: !!fetchResponse.body,
238
- contentType
170
+ hasBody: !!fetchResponse.body
239
171
  });
240
172
  subscriber.next({
241
173
  request,
242
- event: HttpEventType.ResponseHeader,
174
+ type: HttpEventType.ResponseHeader,
243
175
  response: headersResponse
244
176
  });
245
- const body = fetchResponse.body ? await this._parseBody(fetchResponse) : void 0;
246
- const responseInit = {
177
+ let body;
178
+ if (fetchResponse.body) {
179
+ if (init.reportProgress) {
180
+ const fetchBody = fetchResponse.body;
181
+ const contentLength = fetchResponse.headers.get("content-length") || "0";
182
+ const total = parseInt(contentLength, 10) || 0;
183
+ let loaded = 0;
184
+ const res = new Response(new ReadableStream({
185
+ async start(controller) {
186
+ const reader = fetchBody.getReader();
187
+ for (; ; ) {
188
+ const { done, value } = await reader.read();
189
+ if (done)
190
+ break;
191
+ loaded += value.byteLength;
192
+ controller.enqueue(value);
193
+ subscriber.next({
194
+ type: HttpEventType.DownloadProgress,
195
+ request,
196
+ total,
197
+ loaded
198
+ });
199
+ }
200
+ controller.close();
201
+ }
202
+ }));
203
+ body = await this.parseBody(res);
204
+ } else {
205
+ body = await this.parseBody(fetchResponse);
206
+ }
207
+ }
208
+ const response = this.createResponse({
247
209
  url: fetchResponse.url,
248
210
  headers: fetchResponse.headers,
249
211
  status: fetchResponse.status,
250
212
  statusText: fetchResponse.statusText,
251
- contentType,
252
213
  body
253
- };
254
- const response = clientContext.createResponse(responseInit);
255
- for (const interceptor of clientContext.responseInterceptors) {
256
- await interceptor(response, request);
257
- }
214
+ });
258
215
  subscriber.next({
259
216
  request,
260
- event: HttpEventType.Response,
217
+ type: HttpEventType.Response,
261
218
  response
262
219
  });
263
220
  subscriber.complete();
264
221
  })().catch((error) => subscriber.error(error));
265
222
  });
266
223
  }
267
- _prepareRequest() {
268
- const request = this.request;
269
- if (request.body) {
270
- let body;
224
+ send(request) {
225
+ return fetch(request);
226
+ }
227
+ prepareRequest(init) {
228
+ const headers = init.headers || new Headers();
229
+ const requestInit = {
230
+ ...init,
231
+ headers
232
+ };
233
+ const body = requestInit.body;
234
+ if (body) {
271
235
  let contentType;
272
- if (typeof request.body === "string" || typeof request.body === "number" || typeof request.body === "boolean") {
273
- contentType = 'text/plain;charset=UTF-8"';
274
- body = String(request.body);
275
- request.headers.delete("Content-Size");
276
- delete request.duplex;
277
- } else if (isReadableStreamLike(request.body)) {
236
+ if (typeof body === "string" || typeof body === "number" || typeof body === "boolean") {
237
+ contentType = 'text/plain; charset="UTF-8"';
238
+ requestInit.body = new Blob([String(body)], { type: contentType });
239
+ headers.set("Content-Length", String(requestInit.body.size));
240
+ delete requestInit.duplex;
241
+ } else if (isReadableStreamLike(body)) {
278
242
  contentType = "application/octet-stream";
279
- body = request.body;
280
- request.duplex = "half";
281
- } else if (Buffer.isBuffer(request.body)) {
243
+ requestInit.duplex = "half";
244
+ } else if (Buffer.isBuffer(body)) {
282
245
  contentType = "application/octet-stream";
283
- body = request.body;
284
- request.headers.set("Content-Size", String(request.body.length));
285
- delete request.duplex;
286
- } else if (isBlob(request.body)) {
287
- contentType = request.body.type || "application/octet-stream";
288
- body = request.body;
289
- request.headers.set("Content-Size", String(request.body.size));
290
- delete request.duplex;
246
+ headers.set("Content-Length", String(body.length));
247
+ delete requestInit.duplex;
248
+ } else if (isBlob(body)) {
249
+ contentType = body.type || "application/octet-stream";
250
+ headers.set("Content-Length", String(body.size));
251
+ delete requestInit.duplex;
291
252
  } else {
292
- contentType = "application/json";
293
- body = JSON.stringify(request.body);
294
- request.headers.delete("Content-Size");
295
- delete request.duplex;
253
+ contentType = 'application/json;charset="UTF-8"';
254
+ requestInit.body = new Blob([JSON.stringify(body)], { type: contentType });
255
+ headers.set("Content-Length", String(requestInit.body.size));
256
+ delete requestInit.duplex;
296
257
  }
297
- if (!request.headers.has("Content-Type") && contentType)
298
- request.headers.set("Content-Type", contentType);
299
- request.body = body;
258
+ if (contentType && !headers.has("Content-Type"))
259
+ headers.set("Content-Type", contentType);
300
260
  }
261
+ return new Request(requestInit.url.toString(), requestInit);
301
262
  }
302
- async _parseBody(fetchResponse) {
263
+ createResponse(init) {
264
+ return new HttpResponse(init);
265
+ }
266
+ async parseBody(fetchResponse) {
303
267
  let body;
304
268
  const contentType = fetchResponse.headers.get("Content-Type") || "";
305
269
  if (typeIs.is(contentType, ["json", "application/*+json"])) {
@@ -319,17 +283,174 @@ var HttpRequestObservable = class extends Observable {
319
283
  }
320
284
  };
321
285
 
322
- // ../../build/client/esm/impl/collection-node.js
286
+ // ../../build/client/esm/http/http-client-base.js
287
+ import { ApiDocumentFactory } from "@opra/common";
288
+
289
+ // ../../build/client/esm/http/http-collection-node.js
290
+ import { toArrayDef } from "putil-varhelpers";
291
+ import { OpraURL as OpraURL2 } from "@opra/common";
292
+
293
+ // ../../build/client/esm/http/http-request-observable.js
294
+ import { lastValueFrom, Observable as Observable2 } from "rxjs";
295
+ import typeIs2 from "@browsery/type-is";
296
+ import { OpraURL } from "@opra/common";
297
+
298
+ // ../../build/client/esm/http/enums/http-observable-type.enum.js
299
+ var HttpObserveType;
300
+ (function(HttpObserveType2) {
301
+ HttpObserveType2["ResponseHeader"] = "response-header";
302
+ HttpObserveType2["Response"] = "response";
303
+ HttpObserveType2["Body"] = "body";
304
+ HttpObserveType2["Events"] = "events";
305
+ })(HttpObserveType || (HttpObserveType = {}));
306
+
307
+ // ../../build/client/esm/http/http-interceptor-handler.js
308
+ var HttpInterceptorHandler = class {
309
+ static {
310
+ __name(this, "HttpInterceptorHandler");
311
+ }
312
+ constructor(interceptors, finalHandler) {
313
+ this.chain = interceptors.reduceRight((chainTailFn, interceptor) => (initialRequest, handler) => interceptor.intercept(initialRequest, {
314
+ handle: (downstreamRequest) => chainTailFn(downstreamRequest, handler)
315
+ }), chainEnd);
316
+ this.finalHandler = finalHandler;
317
+ }
318
+ handle(initialRequest) {
319
+ return this.chain(initialRequest, (req) => this.finalHandler.handle(req));
320
+ }
321
+ };
322
+ function chainEnd(req, handler) {
323
+ return handler(req);
324
+ }
325
+ __name(chainEnd, "chainEnd");
326
+
327
+ // ../../build/client/esm/http/http-request-observable.js
328
+ var HttpRequestObservable = class _HttpRequestObservable extends Observable2 {
329
+ static {
330
+ __name(this, "HttpRequestObservable");
331
+ }
332
+ constructor(backend, init) {
333
+ super((subscriber) => {
334
+ const observe = this[kContext].observe;
335
+ new HttpInterceptorHandler(backend.interceptors || [], this[kBackend]).handle(this[kContext]).subscribe({
336
+ next(event) {
337
+ if (observe === HttpObserveType.Events) {
338
+ subscriber.next(event);
339
+ return;
340
+ }
341
+ if (observe === HttpObserveType.ResponseHeader && event.type === HttpEventType.ResponseHeader) {
342
+ subscriber.next(event.response);
343
+ subscriber.complete();
344
+ return;
345
+ }
346
+ if (event.type === HttpEventType.Response) {
347
+ const { response } = event;
348
+ if (observe === HttpObserveType.Response) {
349
+ subscriber.next(response);
350
+ subscriber.complete();
351
+ return;
352
+ }
353
+ const isOpraResponse = typeIs2.is(event.response.contentType || "", ["application/opra+json"]);
354
+ if (response.status >= 400 && response.status < 600) {
355
+ subscriber.error(new ClientError({
356
+ message: response.status + " " + response.statusText,
357
+ status: response.status,
358
+ issues: isOpraResponse ? response.body.errors : void 0
359
+ }));
360
+ subscriber.complete();
361
+ return;
362
+ }
363
+ subscriber.next(event.response.body);
364
+ subscriber.complete();
365
+ }
366
+ },
367
+ error(error) {
368
+ subscriber.error(error);
369
+ },
370
+ complete() {
371
+ subscriber.complete();
372
+ }
373
+ });
374
+ });
375
+ Object.defineProperty(this, kBackend, {
376
+ enumerable: false,
377
+ value: backend
378
+ });
379
+ const url = new OpraURL(init?.url, backend.serviceUrl.toString());
380
+ Object.defineProperty(this, kContext, {
381
+ enumerable: false,
382
+ value: {
383
+ ...init,
384
+ observe: HttpObserveType.Body,
385
+ headers: new Headers(init?.headers),
386
+ url
387
+ }
388
+ });
389
+ }
390
+ clone() {
391
+ return new _HttpRequestObservable(this[kBackend], this[kContext]);
392
+ }
393
+ options(options) {
394
+ Object.assign(this[kContext], options);
395
+ return this;
396
+ }
397
+ header(arg0, value) {
398
+ const target = this[kContext].headers;
399
+ if (typeof arg0 === "object") {
400
+ const h = arg0 instanceof Headers ? arg0 : new Headers(arg0);
401
+ h.forEach((v, k) => {
402
+ if (k.toLowerCase() === "set-cookie") {
403
+ target.append(k, v);
404
+ } else
405
+ target.set(k, v);
406
+ });
407
+ return this;
408
+ }
409
+ if (value == null || value === "")
410
+ target.delete(arg0);
411
+ else
412
+ target.append(arg0, String(value));
413
+ return this;
414
+ }
415
+ param(arg0, value) {
416
+ const target = this[kContext].url.searchParams;
417
+ if (typeof arg0 === "object") {
418
+ const h = arg0 instanceof URLSearchParams ? arg0 : new URLSearchParams(arg0);
419
+ h.forEach((v, k) => target.set(k, v));
420
+ return this;
421
+ }
422
+ if (value == null)
423
+ target.delete(arg0);
424
+ else
425
+ target.set(arg0, String(value));
426
+ return this;
427
+ }
428
+ observe(observe) {
429
+ if (observe === this[kContext].observe)
430
+ return this;
431
+ const cloned = this.clone();
432
+ cloned[kContext].observe = observe || HttpObserveType.Body;
433
+ return cloned;
434
+ }
435
+ getBody() {
436
+ return lastValueFrom(this.observe(HttpObserveType.Body));
437
+ }
438
+ getResponse() {
439
+ return lastValueFrom(this.observe(HttpObserveType.Response));
440
+ }
441
+ };
442
+
443
+ // ../../build/client/esm/http/http-collection-node.js
323
444
  var HttpCollectionNode = class {
324
445
  static {
325
446
  __name(this, "HttpCollectionNode");
326
447
  }
327
- constructor(client, path) {
328
- this._client = client;
448
+ constructor(backend, path) {
449
+ this._backend = backend;
329
450
  this._path = path;
330
451
  }
331
452
  create(data, options) {
332
- const observable = new HttpRequestObservable(this._client, {
453
+ const observable = new HttpRequestObservable(this._backend, {
333
454
  method: "POST",
334
455
  url: this._path,
335
456
  body: data
@@ -345,15 +466,15 @@ var HttpCollectionNode = class {
345
466
  delete(id) {
346
467
  if (id == null || id === "")
347
468
  throw new TypeError(`'id' argument must have a value`);
348
- const url = new OpraURL3();
469
+ const url = new OpraURL2();
349
470
  url.join({ resource: this._path, key: id });
350
- return new HttpRequestObservable(this._client, {
471
+ return new HttpRequestObservable(this._backend, {
351
472
  method: "DELETE",
352
473
  url
353
474
  });
354
475
  }
355
476
  deleteMany(options) {
356
- const observable = new HttpRequestObservable(this._client, {
477
+ const observable = new HttpRequestObservable(this._backend, {
357
478
  method: "DELETE",
358
479
  url: this._path
359
480
  });
@@ -364,9 +485,9 @@ var HttpCollectionNode = class {
364
485
  get(id, options) {
365
486
  if (id == null || id === "")
366
487
  throw new TypeError(`'id' argument must have a value`);
367
- const url = new OpraURL3();
488
+ const url = new OpraURL2();
368
489
  url.join({ resource: this._path, key: id });
369
- const observable = new HttpRequestObservable(this._client, {
490
+ const observable = new HttpRequestObservable(this._backend, {
370
491
  method: "GET",
371
492
  url
372
493
  });
@@ -379,7 +500,7 @@ var HttpCollectionNode = class {
379
500
  return observable;
380
501
  }
381
502
  findMany(options) {
382
- const observable = new HttpRequestObservable(this._client, {
503
+ const observable = new HttpRequestObservable(this._backend, {
383
504
  method: "GET",
384
505
  url: this._path
385
506
  });
@@ -406,9 +527,9 @@ var HttpCollectionNode = class {
406
527
  update(id, data, options) {
407
528
  if (id == null)
408
529
  throw new TypeError(`'id' argument must have a value`);
409
- const url = new OpraURL3();
530
+ const url = new OpraURL2();
410
531
  url.join({ resource: this._path, key: id });
411
- const observable = new HttpRequestObservable(this._client, {
532
+ const observable = new HttpRequestObservable(this._backend, {
412
533
  method: "PATCH",
413
534
  url,
414
535
  body: data
@@ -422,7 +543,7 @@ var HttpCollectionNode = class {
422
543
  return observable;
423
544
  }
424
545
  updateMany(data, options) {
425
- const observable = new HttpRequestObservable(this._client, {
546
+ const observable = new HttpRequestObservable(this._backend, {
426
547
  method: "PATCH",
427
548
  url: this._path,
428
549
  body: data
@@ -433,38 +554,18 @@ var HttpCollectionNode = class {
433
554
  }
434
555
  };
435
556
 
436
- // ../../build/client/esm/impl/http-response.js
437
- var HttpResponse = class _HttpResponse {
438
- static {
439
- __name(this, "HttpResponse");
440
- }
441
- constructor(init) {
442
- this.hasBody = false;
443
- this.headers = init?.headers instanceof Headers ? init?.headers : new Headers(init?.headers);
444
- this.status = init?.status || 200;
445
- this.statusText = init?.statusText || "OK";
446
- this.url = init?.url || null;
447
- this.ok = this.status >= 200 && this.status < 300;
448
- this.body = init?.body;
449
- this.hasBody = init?.body != null || !!init?.hasBody;
450
- }
451
- clone(update) {
452
- return new _HttpResponse({ ...this, ...update });
453
- }
454
- };
455
-
456
- // ../../build/client/esm/impl/singleton-node.js
557
+ // ../../build/client/esm/http/http-singleton-node.js
457
558
  import { toArrayDef as toArrayDef2 } from "putil-varhelpers";
458
559
  var HttpSingletonNode = class {
459
560
  static {
460
561
  __name(this, "HttpSingletonNode");
461
562
  }
462
- constructor(client, path) {
463
- this._client = client;
563
+ constructor(backend, path) {
564
+ this._backend = backend;
464
565
  this._path = path;
465
566
  }
466
567
  create(data, options) {
467
- const observable = new HttpRequestObservable(this._client, {
568
+ const observable = new HttpRequestObservable(this._backend, {
468
569
  method: "POST",
469
570
  url: this._path,
470
571
  body: data
@@ -478,13 +579,13 @@ var HttpSingletonNode = class {
478
579
  return observable;
479
580
  }
480
581
  delete() {
481
- return new HttpRequestObservable(this._client, {
582
+ return new HttpRequestObservable(this._backend, {
482
583
  method: "DELETE",
483
584
  url: this._path
484
585
  });
485
586
  }
486
587
  get(options) {
487
- const observable = new HttpRequestObservable(this._client, {
588
+ const observable = new HttpRequestObservable(this._backend, {
488
589
  method: "GET",
489
590
  url: this._path
490
591
  });
@@ -497,7 +598,7 @@ var HttpSingletonNode = class {
497
598
  return observable;
498
599
  }
499
600
  update(data, options) {
500
- const observable = new HttpRequestObservable(this._client, {
601
+ const observable = new HttpRequestObservable(this._backend, {
501
602
  method: "PATCH",
502
603
  url: this._path,
503
604
  body: data
@@ -512,54 +613,32 @@ var HttpSingletonNode = class {
512
613
  }
513
614
  };
514
615
 
515
- // ../../build/client/esm/client.js
516
- var OpraHttpClient = class {
616
+ // ../../build/client/esm/http/http-client-base.js
617
+ var HttpClientBase = class extends ClientBase {
517
618
  static {
518
- __name(this, "OpraHttpClient");
619
+ __name(this, "HttpClientBase");
519
620
  }
520
- constructor(serviceUrl, options) {
521
- const context = {
522
- serviceUrl,
523
- requestInterceptors: [...options?.requestInterceptors || []],
524
- responseInterceptors: [...options?.responseInterceptors || []],
525
- api: options?.api,
526
- defaults: {
527
- ...options?.defaults,
528
- headers: options?.defaults?.headers instanceof Headers ? options?.defaults?.headers : new Headers(options?.defaults?.headers),
529
- params: options?.defaults?.params instanceof URLSearchParams ? options?.defaults?.params : new URLSearchParams(options?.defaults?.params)
530
- },
531
- fetch,
532
- createResponse: (init) => new HttpResponse(init)
533
- };
534
- Object.defineProperty(this, kContext, {
535
- enumerable: false,
536
- value: context
537
- });
621
+ constructor(backend) {
622
+ super(backend);
538
623
  }
539
624
  get serviceUrl() {
540
- return this[kContext].serviceUrl;
541
- }
542
- get api() {
543
- return this[kContext].api;
544
- }
545
- get defaults() {
546
- return this[kContext].defaults;
625
+ return this[kBackend].serviceUrl;
547
626
  }
548
627
  async getMetadata() {
549
628
  let promise = this._metadataPromise;
550
629
  if (promise)
551
630
  return promise;
552
- const controller = new HttpRequestObservable(this, {
631
+ const request = new HttpRequestObservable(this[kBackend], {
553
632
  method: "GET",
554
- url: "",
555
- headers: { "accept": "application/json" }
633
+ url: "/",
634
+ headers: new Headers({ "accept": "application/json" })
556
635
  });
557
- this._metadataPromise = promise = controller.getBody();
636
+ this._metadataPromise = promise = request.getBody();
558
637
  return await promise.then(async (body) => {
559
638
  if (!body)
560
639
  throw new Error(`No response returned.`);
561
640
  const api = await ApiDocumentFactory.createDocument(body);
562
- this[kContext].api = api;
641
+ this[kBackend].api = api;
563
642
  return api;
564
643
  }).catch((e) => {
565
644
  e.message = "Unable to fetch metadata from service url (" + this.serviceUrl + "). " + e.message;
@@ -567,13 +646,13 @@ var OpraHttpClient = class {
567
646
  }).finally(() => delete this._metadataPromise);
568
647
  }
569
648
  collection(path) {
570
- return new HttpCollectionNode(this, path);
649
+ return new HttpCollectionNode(this[kBackend], path);
571
650
  }
572
651
  singleton(path) {
573
- return new HttpSingletonNode(this, path);
652
+ return new HttpSingletonNode(this[kBackend], path);
574
653
  }
575
654
  action(path, params) {
576
- const observable = new HttpRequestObservable(this, {
655
+ const observable = new HttpRequestObservable(this[kBackend], {
577
656
  method: "GET",
578
657
  url: path
579
658
  });
@@ -585,7 +664,20 @@ var OpraHttpClient = class {
585
664
  }
586
665
  };
587
666
 
588
- // ../../build/client/esm/impl/http-service-base.js
667
+ // ../../build/client/esm/http/http-client.js
668
+ var OpraHttpClient = class extends HttpClientBase {
669
+ static {
670
+ __name(this, "OpraHttpClient");
671
+ }
672
+ constructor(serviceUrl, options) {
673
+ super(new FetchBackend(serviceUrl, options));
674
+ }
675
+ get defaults() {
676
+ return this[kBackend].defaults;
677
+ }
678
+ };
679
+
680
+ // ../../build/client/esm/http/http-service-base.js
589
681
  var _a;
590
682
  var HttpServiceBase = class {
591
683
  static {
@@ -593,7 +685,7 @@ var HttpServiceBase = class {
593
685
  }
594
686
  constructor(client) {
595
687
  this[_a] = {
596
- resource: {},
688
+ resources: {},
597
689
  node: null
598
690
  };
599
691
  this[kClient] = client;
@@ -601,16 +693,21 @@ var HttpServiceBase = class {
601
693
  };
602
694
  _a = kContext;
603
695
  export {
696
+ Backend,
697
+ ClientBase,
604
698
  ClientError,
699
+ FetchBackend,
700
+ HttpBackend,
701
+ HttpClientBase,
605
702
  HttpCollectionNode,
606
703
  HttpEventType,
607
704
  HttpObserveType,
608
- HttpRequest,
609
705
  HttpRequestObservable,
610
706
  HttpResponse,
611
707
  HttpServiceBase,
612
708
  HttpSingletonNode,
613
709
  OpraHttpClient,
710
+ kBackend,
614
711
  kClient,
615
712
  kContext
616
713
  };