@opra/client 0.25.5 → 0.26.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 (64) hide show
  1. package/browser.js +410 -463
  2. package/cjs/client.js +49 -234
  3. package/cjs/constants.js +2 -2
  4. package/cjs/enums/http-observable-type.enum.js +10 -0
  5. package/cjs/enums/index.js +4 -0
  6. package/cjs/impl/collection-node.js +119 -0
  7. package/cjs/impl/http-request-observable.js +246 -0
  8. package/cjs/impl/http-request.js +28 -0
  9. package/cjs/impl/http-service-base.js +16 -0
  10. package/cjs/impl/singleton-node.js +62 -0
  11. package/cjs/index.js +12 -6
  12. package/cjs/interfaces/client-context.interface.js +2 -0
  13. package/cjs/interfaces/http-event.interface.js +35 -0
  14. package/cjs/interfaces/http-request-defaults.interface.js +2 -0
  15. package/cjs/interfaces/index.js +5 -0
  16. package/cjs/types.js +0 -42
  17. package/esm/client.js +51 -236
  18. package/esm/constants.js +1 -1
  19. package/esm/enums/http-observable-type.enum.js +7 -0
  20. package/esm/enums/index.js +1 -0
  21. package/esm/impl/collection-node.js +115 -0
  22. package/esm/impl/http-request-observable.js +242 -0
  23. package/esm/impl/http-request.js +24 -0
  24. package/esm/impl/http-service-base.js +12 -0
  25. package/esm/impl/singleton-node.js +58 -0
  26. package/esm/index.js +9 -6
  27. package/esm/interfaces/client-context.interface.js +1 -0
  28. package/esm/interfaces/http-event.interface.js +32 -0
  29. package/esm/interfaces/http-request-defaults.interface.js +1 -0
  30. package/esm/interfaces/index.js +2 -0
  31. package/esm/types.js +1 -41
  32. package/package.json +4 -4
  33. package/types/client.d.ts +26 -38
  34. package/types/constants.d.ts +1 -1
  35. package/types/enums/http-observable-type.enum.d.ts +6 -0
  36. package/types/enums/index.d.ts +1 -0
  37. package/types/impl/collection-node.d.ts +66 -0
  38. package/types/impl/http-request-observable.d.ts +45 -0
  39. package/types/{http-request.d.ts → impl/http-request.d.ts} +23 -27
  40. package/types/impl/http-service-base.d.ts +7 -0
  41. package/types/impl/singleton-node.d.ts +35 -0
  42. package/types/index.d.ts +9 -6
  43. package/types/interfaces/client-context.interface.d.ts +13 -0
  44. package/types/interfaces/http-event.interface.d.ts +88 -0
  45. package/types/interfaces/http-request-defaults.interface.d.ts +6 -0
  46. package/types/interfaces/index.d.ts +2 -0
  47. package/types/types.d.ts +4 -111
  48. package/cjs/collection-node.js +0 -124
  49. package/cjs/http-request-observable.js +0 -40
  50. package/cjs/http-request.js +0 -86
  51. package/cjs/http-service-base.js +0 -9
  52. package/cjs/singleton-node.js +0 -68
  53. package/esm/collection-node.js +0 -120
  54. package/esm/http-request-observable.js +0 -36
  55. package/esm/http-request.js +0 -82
  56. package/esm/http-service-base.js +0 -5
  57. package/esm/singleton-node.js +0 -64
  58. package/types/collection-node.d.ts +0 -117
  59. package/types/http-request-observable.d.ts +0 -23
  60. package/types/http-service-base.d.ts +0 -5
  61. package/types/singleton-node.d.ts +0 -65
  62. /package/cjs/{http-response.js → impl/http-response.js} +0 -0
  63. /package/esm/{http-response.js → impl/http-response.js} +0 -0
  64. /package/types/{http-response.d.ts → impl/http-response.d.ts} +0 -0
package/cjs/client.js CHANGED
@@ -1,268 +1,83 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpraHttpClient = void 0;
4
- const rxjs_1 = require("rxjs");
5
- const isReadableStreamLike_1 = require("rxjs/internal/util/isReadableStreamLike");
6
4
  const common_1 = require("@opra/common");
7
- const client_error_js_1 = require("./client-error.js");
8
- const collection_node_js_1 = require("./collection-node.js");
9
5
  const constants_js_1 = require("./constants.js");
10
- const http_request_js_1 = require("./http-request.js");
11
- const http_response_js_1 = require("./http-response.js");
12
- const singleton_node_js_1 = require("./singleton-node.js");
13
- const types_js_1 = require("./types.js");
14
- const kAssets = Symbol('kAssets');
6
+ const collection_node_js_1 = require("./impl/collection-node.js");
7
+ const http_request_observable_js_1 = require("./impl/http-request-observable.js");
8
+ const http_response_js_1 = require("./impl/http-response.js");
9
+ const singleton_node_js_1 = require("./impl/singleton-node.js");
15
10
  class OpraHttpClient {
16
11
  constructor(serviceUrl, options) {
17
- Object.defineProperty(this, kAssets, {
12
+ const context = {
13
+ serviceUrl,
14
+ requestInterceptors: [...(options?.requestInterceptors || [])],
15
+ responseInterceptors: [...(options?.responseInterceptors || [])],
16
+ api: options?.api,
17
+ defaults: {
18
+ ...options?.defaults,
19
+ headers: options?.defaults?.headers instanceof Headers
20
+ ? options?.defaults?.headers : new Headers(options?.defaults?.headers),
21
+ params: options?.defaults?.params instanceof URLSearchParams
22
+ ? options?.defaults?.params : new URLSearchParams(options?.defaults?.params)
23
+ },
24
+ fetch,
25
+ createResponse: (init) => new http_response_js_1.HttpResponse(init)
26
+ };
27
+ Object.defineProperty(this, constants_js_1.kContext, {
18
28
  enumerable: false,
19
- value: {
20
- serviceUrl,
21
- api: options?.api,
22
- requestInterceptors: options?.requestInterceptors || [],
23
- responseInterceptors: options?.responseInterceptors || []
24
- }
29
+ value: context
25
30
  });
26
- this.defaults = {
27
- ...options?.defaults,
28
- headers: options?.defaults?.headers instanceof Headers
29
- ? options?.defaults?.headers : new Headers(options?.defaults?.headers),
30
- params: options?.defaults?.params instanceof URLSearchParams
31
- ? options?.defaults?.params : new URLSearchParams(options?.defaults?.params)
32
- };
33
31
  }
34
32
  get serviceUrl() {
35
- return this[kAssets].serviceUrl;
33
+ return this[constants_js_1.kContext].serviceUrl;
34
+ }
35
+ get api() {
36
+ return this[constants_js_1.kContext].api;
37
+ }
38
+ get defaults() {
39
+ return this[constants_js_1.kContext].defaults;
36
40
  }
37
41
  async getMetadata() {
38
- if (this[kAssets].api)
39
- return this[kAssets].api;
40
- let promise = this[kAssets].metadataPromise;
41
- if (promise) {
42
+ let promise = this._metadataPromise;
43
+ if (promise)
42
44
  return promise;
43
- }
44
- this[kAssets].metadataPromise = promise = (0, rxjs_1.lastValueFrom)(this._sendRequest(types_js_1.HttpObserveType.Body, new http_request_js_1.HttpRequest({
45
+ const controller = new http_request_observable_js_1.HttpRequestObservable(this, {
45
46
  method: 'GET',
46
47
  url: '',
47
- headers: new Headers({ 'accept': 'application/json' })
48
- })));
48
+ headers: { 'accept': 'application/json' }
49
+ });
50
+ this._metadataPromise = promise = controller.getData();
49
51
  return await promise
50
52
  .then(async (body) => {
51
53
  if (!body)
52
54
  throw new Error(`No response returned.`);
53
- const api = await common_1.DocumentFactory.createDocument(body);
54
- this[kAssets].api = api;
55
+ const api = await common_1.ApiDocumentFactory.createDocument(body);
56
+ this[constants_js_1.kContext].api = api;
55
57
  return api;
56
58
  })
57
59
  .catch((e) => {
58
60
  e.message = 'Unable to fetch metadata from service url (' + this.serviceUrl + '). ' + e.message;
59
61
  throw e;
60
62
  })
61
- .finally(() => delete this[kAssets].metadataPromise);
63
+ .finally(() => delete this._metadataPromise);
62
64
  }
63
- // batch(requests: HttpRequestHost<any>[]): BatchRequest {
64
- // this._assertMetadata();
65
- // return new BatchRequest(request => this._sendRequest('response', request, requests);
66
- // }
67
- collection(resourceName) {
68
- // If name argument is a class, we extract name from the class
69
- if (typeof resourceName === 'function')
70
- resourceName = resourceName.name;
71
- const ctx = {
72
- client: this,
73
- sourceKind: 'Collection',
74
- endpoint: '',
75
- resource: resourceName,
76
- send: (observe, request) => this._sendRequest(observe, request, 'Collection', ctx.endpoint, ctx),
77
- // requestInterceptors: [
78
- // // Validate resource exists and is a collection resource
79
- // async () => {
80
- // const metadata = await this.getMetadata();
81
- // metadata.getCollection(ctx.sourceName);
82
- // }
83
- // ],
84
- responseInterceptors: []
85
- };
86
- return new collection_node_js_1.HttpCollectionNode(ctx);
65
+ collection(path) {
66
+ return new collection_node_js_1.HttpCollectionNode(this, path);
87
67
  }
88
- singleton(sourceName) {
89
- // If name argument is a class, we extract name from the class
90
- if (typeof sourceName === 'function')
91
- sourceName = sourceName.name;
92
- const ctx = {
93
- client: this,
94
- sourceKind: 'Singleton',
95
- endpoint: '',
96
- resource: sourceName,
97
- send: (observe, request) => this._sendRequest(observe, request, 'Singleton', ctx.endpoint, ctx),
98
- // requestInterceptors: [
99
- // // Validate resource exists and is a singleton resource
100
- // async () => {
101
- // const metadata = await this.getMetadata();
102
- // metadata.getSingleton(ctx.sourceName);
103
- // }
104
- // ],
105
- responseInterceptors: []
106
- };
107
- return new singleton_node_js_1.HttpSingletonNode(ctx);
68
+ singleton(path) {
69
+ return new singleton_node_js_1.HttpSingletonNode(this, path);
108
70
  }
109
- _sendRequest(observe, request, sourceKind, endpoint, ctx) {
110
- return new rxjs_1.Observable(subscriber => {
111
- (async () => {
112
- request.inset(this.defaults);
113
- const url = new common_1.OpraURL(request.url, this.serviceUrl);
114
- let body;
115
- if (request.body) {
116
- let contentType;
117
- if (typeof request.body === 'string' || typeof request.body === 'number' || typeof request.body === 'boolean') {
118
- contentType = 'text/plain;charset=UTF-8"';
119
- body = String(request.body);
120
- request.headers.delete('Content-Size');
121
- delete request.duplex;
122
- }
123
- else if ((0, isReadableStreamLike_1.isReadableStreamLike)(request.body)) {
124
- contentType = 'application/octet-stream';
125
- body = request.body;
126
- request.duplex = 'half';
127
- }
128
- else if (Buffer.isBuffer(request.body)) {
129
- contentType = 'application/octet-stream';
130
- body = request.body;
131
- request.headers.set('Content-Size', String(request.body.length));
132
- delete request.duplex;
133
- }
134
- else if ((0, common_1.isBlob)(request.body)) {
135
- contentType = request.body.type || 'application/octet-stream';
136
- body = request.body;
137
- request.headers.set('Content-Size', String(request.body.length));
138
- delete request.duplex;
139
- }
140
- else {
141
- contentType = 'application/json';
142
- body = JSON.stringify(request.body);
143
- request.headers.delete('Content-Size');
144
- delete request.duplex;
145
- }
146
- if (!request.headers.has('Content-Type') && contentType)
147
- request.headers.set('Content-Type', contentType);
148
- request.body = body;
149
- }
150
- if (ctx) {
151
- const requestInterceptors = [
152
- ...this[kAssets].requestInterceptors,
153
- ...(ctx.requestInterceptors || [])
154
- ];
155
- for (const interceptor of requestInterceptors) {
156
- await interceptor(ctx, request);
157
- }
158
- }
159
- if (observe === types_js_1.HttpObserveType.Events)
160
- subscriber.next({
161
- observe,
162
- request,
163
- event: types_js_1.HttpEventType.Sent,
164
- });
165
- const response = await this._fetch(url.toString(), request);
166
- await this._handleResponse(observe, subscriber, request, response, sourceKind, endpoint, ctx);
167
- })().catch(error => subscriber.error(error));
71
+ action(path, params) {
72
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this, {
73
+ method: 'GET',
74
+ url: path
168
75
  });
169
- }
170
- _fetch(url, init = {}) {
171
- return fetch(url, init);
172
- }
173
- _createResponse(init) {
174
- return new http_response_js_1.HttpResponse(init);
175
- }
176
- async _handleResponse(observe, subscriber, request, fetchResponse, sourceKind, endpoint, ctx) {
177
- const headers = fetchResponse.headers;
178
- if (observe === types_js_1.HttpObserveType.Events) {
179
- const response = this._createResponse({
180
- url: fetchResponse.url,
181
- headers,
182
- status: fetchResponse.status,
183
- statusText: fetchResponse.statusText,
184
- hasBody: !!fetchResponse.body
185
- });
186
- subscriber.next({
187
- observe,
188
- request,
189
- event: types_js_1.HttpEventType.ResponseHeader,
190
- response
191
- });
192
- }
193
- let body;
194
- let totalCount;
195
- let affected;
196
- const contentType = headers.get('Content-Type') || '';
197
- if (fetchResponse.body) {
198
- if (constants_js_1.JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
199
- body = await fetchResponse.json();
200
- if (typeof body === 'string')
201
- body = JSON.parse(body);
202
- if (constants_js_1.OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType)) {
203
- totalCount = body.totalCount;
204
- affected = body.affected;
205
- }
206
- }
207
- else if (constants_js_1.TEXT_CONTENT_TYPE_PATTERN.test(headers.get('Content-Type') || ''))
208
- body = await fetchResponse.text();
209
- else if (constants_js_1.FORMDATA_CONTENT_TYPE_PATTERN.test(headers.get('Content-Type') || ''))
210
- body = await fetchResponse.formData();
211
- else {
212
- const buf = await fetchResponse.arrayBuffer();
213
- if (buf.byteLength)
214
- body = buf;
215
- }
216
- }
217
- if (observe === types_js_1.HttpObserveType.Body && fetchResponse.status >= 400 && fetchResponse.status < 600) {
218
- subscriber.error(new client_error_js_1.ClientError({
219
- message: fetchResponse.status + ' ' + fetchResponse.statusText,
220
- status: fetchResponse.status,
221
- issues: body?.errors
222
- }));
223
- subscriber.complete();
224
- return;
225
- }
226
- const responseInit = {
227
- url: fetchResponse.url,
228
- headers,
229
- status: fetchResponse.status,
230
- statusText: fetchResponse.statusText,
231
- body
232
- };
233
- if (totalCount != null)
234
- responseInit.totalCount = totalCount;
235
- if (affected != null)
236
- responseInit.affected = affected;
237
- const response = this._createResponse(responseInit);
238
- if (ctx) {
239
- const responseInterceptors = [
240
- ...this[kAssets].responseInterceptors,
241
- ...(ctx.responseInterceptors || [])
242
- ];
243
- for (const interceptor of responseInterceptors) {
244
- await interceptor(ctx, observe, request);
245
- }
246
- }
247
- if (observe === types_js_1.HttpObserveType.Body) {
248
- if (constants_js_1.OPRA_JSON_CONTENT_TYPE_PATTERN.test(contentType))
249
- subscriber.next(body.data);
250
- else
251
- subscriber.next(body);
252
- }
253
- else {
254
- if (observe === types_js_1.HttpObserveType.Events)
255
- subscriber.next({
256
- observe,
257
- request,
258
- event: types_js_1.HttpEventType.Response,
259
- response
260
- });
261
- else
262
- subscriber.next(response);
76
+ if (params) {
77
+ Object.keys(params).forEach(k => params[k] = String(params[k]));
78
+ observable.param(params);
263
79
  }
264
- subscriber.complete();
80
+ return observable;
265
81
  }
266
82
  }
267
83
  exports.OpraHttpClient = OpraHttpClient;
268
- OpraHttpClient.kAssets = kAssets;
package/cjs/constants.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.kContext = exports.kRequest = exports.FORMDATA_CONTENT_TYPE_PATTERN = exports.TEXT_CONTENT_TYPE_PATTERN = exports.JSON_CONTENT_TYPE_PATTERN = exports.OPRA_JSON_CONTENT_TYPE_PATTERN = void 0;
3
+ exports.kContext = exports.kClient = exports.FORMDATA_CONTENT_TYPE_PATTERN = exports.TEXT_CONTENT_TYPE_PATTERN = exports.JSON_CONTENT_TYPE_PATTERN = exports.OPRA_JSON_CONTENT_TYPE_PATTERN = void 0;
4
4
  exports.OPRA_JSON_CONTENT_TYPE_PATTERN = /^application\/\bopra\+json\b/i;
5
5
  exports.JSON_CONTENT_TYPE_PATTERN = /^application\/([\w-]+\+)?\bjson\b/i;
6
6
  exports.TEXT_CONTENT_TYPE_PATTERN = /^text\/.*$/i;
7
7
  exports.FORMDATA_CONTENT_TYPE_PATTERN = /^multipart\/\bform-data\b/i;
8
- exports.kRequest = Symbol.for('kRequest');
8
+ exports.kClient = Symbol.for('kClient');
9
9
  exports.kContext = Symbol.for('kContext');
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpObserveType = void 0;
4
+ var HttpObserveType;
5
+ (function (HttpObserveType) {
6
+ HttpObserveType["ResponseHeader"] = "response-header";
7
+ HttpObserveType["Response"] = "response";
8
+ HttpObserveType["Body"] = "body";
9
+ HttpObserveType["Events"] = "events";
10
+ })(HttpObserveType || (exports.HttpObserveType = HttpObserveType = {}));
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./http-observable-type.enum.js"), exports);
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpCollectionNode = void 0;
4
+ const putil_varhelpers_1 = require("putil-varhelpers");
5
+ const common_1 = require("@opra/common");
6
+ const http_request_observable_js_1 = require("./http-request-observable.js");
7
+ /**
8
+ * @class HttpCollectionNode
9
+ */
10
+ class HttpCollectionNode {
11
+ constructor(client, path) {
12
+ this._client = client;
13
+ this._path = path;
14
+ }
15
+ create(data, options) {
16
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
17
+ method: 'POST',
18
+ url: this._path,
19
+ body: data
20
+ });
21
+ if (options?.include)
22
+ observable.param('include', (0, putil_varhelpers_1.toArrayDef)(options.include, []).join(','));
23
+ if (options?.pick)
24
+ observable.param('pick', (0, putil_varhelpers_1.toArrayDef)(options.pick, []).join(','));
25
+ if (options?.omit)
26
+ observable.param('omit', (0, putil_varhelpers_1.toArrayDef)(options.omit, []).join(','));
27
+ return observable;
28
+ }
29
+ delete(id) {
30
+ if (id == null || id === '')
31
+ throw new TypeError(`'id' argument must have a value`);
32
+ const url = new common_1.OpraURL();
33
+ url.join({ resource: this._path, key: id });
34
+ return new http_request_observable_js_1.HttpRequestObservable(this._client, {
35
+ method: 'DELETE',
36
+ url
37
+ });
38
+ }
39
+ deleteMany(options) {
40
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
41
+ method: 'DELETE',
42
+ url: this._path
43
+ });
44
+ if (options?.filter)
45
+ observable.param('filter', String(options.filter));
46
+ return observable;
47
+ }
48
+ get(id, options) {
49
+ if (id == null || id === '')
50
+ throw new TypeError(`'id' argument must have a value`);
51
+ const url = new common_1.OpraURL();
52
+ url.join({ resource: this._path, key: id });
53
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
54
+ method: 'GET',
55
+ url
56
+ });
57
+ if (options?.include)
58
+ observable.param('include', (0, putil_varhelpers_1.toArrayDef)(options.include, []).join(','));
59
+ if (options?.pick)
60
+ observable.param('pick', (0, putil_varhelpers_1.toArrayDef)(options.pick, []).join(','));
61
+ if (options?.omit)
62
+ observable.param('omit', (0, putil_varhelpers_1.toArrayDef)(options.omit, []).join(','));
63
+ return observable;
64
+ }
65
+ findMany(options) {
66
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
67
+ method: 'GET',
68
+ url: this._path
69
+ });
70
+ if (options?.include)
71
+ observable.param('include', (0, putil_varhelpers_1.toArrayDef)(options.include, []).join(','));
72
+ if (options?.pick)
73
+ observable.param('pick', (0, putil_varhelpers_1.toArrayDef)(options.pick, []).join(','));
74
+ if (options?.omit)
75
+ observable.param('omit', (0, putil_varhelpers_1.toArrayDef)(options.omit, []).join(','));
76
+ if (options?.sort)
77
+ observable.param('sort', (0, putil_varhelpers_1.toArrayDef)(options.sort, []).join(','));
78
+ if (options?.filter)
79
+ observable.param('filter', String(options.filter));
80
+ if (options?.limit != null)
81
+ observable.param('limit', String(options.limit));
82
+ if (options?.skip != null)
83
+ observable.param('skip', String(options.skip));
84
+ if (options?.count != null)
85
+ observable.param('count', String(options.count));
86
+ if (options?.distinct != null)
87
+ observable.param('distinct', String(options.distinct));
88
+ return observable;
89
+ }
90
+ update(id, data, options) {
91
+ if (id == null)
92
+ throw new TypeError(`'id' argument must have a value`);
93
+ const url = new common_1.OpraURL();
94
+ url.join({ resource: this._path, key: id });
95
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
96
+ method: 'PATCH',
97
+ url,
98
+ body: data
99
+ });
100
+ if (options?.include)
101
+ observable.param('include', (0, putil_varhelpers_1.toArrayDef)(options.include, []).join(','));
102
+ if (options?.pick)
103
+ observable.param('pick', (0, putil_varhelpers_1.toArrayDef)(options.pick, []).join(','));
104
+ if (options?.omit)
105
+ observable.param('omit', (0, putil_varhelpers_1.toArrayDef)(options.omit, []).join(','));
106
+ return observable;
107
+ }
108
+ updateMany(data, options) {
109
+ const observable = new http_request_observable_js_1.HttpRequestObservable(this._client, {
110
+ method: 'PATCH',
111
+ url: this._path,
112
+ body: data
113
+ });
114
+ if (options?.filter)
115
+ observable.param('filter', String(options.filter));
116
+ return observable;
117
+ }
118
+ }
119
+ exports.HttpCollectionNode = HttpCollectionNode;