@opra/client 0.4.0 → 0.6.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.
- package/cjs/constants.js +6 -0
- package/cjs/http/http-client.js +164 -0
- package/cjs/http/http-collection-service.js +40 -0
- package/cjs/http/http-request.js +66 -0
- package/cjs/{response.js → http/http-response.js} +0 -0
- package/cjs/http/http-singleton-service.js +16 -0
- package/cjs/http/http-types.js +2 -0
- package/cjs/http/requests/batch-request.js +80 -0
- package/cjs/http/requests/collection-create-request.js +35 -0
- package/cjs/http/requests/collection-delete-many-request.js +28 -0
- package/cjs/http/requests/collection-delete-request.js +29 -0
- package/cjs/http/requests/collection-get-request.js +37 -0
- package/cjs/http/requests/collection-search-request.js +44 -0
- package/cjs/http/requests/collection-update-many-request.js +31 -0
- package/cjs/http/requests/collection-update-request.js +40 -0
- package/cjs/http/requests/http-singleton-get-request.js +31 -0
- package/cjs/http/utils/merge-raw-http-requests.util.js +29 -0
- package/cjs/index.js +16 -4
- package/cjs/interfaces/http-options.interface.js +2 -0
- package/cjs/interfaces/http-request-config.interface.js +2 -0
- package/cjs/types.js +0 -9
- package/esm/client-error.d.ts +1 -1
- package/esm/constants.d.ts +3 -0
- package/esm/constants.js +3 -0
- package/esm/http/http-client.d.ts +26 -0
- package/esm/http/http-client.js +160 -0
- package/esm/http/http-collection-service.d.ts +22 -0
- package/esm/http/http-collection-service.js +36 -0
- package/esm/http/http-request.d.ts +25 -0
- package/esm/http/http-request.js +62 -0
- package/esm/http/http-response.d.ts +14 -0
- package/esm/{response.js → http/http-response.js} +0 -0
- package/esm/http/http-singleton-service.d.ts +10 -0
- package/esm/http/http-singleton-service.js +12 -0
- package/esm/http/http-types.d.ts +26 -0
- package/esm/http/http-types.js +1 -0
- package/esm/http/requests/batch-request.d.ts +18 -0
- package/esm/http/requests/batch-request.js +76 -0
- package/esm/http/requests/collection-create-request.d.ts +12 -0
- package/esm/http/requests/collection-create-request.js +31 -0
- package/esm/http/requests/collection-delete-many-request.d.ts +11 -0
- package/esm/http/requests/collection-delete-many-request.js +24 -0
- package/esm/http/requests/collection-delete-request.d.ts +12 -0
- package/esm/http/requests/collection-delete-request.js +25 -0
- package/esm/http/requests/collection-get-request.d.ts +12 -0
- package/esm/http/requests/collection-get-request.js +33 -0
- package/esm/http/requests/collection-search-request.d.ts +11 -0
- package/esm/http/requests/collection-search-request.js +40 -0
- package/esm/http/requests/collection-update-many-request.d.ts +12 -0
- package/esm/http/requests/collection-update-many-request.js +27 -0
- package/esm/http/requests/collection-update-request.d.ts +13 -0
- package/esm/http/requests/collection-update-request.js +36 -0
- package/esm/http/requests/http-singleton-get-request.d.ts +11 -0
- package/esm/http/requests/http-singleton-get-request.js +27 -0
- package/esm/http/utils/merge-raw-http-requests.util.d.ts +2 -0
- package/esm/http/utils/merge-raw-http-requests.util.js +25 -0
- package/esm/index.d.ts +16 -4
- package/esm/index.js +16 -4
- package/esm/interfaces/http-options.interface.d.ts +20 -0
- package/esm/interfaces/http-options.interface.js +1 -0
- package/esm/interfaces/http-request-config.interface.d.ts +23 -0
- package/esm/interfaces/http-request-config.interface.js +1 -0
- package/esm/types.d.ts +7 -10
- package/esm/types.js +0 -9
- package/package.json +25 -16
- package/umd/opra-client.umd.min.js +1 -0
- package/cjs/client.js +0 -96
- package/cjs/observable-promise.js +0 -10
- package/cjs/requests/collection-create-request.js +0 -22
- package/cjs/requests/collection-delete-many-request.js +0 -14
- package/cjs/requests/collection-get-request.js +0 -22
- package/cjs/requests/collection-search-request.js +0 -46
- package/cjs/requests/collection-update-many-request.js +0 -14
- package/cjs/requests/collection-update-request.js +0 -22
- package/cjs/requests/singleton-get-request.js +0 -22
- package/cjs/services/collection-service.js +0 -192
- package/cjs/services/singleton-service.js +0 -43
- package/esm/client.d.ts +0 -25
- package/esm/client.js +0 -91
- package/esm/observable-promise.d.ts +0 -2
- package/esm/observable-promise.js +0 -6
- package/esm/requests/collection-create-request.d.ts +0 -8
- package/esm/requests/collection-create-request.js +0 -18
- package/esm/requests/collection-delete-many-request.d.ts +0 -7
- package/esm/requests/collection-delete-many-request.js +0 -10
- package/esm/requests/collection-get-request.d.ts +0 -8
- package/esm/requests/collection-get-request.js +0 -18
- package/esm/requests/collection-search-request.d.ts +0 -15
- package/esm/requests/collection-search-request.js +0 -42
- package/esm/requests/collection-update-many-request.d.ts +0 -7
- package/esm/requests/collection-update-many-request.js +0 -10
- package/esm/requests/collection-update-request.d.ts +0 -8
- package/esm/requests/collection-update-request.js +0 -18
- package/esm/requests/singleton-get-request.d.ts +0 -8
- package/esm/requests/singleton-get-request.js +0 -18
- package/esm/response.d.ts +0 -9
- package/esm/services/collection-service.d.ts +0 -31
- package/esm/services/collection-service.js +0 -188
- package/esm/services/singleton-service.d.ts +0 -14
- package/esm/services/singleton-service.js +0 -39
package/cjs/client.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
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;
|
|
@@ -1,10 +0,0 @@
|
|
|
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;
|
|
@@ -1,22 +0,0 @@
|
|
|
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;
|
|
@@ -1,14 +0,0 @@
|
|
|
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;
|
|
@@ -1,22 +0,0 @@
|
|
|
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;
|
|
@@ -1,46 +0,0 @@
|
|
|
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;
|
|
@@ -1,14 +0,0 @@
|
|
|
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;
|
|
@@ -1,22 +0,0 @@
|
|
|
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;
|
|
@@ -1,22 +0,0 @@
|
|
|
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;
|
|
@@ -1,192 +0,0 @@
|
|
|
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;
|
|
@@ -1,43 +0,0 @@
|
|
|
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/esm/client.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|
package/esm/client.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import { ResponsiveMap } from '@opra/common';
|
|
3
|
-
import { OpraDocument } from '@opra/schema';
|
|
4
|
-
import { joinPath, normalizePath } from '@opra/url';
|
|
5
|
-
import { ClientError } from './client-error.js';
|
|
6
|
-
import { CollectionService } from './services/collection-service.js';
|
|
7
|
-
import { SingletonService } from './services/singleton-service.js';
|
|
8
|
-
export class OpraClient {
|
|
9
|
-
serviceUrl;
|
|
10
|
-
options;
|
|
11
|
-
_metadata;
|
|
12
|
-
constructor(serviceUrl, options) {
|
|
13
|
-
this.options = { ...options };
|
|
14
|
-
this.serviceUrl = normalizePath(serviceUrl);
|
|
15
|
-
}
|
|
16
|
-
get metadata() {
|
|
17
|
-
return this._metadata;
|
|
18
|
-
}
|
|
19
|
-
collection(name, options) {
|
|
20
|
-
const resource = this.metadata.getCollectionResource(name);
|
|
21
|
-
const commonOptions = {
|
|
22
|
-
headers: this.options.defaultHeaders,
|
|
23
|
-
validateStatus: this.options.validateStatus,
|
|
24
|
-
...options,
|
|
25
|
-
};
|
|
26
|
-
return new CollectionService(this.serviceUrl, this.metadata, (req) => this._send(req, commonOptions), resource);
|
|
27
|
-
}
|
|
28
|
-
singleton(name, options) {
|
|
29
|
-
const resource = this.metadata.getSingletonResource(name);
|
|
30
|
-
const commonOptions = {
|
|
31
|
-
headers: this.options.defaultHeaders,
|
|
32
|
-
validateStatus: this.options.validateStatus,
|
|
33
|
-
...options,
|
|
34
|
-
};
|
|
35
|
-
return new SingletonService(this.serviceUrl, this.metadata, (req) => this._send(req, commonOptions), resource);
|
|
36
|
-
}
|
|
37
|
-
async _send(req, options) {
|
|
38
|
-
const axiosInstance = this._createAxiosInstance(options);
|
|
39
|
-
const resp = await axiosInstance.request({ ...req, validateStatus: undefined });
|
|
40
|
-
const validateStatus = options?.validateStatus;
|
|
41
|
-
if ((validateStatus === true && !(resp.status >= 200 && resp.status < 300)) ||
|
|
42
|
-
(typeof validateStatus === 'function' && !validateStatus(resp.status))) {
|
|
43
|
-
throw new ClientError({
|
|
44
|
-
message: resp.statusText,
|
|
45
|
-
status: resp.status,
|
|
46
|
-
issues: resp.data.errors
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
const rawHeaders = (typeof resp.headers.toJSON === 'function'
|
|
50
|
-
? resp.headers.toJSON()
|
|
51
|
-
: { ...resp.headers });
|
|
52
|
-
const headers = new ResponsiveMap(rawHeaders);
|
|
53
|
-
return {
|
|
54
|
-
status: resp.status,
|
|
55
|
-
statusText: resp.statusText,
|
|
56
|
-
data: resp.data,
|
|
57
|
-
rawHeaders,
|
|
58
|
-
headers
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
_createAxiosInstance(options) {
|
|
62
|
-
const axiosInstance = axios.create();
|
|
63
|
-
axiosInstance.defaults.adapter = this.options.adapter;
|
|
64
|
-
axiosInstance.defaults.headers.common = { ...options?.headers, ...this.options.defaultHeaders };
|
|
65
|
-
if (options?.validateStatus != null) {
|
|
66
|
-
if (options.validateStatus === false)
|
|
67
|
-
axiosInstance.defaults.validateStatus = () => true;
|
|
68
|
-
else if (typeof options.validateStatus === 'function')
|
|
69
|
-
axiosInstance.defaults.validateStatus = options.validateStatus;
|
|
70
|
-
}
|
|
71
|
-
else if (this.options.validateStatus != null) {
|
|
72
|
-
if (this.options.validateStatus === false)
|
|
73
|
-
axiosInstance.defaults.validateStatus = () => true;
|
|
74
|
-
else if (typeof this.options.validateStatus === 'function')
|
|
75
|
-
axiosInstance.defaults.validateStatus = this.options.validateStatus;
|
|
76
|
-
}
|
|
77
|
-
return axiosInstance;
|
|
78
|
-
}
|
|
79
|
-
async _fetchMetadata() {
|
|
80
|
-
const resp = await this._send({
|
|
81
|
-
method: 'GET',
|
|
82
|
-
url: joinPath(this.serviceUrl || '/', '$metadata'),
|
|
83
|
-
}, { validateStatus: true });
|
|
84
|
-
this._metadata = new OpraDocument(resp.data);
|
|
85
|
-
}
|
|
86
|
-
static async create(serviceUrl, options) {
|
|
87
|
-
const client = new this(serviceUrl, options);
|
|
88
|
-
await client._fetchMetadata();
|
|
89
|
-
return client;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { CollectionCreateQueryOptions } from '@opra/schema';
|
|
2
|
-
export declare class CollectionCreateRequest {
|
|
3
|
-
protected _options: CollectionCreateQueryOptions;
|
|
4
|
-
constructor(_options?: CollectionCreateQueryOptions);
|
|
5
|
-
omit(...fields: (string | string[])[]): this;
|
|
6
|
-
pick(...fields: (string | string[])[]): this;
|
|
7
|
-
include(...fields: (string | string[])[]): this;
|
|
8
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export class CollectionCreateRequest {
|
|
2
|
-
_options;
|
|
3
|
-
constructor(_options = {}) {
|
|
4
|
-
this._options = _options;
|
|
5
|
-
}
|
|
6
|
-
omit(...fields) {
|
|
7
|
-
this._options.omit = fields.flat();
|
|
8
|
-
return this;
|
|
9
|
-
}
|
|
10
|
-
pick(...fields) {
|
|
11
|
-
this._options.pick = fields.flat();
|
|
12
|
-
return this;
|
|
13
|
-
}
|
|
14
|
-
include(...fields) {
|
|
15
|
-
this._options.include = fields.flat();
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
}
|