@opra/client 0.4.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 (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/cjs/client-error.js +20 -0
  4. package/cjs/client.js +96 -0
  5. package/cjs/index.js +8 -0
  6. package/cjs/observable-promise.js +10 -0
  7. package/cjs/package.json +3 -0
  8. package/cjs/requests/collection-create-request.js +22 -0
  9. package/cjs/requests/collection-delete-many-request.js +14 -0
  10. package/cjs/requests/collection-get-request.js +22 -0
  11. package/cjs/requests/collection-search-request.js +46 -0
  12. package/cjs/requests/collection-update-many-request.js +14 -0
  13. package/cjs/requests/collection-update-request.js +22 -0
  14. package/cjs/requests/singleton-get-request.js +22 -0
  15. package/cjs/response.js +2 -0
  16. package/cjs/services/collection-service.js +192 -0
  17. package/cjs/services/singleton-service.js +43 -0
  18. package/cjs/types.js +11 -0
  19. package/esm/client-error.d.ts +11 -0
  20. package/esm/client-error.js +16 -0
  21. package/esm/client.d.ts +25 -0
  22. package/esm/client.js +91 -0
  23. package/esm/index.d.ts +5 -0
  24. package/esm/index.js +5 -0
  25. package/esm/observable-promise.d.ts +2 -0
  26. package/esm/observable-promise.js +6 -0
  27. package/esm/requests/collection-create-request.d.ts +8 -0
  28. package/esm/requests/collection-create-request.js +18 -0
  29. package/esm/requests/collection-delete-many-request.d.ts +7 -0
  30. package/esm/requests/collection-delete-many-request.js +10 -0
  31. package/esm/requests/collection-get-request.d.ts +8 -0
  32. package/esm/requests/collection-get-request.js +18 -0
  33. package/esm/requests/collection-search-request.d.ts +15 -0
  34. package/esm/requests/collection-search-request.js +42 -0
  35. package/esm/requests/collection-update-many-request.d.ts +7 -0
  36. package/esm/requests/collection-update-many-request.js +10 -0
  37. package/esm/requests/collection-update-request.d.ts +8 -0
  38. package/esm/requests/collection-update-request.js +18 -0
  39. package/esm/requests/singleton-get-request.d.ts +8 -0
  40. package/esm/requests/singleton-get-request.js +18 -0
  41. package/esm/response.d.ts +9 -0
  42. package/esm/response.js +1 -0
  43. package/esm/services/collection-service.d.ts +31 -0
  44. package/esm/services/collection-service.js +188 -0
  45. package/esm/services/singleton-service.d.ts +14 -0
  46. package/esm/services/singleton-service.js +39 -0
  47. package/esm/types.d.ts +10 -0
  48. package/esm/types.js +10 -0
  49. package/package.json +68 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Panates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @opra/client
2
+
3
+ OPRA client package.
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientError = void 0;
4
+ class ClientError extends Error {
5
+ cause;
6
+ issues;
7
+ status;
8
+ constructor(init, cause) {
9
+ super(init.message);
10
+ this.cause = cause;
11
+ this.issues = init.issues || [];
12
+ this.status = init.status;
13
+ if (cause) {
14
+ this.cause = cause;
15
+ if (cause.stack)
16
+ this.stack = cause.stack;
17
+ }
18
+ }
19
+ }
20
+ exports.ClientError = ClientError;
package/cjs/client.js ADDED
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraClient = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const axios_1 = tslib_1.__importDefault(require("axios"));
6
+ const common_1 = require("@opra/common");
7
+ const schema_1 = require("@opra/schema");
8
+ const url_1 = require("@opra/url");
9
+ 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");
12
+ class OpraClient {
13
+ serviceUrl;
14
+ options;
15
+ _metadata;
16
+ constructor(serviceUrl, options) {
17
+ this.options = { ...options };
18
+ this.serviceUrl = (0, url_1.normalizePath)(serviceUrl);
19
+ }
20
+ get metadata() {
21
+ return this._metadata;
22
+ }
23
+ collection(name, options) {
24
+ 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);
31
+ }
32
+ singleton(name, options) {
33
+ 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);
40
+ }
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
51
+ });
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;
82
+ }
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);
89
+ }
90
+ static async create(serviceUrl, options) {
91
+ const client = new this(serviceUrl, options);
92
+ await client._fetchMetadata();
93
+ return client;
94
+ }
95
+ }
96
+ exports.OpraClient = OpraClient;
package/cjs/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./client.js"), exports);
5
+ tslib_1.__exportStar(require("./response.js"), exports);
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);
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.observablePromise = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ function observablePromise(promise) {
6
+ const observable = (0, rxjs_1.from)(promise);
7
+ observable.then = (...args) => promise.then(...args);
8
+ return observable;
9
+ }
10
+ exports.observablePromise = observablePromise;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionCreateRequest = void 0;
4
+ class CollectionCreateRequest {
5
+ _options;
6
+ constructor(_options = {}) {
7
+ this._options = _options;
8
+ }
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;
20
+ }
21
+ }
22
+ exports.CollectionCreateRequest = CollectionCreateRequest;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionDeleteManyRequest = void 0;
4
+ class CollectionDeleteManyRequest {
5
+ _options;
6
+ constructor(_options = {}) {
7
+ this._options = _options;
8
+ }
9
+ filter(value) {
10
+ this._options.filter = value;
11
+ return this;
12
+ }
13
+ }
14
+ exports.CollectionDeleteManyRequest = CollectionDeleteManyRequest;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionGetRequest = void 0;
4
+ class CollectionGetRequest {
5
+ _options;
6
+ constructor(_options = {}) {
7
+ this._options = _options;
8
+ }
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;
20
+ }
21
+ }
22
+ exports.CollectionGetRequest = CollectionGetRequest;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionSearchRequest = void 0;
4
+ class CollectionSearchRequest {
5
+ _options;
6
+ constructor(_options = {}) {
7
+ this._options = _options;
8
+ }
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;
20
+ }
21
+ limit(value) {
22
+ this._options.limit = value;
23
+ return this;
24
+ }
25
+ skip(value) {
26
+ this._options.skip = value;
27
+ return this;
28
+ }
29
+ count(value = true) {
30
+ this._options.count = value;
31
+ return this;
32
+ }
33
+ distinct(value) {
34
+ this._options.distinct = value;
35
+ return this;
36
+ }
37
+ sort(...fields) {
38
+ this._options.sort = fields.flat();
39
+ return this;
40
+ }
41
+ filter(value) {
42
+ this._options.filter = value;
43
+ return this;
44
+ }
45
+ }
46
+ exports.CollectionSearchRequest = CollectionSearchRequest;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionUpdateManyRequest = void 0;
4
+ class CollectionUpdateManyRequest {
5
+ _options;
6
+ constructor(_options = {}) {
7
+ this._options = _options;
8
+ }
9
+ filter(value) {
10
+ this._options.filter = value;
11
+ return this;
12
+ }
13
+ }
14
+ exports.CollectionUpdateManyRequest = CollectionUpdateManyRequest;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionUpdateRequest = void 0;
4
+ class CollectionUpdateRequest {
5
+ _options;
6
+ constructor(_options = {}) {
7
+ this._options = _options;
8
+ }
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;
20
+ }
21
+ }
22
+ exports.CollectionUpdateRequest = CollectionUpdateRequest;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SingletonGetRequest = void 0;
4
+ class SingletonGetRequest {
5
+ _options;
6
+ constructor(_options = {}) {
7
+ this._options = _options;
8
+ }
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;
20
+ }
21
+ }
22
+ exports.SingletonGetRequest = SingletonGetRequest;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionService = void 0;
4
+ const url_1 = require("@opra/url");
5
+ const observable_promise_js_1 = require("../observable-promise.js");
6
+ const collection_create_request_js_1 = require("../requests/collection-create-request.js");
7
+ const collection_delete_many_request_js_1 = require("../requests/collection-delete-many-request.js");
8
+ const collection_get_request_js_1 = require("../requests/collection-get-request.js");
9
+ const collection_search_request_js_1 = require("../requests/collection-search-request.js");
10
+ const collection_update_many_request_js_1 = require("../requests/collection-update-many-request.js");
11
+ const collection_update_request_js_1 = require("../requests/collection-update-request.js");
12
+ class CollectionService {
13
+ _serviceUrl;
14
+ _document;
15
+ _handler;
16
+ _resource;
17
+ constructor(_serviceUrl, _document, _handler, _resource) {
18
+ this._serviceUrl = _serviceUrl;
19
+ this._document = _document;
20
+ this._handler = _handler;
21
+ this._resource = _resource;
22
+ }
23
+ create(data, options) {
24
+ const requestOptions = options && typeof options === 'object' ? options : {};
25
+ const requestWrapper = new collection_create_request_js_1.CollectionCreateRequest(requestOptions);
26
+ if (typeof options === 'function')
27
+ options(requestWrapper);
28
+ const req = this._prepareCreateRequest(data, requestOptions);
29
+ const promise = this._handler(req);
30
+ return (0, observable_promise_js_1.observablePromise)(promise);
31
+ }
32
+ delete(keyValue) {
33
+ const req = this._prepareDeleteRequest(keyValue);
34
+ const promise = this._handler(req);
35
+ return (0, observable_promise_js_1.observablePromise)(promise);
36
+ }
37
+ deleteMany(options) {
38
+ const requestOptions = options && typeof options === 'object' ? options : {};
39
+ const requestWrapper = new collection_delete_many_request_js_1.CollectionDeleteManyRequest(requestOptions);
40
+ if (typeof options === 'function')
41
+ options(requestWrapper);
42
+ const req = this._prepareDeleteManyRequest(requestOptions);
43
+ const promise = this._handler(req);
44
+ return (0, observable_promise_js_1.observablePromise)(promise);
45
+ }
46
+ get(keyValue, options) {
47
+ const requestOptions = options && typeof options === 'object' ? options : {};
48
+ const requestWrapper = new collection_get_request_js_1.CollectionGetRequest(requestOptions);
49
+ if (typeof options === 'function')
50
+ options(requestWrapper);
51
+ const req = this._prepareGetRequest(keyValue, requestOptions);
52
+ const promise = this._handler(req);
53
+ return (0, observable_promise_js_1.observablePromise)(promise);
54
+ }
55
+ search(options) {
56
+ const requestOptions = options && typeof options === 'object' ? options : {};
57
+ const requestWrapper = new collection_search_request_js_1.CollectionSearchRequest(requestOptions);
58
+ if (typeof options === 'function')
59
+ options(requestWrapper);
60
+ const req = this._prepareSearchRequest(requestOptions);
61
+ const promise = this._handler(req);
62
+ return (0, observable_promise_js_1.observablePromise)(promise);
63
+ }
64
+ update(keyValue, data, options) {
65
+ const requestOptions = options && typeof options === 'object' ? options : {};
66
+ const requestWrapper = new collection_update_request_js_1.CollectionUpdateRequest(requestOptions);
67
+ if (typeof options === 'function')
68
+ options(requestWrapper);
69
+ const req = this._prepareUpdateRequest(keyValue, data, requestOptions);
70
+ const promise = this._handler(req);
71
+ return (0, observable_promise_js_1.observablePromise)(promise);
72
+ }
73
+ updateMany(data, options) {
74
+ const requestOptions = options && typeof options === 'object' ? options : {};
75
+ const requestWrapper = new collection_update_many_request_js_1.CollectionUpdateManyRequest(requestOptions);
76
+ if (typeof options === 'function')
77
+ options(requestWrapper);
78
+ const req = this._prepareUpdateManyRequest(data, requestOptions);
79
+ const promise = this._handler(req);
80
+ return (0, observable_promise_js_1.observablePromise)(promise);
81
+ }
82
+ _prepareCreateRequest(data, options) {
83
+ const url = new url_1.OpraURL(this._serviceUrl);
84
+ url.path.join(this._resource.name);
85
+ if (options.include)
86
+ url.searchParams.set('$include', options.include);
87
+ if (options.pick)
88
+ url.searchParams.set('$pick', options.pick);
89
+ if (options.omit)
90
+ url.searchParams.set('$omit', options.omit);
91
+ return {
92
+ method: 'POST',
93
+ url: url.address,
94
+ data,
95
+ params: url.searchParams
96
+ };
97
+ }
98
+ _prepareDeleteRequest(keyValue) {
99
+ const url = new url_1.OpraURL(this._serviceUrl);
100
+ url.path.join(this._resource.name);
101
+ url.path.get(url.path.size - 1).key = keyValue;
102
+ return {
103
+ method: 'DELETE',
104
+ url: url.address,
105
+ params: url.searchParams
106
+ };
107
+ }
108
+ _prepareDeleteManyRequest(options) {
109
+ const url = new url_1.OpraURL(this._serviceUrl);
110
+ url.path.join(this._resource.name);
111
+ if (options.filter)
112
+ url.searchParams.set('$filter', options.filter);
113
+ return {
114
+ method: 'DELETE',
115
+ url: url.address,
116
+ params: url.searchParams
117
+ };
118
+ }
119
+ _prepareGetRequest(keyValue, options) {
120
+ const url = new url_1.OpraURL(this._serviceUrl);
121
+ url.path.join(this._resource.name);
122
+ url.path.get(url.path.size - 1).key = keyValue;
123
+ if (options.include)
124
+ url.searchParams.set('$include', options.include);
125
+ if (options.pick)
126
+ url.searchParams.set('$pick', options.pick);
127
+ if (options.omit)
128
+ url.searchParams.set('$omit', options.omit);
129
+ return {
130
+ method: 'GET',
131
+ url: url.address,
132
+ params: url.searchParams
133
+ };
134
+ }
135
+ _prepareSearchRequest(options) {
136
+ const url = new url_1.OpraURL(this._serviceUrl);
137
+ url.path.join(this._resource.name);
138
+ if (options.include)
139
+ url.searchParams.set('$include', options.include);
140
+ if (options.pick)
141
+ url.searchParams.set('$pick', options.pick);
142
+ if (options.omit)
143
+ url.searchParams.set('$omit', options.omit);
144
+ if (options.sort)
145
+ url.searchParams.set('$sort', options.sort);
146
+ if (options.filter)
147
+ url.searchParams.set('$filter', options.filter);
148
+ if (options.limit != null)
149
+ url.searchParams.set('$limit', options.limit);
150
+ if (options.skip != null)
151
+ url.searchParams.set('$skip', options.skip);
152
+ if (options.count != null)
153
+ url.searchParams.set('$count', options.count);
154
+ if (options.distinct != null)
155
+ url.searchParams.set('$distinct', options.distinct);
156
+ return {
157
+ method: 'GET',
158
+ url: url.address,
159
+ params: url.searchParams
160
+ };
161
+ }
162
+ _prepareUpdateRequest(keyValue, data, options) {
163
+ const url = new url_1.OpraURL(this._serviceUrl);
164
+ url.path.join(this._resource.name);
165
+ url.path.get(url.path.size - 1).key = keyValue;
166
+ if (options.include)
167
+ url.searchParams.set('$include', options.include);
168
+ if (options.pick)
169
+ url.searchParams.set('$pick', options.pick);
170
+ if (options.omit)
171
+ url.searchParams.set('$omit', options.omit);
172
+ return {
173
+ method: 'PATCH',
174
+ url: url.address,
175
+ data,
176
+ params: url.searchParams
177
+ };
178
+ }
179
+ _prepareUpdateManyRequest(data, options) {
180
+ const url = new url_1.OpraURL(this._serviceUrl);
181
+ url.path.join(this._resource.name);
182
+ if (options.filter)
183
+ url.searchParams.set('$filter', options.filter);
184
+ return {
185
+ method: 'PATCH',
186
+ url: url.address,
187
+ data,
188
+ params: url.searchParams
189
+ };
190
+ }
191
+ }
192
+ exports.CollectionService = CollectionService;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SingletonService = void 0;
4
+ const url_1 = require("@opra/url");
5
+ const observable_promise_js_1 = require("../observable-promise.js");
6
+ const singleton_get_request_js_1 = require("../requests/singleton-get-request.js");
7
+ class SingletonService {
8
+ _serviceUrl;
9
+ _document;
10
+ _handler;
11
+ _resource;
12
+ constructor(_serviceUrl, _document, _handler, _resource) {
13
+ this._serviceUrl = _serviceUrl;
14
+ this._document = _document;
15
+ this._handler = _handler;
16
+ this._resource = _resource;
17
+ }
18
+ get(options) {
19
+ const requestOptions = options && typeof options === 'object' ? options : {};
20
+ const requestWrapper = new singleton_get_request_js_1.SingletonGetRequest(requestOptions);
21
+ if (typeof options === 'function')
22
+ options(requestWrapper);
23
+ const req = this._prepareGetRequest(requestOptions);
24
+ const promise = this._handler(req);
25
+ return (0, observable_promise_js_1.observablePromise)(promise);
26
+ }
27
+ _prepareGetRequest(options) {
28
+ const url = new url_1.OpraURL(this._serviceUrl);
29
+ url.path.join(this._resource.name);
30
+ if (options.include)
31
+ url.searchParams.set('$include', options.include);
32
+ if (options.pick)
33
+ url.searchParams.set('$pick', options.pick);
34
+ if (options.omit)
35
+ url.searchParams.set('$omit', options.omit);
36
+ return {
37
+ method: 'GET',
38
+ url: url.address,
39
+ params: url.searchParams
40
+ };
41
+ }
42
+ }
43
+ exports.SingletonService = SingletonService;
package/cjs/types.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // export type CollectionCreateRequestOptions = CollectionCreateQueryOptions & CommonRequestOptions;
4
+ // export type CollectionDeleteRequestOptions = CommonRequestOptions;
5
+ // export type CollectionDeleteManyRequestOptions = CollectionDeleteManyQueryOptions & CommonRequestOptions;
6
+ // export type CollectionGetRequestOptions = CollectionGetQueryOptions & CommonRequestOptions;
7
+ // export type CollectionUpdateRequestOptions = CollectionUpdateQueryOptions & CommonRequestOptions;
8
+ // export type CollectionUpdateManyRequestOptions = CollectionUpdateManyQueryOptions & CommonRequestOptions;
9
+ // export type CollectionSearchRequestOptions = CollectionSearchQueryOptions & CommonRequestOptions;
10
+ //
11
+ // export type SingletonGetRequestOptions = SingletonGetQueryOptions & CommonRequestOptions;
@@ -0,0 +1,11 @@
1
+ import { ErrorIssue } from '@opra/exception';
2
+ export declare class ClientError extends Error {
3
+ cause?: Error | undefined;
4
+ issues: ErrorIssue[];
5
+ status?: number;
6
+ constructor(init: {
7
+ message: string;
8
+ issues?: ErrorIssue[];
9
+ status?: number;
10
+ }, cause?: Error | undefined);
11
+ }
@@ -0,0 +1,16 @@
1
+ export class ClientError extends Error {
2
+ cause;
3
+ issues;
4
+ status;
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
+ }
@@ -0,0 +1,25 @@
1
+ import { AxiosAdapter, AxiosInstance, AxiosRequestConfig } from 'axios';
2
+ import { OpraDocument } from '@opra/schema';
3
+ import { OpraResponse } from './response.js';
4
+ import { CollectionService } from './services/collection-service.js';
5
+ import { SingletonService } from './services/singleton-service.js';
6
+ import { CommonRequestOptions } from './types.js';
7
+ export interface OpraClientOptions {
8
+ adapter?: AxiosAdapter;
9
+ resetCache?: boolean;
10
+ defaultHeaders?: Record<string, string>;
11
+ validateStatus?: boolean | ((status: number) => boolean);
12
+ }
13
+ export declare class OpraClient {
14
+ serviceUrl: string;
15
+ options: OpraClientOptions;
16
+ protected _metadata: OpraDocument;
17
+ protected constructor(serviceUrl: string, options?: OpraClientOptions);
18
+ get metadata(): OpraDocument;
19
+ collection<T = any, TResponse extends OpraResponse<T> = OpraResponse<T>>(name: string, options?: CommonRequestOptions): CollectionService<T, TResponse>;
20
+ singleton<T = any, TResponse extends OpraResponse<T> = OpraResponse<T>>(name: string, options?: CommonRequestOptions): SingletonService<T, TResponse>;
21
+ protected _send(req: AxiosRequestConfig, options?: CommonRequestOptions): Promise<OpraResponse>;
22
+ protected _createAxiosInstance(options?: CommonRequestOptions): AxiosInstance;
23
+ protected _fetchMetadata(): Promise<void>;
24
+ static create(serviceUrl: string, options?: OpraClientOptions): Promise<OpraClient>;
25
+ }