@opra/client 0.4.0 → 0.5.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 (58) hide show
  1. package/cjs/client-request.js +48 -0
  2. package/cjs/client.js +111 -68
  3. package/cjs/index.js +13 -3
  4. package/cjs/requests/batch-request.js +136 -0
  5. package/cjs/requests/collection-create-request.js +28 -15
  6. package/cjs/requests/collection-delete-many-request.js +21 -7
  7. package/cjs/requests/collection-delete-request.js +29 -0
  8. package/cjs/requests/collection-get-request.js +28 -15
  9. package/cjs/requests/collection-search-request.js +38 -40
  10. package/cjs/requests/collection-update-many-request.js +24 -7
  11. package/cjs/requests/collection-update-request.js +31 -15
  12. package/cjs/requests/singleton-get-request.js +25 -15
  13. package/cjs/services/collection-node.js +42 -0
  14. package/cjs/services/singleton-node.js +18 -0
  15. package/cjs/types.js +0 -9
  16. package/esm/client-request.d.ts +17 -0
  17. package/esm/client-request.js +44 -0
  18. package/esm/client.d.ts +27 -18
  19. package/esm/client.js +112 -69
  20. package/esm/index.d.ts +13 -3
  21. package/esm/index.js +13 -3
  22. package/esm/requests/batch-request.d.ts +19 -0
  23. package/esm/requests/batch-request.js +131 -0
  24. package/esm/requests/collection-create-request.d.ts +12 -7
  25. package/esm/requests/collection-create-request.js +28 -15
  26. package/esm/requests/collection-delete-many-request.d.ts +11 -6
  27. package/esm/requests/collection-delete-many-request.js +21 -7
  28. package/esm/requests/collection-delete-request.d.ts +13 -0
  29. package/esm/requests/collection-delete-request.js +25 -0
  30. package/esm/requests/collection-get-request.d.ts +12 -7
  31. package/esm/requests/collection-get-request.js +28 -15
  32. package/esm/requests/collection-search-request.d.ts +11 -14
  33. package/esm/requests/collection-search-request.js +38 -40
  34. package/esm/requests/collection-update-many-request.d.ts +12 -6
  35. package/esm/requests/collection-update-many-request.js +24 -7
  36. package/esm/requests/collection-update-request.d.ts +13 -7
  37. package/esm/requests/collection-update-request.js +31 -15
  38. package/esm/requests/singleton-get-request.d.ts +11 -7
  39. package/esm/requests/singleton-get-request.js +25 -15
  40. package/esm/services/collection-node.d.ts +24 -0
  41. package/esm/services/collection-node.js +38 -0
  42. package/esm/services/singleton-node.d.ts +12 -0
  43. package/esm/services/singleton-node.js +14 -0
  44. package/esm/types.d.ts +33 -6
  45. package/esm/types.js +0 -9
  46. package/package.json +13 -6
  47. package/cjs/observable-promise.js +0 -10
  48. package/cjs/response.js +0 -2
  49. package/cjs/services/collection-service.js +0 -192
  50. package/cjs/services/singleton-service.js +0 -43
  51. package/esm/observable-promise.d.ts +0 -2
  52. package/esm/observable-promise.js +0 -6
  53. package/esm/response.d.ts +0 -9
  54. package/esm/response.js +0 -1
  55. package/esm/services/collection-service.d.ts +0 -31
  56. package/esm/services/collection-service.js +0 -188
  57. package/esm/services/singleton-service.d.ts +0 -14
  58. package/esm/services/singleton-service.js +0 -39
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraClientRequest = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const uid_1 = require("uid");
6
+ const common_1 = require("@opra/common");
7
+ class OpraClientRequest extends rxjs_1.Observable {
8
+ client;
9
+ _send;
10
+ options;
11
+ id;
12
+ _promise;
13
+ constructor(client, _send, options = {}) {
14
+ super((subscriber) => {
15
+ this.execute().then(v => {
16
+ subscriber.next(v);
17
+ subscriber.complete();
18
+ }).catch(e => subscriber.error(e));
19
+ });
20
+ this.client = client;
21
+ this._send = _send;
22
+ this.options = options;
23
+ this.id = (0, uid_1.uid)(10);
24
+ this.options = options;
25
+ }
26
+ async toPromise() {
27
+ return this.execute();
28
+ }
29
+ async execute() {
30
+ const promise = this._promise || (this._promise = this._execute());
31
+ return await promise;
32
+ }
33
+ async _execute() {
34
+ const req = this.prepare();
35
+ const headers = this.options.http?.headers
36
+ ? new common_1.HeadersMap({ ...req.headers, ...this.options.http.headers }).toObject()
37
+ : req.headers;
38
+ return this._send({
39
+ ...this.options.http,
40
+ ...req,
41
+ headers
42
+ });
43
+ }
44
+ binding() {
45
+ return {};
46
+ }
47
+ }
48
+ exports.OpraClientRequest = OpraClientRequest;
package/cjs/client.js CHANGED
@@ -3,93 +3,136 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpraClient = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const axios_1 = tslib_1.__importDefault(require("axios"));
6
+ const power_tasks_1 = require("power-tasks");
6
7
  const common_1 = require("@opra/common");
7
8
  const schema_1 = require("@opra/schema");
8
9
  const url_1 = require("@opra/url");
9
10
  const client_error_js_1 = require("./client-error.js");
10
- const collection_service_js_1 = require("./services/collection-service.js");
11
- const singleton_service_js_1 = require("./services/singleton-service.js");
11
+ const batch_request_js_1 = require("./requests/batch-request.js");
12
+ const collection_node_js_1 = require("./services/collection-node.js");
13
+ const singleton_node_js_1 = require("./services/singleton-node.js");
14
+ const documentCache = new Map();
15
+ const documentCacheResolvers = new Map();
12
16
  class OpraClient {
13
- serviceUrl;
14
- options;
17
+ _axios;
15
18
  _metadata;
16
- constructor(serviceUrl, options) {
17
- this.options = { ...options };
18
- this.serviceUrl = (0, url_1.normalizePath)(serviceUrl);
19
+ _taskQueue;
20
+ constructor(serviceUrl, arg1, arg2) {
21
+ let options;
22
+ if (arg1 instanceof schema_1.OpraDocument) {
23
+ this._metadata = arg1;
24
+ options = arg2;
25
+ }
26
+ else
27
+ options = arg1 || arg2;
28
+ this._taskQueue = new power_tasks_1.TaskQueue({
29
+ maxQueue: options?.maxQueue,
30
+ concurrency: options?.concurrency
31
+ });
32
+ this._axios = axios_1.default.create();
33
+ this._axios.defaults.baseURL = (0, url_1.normalizePath)(serviceUrl);
34
+ this._axios.defaults.adapter = options?.adapter;
35
+ if (options?.defaults?.headers)
36
+ this._axios.defaults.headers.common = options.defaults.headers;
37
+ this._axios.defaults.auth = options?.defaults?.auth;
38
+ this._axios.defaults.timeout = options?.defaults?.timeout;
39
+ this._axios.defaults.timeoutErrorMessage = options?.defaults?.timeoutErrorMessage;
40
+ this._axios.defaults.xsrfCookieName = options?.defaults?.xsrfCookieName;
41
+ this._axios.defaults.xsrfHeaderName = options?.defaults?.xsrfHeaderName;
42
+ this._axios.defaults.maxRedirects = options?.defaults?.maxRedirects;
43
+ this._axios.defaults.maxRate = options?.defaults?.maxRate;
44
+ this._axios.defaults.httpAgent = options?.defaults?.httpAgent;
45
+ this._axios.defaults.httpsAgent = options?.defaults?.httpsAgent;
46
+ this._axios.defaults.proxy = options?.defaults?.proxy;
47
+ this._axios.defaults.validateStatus = options?.defaults?.validateStatus;
48
+ const document = documentCache.get(serviceUrl.toLowerCase());
49
+ if (document)
50
+ this._metadata = document;
51
+ }
52
+ get requestInterceptors() {
53
+ return this._axios.interceptors.request;
54
+ }
55
+ get responseInterceptors() {
56
+ return this._axios.interceptors.response;
57
+ }
58
+ get serviceUrl() {
59
+ return this._axios.defaults.baseURL || '';
60
+ }
61
+ get initialized() {
62
+ return !!this._metadata;
19
63
  }
20
64
  get metadata() {
65
+ this._assertMetadata();
21
66
  return this._metadata;
22
67
  }
23
- collection(name, options) {
68
+ async init() {
69
+ let promise = documentCacheResolvers.get(this.serviceUrl.toLowerCase());
70
+ if (promise) {
71
+ await promise;
72
+ return;
73
+ }
74
+ promise = this._send({
75
+ method: 'GET',
76
+ url: '/$metadata',
77
+ });
78
+ documentCacheResolvers.set(this.serviceUrl.toLowerCase(), promise);
79
+ try {
80
+ const resp = await promise;
81
+ this._metadata = new schema_1.OpraDocument(resp.data);
82
+ }
83
+ finally {
84
+ documentCacheResolvers.delete(this.serviceUrl.toLowerCase());
85
+ }
86
+ }
87
+ batch(requests) {
88
+ this._assertMetadata();
89
+ return new batch_request_js_1.BatchRequest(this, requests, req => this._send(req));
90
+ }
91
+ collection(name) {
92
+ this._assertMetadata();
24
93
  const resource = this.metadata.getCollectionResource(name);
25
- const commonOptions = {
26
- headers: this.options.defaultHeaders,
27
- validateStatus: this.options.validateStatus,
28
- ...options,
29
- };
30
- return new collection_service_js_1.CollectionService(this.serviceUrl, this.metadata, (req) => this._send(req, commonOptions), resource);
94
+ return new collection_node_js_1.CollectionNode(this, resource, req => this._send(req));
31
95
  }
32
- singleton(name, options) {
96
+ singleton(name) {
97
+ this._assertMetadata();
33
98
  const resource = this.metadata.getSingletonResource(name);
34
- const commonOptions = {
35
- headers: this.options.defaultHeaders,
36
- validateStatus: this.options.validateStatus,
37
- ...options,
38
- };
39
- return new singleton_service_js_1.SingletonService(this.serviceUrl, this.metadata, (req) => this._send(req, commonOptions), resource);
99
+ return new singleton_node_js_1.SingletonNode(this, resource, req => this._send(req));
40
100
  }
41
- async _send(req, options) {
42
- const axiosInstance = this._createAxiosInstance(options);
43
- const resp = await axiosInstance.request({ ...req, validateStatus: undefined });
44
- const validateStatus = options?.validateStatus;
45
- if ((validateStatus === true && !(resp.status >= 200 && resp.status < 300)) ||
46
- (typeof validateStatus === 'function' && !validateStatus(resp.status))) {
47
- throw new client_error_js_1.ClientError({
48
- message: resp.statusText,
49
- status: resp.status,
50
- issues: resp.data.errors
101
+ async _send(req) {
102
+ return this._taskQueue.enqueue(async () => {
103
+ const resp = await this._axios.request({
104
+ ...req,
105
+ validateStatus: () => true
51
106
  });
52
- }
53
- const rawHeaders = (typeof resp.headers.toJSON === 'function'
54
- ? resp.headers.toJSON()
55
- : { ...resp.headers });
56
- const headers = new common_1.ResponsiveMap(rawHeaders);
57
- return {
58
- status: resp.status,
59
- statusText: resp.statusText,
60
- data: resp.data,
61
- rawHeaders,
62
- headers
63
- };
64
- }
65
- _createAxiosInstance(options) {
66
- const axiosInstance = axios_1.default.create();
67
- axiosInstance.defaults.adapter = this.options.adapter;
68
- axiosInstance.defaults.headers.common = { ...options?.headers, ...this.options.defaultHeaders };
69
- if (options?.validateStatus != null) {
70
- if (options.validateStatus === false)
71
- axiosInstance.defaults.validateStatus = () => true;
72
- else if (typeof options.validateStatus === 'function')
73
- axiosInstance.defaults.validateStatus = options.validateStatus;
74
- }
75
- else if (this.options.validateStatus != null) {
76
- if (this.options.validateStatus === false)
77
- axiosInstance.defaults.validateStatus = () => true;
78
- else if (typeof this.options.validateStatus === 'function')
79
- axiosInstance.defaults.validateStatus = this.options.validateStatus;
80
- }
81
- return axiosInstance;
107
+ if ((resp.status >= 400 && resp.status < 600) &&
108
+ (this._axios.defaults.validateStatus && !this._axios.defaults.validateStatus(resp.status))) {
109
+ throw new client_error_js_1.ClientError({
110
+ message: resp.status + ' ' + resp.statusText,
111
+ status: resp.status,
112
+ issues: resp.data.errors
113
+ });
114
+ }
115
+ const rawHeaders = (typeof resp.headers.toJSON === 'function'
116
+ ? resp.headers.toJSON()
117
+ : { ...resp.headers });
118
+ const headers = new common_1.ResponsiveMap(rawHeaders);
119
+ return {
120
+ status: resp.status,
121
+ statusText: resp.statusText,
122
+ data: resp.data,
123
+ rawHeaders,
124
+ headers
125
+ };
126
+ }).toPromise();
82
127
  }
83
- async _fetchMetadata() {
84
- const resp = await this._send({
85
- method: 'GET',
86
- url: (0, url_1.joinPath)(this.serviceUrl || '/', '$metadata'),
87
- }, { validateStatus: true });
88
- this._metadata = new schema_1.OpraDocument(resp.data);
128
+ _assertMetadata() {
129
+ if (!this._metadata)
130
+ throw new Error('You must call init() to before using the client instance');
89
131
  }
90
132
  static async create(serviceUrl, options) {
91
133
  const client = new this(serviceUrl, options);
92
- await client._fetchMetadata();
134
+ if (!client._metadata)
135
+ await client.init();
93
136
  return client;
94
137
  }
95
138
  }
package/cjs/index.js CHANGED
@@ -2,7 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./client.js"), exports);
5
- tslib_1.__exportStar(require("./response.js"), exports);
5
+ tslib_1.__exportStar(require("./client-request.js"), exports);
6
6
  tslib_1.__exportStar(require("./types.js"), exports);
7
- tslib_1.__exportStar(require("./services/collection-service.js"), exports);
8
- tslib_1.__exportStar(require("./services/singleton-service.js"), exports);
7
+ tslib_1.__exportStar(require("./client-error.js"), exports);
8
+ tslib_1.__exportStar(require("./services/collection-node.js"), exports);
9
+ tslib_1.__exportStar(require("./services/singleton-node.js"), exports);
10
+ tslib_1.__exportStar(require("./requests/batch-request.js"), exports);
11
+ tslib_1.__exportStar(require("./requests/collection-create-request.js"), exports);
12
+ tslib_1.__exportStar(require("./requests/collection-delete-request.js"), exports);
13
+ tslib_1.__exportStar(require("./requests/collection-delete-many-request.js"), exports);
14
+ tslib_1.__exportStar(require("./requests/collection-get-request.js"), exports);
15
+ tslib_1.__exportStar(require("./requests/collection-search-request.js"), exports);
16
+ tslib_1.__exportStar(require("./requests/collection-update-request.js"), exports);
17
+ tslib_1.__exportStar(require("./requests/collection-update-many-request.js"), exports);
18
+ tslib_1.__exportStar(require("./requests/singleton-get-request.js"), exports);
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BatchRequest = exports.CRLF = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const highland_1 = tslib_1.__importDefault(require("highland"));
6
+ const uid_1 = require("uid");
7
+ const common_1 = require("@opra/common");
8
+ const url_1 = require("@opra/url");
9
+ const client_request_js_1 = require("../client-request.js");
10
+ exports.CRLF = '\r\n';
11
+ class BatchRequest// todo
12
+ extends client_request_js_1.OpraClientRequest {
13
+ client;
14
+ requests;
15
+ options;
16
+ _results = new WeakMap();
17
+ _listeners = new Set();
18
+ constructor(client, requests, send, options = {}) {
19
+ super(client, send, options);
20
+ this.client = client;
21
+ this.requests = requests;
22
+ this.options = options;
23
+ requests.forEach(request => {
24
+ Object.defineProperty(request, '_execute', {
25
+ writable: true,
26
+ enumerable: false,
27
+ configurable: true,
28
+ value: () => {
29
+ return new Promise((resolve, reject) => {
30
+ const x = this._results.get(request);
31
+ if (x) {
32
+ if (x.error)
33
+ return reject(x.error);
34
+ return resolve(x.response);
35
+ }
36
+ const callback = (e, r) => {
37
+ this._listeners.delete(callback);
38
+ if (e)
39
+ return reject(e);
40
+ resolve(r);
41
+ };
42
+ this._listeners.add(callback);
43
+ });
44
+ }
45
+ });
46
+ });
47
+ }
48
+ prepare() {
49
+ const { boundary, stream } = buildBatchStream(this);
50
+ const headers = {
51
+ 'Content-Type': 'multipart/mixed;boundary=' + boundary
52
+ };
53
+ const url = new url_1.OpraURL(this.client.serviceUrl);
54
+ url.path.join('/$batch');
55
+ return {
56
+ method: 'POST',
57
+ url: url.address,
58
+ params: url.searchParams,
59
+ headers,
60
+ data: stream
61
+ };
62
+ }
63
+ async _execute() {
64
+ const req = this.prepare();
65
+ return this._send(req);
66
+ }
67
+ }
68
+ exports.BatchRequest = BatchRequest;
69
+ function buildBatchStream(batch) {
70
+ const chunks = [];
71
+ let batchIndex = 0;
72
+ let level = 0;
73
+ const processBatch = (_batch) => {
74
+ const boundary = `batch_${++batchIndex}_L${level}_${(0, uid_1.uid)(10)}`;
75
+ for (const req of _batch.requests) {
76
+ chunks.push('--' + boundary + exports.CRLF);
77
+ if (req instanceof BatchRequest) {
78
+ const contentHeaderIndex = chunks.push('');
79
+ chunks.push('Content-Transfer-Encoding: binary' + exports.CRLF + exports.CRLF);
80
+ level++;
81
+ const subBoundary = processBatch(req);
82
+ level--;
83
+ processBatch[contentHeaderIndex] = 'Content-Type: multipart/mixed;boundary=' + subBoundary + exports.CRLF;
84
+ continue;
85
+ }
86
+ const prepared = req.prepare();
87
+ let s = 'Content-Type: application/http' + exports.CRLF +
88
+ 'Content-Transfer-Encoding: binary' + exports.CRLF +
89
+ 'Content-ID:' + req.id + exports.CRLF +
90
+ exports.CRLF +
91
+ (prepared.method || 'GET').toUpperCase() + ' ' + prepared.url + ' HTTP/1.1' + exports.CRLF;
92
+ const headers = new common_1.HeadersMap(prepared.headers);
93
+ let data;
94
+ let contentType = '';
95
+ if (prepared.data) {
96
+ if (typeof prepared.data === 'string') {
97
+ contentType = 'text/plain; charset=utf-8';
98
+ data = prepared.data;
99
+ }
100
+ else if (highland_1.default.isStream(prepared.data) || Buffer.isBuffer(prepared.data)) {
101
+ data = prepared.data;
102
+ }
103
+ else if (typeof prepared.data.stream === 'function') {
104
+ contentType = prepared.data.type || 'binary';
105
+ data = prepared.data.stream(); // File and Blob
106
+ }
107
+ else if (typeof prepared.data === 'object') {
108
+ contentType = 'application/json; charset=utf-8';
109
+ data = JSON.stringify(prepared.data);
110
+ }
111
+ else
112
+ throw new TypeError(`Invalid data type ${typeof prepared.data}`);
113
+ }
114
+ if (contentType && !headers.has('Content-Type'))
115
+ headers.set('Content-Type', contentType);
116
+ headers.forEach((v, k) => {
117
+ s += k + ': ' + (Array.isArray(v) ? v.join(';') : v) + exports.CRLF;
118
+ });
119
+ if (data) {
120
+ if (typeof data === 'string')
121
+ chunks.push(s + exports.CRLF + data + exports.CRLF + exports.CRLF);
122
+ else {
123
+ chunks.push(s + exports.CRLF);
124
+ chunks.push(data);
125
+ chunks.push(exports.CRLF + exports.CRLF);
126
+ }
127
+ }
128
+ else
129
+ chunks.push(s + exports.CRLF + exports.CRLF);
130
+ }
131
+ chunks.push('--' + boundary + '--' + exports.CRLF);
132
+ return boundary;
133
+ };
134
+ const boundary = processBatch(batch);
135
+ return { boundary, stream: (0, highland_1.default)(chunks).flatten() };
136
+ }
@@ -1,22 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CollectionCreateRequest = void 0;
4
- class CollectionCreateRequest {
5
- _options;
6
- constructor(_options = {}) {
7
- this._options = _options;
4
+ const url_1 = require("@opra/url");
5
+ const client_request_js_1 = require("../client-request.js");
6
+ class CollectionCreateRequest extends client_request_js_1.OpraClientRequest {
7
+ client;
8
+ resource;
9
+ data;
10
+ options;
11
+ constructor(client, send, resource, data, options = {}) {
12
+ super(client, send, options);
13
+ this.client = client;
14
+ this.resource = resource;
15
+ this.data = data;
16
+ this.options = options;
8
17
  }
9
- omit(...fields) {
10
- this._options.omit = fields.flat();
11
- return this;
12
- }
13
- pick(...fields) {
14
- this._options.pick = fields.flat();
15
- return this;
16
- }
17
- include(...fields) {
18
- this._options.include = fields.flat();
19
- return this;
18
+ prepare() {
19
+ const url = new url_1.OpraURL(this.client.serviceUrl);
20
+ url.path.join(this.resource.name);
21
+ if (this.options.include)
22
+ url.searchParams.set('$include', this.options.include);
23
+ if (this.options.pick)
24
+ url.searchParams.set('$pick', this.options.pick);
25
+ if (this.options.omit)
26
+ url.searchParams.set('$omit', this.options.omit);
27
+ return {
28
+ method: 'POST',
29
+ url: url.address,
30
+ data: this.data,
31
+ params: url.searchParams
32
+ };
20
33
  }
21
34
  }
22
35
  exports.CollectionCreateRequest = CollectionCreateRequest;
@@ -1,14 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CollectionDeleteManyRequest = void 0;
4
- class CollectionDeleteManyRequest {
5
- _options;
6
- constructor(_options = {}) {
7
- this._options = _options;
4
+ const url_1 = require("@opra/url");
5
+ const client_request_js_1 = require("../client-request.js");
6
+ class CollectionDeleteManyRequest extends client_request_js_1.OpraClientRequest {
7
+ client;
8
+ resource;
9
+ options;
10
+ constructor(client, send, resource, options = {}) {
11
+ super(client, send, options);
12
+ this.client = client;
13
+ this.resource = resource;
14
+ this.options = options;
8
15
  }
9
- filter(value) {
10
- this._options.filter = value;
11
- return this;
16
+ prepare() {
17
+ const url = new url_1.OpraURL(this.client.serviceUrl);
18
+ url.path.join(this.resource.name);
19
+ if (this.options.filter)
20
+ url.searchParams.set('$filter', this.options.filter);
21
+ return {
22
+ method: 'DELETE',
23
+ url: url.address,
24
+ params: url.searchParams
25
+ };
12
26
  }
13
27
  }
14
28
  exports.CollectionDeleteManyRequest = CollectionDeleteManyRequest;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionDeleteRequest = void 0;
4
+ const url_1 = require("@opra/url");
5
+ const client_request_js_1 = require("../client-request.js");
6
+ class CollectionDeleteRequest extends client_request_js_1.OpraClientRequest {
7
+ client;
8
+ resource;
9
+ keyValue;
10
+ options;
11
+ constructor(client, send, resource, keyValue, options = {}) {
12
+ super(client, send, options);
13
+ this.client = client;
14
+ this.resource = resource;
15
+ this.keyValue = keyValue;
16
+ this.options = options;
17
+ }
18
+ prepare() {
19
+ const url = new url_1.OpraURL(this.client.serviceUrl);
20
+ url.path.join(this.resource.name);
21
+ url.path.get(url.path.size - 1).key = this.keyValue;
22
+ return {
23
+ method: 'DELETE',
24
+ url: url.address,
25
+ params: url.searchParams
26
+ };
27
+ }
28
+ }
29
+ exports.CollectionDeleteRequest = CollectionDeleteRequest;
@@ -1,22 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CollectionGetRequest = void 0;
4
- class CollectionGetRequest {
5
- _options;
6
- constructor(_options = {}) {
7
- this._options = _options;
4
+ const url_1 = require("@opra/url");
5
+ const client_request_js_1 = require("../client-request.js");
6
+ class CollectionGetRequest extends client_request_js_1.OpraClientRequest {
7
+ client;
8
+ resource;
9
+ keyValue;
10
+ options;
11
+ constructor(client, send, resource, keyValue, options = {}) {
12
+ super(client, send, options);
13
+ this.client = client;
14
+ this.resource = resource;
15
+ this.keyValue = keyValue;
16
+ this.options = options;
8
17
  }
9
- omit(...fields) {
10
- this._options.omit = fields.flat();
11
- return this;
12
- }
13
- pick(...fields) {
14
- this._options.pick = fields.flat();
15
- return this;
16
- }
17
- include(...fields) {
18
- this._options.include = fields.flat();
19
- return this;
18
+ prepare() {
19
+ const url = new url_1.OpraURL(this.client.serviceUrl);
20
+ url.path.join(this.resource.name);
21
+ url.path.get(url.path.size - 1).key = this.keyValue;
22
+ if (this.options.include)
23
+ url.searchParams.set('$include', this.options.include);
24
+ if (this.options.pick)
25
+ url.searchParams.set('$pick', this.options.pick);
26
+ if (this.options.omit)
27
+ url.searchParams.set('$omit', this.options.omit);
28
+ return {
29
+ method: 'GET',
30
+ url: url.address,
31
+ params: url.searchParams
32
+ };
20
33
  }
21
34
  }
22
35
  exports.CollectionGetRequest = CollectionGetRequest;