@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.
- package/cjs/client-request.js +48 -0
- package/cjs/client.js +111 -68
- package/cjs/index.js +13 -3
- package/cjs/requests/batch-request.js +136 -0
- package/cjs/requests/collection-create-request.js +28 -15
- package/cjs/requests/collection-delete-many-request.js +21 -7
- package/cjs/requests/collection-delete-request.js +29 -0
- package/cjs/requests/collection-get-request.js +28 -15
- package/cjs/requests/collection-search-request.js +38 -40
- package/cjs/requests/collection-update-many-request.js +24 -7
- package/cjs/requests/collection-update-request.js +31 -15
- package/cjs/requests/singleton-get-request.js +25 -15
- package/cjs/services/collection-node.js +42 -0
- package/cjs/services/singleton-node.js +18 -0
- package/cjs/types.js +0 -9
- package/esm/client-request.d.ts +17 -0
- package/esm/client-request.js +44 -0
- package/esm/client.d.ts +27 -18
- package/esm/client.js +112 -69
- package/esm/index.d.ts +13 -3
- package/esm/index.js +13 -3
- package/esm/requests/batch-request.d.ts +19 -0
- package/esm/requests/batch-request.js +131 -0
- package/esm/requests/collection-create-request.d.ts +12 -7
- package/esm/requests/collection-create-request.js +28 -15
- package/esm/requests/collection-delete-many-request.d.ts +11 -6
- package/esm/requests/collection-delete-many-request.js +21 -7
- package/esm/requests/collection-delete-request.d.ts +13 -0
- package/esm/requests/collection-delete-request.js +25 -0
- package/esm/requests/collection-get-request.d.ts +12 -7
- package/esm/requests/collection-get-request.js +28 -15
- package/esm/requests/collection-search-request.d.ts +11 -14
- package/esm/requests/collection-search-request.js +38 -40
- package/esm/requests/collection-update-many-request.d.ts +12 -6
- package/esm/requests/collection-update-many-request.js +24 -7
- package/esm/requests/collection-update-request.d.ts +13 -7
- package/esm/requests/collection-update-request.js +31 -15
- package/esm/requests/singleton-get-request.d.ts +11 -7
- package/esm/requests/singleton-get-request.js +25 -15
- package/esm/services/collection-node.d.ts +24 -0
- package/esm/services/collection-node.js +38 -0
- package/esm/services/singleton-node.d.ts +12 -0
- package/esm/services/singleton-node.js +14 -0
- package/esm/types.d.ts +33 -6
- package/esm/types.js +0 -9
- package/package.json +13 -6
- package/cjs/observable-promise.js +0 -10
- package/cjs/response.js +0 -2
- package/cjs/services/collection-service.js +0 -192
- package/cjs/services/singleton-service.js +0 -43
- package/esm/observable-promise.d.ts +0 -2
- package/esm/observable-promise.js +0 -6
- package/esm/response.d.ts +0 -9
- package/esm/response.js +0 -1
- 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
|
@@ -1,46 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CollectionSearchRequest = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this._options.filter = value;
|
|
43
|
-
return this;
|
|
4
|
+
const url_1 = require("@opra/url");
|
|
5
|
+
const client_request_js_1 = require("../client-request.js");
|
|
6
|
+
class CollectionSearchRequest 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;
|
|
15
|
+
}
|
|
16
|
+
prepare() {
|
|
17
|
+
const url = new url_1.OpraURL(this.client.serviceUrl);
|
|
18
|
+
url.path.join(this.resource.name);
|
|
19
|
+
if (this.options.include)
|
|
20
|
+
url.searchParams.set('$include', this.options.include);
|
|
21
|
+
if (this.options.pick)
|
|
22
|
+
url.searchParams.set('$pick', this.options.pick);
|
|
23
|
+
if (this.options.omit)
|
|
24
|
+
url.searchParams.set('$omit', this.options.omit);
|
|
25
|
+
if (this.options.sort)
|
|
26
|
+
url.searchParams.set('$sort', this.options.sort);
|
|
27
|
+
if (this.options.filter)
|
|
28
|
+
url.searchParams.set('$filter', this.options.filter);
|
|
29
|
+
if (this.options.limit != null)
|
|
30
|
+
url.searchParams.set('$limit', this.options.limit);
|
|
31
|
+
if (this.options.skip != null)
|
|
32
|
+
url.searchParams.set('$skip', this.options.skip);
|
|
33
|
+
if (this.options.count != null)
|
|
34
|
+
url.searchParams.set('$count', this.options.count);
|
|
35
|
+
if (this.options.distinct != null)
|
|
36
|
+
url.searchParams.set('$distinct', this.options.distinct);
|
|
37
|
+
return {
|
|
38
|
+
method: 'GET',
|
|
39
|
+
url: url.address,
|
|
40
|
+
params: url.searchParams
|
|
41
|
+
};
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
44
|
exports.CollectionSearchRequest = CollectionSearchRequest;
|
|
@@ -1,14 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CollectionUpdateManyRequest = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const url_1 = require("@opra/url");
|
|
5
|
+
const client_request_js_1 = require("../client-request.js");
|
|
6
|
+
class CollectionUpdateManyRequest 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
|
-
|
|
10
|
-
this.
|
|
11
|
-
|
|
18
|
+
prepare() {
|
|
19
|
+
const url = new url_1.OpraURL(this.client.serviceUrl);
|
|
20
|
+
url.path.join(this.resource.name);
|
|
21
|
+
if (this.options.filter)
|
|
22
|
+
url.searchParams.set('$filter', this.options.filter);
|
|
23
|
+
return {
|
|
24
|
+
method: 'PATCH',
|
|
25
|
+
url: url.address,
|
|
26
|
+
data: this.data,
|
|
27
|
+
params: url.searchParams
|
|
28
|
+
};
|
|
12
29
|
}
|
|
13
30
|
}
|
|
14
31
|
exports.CollectionUpdateManyRequest = CollectionUpdateManyRequest;
|
|
@@ -1,22 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CollectionUpdateRequest = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const url_1 = require("@opra/url");
|
|
5
|
+
const client_request_js_1 = require("../client-request.js");
|
|
6
|
+
class CollectionUpdateRequest extends client_request_js_1.OpraClientRequest {
|
|
7
|
+
client;
|
|
8
|
+
resource;
|
|
9
|
+
keyValue;
|
|
10
|
+
data;
|
|
11
|
+
options;
|
|
12
|
+
constructor(client, send, resource, keyValue, data, options = {}) {
|
|
13
|
+
super(client, send, options);
|
|
14
|
+
this.client = client;
|
|
15
|
+
this.resource = resource;
|
|
16
|
+
this.keyValue = keyValue;
|
|
17
|
+
this.data = data;
|
|
18
|
+
this.options = options;
|
|
8
19
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return
|
|
20
|
+
prepare() {
|
|
21
|
+
const url = new url_1.OpraURL(this.client.serviceUrl);
|
|
22
|
+
url.path.join(this.resource.name);
|
|
23
|
+
url.path.get(url.path.size - 1).key = this.keyValue;
|
|
24
|
+
if (this.options.include)
|
|
25
|
+
url.searchParams.set('$include', this.options.include);
|
|
26
|
+
if (this.options.pick)
|
|
27
|
+
url.searchParams.set('$pick', this.options.pick);
|
|
28
|
+
if (this.options.omit)
|
|
29
|
+
url.searchParams.set('$omit', this.options.omit);
|
|
30
|
+
return {
|
|
31
|
+
method: 'PATCH',
|
|
32
|
+
url: url.address,
|
|
33
|
+
data: this.data,
|
|
34
|
+
params: url.searchParams
|
|
35
|
+
};
|
|
20
36
|
}
|
|
21
37
|
}
|
|
22
38
|
exports.CollectionUpdateRequest = CollectionUpdateRequest;
|
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SingletonGetRequest = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const url_1 = require("@opra/url");
|
|
5
|
+
const client_request_js_1 = require("../client-request.js");
|
|
6
|
+
class SingletonGetRequest 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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
prepare() {
|
|
17
|
+
const url = new url_1.OpraURL(this.client.serviceUrl);
|
|
18
|
+
url.path.join(this.resource.name);
|
|
19
|
+
if (this.options.include)
|
|
20
|
+
url.searchParams.set('$include', this.options.include);
|
|
21
|
+
if (this.options.pick)
|
|
22
|
+
url.searchParams.set('$pick', this.options.pick);
|
|
23
|
+
if (this.options.omit)
|
|
24
|
+
url.searchParams.set('$omit', this.options.omit);
|
|
25
|
+
return {
|
|
26
|
+
method: 'GET',
|
|
27
|
+
url: url.address,
|
|
28
|
+
params: url.searchParams
|
|
29
|
+
};
|
|
20
30
|
}
|
|
21
31
|
}
|
|
22
32
|
exports.SingletonGetRequest = SingletonGetRequest;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CollectionNode = void 0;
|
|
4
|
+
const collection_create_request_js_1 = require("../requests/collection-create-request.js");
|
|
5
|
+
const collection_delete_many_request_js_1 = require("../requests/collection-delete-many-request.js");
|
|
6
|
+
const collection_delete_request_js_1 = require("../requests/collection-delete-request.js");
|
|
7
|
+
const collection_get_request_js_1 = require("../requests/collection-get-request.js");
|
|
8
|
+
const collection_search_request_js_1 = require("../requests/collection-search-request.js");
|
|
9
|
+
const collection_update_many_request_js_1 = require("../requests/collection-update-many-request.js");
|
|
10
|
+
const collection_update_request_js_1 = require("../requests/collection-update-request.js");
|
|
11
|
+
class CollectionNode {
|
|
12
|
+
client;
|
|
13
|
+
resource;
|
|
14
|
+
_send;
|
|
15
|
+
constructor(client, resource, _send) {
|
|
16
|
+
this.client = client;
|
|
17
|
+
this.resource = resource;
|
|
18
|
+
this._send = _send;
|
|
19
|
+
}
|
|
20
|
+
create(data, options) {
|
|
21
|
+
return new collection_create_request_js_1.CollectionCreateRequest(this.client, this._send, this.resource, data, options);
|
|
22
|
+
}
|
|
23
|
+
delete(keyValue, options) {
|
|
24
|
+
return new collection_delete_request_js_1.CollectionDeleteRequest(this.client, this._send, this.resource, keyValue, options);
|
|
25
|
+
}
|
|
26
|
+
deleteMany(options) {
|
|
27
|
+
return new collection_delete_many_request_js_1.CollectionDeleteManyRequest(this.client, this._send, this.resource, options);
|
|
28
|
+
}
|
|
29
|
+
get(keyValue, options) {
|
|
30
|
+
return new collection_get_request_js_1.CollectionGetRequest(this.client, this._send, this.resource, keyValue, options);
|
|
31
|
+
}
|
|
32
|
+
search(options) {
|
|
33
|
+
return new collection_search_request_js_1.CollectionSearchRequest(this.client, this._send, this.resource, options);
|
|
34
|
+
}
|
|
35
|
+
update(keyValue, data, options) {
|
|
36
|
+
return new collection_update_request_js_1.CollectionUpdateRequest(this.client, this._send, this.resource, keyValue, data, options);
|
|
37
|
+
}
|
|
38
|
+
updateMany(data, options) {
|
|
39
|
+
return new collection_update_many_request_js_1.CollectionUpdateManyRequest(this.client, this._send, this.resource, data, options);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.CollectionNode = CollectionNode;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SingletonNode = void 0;
|
|
4
|
+
const singleton_get_request_js_1 = require("../requests/singleton-get-request.js");
|
|
5
|
+
class SingletonNode {
|
|
6
|
+
client;
|
|
7
|
+
resource;
|
|
8
|
+
_send;
|
|
9
|
+
constructor(client, resource, _send) {
|
|
10
|
+
this.client = client;
|
|
11
|
+
this.resource = resource;
|
|
12
|
+
this._send = _send;
|
|
13
|
+
}
|
|
14
|
+
get(options) {
|
|
15
|
+
return new singleton_get_request_js_1.SingletonGetRequest(this.client, this._send, this.resource, options);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.SingletonNode = SingletonNode;
|
package/cjs/types.js
CHANGED
|
@@ -1,11 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
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,17 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import type { OpraClient } from './client.js';
|
|
4
|
+
import { ClientResponse, CommonQueryOptions } from './types.js';
|
|
5
|
+
export declare abstract class OpraClientRequest<T = any, TResponse extends ClientResponse<T> = ClientResponse<T>> extends Observable<TResponse> {
|
|
6
|
+
readonly client: OpraClient;
|
|
7
|
+
protected _send: (req: AxiosRequestConfig) => Promise<TResponse>;
|
|
8
|
+
options: CommonQueryOptions;
|
|
9
|
+
readonly id: string;
|
|
10
|
+
protected _promise?: Promise<TResponse>;
|
|
11
|
+
protected constructor(client: OpraClient, _send: (req: AxiosRequestConfig) => Promise<TResponse>, options?: CommonQueryOptions);
|
|
12
|
+
abstract prepare(): AxiosRequestConfig;
|
|
13
|
+
toPromise(): Promise<TResponse>;
|
|
14
|
+
execute(): Promise<TResponse>;
|
|
15
|
+
protected _execute(): Promise<TResponse>;
|
|
16
|
+
binding(): any;
|
|
17
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { uid } from 'uid';
|
|
3
|
+
import { HeadersMap } from '@opra/common';
|
|
4
|
+
export class OpraClientRequest extends Observable {
|
|
5
|
+
client;
|
|
6
|
+
_send;
|
|
7
|
+
options;
|
|
8
|
+
id;
|
|
9
|
+
_promise;
|
|
10
|
+
constructor(client, _send, options = {}) {
|
|
11
|
+
super((subscriber) => {
|
|
12
|
+
this.execute().then(v => {
|
|
13
|
+
subscriber.next(v);
|
|
14
|
+
subscriber.complete();
|
|
15
|
+
}).catch(e => subscriber.error(e));
|
|
16
|
+
});
|
|
17
|
+
this.client = client;
|
|
18
|
+
this._send = _send;
|
|
19
|
+
this.options = options;
|
|
20
|
+
this.id = uid(10);
|
|
21
|
+
this.options = options;
|
|
22
|
+
}
|
|
23
|
+
async toPromise() {
|
|
24
|
+
return this.execute();
|
|
25
|
+
}
|
|
26
|
+
async execute() {
|
|
27
|
+
const promise = this._promise || (this._promise = this._execute());
|
|
28
|
+
return await promise;
|
|
29
|
+
}
|
|
30
|
+
async _execute() {
|
|
31
|
+
const req = this.prepare();
|
|
32
|
+
const headers = this.options.http?.headers
|
|
33
|
+
? new HeadersMap({ ...req.headers, ...this.options.http.headers }).toObject()
|
|
34
|
+
: req.headers;
|
|
35
|
+
return this._send({
|
|
36
|
+
...this.options.http,
|
|
37
|
+
...req,
|
|
38
|
+
headers
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
binding() {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
}
|
package/esm/client.d.ts
CHANGED
|
@@ -1,25 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AxiosInstance, AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import { TaskQueue } from 'power-tasks';
|
|
3
|
+
import { Type } from 'ts-gems';
|
|
2
4
|
import { OpraDocument } from '@opra/schema';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { OpraClientRequest } from './client-request.js';
|
|
6
|
+
import { CollectionNode } from './services/collection-node.js';
|
|
7
|
+
import { SingletonNode } from './services/singleton-node.js';
|
|
8
|
+
import { ClientAdapter, ClientResponse, HttpRequestOptions } from './types.js';
|
|
7
9
|
export interface OpraClientOptions {
|
|
8
|
-
adapter?:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
adapter?: ClientAdapter;
|
|
11
|
+
defaults?: HttpRequestOptions;
|
|
12
|
+
invalidateCache?: boolean;
|
|
13
|
+
concurrency?: number;
|
|
14
|
+
maxQueue?: number;
|
|
12
15
|
}
|
|
13
16
|
export declare class OpraClient {
|
|
14
|
-
|
|
15
|
-
options: OpraClientOptions;
|
|
17
|
+
protected _axios: AxiosInstance;
|
|
16
18
|
protected _metadata: OpraDocument;
|
|
17
|
-
protected
|
|
19
|
+
protected _taskQueue: TaskQueue;
|
|
20
|
+
constructor(serviceUrl: string, options?: OpraClientOptions);
|
|
21
|
+
constructor(serviceUrl: string, metadata: OpraDocument, options?: OpraClientOptions);
|
|
22
|
+
get requestInterceptors(): AxiosInterceptorManager<AxiosRequestConfig>;
|
|
23
|
+
get responseInterceptors(): AxiosInterceptorManager<AxiosResponse>;
|
|
24
|
+
get serviceUrl(): string;
|
|
25
|
+
get initialized(): boolean;
|
|
18
26
|
get metadata(): OpraDocument;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
protected
|
|
24
|
-
|
|
27
|
+
init(): Promise<void>;
|
|
28
|
+
batch<TResponse extends ClientResponse<any> = ClientResponse<any>>(requests: OpraClientRequest[]): OpraClientRequest<any, TResponse>;
|
|
29
|
+
collection<T = any, TResponse extends ClientResponse<T> = ClientResponse<T>>(name: string): CollectionNode<T, TResponse>;
|
|
30
|
+
singleton<T = any, TResponse extends ClientResponse<T> = ClientResponse<T>>(name: string): SingletonNode<T, TResponse>;
|
|
31
|
+
protected _send<TResponse extends ClientResponse>(req: AxiosRequestConfig): Promise<TResponse>;
|
|
32
|
+
protected _assertMetadata(): void;
|
|
33
|
+
static create<T extends OpraClient>(this: Type<T>, serviceUrl: string, options?: OpraClientOptions): Promise<T>;
|
|
25
34
|
}
|
package/esm/client.js
CHANGED
|
@@ -1,91 +1,134 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
+
import { TaskQueue } from 'power-tasks';
|
|
2
3
|
import { ResponsiveMap } from '@opra/common';
|
|
3
4
|
import { OpraDocument } from '@opra/schema';
|
|
4
|
-
import {
|
|
5
|
+
import { normalizePath } from '@opra/url';
|
|
5
6
|
import { ClientError } from './client-error.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { BatchRequest } from './requests/batch-request.js';
|
|
8
|
+
import { CollectionNode } from './services/collection-node.js';
|
|
9
|
+
import { SingletonNode } from './services/singleton-node.js';
|
|
10
|
+
const documentCache = new Map();
|
|
11
|
+
const documentCacheResolvers = new Map();
|
|
8
12
|
export class OpraClient {
|
|
9
|
-
|
|
10
|
-
options;
|
|
13
|
+
_axios;
|
|
11
14
|
_metadata;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
_taskQueue;
|
|
16
|
+
constructor(serviceUrl, arg1, arg2) {
|
|
17
|
+
let options;
|
|
18
|
+
if (arg1 instanceof OpraDocument) {
|
|
19
|
+
this._metadata = arg1;
|
|
20
|
+
options = arg2;
|
|
21
|
+
}
|
|
22
|
+
else
|
|
23
|
+
options = arg1 || arg2;
|
|
24
|
+
this._taskQueue = new TaskQueue({
|
|
25
|
+
maxQueue: options?.maxQueue,
|
|
26
|
+
concurrency: options?.concurrency
|
|
27
|
+
});
|
|
28
|
+
this._axios = axios.create();
|
|
29
|
+
this._axios.defaults.baseURL = normalizePath(serviceUrl);
|
|
30
|
+
this._axios.defaults.adapter = options?.adapter;
|
|
31
|
+
if (options?.defaults?.headers)
|
|
32
|
+
this._axios.defaults.headers.common = options.defaults.headers;
|
|
33
|
+
this._axios.defaults.auth = options?.defaults?.auth;
|
|
34
|
+
this._axios.defaults.timeout = options?.defaults?.timeout;
|
|
35
|
+
this._axios.defaults.timeoutErrorMessage = options?.defaults?.timeoutErrorMessage;
|
|
36
|
+
this._axios.defaults.xsrfCookieName = options?.defaults?.xsrfCookieName;
|
|
37
|
+
this._axios.defaults.xsrfHeaderName = options?.defaults?.xsrfHeaderName;
|
|
38
|
+
this._axios.defaults.maxRedirects = options?.defaults?.maxRedirects;
|
|
39
|
+
this._axios.defaults.maxRate = options?.defaults?.maxRate;
|
|
40
|
+
this._axios.defaults.httpAgent = options?.defaults?.httpAgent;
|
|
41
|
+
this._axios.defaults.httpsAgent = options?.defaults?.httpsAgent;
|
|
42
|
+
this._axios.defaults.proxy = options?.defaults?.proxy;
|
|
43
|
+
this._axios.defaults.validateStatus = options?.defaults?.validateStatus;
|
|
44
|
+
const document = documentCache.get(serviceUrl.toLowerCase());
|
|
45
|
+
if (document)
|
|
46
|
+
this._metadata = document;
|
|
47
|
+
}
|
|
48
|
+
get requestInterceptors() {
|
|
49
|
+
return this._axios.interceptors.request;
|
|
50
|
+
}
|
|
51
|
+
get responseInterceptors() {
|
|
52
|
+
return this._axios.interceptors.response;
|
|
53
|
+
}
|
|
54
|
+
get serviceUrl() {
|
|
55
|
+
return this._axios.defaults.baseURL || '';
|
|
56
|
+
}
|
|
57
|
+
get initialized() {
|
|
58
|
+
return !!this._metadata;
|
|
15
59
|
}
|
|
16
60
|
get metadata() {
|
|
61
|
+
this._assertMetadata();
|
|
17
62
|
return this._metadata;
|
|
18
63
|
}
|
|
19
|
-
|
|
64
|
+
async init() {
|
|
65
|
+
let promise = documentCacheResolvers.get(this.serviceUrl.toLowerCase());
|
|
66
|
+
if (promise) {
|
|
67
|
+
await promise;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
promise = this._send({
|
|
71
|
+
method: 'GET',
|
|
72
|
+
url: '/$metadata',
|
|
73
|
+
});
|
|
74
|
+
documentCacheResolvers.set(this.serviceUrl.toLowerCase(), promise);
|
|
75
|
+
try {
|
|
76
|
+
const resp = await promise;
|
|
77
|
+
this._metadata = new OpraDocument(resp.data);
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
documentCacheResolvers.delete(this.serviceUrl.toLowerCase());
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
batch(requests) {
|
|
84
|
+
this._assertMetadata();
|
|
85
|
+
return new BatchRequest(this, requests, req => this._send(req));
|
|
86
|
+
}
|
|
87
|
+
collection(name) {
|
|
88
|
+
this._assertMetadata();
|
|
20
89
|
const resource = this.metadata.getCollectionResource(name);
|
|
21
|
-
|
|
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);
|
|
90
|
+
return new CollectionNode(this, resource, req => this._send(req));
|
|
27
91
|
}
|
|
28
|
-
singleton(name
|
|
92
|
+
singleton(name) {
|
|
93
|
+
this._assertMetadata();
|
|
29
94
|
const resource = this.metadata.getSingletonResource(name);
|
|
30
|
-
|
|
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);
|
|
95
|
+
return new SingletonNode(this, resource, req => this._send(req));
|
|
36
96
|
}
|
|
37
|
-
async _send(req
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
(typeof validateStatus === 'function' && !validateStatus(resp.status))) {
|
|
43
|
-
throw new ClientError({
|
|
44
|
-
message: resp.statusText,
|
|
45
|
-
status: resp.status,
|
|
46
|
-
issues: resp.data.errors
|
|
97
|
+
async _send(req) {
|
|
98
|
+
return this._taskQueue.enqueue(async () => {
|
|
99
|
+
const resp = await this._axios.request({
|
|
100
|
+
...req,
|
|
101
|
+
validateStatus: () => true
|
|
47
102
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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;
|
|
103
|
+
if ((resp.status >= 400 && resp.status < 600) &&
|
|
104
|
+
(this._axios.defaults.validateStatus && !this._axios.defaults.validateStatus(resp.status))) {
|
|
105
|
+
throw new ClientError({
|
|
106
|
+
message: resp.status + ' ' + resp.statusText,
|
|
107
|
+
status: resp.status,
|
|
108
|
+
issues: resp.data.errors
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const rawHeaders = (typeof resp.headers.toJSON === 'function'
|
|
112
|
+
? resp.headers.toJSON()
|
|
113
|
+
: { ...resp.headers });
|
|
114
|
+
const headers = new ResponsiveMap(rawHeaders);
|
|
115
|
+
return {
|
|
116
|
+
status: resp.status,
|
|
117
|
+
statusText: resp.statusText,
|
|
118
|
+
data: resp.data,
|
|
119
|
+
rawHeaders,
|
|
120
|
+
headers
|
|
121
|
+
};
|
|
122
|
+
}).toPromise();
|
|
78
123
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
url: joinPath(this.serviceUrl || '/', '$metadata'),
|
|
83
|
-
}, { validateStatus: true });
|
|
84
|
-
this._metadata = new OpraDocument(resp.data);
|
|
124
|
+
_assertMetadata() {
|
|
125
|
+
if (!this._metadata)
|
|
126
|
+
throw new Error('You must call init() to before using the client instance');
|
|
85
127
|
}
|
|
86
128
|
static async create(serviceUrl, options) {
|
|
87
129
|
const client = new this(serviceUrl, options);
|
|
88
|
-
|
|
130
|
+
if (!client._metadata)
|
|
131
|
+
await client.init();
|
|
89
132
|
return client;
|
|
90
133
|
}
|
|
91
134
|
}
|
package/esm/index.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
export * from './client.js';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './client-request.js';
|
|
3
3
|
export * from './types.js';
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './services/
|
|
4
|
+
export * from './client-error.js';
|
|
5
|
+
export * from './services/collection-node.js';
|
|
6
|
+
export * from './services/singleton-node.js';
|
|
7
|
+
export * from './requests/batch-request.js';
|
|
8
|
+
export * from './requests/collection-create-request.js';
|
|
9
|
+
export * from './requests/collection-delete-request.js';
|
|
10
|
+
export * from './requests/collection-delete-many-request.js';
|
|
11
|
+
export * from './requests/collection-get-request.js';
|
|
12
|
+
export * from './requests/collection-search-request.js';
|
|
13
|
+
export * from './requests/collection-update-request.js';
|
|
14
|
+
export * from './requests/collection-update-many-request.js';
|
|
15
|
+
export * from './requests/singleton-get-request.js';
|