@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/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 "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,257 +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.mode = init?.mode || "cors";
80
- this.referrer = init?.referrer || "";
81
- this.referrerPolicy = init?.referrerPolicy || "";
82
- this.signal = init?.signal || new AbortController().signal;
83
- this.body = init?.body;
84
- this.url = init?.url instanceof OpraURL ? init.url : new OpraURL(init?.url);
85
- this.headers = init?.headers instanceof Headers ? init.headers : new Headers(init?.headers);
66
+ constructor(serviceUrl, options) {
67
+ super(options);
68
+ this.serviceUrl = serviceUrl;
86
69
  }
87
70
  };
88
71
 
89
- // ../../build/client/esm/impl/http-request-observable.js
90
- var directCopyProperties = [
91
- "cache",
92
- "credentials",
93
- "destination",
94
- "headers",
95
- "integrity",
96
- "keepalive",
97
- "mode",
98
- "redirect",
99
- "referrer",
100
- "referrerPolicy"
101
- ];
102
- var kIntlObservable = Symbol.for("kIntlObservable");
103
- var HttpRequestObservable = class extends Observable {
72
+ // ../../build/client/esm/http/http-response.js
73
+ var HttpResponse = class _HttpResponse {
104
74
  static {
105
- __name(this, "HttpRequestObservable");
106
- }
107
- constructor(client, init) {
108
- super((subscriber) => {
109
- this[kIntlObservable].subscribe((event) => {
110
- if (event.event === HttpEventType.Response) {
111
- subscriber.next(event.response.body);
112
- subscriber.complete();
113
- }
114
- }, (error) => subscriber.error(error), () => subscriber.complete());
115
- });
116
- Object.defineProperty(this, kClient, {
117
- enumerable: false,
118
- value: client
119
- });
120
- this.request = new HttpRequest(init);
121
- if (init?.headers)
122
- this.header(init.headers);
123
- this[kIntlObservable] = this._send();
124
- }
125
- httpOptions(options) {
126
- directCopyProperties.forEach((k) => {
127
- if (options[k] !== void 0)
128
- this.request[k] = options[k];
129
- });
130
- return this;
131
- }
132
- header(arg0, value) {
133
- const headers = this.request.headers;
134
- if (typeof arg0 === "object") {
135
- const h = arg0 instanceof Headers ? arg0 : new Headers(arg0);
136
- h.forEach((v, k) => {
137
- if (k.toLowerCase() === "set-cookie") {
138
- headers.append(k, v);
139
- } else
140
- headers.set(k, v);
141
- });
142
- return this;
143
- }
144
- if (value == null)
145
- headers.delete(arg0);
146
- else
147
- headers.append(arg0, String(value));
148
- return this;
149
- }
150
- param(arg0, value) {
151
- const params = this.request.url.searchParams;
152
- if (typeof arg0 === "object") {
153
- const h = arg0 instanceof URLSearchParams ? arg0 : new URLSearchParams(arg0);
154
- h.forEach((v, k) => params.set(k, v));
155
- return this;
156
- }
157
- if (value == null)
158
- params.delete(arg0);
159
- else
160
- params.set(arg0, String(value));
161
- return this;
75
+ __name(this, "HttpResponse");
162
76
  }
163
- observe(observe) {
164
- observe = observe || HttpObserveType.Body;
165
- return new Observable((subscriber) => {
166
- this[kIntlObservable].subscribe((event) => {
167
- if (observe === HttpObserveType.Events) {
168
- subscriber.next(event);
169
- return;
170
- }
171
- if (observe === HttpObserveType.ResponseHeader && event.event === HttpEventType.ResponseHeader) {
172
- subscriber.next(event.response);
173
- subscriber.complete();
174
- return;
175
- }
176
- if (event.event === HttpEventType.Response) {
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);
194
- subscriber.complete();
195
- }
196
- }, (error) => subscriber.error(error), () => subscriber.complete());
197
- });
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];
198
87
  }
199
- toPromise() {
200
- return this.getBody();
88
+ clone(update) {
89
+ return new _HttpResponse({ ...this, ...update });
201
90
  }
202
- getBody() {
203
- 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");
204
108
  }
205
- getResponse() {
206
- 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
+ };
207
117
  }
208
- _send() {
209
- const request = this.request;
210
- const clientContext = this[kClient][kContext];
118
+ handle(init) {
211
119
  return new Observable((subscriber) => {
212
120
  (async () => {
213
- this._prepareRequest();
214
- for (const interceptor of clientContext.requestInterceptors) {
215
- 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
+ });
216
158
  }
159
+ const fetchPromise = this.send(request);
217
160
  subscriber.next({
218
161
  request,
219
- event: HttpEventType.Sent
162
+ type: HttpEventType.Sent
220
163
  });
221
- const url = new OpraURL2(request.url, clientContext.serviceUrl);
222
- const fetchResponse = await clientContext.fetch(url.toString(), request);
223
- const contentType = (fetchResponse.headers.get("content-type") || "").split(";")[0];
224
- const headersResponse = clientContext.createResponse({
164
+ const fetchResponse = await fetchPromise;
165
+ const headersResponse = this.createResponse({
225
166
  url: fetchResponse.url,
226
167
  headers: fetchResponse.headers,
227
168
  status: fetchResponse.status,
228
169
  statusText: fetchResponse.statusText,
229
- hasBody: !!fetchResponse.body,
230
- contentType
170
+ hasBody: !!fetchResponse.body
231
171
  });
232
172
  subscriber.next({
233
173
  request,
234
- event: HttpEventType.ResponseHeader,
174
+ type: HttpEventType.ResponseHeader,
235
175
  response: headersResponse
236
176
  });
237
- const body = fetchResponse.body ? await this._parseBody(fetchResponse) : void 0;
238
- 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({
239
209
  url: fetchResponse.url,
240
210
  headers: fetchResponse.headers,
241
211
  status: fetchResponse.status,
242
212
  statusText: fetchResponse.statusText,
243
- contentType,
244
213
  body
245
- };
246
- const response = clientContext.createResponse(responseInit);
247
- for (const interceptor of clientContext.responseInterceptors) {
248
- await interceptor(response, request);
249
- }
214
+ });
250
215
  subscriber.next({
251
216
  request,
252
- event: HttpEventType.Response,
217
+ type: HttpEventType.Response,
253
218
  response
254
219
  });
255
220
  subscriber.complete();
256
221
  })().catch((error) => subscriber.error(error));
257
222
  });
258
223
  }
259
- _prepareRequest() {
260
- const request = this.request;
261
- if (request.body) {
262
- 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) {
263
235
  let contentType;
264
- if (typeof request.body === "string" || typeof request.body === "number" || typeof request.body === "boolean") {
265
- contentType = 'text/plain;charset=UTF-8"';
266
- body = String(request.body);
267
- request.headers.delete("Content-Size");
268
- delete request.duplex;
269
- } 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)) {
270
242
  contentType = "application/octet-stream";
271
- body = request.body;
272
- request.duplex = "half";
273
- } else if (Buffer.isBuffer(request.body)) {
243
+ requestInit.duplex = "half";
244
+ } else if (Buffer.isBuffer(body)) {
274
245
  contentType = "application/octet-stream";
275
- body = request.body;
276
- request.headers.set("Content-Size", String(request.body.length));
277
- delete request.duplex;
278
- } else if (isBlob(request.body)) {
279
- contentType = request.body.type || "application/octet-stream";
280
- body = request.body;
281
- request.headers.set("Content-Size", String(request.body.length));
282
- 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;
283
252
  } else {
284
- contentType = "application/json";
285
- body = JSON.stringify(request.body);
286
- request.headers.delete("Content-Size");
287
- 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;
288
257
  }
289
- if (!request.headers.has("Content-Type") && contentType)
290
- request.headers.set("Content-Type", contentType);
291
- request.body = body;
258
+ if (contentType && !headers.has("Content-Type"))
259
+ headers.set("Content-Type", contentType);
292
260
  }
261
+ return new Request(requestInit.url.toString(), requestInit);
293
262
  }
294
- async _parseBody(fetchResponse) {
263
+ createResponse(init) {
264
+ return new HttpResponse(init);
265
+ }
266
+ async parseBody(fetchResponse) {
295
267
  let body;
296
268
  const contentType = fetchResponse.headers.get("Content-Type") || "";
297
269
  if (typeIs.is(contentType, ["json", "application/*+json"])) {
@@ -311,17 +283,174 @@ var HttpRequestObservable = class extends Observable {
311
283
  }
312
284
  };
313
285
 
314
- // ../../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
315
444
  var HttpCollectionNode = class {
316
445
  static {
317
446
  __name(this, "HttpCollectionNode");
318
447
  }
319
- constructor(client, path) {
320
- this._client = client;
448
+ constructor(backend, path) {
449
+ this._backend = backend;
321
450
  this._path = path;
322
451
  }
323
452
  create(data, options) {
324
- const observable = new HttpRequestObservable(this._client, {
453
+ const observable = new HttpRequestObservable(this._backend, {
325
454
  method: "POST",
326
455
  url: this._path,
327
456
  body: data
@@ -337,15 +466,15 @@ var HttpCollectionNode = class {
337
466
  delete(id) {
338
467
  if (id == null || id === "")
339
468
  throw new TypeError(`'id' argument must have a value`);
340
- const url = new OpraURL3();
469
+ const url = new OpraURL2();
341
470
  url.join({ resource: this._path, key: id });
342
- return new HttpRequestObservable(this._client, {
471
+ return new HttpRequestObservable(this._backend, {
343
472
  method: "DELETE",
344
473
  url
345
474
  });
346
475
  }
347
476
  deleteMany(options) {
348
- const observable = new HttpRequestObservable(this._client, {
477
+ const observable = new HttpRequestObservable(this._backend, {
349
478
  method: "DELETE",
350
479
  url: this._path
351
480
  });
@@ -356,9 +485,9 @@ var HttpCollectionNode = class {
356
485
  get(id, options) {
357
486
  if (id == null || id === "")
358
487
  throw new TypeError(`'id' argument must have a value`);
359
- const url = new OpraURL3();
488
+ const url = new OpraURL2();
360
489
  url.join({ resource: this._path, key: id });
361
- const observable = new HttpRequestObservable(this._client, {
490
+ const observable = new HttpRequestObservable(this._backend, {
362
491
  method: "GET",
363
492
  url
364
493
  });
@@ -371,7 +500,7 @@ var HttpCollectionNode = class {
371
500
  return observable;
372
501
  }
373
502
  findMany(options) {
374
- const observable = new HttpRequestObservable(this._client, {
503
+ const observable = new HttpRequestObservable(this._backend, {
375
504
  method: "GET",
376
505
  url: this._path
377
506
  });
@@ -398,9 +527,9 @@ var HttpCollectionNode = class {
398
527
  update(id, data, options) {
399
528
  if (id == null)
400
529
  throw new TypeError(`'id' argument must have a value`);
401
- const url = new OpraURL3();
530
+ const url = new OpraURL2();
402
531
  url.join({ resource: this._path, key: id });
403
- const observable = new HttpRequestObservable(this._client, {
532
+ const observable = new HttpRequestObservable(this._backend, {
404
533
  method: "PATCH",
405
534
  url,
406
535
  body: data
@@ -414,7 +543,7 @@ var HttpCollectionNode = class {
414
543
  return observable;
415
544
  }
416
545
  updateMany(data, options) {
417
- const observable = new HttpRequestObservable(this._client, {
546
+ const observable = new HttpRequestObservable(this._backend, {
418
547
  method: "PATCH",
419
548
  url: this._path,
420
549
  body: data
@@ -425,38 +554,18 @@ var HttpCollectionNode = class {
425
554
  }
426
555
  };
427
556
 
428
- // ../../build/client/esm/impl/http-response.js
429
- var HttpResponse = class _HttpResponse {
430
- static {
431
- __name(this, "HttpResponse");
432
- }
433
- constructor(init) {
434
- this.hasBody = false;
435
- this.headers = init?.headers instanceof Headers ? init?.headers : new Headers(init?.headers);
436
- this.status = init?.status || 200;
437
- this.statusText = init?.statusText || "OK";
438
- this.url = init?.url || null;
439
- this.ok = this.status >= 200 && this.status < 300;
440
- this.body = init?.body;
441
- this.hasBody = init?.body != null || !!init?.hasBody;
442
- }
443
- clone(update) {
444
- return new _HttpResponse({ ...this, ...update });
445
- }
446
- };
447
-
448
- // ../../build/client/esm/impl/singleton-node.js
557
+ // ../../build/client/esm/http/http-singleton-node.js
449
558
  import { toArrayDef as toArrayDef2 } from "putil-varhelpers";
450
559
  var HttpSingletonNode = class {
451
560
  static {
452
561
  __name(this, "HttpSingletonNode");
453
562
  }
454
- constructor(client, path) {
455
- this._client = client;
563
+ constructor(backend, path) {
564
+ this._backend = backend;
456
565
  this._path = path;
457
566
  }
458
567
  create(data, options) {
459
- const observable = new HttpRequestObservable(this._client, {
568
+ const observable = new HttpRequestObservable(this._backend, {
460
569
  method: "POST",
461
570
  url: this._path,
462
571
  body: data
@@ -470,13 +579,13 @@ var HttpSingletonNode = class {
470
579
  return observable;
471
580
  }
472
581
  delete() {
473
- return new HttpRequestObservable(this._client, {
582
+ return new HttpRequestObservable(this._backend, {
474
583
  method: "DELETE",
475
584
  url: this._path
476
585
  });
477
586
  }
478
587
  get(options) {
479
- const observable = new HttpRequestObservable(this._client, {
588
+ const observable = new HttpRequestObservable(this._backend, {
480
589
  method: "GET",
481
590
  url: this._path
482
591
  });
@@ -489,7 +598,7 @@ var HttpSingletonNode = class {
489
598
  return observable;
490
599
  }
491
600
  update(data, options) {
492
- const observable = new HttpRequestObservable(this._client, {
601
+ const observable = new HttpRequestObservable(this._backend, {
493
602
  method: "PATCH",
494
603
  url: this._path,
495
604
  body: data
@@ -504,54 +613,32 @@ var HttpSingletonNode = class {
504
613
  }
505
614
  };
506
615
 
507
- // ../../build/client/esm/client.js
508
- var OpraHttpClient = class {
616
+ // ../../build/client/esm/http/http-client-base.js
617
+ var HttpClientBase = class extends ClientBase {
509
618
  static {
510
- __name(this, "OpraHttpClient");
619
+ __name(this, "HttpClientBase");
511
620
  }
512
- constructor(serviceUrl, options) {
513
- const context = {
514
- serviceUrl,
515
- requestInterceptors: [...options?.requestInterceptors || []],
516
- responseInterceptors: [...options?.responseInterceptors || []],
517
- api: options?.api,
518
- defaults: {
519
- ...options?.defaults,
520
- headers: options?.defaults?.headers instanceof Headers ? options?.defaults?.headers : new Headers(options?.defaults?.headers),
521
- params: options?.defaults?.params instanceof URLSearchParams ? options?.defaults?.params : new URLSearchParams(options?.defaults?.params)
522
- },
523
- fetch,
524
- createResponse: (init) => new HttpResponse(init)
525
- };
526
- Object.defineProperty(this, kContext, {
527
- enumerable: false,
528
- value: context
529
- });
621
+ constructor(backend) {
622
+ super(backend);
530
623
  }
531
624
  get serviceUrl() {
532
- return this[kContext].serviceUrl;
533
- }
534
- get api() {
535
- return this[kContext].api;
536
- }
537
- get defaults() {
538
- return this[kContext].defaults;
625
+ return this[kBackend].serviceUrl;
539
626
  }
540
627
  async getMetadata() {
541
628
  let promise = this._metadataPromise;
542
629
  if (promise)
543
630
  return promise;
544
- const controller = new HttpRequestObservable(this, {
631
+ const request = new HttpRequestObservable(this[kBackend], {
545
632
  method: "GET",
546
- url: "",
547
- headers: { "accept": "application/json" }
633
+ url: "/",
634
+ headers: new Headers({ "accept": "application/json" })
548
635
  });
549
- this._metadataPromise = promise = controller.getBody();
636
+ this._metadataPromise = promise = request.getBody();
550
637
  return await promise.then(async (body) => {
551
638
  if (!body)
552
639
  throw new Error(`No response returned.`);
553
640
  const api = await ApiDocumentFactory.createDocument(body);
554
- this[kContext].api = api;
641
+ this[kBackend].api = api;
555
642
  return api;
556
643
  }).catch((e) => {
557
644
  e.message = "Unable to fetch metadata from service url (" + this.serviceUrl + "). " + e.message;
@@ -559,13 +646,13 @@ var OpraHttpClient = class {
559
646
  }).finally(() => delete this._metadataPromise);
560
647
  }
561
648
  collection(path) {
562
- return new HttpCollectionNode(this, path);
649
+ return new HttpCollectionNode(this[kBackend], path);
563
650
  }
564
651
  singleton(path) {
565
- return new HttpSingletonNode(this, path);
652
+ return new HttpSingletonNode(this[kBackend], path);
566
653
  }
567
654
  action(path, params) {
568
- const observable = new HttpRequestObservable(this, {
655
+ const observable = new HttpRequestObservable(this[kBackend], {
569
656
  method: "GET",
570
657
  url: path
571
658
  });
@@ -577,7 +664,20 @@ var OpraHttpClient = class {
577
664
  }
578
665
  };
579
666
 
580
- // ../../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
581
681
  var _a;
582
682
  var HttpServiceBase = class {
583
683
  static {
@@ -585,7 +685,7 @@ var HttpServiceBase = class {
585
685
  }
586
686
  constructor(client) {
587
687
  this[_a] = {
588
- resource: {},
688
+ resources: {},
589
689
  node: null
590
690
  };
591
691
  this[kClient] = client;
@@ -593,16 +693,21 @@ var HttpServiceBase = class {
593
693
  };
594
694
  _a = kContext;
595
695
  export {
696
+ Backend,
697
+ ClientBase,
596
698
  ClientError,
699
+ FetchBackend,
700
+ HttpBackend,
701
+ HttpClientBase,
597
702
  HttpCollectionNode,
598
703
  HttpEventType,
599
704
  HttpObserveType,
600
- HttpRequest,
601
705
  HttpRequestObservable,
602
706
  HttpResponse,
603
707
  HttpServiceBase,
604
708
  HttpSingletonNode,
605
709
  OpraHttpClient,
710
+ kBackend,
606
711
  kClient,
607
712
  kContext
608
713
  };