@opra/client 0.17.1 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/cjs/client-error.js +17 -0
  2. package/cjs/constants.js +7 -0
  3. package/cjs/http/batch-request.js +90 -0
  4. package/cjs/http/http-client.js +232 -0
  5. package/cjs/http/http-collection-node.js +110 -0
  6. package/cjs/http/http-request-observable.js +36 -0
  7. package/cjs/http/http-request.js +88 -0
  8. package/cjs/http/http-response.js +24 -0
  9. package/cjs/http/http-service-base.js +9 -0
  10. package/cjs/http/http-singleton-node.js +63 -0
  11. package/cjs/http/http-types.js +2 -0
  12. package/cjs/index.js +12 -0
  13. package/cjs/package.json +3 -0
  14. package/esm/client-error.js +13 -0
  15. package/esm/constants.js +4 -0
  16. package/esm/http/batch-request.js +90 -0
  17. package/esm/http/http-client.js +228 -0
  18. package/esm/http/http-collection-node.js +106 -0
  19. package/esm/http/http-request-observable.js +32 -0
  20. package/esm/http/http-request.js +84 -0
  21. package/esm/http/http-response.js +20 -0
  22. package/esm/http/http-service-base.js +5 -0
  23. package/esm/http/http-singleton-node.js +59 -0
  24. package/esm/http/http-types.js +1 -0
  25. package/esm/index.js +9 -0
  26. package/package.json +32 -12
  27. package/types/client-error.d.ts +1 -1
  28. package/types/http/http-client.d.ts +1 -1
  29. package/types/http/http-collection-node.d.ts +5 -5
  30. package/types/http/http-request-observable.d.ts +1 -1
  31. package/types/http/http-request.d.ts +1 -1
  32. package/types/http/http-response.d.ts +3 -1
  33. package/types/http/http-singleton-node.d.ts +1 -1
  34. package/types/http/http-types.d.ts +1 -1
  35. package/types/index.d.ts +0 -6
  36. package/cjs/content-disposition-d575fee5.js +0 -473
  37. package/cjs/cookie-signature-3e974173.js +0 -64
  38. package/cjs/index.min.mjs +0 -9295
  39. package/cjs/mime-db-49f150d0.js +0 -10719
  40. package/cjs/mime-types-6e2bdb57.js +0 -201
  41. package/cjs/safe-buffer-8acd60c7.js +0 -78
  42. package/esm/content-disposition-65f281a9.js +0 -470
  43. package/esm/cookie-signature-bf274b6b.js +0 -62
  44. package/esm/index.min.mjs +0 -9174
  45. package/esm/mime-db-d0f15d8f.js +0 -10717
  46. package/esm/mime-types-79c091fd.js +0 -199
  47. package/esm/safe-buffer-63383356.js +0 -76
  48. package/types/common/document/api-document.d.ts +0 -102
  49. package/types/common/document/constants.d.ts +0 -3
  50. package/types/common/document/data-type/api-field.d.ts +0 -41
  51. package/types/common/document/data-type/builtin/any.type.d.ts +0 -2
  52. package/types/common/document/data-type/builtin/base64.type.d.ts +0 -6
  53. package/types/common/document/data-type/builtin/bigint.type.d.ts +0 -6
  54. package/types/common/document/data-type/builtin/boolean.type.d.ts +0 -5
  55. package/types/common/document/data-type/builtin/date.type.d.ts +0 -4
  56. package/types/common/document/data-type/builtin/guid.type.d.ts +0 -7
  57. package/types/common/document/data-type/builtin/index.d.ts +0 -10
  58. package/types/common/document/data-type/builtin/integer.type.d.ts +0 -6
  59. package/types/common/document/data-type/builtin/number.type.d.ts +0 -6
  60. package/types/common/document/data-type/builtin/object.type.d.ts +0 -3
  61. package/types/common/document/data-type/builtin/string.type.d.ts +0 -5
  62. package/types/common/document/data-type/builtin/time.type.d.ts +0 -7
  63. package/types/common/document/data-type/builtin/timestamp.type.d.ts +0 -5
  64. package/types/common/document/data-type/complex-type.d.ts +0 -56
  65. package/types/common/document/data-type/data-type.d.ts +0 -33
  66. package/types/common/document/data-type/enum-type.d.ts +0 -32
  67. package/types/common/document/data-type/mapped-type.d.ts +0 -53
  68. package/types/common/document/data-type/simple-type.d.ts +0 -33
  69. package/types/common/document/data-type/union-type.d.ts +0 -42
  70. package/types/common/document/factory/add-references.d.ts +0 -4
  71. package/types/common/document/factory/create-document.d.ts +0 -12
  72. package/types/common/document/factory/factory.d.ts +0 -61
  73. package/types/common/document/factory/import-resource-class.d.ts +0 -10
  74. package/types/common/document/factory/import-type-class.d.ts +0 -17
  75. package/types/common/document/factory/index.d.ts +0 -1
  76. package/types/common/document/factory/process-resources.d.ts +0 -7
  77. package/types/common/document/factory/process-types.d.ts +0 -6
  78. package/types/common/document/index.d.ts +0 -14
  79. package/types/common/document/resource/collection.d.ts +0 -69
  80. package/types/common/document/resource/resource.d.ts +0 -29
  81. package/types/common/document/resource/singleton.d.ts +0 -51
  82. package/types/common/document/utils/inspect.util.d.ts +0 -4
  83. package/types/common/document/utils/string-compare.util.d.ts +0 -1
  84. package/types/common/exception/enums/issue-severity.enum.d.ts +0 -13
  85. package/types/common/exception/error-issue.d.ts +0 -9
  86. package/types/common/exception/http-errors/bad-request.error.d.ts +0 -10
  87. package/types/common/exception/http-errors/failed-dependency.error.d.ts +0 -9
  88. package/types/common/exception/http-errors/forbidden.error.d.ts +0 -11
  89. package/types/common/exception/http-errors/internal-server.error.d.ts +0 -9
  90. package/types/common/exception/http-errors/method-not-allowed.error.d.ts +0 -10
  91. package/types/common/exception/http-errors/not-acceptable.error.d.ts +0 -10
  92. package/types/common/exception/http-errors/not-found.error.d.ts +0 -13
  93. package/types/common/exception/http-errors/unauthorized.error.d.ts +0 -10
  94. package/types/common/exception/http-errors/unprocessable-entity.error.d.ts +0 -9
  95. package/types/common/exception/index.d.ts +0 -15
  96. package/types/common/exception/opra-exception.d.ts +0 -15
  97. package/types/common/exception/resource-errors/resource-conflict.error.d.ts +0 -5
  98. package/types/common/exception/resource-errors/resource-not-found.error.d.ts +0 -8
  99. package/types/common/exception/wrap-exception.d.ts +0 -2
  100. package/types/common/filter/antlr/OpraFilterLexer.d.ts +0 -77
  101. package/types/common/filter/antlr/OpraFilterListener.d.ts +0 -430
  102. package/types/common/filter/antlr/OpraFilterParser.d.ts +0 -437
  103. package/types/common/filter/antlr/OpraFilterVisitor.d.ts +0 -260
  104. package/types/common/filter/ast/abstract/ast.d.ts +0 -5
  105. package/types/common/filter/ast/abstract/expression.d.ts +0 -3
  106. package/types/common/filter/ast/abstract/literal.d.ts +0 -6
  107. package/types/common/filter/ast/abstract/term.d.ts +0 -3
  108. package/types/common/filter/ast/expressions/arithmetic-expression.d.ts +0 -13
  109. package/types/common/filter/ast/expressions/array-expression.d.ts +0 -7
  110. package/types/common/filter/ast/expressions/comparison-expression.d.ts +0 -10
  111. package/types/common/filter/ast/expressions/logical-expression.d.ts +0 -8
  112. package/types/common/filter/ast/expressions/parentheses-expression.d.ts +0 -6
  113. package/types/common/filter/ast/index.d.ts +0 -16
  114. package/types/common/filter/ast/terms/boolean-literal.d.ts +0 -5
  115. package/types/common/filter/ast/terms/date-literal.d.ts +0 -6
  116. package/types/common/filter/ast/terms/external-constant.d.ts +0 -5
  117. package/types/common/filter/ast/terms/null-literal.d.ts +0 -5
  118. package/types/common/filter/ast/terms/number-literal.d.ts +0 -6
  119. package/types/common/filter/ast/terms/qualified-identifier.d.ts +0 -8
  120. package/types/common/filter/ast/terms/string-literal.d.ts +0 -5
  121. package/types/common/filter/ast/terms/time-literal.d.ts +0 -6
  122. package/types/common/filter/build.d.ts +0 -31
  123. package/types/common/filter/errors.d.ts +0 -20
  124. package/types/common/filter/filter-tree-visitor.d.ts +0 -33
  125. package/types/common/filter/index.d.ts +0 -5
  126. package/types/common/filter/opra-error-listener.d.ts +0 -8
  127. package/types/common/filter/parse.d.ts +0 -2
  128. package/types/common/filter/utils.d.ts +0 -2
  129. package/types/common/helpers/function-utils.d.ts +0 -4
  130. package/types/common/helpers/get-stack-filename.d.ts +0 -1
  131. package/types/common/helpers/index.d.ts +0 -8
  132. package/types/common/helpers/is-url.d.ts +0 -1
  133. package/types/common/helpers/mixin-utils.d.ts +0 -3
  134. package/types/common/helpers/object-utils.d.ts +0 -2
  135. package/types/common/helpers/path-to-object-tree.d.ts +0 -4
  136. package/types/common/helpers/responsive-map.d.ts +0 -34
  137. package/types/common/helpers/type-guards.d.ts +0 -11
  138. package/types/common/http/codecs/boolean-codec.d.ts +0 -5
  139. package/types/common/http/codecs/date-codec.d.ts +0 -16
  140. package/types/common/http/codecs/filter-codec.d.ts +0 -6
  141. package/types/common/http/codecs/integer-codec.d.ts +0 -11
  142. package/types/common/http/codecs/number-codec.d.ts +0 -14
  143. package/types/common/http/codecs/string-codec.d.ts +0 -16
  144. package/types/common/http/enums/http-headers-codes.enum.d.ts +0 -388
  145. package/types/common/http/enums/http-status-codes.enum.d.ts +0 -290
  146. package/types/common/http/enums/http-status-messages.d.ts +0 -65
  147. package/types/common/http/http-headers.d.ts +0 -86
  148. package/types/common/http/http-message.host.d.ts +0 -122
  149. package/types/common/http/http-params.d.ts +0 -114
  150. package/types/common/http/http-request-message.d.ts +0 -213
  151. package/types/common/http/http-response-message.d.ts +0 -318
  152. package/types/common/http/index.d.ts +0 -14
  153. package/types/common/http/interfaces/client-http-headers.interface.d.ts +0 -65
  154. package/types/common/http/interfaces/cookie-options.interface.d.ts +0 -4
  155. package/types/common/http/interfaces/server-http-headers.interface.d.ts +0 -1
  156. package/types/common/http/multipart/batch-multipart.d.ts +0 -0
  157. package/types/common/http/multipart/http-request-content.d.ts +0 -0
  158. package/types/common/http/multipart/http-response-content.d.ts +0 -0
  159. package/types/common/http/multipart/index.d.ts +0 -0
  160. package/types/common/i18n/i18n.d.ts +0 -28
  161. package/types/common/i18n/index.d.ts +0 -5
  162. package/types/common/i18n/string-utils.d.ts +0 -2
  163. package/types/common/i18n/translate.d.ts +0 -4
  164. package/types/common/index.d.ts +0 -11
  165. package/types/common/schema/constants.d.ts +0 -2
  166. package/types/common/schema/data-type/complex-type.interface.d.ts +0 -16
  167. package/types/common/schema/data-type/data-type.interface.d.ts +0 -14
  168. package/types/common/schema/data-type/enum-type.interface.d.ts +0 -15
  169. package/types/common/schema/data-type/field.interface.d.ts +0 -21
  170. package/types/common/schema/data-type/mapped-type.interface.d.ts +0 -11
  171. package/types/common/schema/data-type/simple-type.interface.d.ts +0 -18
  172. package/types/common/schema/data-type/union-type.interface.d.ts +0 -10
  173. package/types/common/schema/document.interface.d.ts +0 -34
  174. package/types/common/schema/index.d.ts +0 -51
  175. package/types/common/schema/resource/collection.interface.d.ts +0 -33
  176. package/types/common/schema/resource/container.interface.d.ts +0 -9
  177. package/types/common/schema/resource/endpoint.interface.d.ts +0 -29
  178. package/types/common/schema/resource/resource.interface.d.ts +0 -12
  179. package/types/common/schema/resource/singleton.interface.d.ts +0 -22
  180. package/types/common/schema/type-guards.d.ts +0 -20
  181. package/types/common/types.d.ts +0 -11
  182. package/types/common/url/index.d.ts +0 -5
  183. package/types/common/url/opra-url-path-component.d.ts +0 -15
  184. package/types/common/url/opra-url-path.d.ts +0 -36
  185. package/types/common/url/opra-url.d.ts +0 -70
  186. package/types/common/url/utils/decode-path-component.d.ts +0 -5
  187. package/types/common/url/utils/encode-path-component.d.ts +0 -1
  188. package/types/common/utils/path-utils.d.ts +0 -2
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientError = void 0;
4
+ class ClientError extends Error {
5
+ constructor(init, cause) {
6
+ super(init.message);
7
+ this.cause = cause;
8
+ this.issues = init.issues || [];
9
+ this.status = init.status;
10
+ if (cause) {
11
+ this.cause = cause;
12
+ if (cause.stack)
13
+ this.stack = cause.stack;
14
+ }
15
+ }
16
+ }
17
+ exports.ClientError = ClientError;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.kHttpClientContext = exports.FORMDATA_CONTENT_TYPE_PATTERN = exports.TEXT_CONTENT_TYPE_PATTERN = exports.JSON_CONTENT_TYPE_PATTERN = void 0;
4
+ exports.JSON_CONTENT_TYPE_PATTERN = /^application\/([\w-]+\+)?\bjson\b/i;
5
+ exports.TEXT_CONTENT_TYPE_PATTERN = /^text\/.*$/i;
6
+ exports.FORMDATA_CONTENT_TYPE_PATTERN = /^multipart\/\bform-data\b/i;
7
+ exports.kHttpClientContext = Symbol('kContext');
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ // import { BatchMultipart } from '@opra/common';
3
+ // import { HttpRequestHost } from '../http-request-host.js';
4
+ // import { HttpResponse } from '../http-response.js';
5
+ // import { CommonHttpRequestOptions, HttpRequestHandler, RawHttpRequest } from '../http-types.js';
6
+ //
7
+ // export class BatchRequest extends HttpRequestHost {
8
+ //
9
+ // protected _results = new WeakMap<HttpRequestHost, { error?: Error, response?: HttpResponse }>();
10
+ // protected _listeners = new Set<((error: Error | undefined, response?: HttpResponse) => void)>();
11
+ //
12
+ // constructor(
13
+ // protected _handler: HttpRequestHandler,
14
+ // readonly requests: HttpRequestHost[],
15
+ // public options: CommonHttpRequestOptions = {}
16
+ // ) {
17
+ // super(_handler, options);
18
+ //
19
+ // requests.forEach(request => {
20
+ // // Overwrite the _execute method
21
+ // Object.defineProperty(request, '_execute', {
22
+ // writable: true,
23
+ // enumerable: false,
24
+ // configurable: true,
25
+ // value: () => {
26
+ // return new Promise((resolve, reject) => {
27
+ // const x = this._results.get(request);
28
+ // if (x) {
29
+ // if (x.error)
30
+ // return reject(x.error);
31
+ // return resolve(x.response);
32
+ // }
33
+ // const callback = (error, response) => {
34
+ // this._listeners.delete(callback);
35
+ // if (error)
36
+ // return reject(error);
37
+ // resolve(response);
38
+ // };
39
+ // this._listeners.add(callback);
40
+ // })
41
+ // }
42
+ // })
43
+ // });
44
+ // }
45
+ //
46
+ // prepare(): RawHttpRequest {
47
+ // const batch = this._buildBatchMultipart(this);
48
+ // const headers = {
49
+ // 'content-type': 'multipart/mixed;boundary=' + batch.boundary
50
+ // }
51
+ // return mergeRawHttpRequests({
52
+ // method: 'POST',
53
+ // path: '/$batch',
54
+ // headers,
55
+ // body: batch.stream()
56
+ // },
57
+ // this.options.http
58
+ // );
59
+ // }
60
+ //
61
+ // protected _buildBatchMultipart(batch: BatchRequest): BatchMultipart {
62
+ // const multipart = new BatchMultipart();
63
+ //
64
+ // for (const req of batch.requests) {
65
+ // const prepared = req.prepare();
66
+ //
67
+ // if (req instanceof BatchRequest) {
68
+ // const subMultipart = this._buildBatchMultipart(req);
69
+ // multipart.addBatch(subMultipart);
70
+ // continue;
71
+ // }
72
+ //
73
+ // multipart.addRequestPart({
74
+ // method: prepared.method,
75
+ // url: prepared.path,
76
+ // headers: prepared.headers,
77
+ // data: prepared.body
78
+ // });
79
+ // }
80
+ //
81
+ // return multipart;
82
+ // }
83
+ //
84
+ // protected async _execute(): Promise<HttpResponse> {
85
+ // const req = this.prepare();
86
+ // const resp = await this._handler(req) as HttpResponse;
87
+ // return resp;
88
+ // }
89
+ //
90
+ // }
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraHttpClient = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const isReadableStreamLike_1 = require("rxjs/internal/util/isReadableStreamLike");
6
+ const common_1 = require("@opra/common");
7
+ const client_error_js_1 = require("../client-error.js");
8
+ const constants_js_1 = require("../constants.js");
9
+ const http_collection_node_js_1 = require("./http-collection-node.js");
10
+ const http_request_js_1 = require("./http-request.js");
11
+ const http_response_js_1 = require("./http-response.js");
12
+ const http_singleton_node_js_1 = require("./http-singleton-node.js");
13
+ const kAssets = Symbol('kAssets');
14
+ class OpraHttpClient {
15
+ constructor(serviceUrl, options) {
16
+ Object.defineProperty(this, kAssets, {
17
+ enumerable: false,
18
+ value: {
19
+ serviceUrl,
20
+ api: options?.api,
21
+ requestInterceptors: options?.requestInterceptors || [],
22
+ responseInterceptors: options?.responseInterceptors || []
23
+ }
24
+ });
25
+ this.defaults = {
26
+ ...options?.defaults,
27
+ headers: options?.defaults?.headers instanceof common_1.HttpHeaders
28
+ ? options?.defaults?.headers : new common_1.HttpHeaders(options?.defaults?.headers),
29
+ params: options?.defaults?.params instanceof common_1.HttpParams
30
+ ? options?.defaults?.params : new common_1.HttpParams(options?.defaults?.params)
31
+ };
32
+ }
33
+ get serviceUrl() {
34
+ return this[kAssets].serviceUrl;
35
+ }
36
+ async getMetadata() {
37
+ if (this[kAssets].api)
38
+ return this[kAssets].api;
39
+ let promise = this[kAssets].metadataPromise;
40
+ if (promise) {
41
+ return promise;
42
+ }
43
+ this[kAssets].metadataPromise = promise = (0, rxjs_1.lastValueFrom)(this._sendRequest('body', new http_request_js_1.HttpRequest({
44
+ method: 'GET',
45
+ url: '$metadata',
46
+ headers: new common_1.HttpHeaders({ 'accept': 'application/json' })
47
+ })));
48
+ return await promise
49
+ .then(body => common_1.DocumentFactory.createDocument(body))
50
+ .then(api => this[kAssets].api = api)
51
+ .catch((e) => {
52
+ e.message = 'Unable to fetch metadata from ' + this.serviceUrl + '. ' + e.message;
53
+ throw e;
54
+ })
55
+ .finally(() => delete this[kAssets].metadataPromise);
56
+ }
57
+ // batch(requests: HttpRequestHost<any>[]): BatchRequest {
58
+ // this._assertMetadata();
59
+ // return new BatchRequest(request => this._sendRequest('response', request, requests);
60
+ // }
61
+ collection(resourceName) {
62
+ // If name argument is a class, we extract name from the class
63
+ if (typeof resourceName === 'function')
64
+ resourceName = resourceName.name;
65
+ const ctx = {
66
+ client: this,
67
+ resourceName,
68
+ send: (observe, request) => this._sendRequest(observe, request, ctx),
69
+ requestInterceptors: [
70
+ // Validate resource exists and is a collection resource
71
+ async () => {
72
+ const metadata = await this.getMetadata();
73
+ metadata.getCollection(ctx.resourceName);
74
+ }
75
+ ],
76
+ responseInterceptors: []
77
+ };
78
+ return new http_collection_node_js_1.HttpCollectionNode(ctx);
79
+ }
80
+ singleton(resourceName) {
81
+ // If name argument is a class, we extract name from the class
82
+ if (typeof resourceName === 'function')
83
+ resourceName = resourceName.name;
84
+ const ctx = {
85
+ client: this,
86
+ resourceName,
87
+ send: (observe, request) => this._sendRequest(observe, request, ctx),
88
+ requestInterceptors: [
89
+ // Validate resource exists and is a singleton resource
90
+ async () => {
91
+ const metadata = await this.getMetadata();
92
+ metadata.getSingleton(ctx.resourceName);
93
+ }
94
+ ],
95
+ responseInterceptors: []
96
+ };
97
+ return new http_singleton_node_js_1.HttpSingletonNode(ctx);
98
+ }
99
+ _sendRequest(observe, request, ctx) {
100
+ return new rxjs_1.Observable(subscriber => {
101
+ (async () => {
102
+ request.inset(this.defaults);
103
+ const url = request.url.includes('://') ? request.url : (0, common_1.joinPath)(this.serviceUrl, request.url);
104
+ let body;
105
+ if (request.body) {
106
+ let contentType;
107
+ if (typeof request.body === 'string' || typeof request.body === 'number' || typeof request.body === 'boolean') {
108
+ contentType = 'text/plain;charset=UTF-8"';
109
+ body = String(request.body);
110
+ request.headers.delete('Content-Size');
111
+ delete request.duplex;
112
+ }
113
+ else if ((0, isReadableStreamLike_1.isReadableStreamLike)(request.body)) {
114
+ contentType = 'application/octet-stream';
115
+ body = request.body;
116
+ request.duplex = 'half';
117
+ }
118
+ else if (Buffer.isBuffer(request.body)) {
119
+ contentType = 'application/octet-stream';
120
+ body = request.body;
121
+ request.headers.set('Content-Size', request.body.length);
122
+ delete request.duplex;
123
+ }
124
+ else if ((0, common_1.isBlob)(request.body)) {
125
+ contentType = request.body.type || 'application/octet-stream';
126
+ body = request.body;
127
+ request.headers.set('Content-Size', request.body.length);
128
+ delete request.duplex;
129
+ }
130
+ else {
131
+ contentType = 'application/json';
132
+ body = JSON.stringify(request.body);
133
+ request.headers.delete('Content-Size');
134
+ delete request.duplex;
135
+ }
136
+ if (!request.headers.has('Content-Type') && contentType)
137
+ request.headers.set('Content-Type', contentType);
138
+ request.body = body;
139
+ }
140
+ if (ctx) {
141
+ const requestInterceptors = [
142
+ ...this[kAssets].requestInterceptors,
143
+ ...ctx.requestInterceptors
144
+ ];
145
+ for (const interceptor of requestInterceptors) {
146
+ await interceptor(ctx, request);
147
+ }
148
+ }
149
+ if (observe === 'events')
150
+ subscriber.next({ event: 'sent', request });
151
+ const response = await this._fetch(url, { ...request, headers: request.headers.toObject() });
152
+ await this._handleResponse(observe, subscriber, request, response);
153
+ })().catch(error => subscriber.error(error));
154
+ });
155
+ }
156
+ _fetch(url, init = {}) {
157
+ return fetch(url, init);
158
+ }
159
+ _createResponse(init) {
160
+ return new http_response_js_1.HttpResponse(init);
161
+ }
162
+ async _handleResponse(observe, subscriber, request, fetchResponse, ctx) {
163
+ const headers = new common_1.HttpHeaders(fetchResponse.headers);
164
+ if (observe === 'events') {
165
+ const response = this._createResponse({
166
+ url: fetchResponse.url,
167
+ headers,
168
+ status: fetchResponse.status,
169
+ statusText: fetchResponse.statusText,
170
+ hasBody: !!fetchResponse.body
171
+ });
172
+ subscriber.next({ event: 'headers-received', request, response });
173
+ }
174
+ let body;
175
+ if (fetchResponse.body) {
176
+ if (constants_js_1.JSON_CONTENT_TYPE_PATTERN.test(fetchResponse.headers.get('Content-Type') || '')) {
177
+ body = await fetchResponse.json();
178
+ if (typeof body === 'string')
179
+ body = JSON.parse(body);
180
+ }
181
+ else if (constants_js_1.TEXT_CONTENT_TYPE_PATTERN.test(fetchResponse.headers.get('Content-Type') || ''))
182
+ body = await fetchResponse.text();
183
+ else if (constants_js_1.FORMDATA_CONTENT_TYPE_PATTERN.test(fetchResponse.headers.get('Content-Type') || ''))
184
+ body = await fetchResponse.formData();
185
+ else {
186
+ const buf = await fetchResponse.arrayBuffer();
187
+ if (buf.byteLength)
188
+ body = buf;
189
+ }
190
+ }
191
+ if (observe === 'body' && fetchResponse.status >= 400 && fetchResponse.status < 600) {
192
+ subscriber.error(new client_error_js_1.ClientError({
193
+ message: fetchResponse.status + ' ' + fetchResponse.statusText,
194
+ status: fetchResponse.status,
195
+ issues: body?.errors
196
+ }));
197
+ subscriber.complete();
198
+ return;
199
+ }
200
+ const responseInit = {
201
+ url: fetchResponse.url,
202
+ headers,
203
+ status: fetchResponse.status,
204
+ statusText: fetchResponse.statusText,
205
+ body
206
+ };
207
+ if (fetchResponse.headers.has(common_1.HttpHeaderCodes.X_Opra_Total_Matches))
208
+ responseInit.totalMatches = parseInt(fetchResponse.headers.get(common_1.HttpHeaderCodes.X_Opra_Total_Matches), 10);
209
+ const response = this._createResponse(responseInit);
210
+ if (ctx) {
211
+ const responseInterceptors = [
212
+ ...this[kAssets].responseInterceptors,
213
+ ...ctx.responseInterceptors
214
+ ];
215
+ for (const interceptor of responseInterceptors) {
216
+ await interceptor(ctx, observe, request);
217
+ }
218
+ }
219
+ if (observe === 'body') {
220
+ subscriber.next(body);
221
+ }
222
+ else {
223
+ if (observe === 'events')
224
+ subscriber.next({ event: 'response', request, response });
225
+ else
226
+ subscriber.next(response);
227
+ }
228
+ subscriber.complete();
229
+ }
230
+ }
231
+ exports.OpraHttpClient = OpraHttpClient;
232
+ OpraHttpClient.kAssets = kAssets;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpCollectionNode = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const http_request_observable_js_1 = require("./http-request-observable.js");
6
+ class HttpCollectionNode {
7
+ constructor(context) {
8
+ this[constants_js_1.kHttpClientContext] = context;
9
+ }
10
+ create(data, options) {
11
+ const context = this[constants_js_1.kHttpClientContext];
12
+ const requestHost = new http_request_observable_js_1.HttpRequestObservable(context, options);
13
+ const request = requestHost[http_request_observable_js_1.HttpRequestObservable.kRequest];
14
+ request.method = 'POST';
15
+ request.url = context.resourceName;
16
+ request.body = data;
17
+ if (options?.include)
18
+ request.params.set('$include', options.include);
19
+ if (options?.pick)
20
+ request.params.set('$pick', options.pick);
21
+ if (options?.omit)
22
+ request.params.set('$omit', options.omit);
23
+ return requestHost;
24
+ }
25
+ delete(id, options) {
26
+ const context = this[constants_js_1.kHttpClientContext];
27
+ const requestHost = new http_request_observable_js_1.HttpRequestObservable(context, options);
28
+ const request = requestHost[http_request_observable_js_1.HttpRequestObservable.kRequest];
29
+ request.method = 'DELETE';
30
+ request.path.join({ resource: context.resourceName, key: id });
31
+ return requestHost;
32
+ }
33
+ deleteMany(options) {
34
+ const context = this[constants_js_1.kHttpClientContext];
35
+ const requestHost = new http_request_observable_js_1.HttpRequestObservable(context, options);
36
+ const request = requestHost[http_request_observable_js_1.HttpRequestObservable.kRequest];
37
+ request.method = 'DELETE';
38
+ request.url = context.resourceName;
39
+ if (options?.filter)
40
+ request.params.set('$filter', options.filter);
41
+ return requestHost;
42
+ }
43
+ get(id, options) {
44
+ const context = this[constants_js_1.kHttpClientContext];
45
+ const requestHost = new http_request_observable_js_1.HttpRequestObservable(context, options);
46
+ const request = requestHost[http_request_observable_js_1.HttpRequestObservable.kRequest];
47
+ request.method = 'GET';
48
+ request.path.join({ resource: context.resourceName, key: id });
49
+ if (options?.include)
50
+ request.params.set('$include', options.include);
51
+ if (options?.pick)
52
+ request.params.set('$pick', options.pick);
53
+ if (options?.omit)
54
+ request.params.set('$omit', options.omit);
55
+ return requestHost;
56
+ }
57
+ findMany(options) {
58
+ const context = this[constants_js_1.kHttpClientContext];
59
+ const requestHost = new http_request_observable_js_1.HttpRequestObservable(context, options);
60
+ const request = requestHost[http_request_observable_js_1.HttpRequestObservable.kRequest];
61
+ request.method = 'GET';
62
+ request.url = context.resourceName;
63
+ if (options?.include)
64
+ request.params.set('$include', options.include);
65
+ if (options?.pick)
66
+ request.params.set('$pick', options.pick);
67
+ if (options?.omit)
68
+ request.params.set('$omit', options.omit);
69
+ if (options?.sort)
70
+ request.params.set('$sort', options.sort);
71
+ if (options?.filter)
72
+ request.params.set('$filter', options.filter);
73
+ if (options?.limit != null)
74
+ request.params.set('$limit', options.limit);
75
+ if (options?.skip != null)
76
+ request.params.set('$skip', options.skip);
77
+ if (options?.count != null)
78
+ request.params.set('$count', options.count);
79
+ if (options?.distinct != null)
80
+ request.params.set('$distinct', options.distinct);
81
+ return requestHost;
82
+ }
83
+ update(id, data, options) {
84
+ const context = this[constants_js_1.kHttpClientContext];
85
+ const requestHost = new http_request_observable_js_1.HttpRequestObservable(context, options);
86
+ const request = requestHost[http_request_observable_js_1.HttpRequestObservable.kRequest];
87
+ request.method = 'PATCH';
88
+ request.path.join({ resource: context.resourceName, key: id });
89
+ request.body = data;
90
+ if (options?.include)
91
+ request.params.set('$include', options.include);
92
+ if (options?.pick)
93
+ request.params.set('$pick', options.pick);
94
+ if (options?.omit)
95
+ request.params.set('$omit', options.omit);
96
+ return requestHost;
97
+ }
98
+ updateMany(data, options) {
99
+ const context = this[constants_js_1.kHttpClientContext];
100
+ const requestHost = new http_request_observable_js_1.HttpRequestObservable(context, options);
101
+ const request = requestHost[http_request_observable_js_1.HttpRequestObservable.kRequest];
102
+ request.method = 'PATCH';
103
+ request.url = context.resourceName;
104
+ request.body = data;
105
+ if (options?.filter)
106
+ request.params.set('$filter', options.filter);
107
+ return requestHost;
108
+ }
109
+ }
110
+ exports.HttpCollectionNode = HttpCollectionNode;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpRequestObservable = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const common_1 = require("@opra/common");
6
+ const constants_js_1 = require("../constants.js");
7
+ const http_request_js_1 = require("./http-request.js");
8
+ const kRequest = Symbol('kRequest');
9
+ class HttpRequestObservable extends rxjs_1.Observable {
10
+ constructor(context, options) {
11
+ super((subscriber) => {
12
+ context.send(options?.observe || 'body', this[kRequest]).subscribe((subscriber));
13
+ });
14
+ this[constants_js_1.kHttpClientContext] = context;
15
+ this[kRequest] = new http_request_js_1.HttpRequest(options?.http);
16
+ this.contentId = (0, common_1.uid)(6);
17
+ }
18
+ header(name, value) {
19
+ this[kRequest].headers.append(name, value);
20
+ return this;
21
+ }
22
+ param(name, value) {
23
+ this[kRequest].params.append(name, value);
24
+ return this;
25
+ }
26
+ async fetch(observe) {
27
+ return (0, rxjs_1.lastValueFrom)(this[constants_js_1.kHttpClientContext].send(observe || 'body', this[kRequest]));
28
+ }
29
+ with(cb) {
30
+ cb(this);
31
+ return this;
32
+ }
33
+ }
34
+ exports.HttpRequestObservable = HttpRequestObservable;
35
+ HttpRequestObservable.kContext = constants_js_1.kHttpClientContext;
36
+ HttpRequestObservable.kRequest = kRequest;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpRequest = void 0;
4
+ /// <reference lib="dom" />
5
+ const common_1 = require("@opra/common");
6
+ const directCopyProperties = ['cache', 'credentials', 'destination', 'headers', 'integrity',
7
+ 'keepalive', 'mode', 'redirect', 'referrer', 'referrerPolicy'];
8
+ class HttpRequest {
9
+ constructor(init) {
10
+ this.cache = init?.cache || 'default';
11
+ this.credentials = init?.credentials || 'same-origin';
12
+ this.destination = init?.destination || '';
13
+ this.headers = new common_1.HttpHeaders(init?.headers);
14
+ this.integrity = init?.integrity || '';
15
+ this.keepalive = init?.keepalive ?? false;
16
+ this.method = (init?.method || 'GET').toUpperCase();
17
+ this.mode = init?.mode || 'cors';
18
+ this.redirect = init?.redirect || 'follow';
19
+ this.mode = init?.mode || 'cors';
20
+ this.referrer = init?.referrer || '';
21
+ this.referrerPolicy = init?.referrerPolicy || '';
22
+ this.signal = init?.signal || new AbortController().signal;
23
+ this.body = init?.body;
24
+ this.urlInstance = new common_1.OpraURL(init?.url);
25
+ if (init?.params)
26
+ this.params.appendAll(init.params);
27
+ }
28
+ /** Returns the URL of request as a string. */
29
+ get url() {
30
+ return this.urlInstance.href;
31
+ }
32
+ set url(value) {
33
+ this.urlInstance.parse(value);
34
+ }
35
+ /** Returns the searchParams of the URL as OpraURLSearchParams */
36
+ get params() {
37
+ return this.urlInstance.searchParams;
38
+ }
39
+ /** Returns the path part of URL as OpraURLPath */
40
+ get path() {
41
+ return this.urlInstance.path;
42
+ }
43
+ clone(...update) {
44
+ const out = new HttpRequest();
45
+ out.merge(this);
46
+ for (const upd of update) {
47
+ out.merge(upd);
48
+ }
49
+ return out;
50
+ }
51
+ merge(update) {
52
+ directCopyProperties.forEach(k => {
53
+ if (update[k] != null)
54
+ this[k] = update[k];
55
+ });
56
+ if (update.headers) {
57
+ const h = update.headers instanceof common_1.HttpHeaders
58
+ ? update.headers
59
+ : new common_1.HttpHeaders(update.headers);
60
+ h.forEach((v, k) => {
61
+ if (k.toLowerCase() === 'set-cookie') {
62
+ this.headers.append(k, v);
63
+ }
64
+ else
65
+ this.headers.set(k, v);
66
+ });
67
+ }
68
+ }
69
+ inset(src) {
70
+ directCopyProperties.forEach(k => {
71
+ if (this[k] == null && src[k] != null)
72
+ this[k] = src[k];
73
+ });
74
+ if (src.headers) {
75
+ const h = src.headers instanceof common_1.HttpHeaders
76
+ ? src.headers
77
+ : new common_1.HttpHeaders(src.headers);
78
+ h.forEach((v, k) => {
79
+ if (k.toLowerCase() === 'set-cookie') {
80
+ this.headers.append(k, v);
81
+ }
82
+ else if (!this.headers.has(k))
83
+ this.headers.set(k, v);
84
+ });
85
+ }
86
+ }
87
+ }
88
+ exports.HttpRequest = HttpRequest;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpResponse = void 0;
4
+ const common_1 = require("@opra/common");
5
+ class HttpResponse {
6
+ constructor(init) {
7
+ /**
8
+ * Returns true if response has body to be received
9
+ */
10
+ this.hasBody = false;
11
+ this.headers = new common_1.HttpHeaders(init?.headers);
12
+ this.status = init?.status || 200;
13
+ this.statusText = init?.statusText || 'OK';
14
+ this.url = init?.url || null;
15
+ this.ok = this.status >= 200 && this.status < 300;
16
+ this.body = init?.body;
17
+ this.hasBody = init?.body != null || !!init?.hasBody;
18
+ this.totalMatches = init?.totalMatches;
19
+ }
20
+ clone(update) {
21
+ return new HttpResponse({ ...this, ...update });
22
+ }
23
+ }
24
+ exports.HttpResponse = HttpResponse;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpServiceBase = void 0;
4
+ class HttpServiceBase {
5
+ constructor($client) {
6
+ this.$client = $client;
7
+ }
8
+ }
9
+ exports.HttpServiceBase = HttpServiceBase;