@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/esm/client.js ADDED
@@ -0,0 +1,91 @@
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
+ }
package/esm/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './client.js';
2
+ export * from './response.js';
3
+ export * from './types.js';
4
+ export * from './services/collection-service.js';
5
+ export * from './services/singleton-service.js';
package/esm/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './client.js';
2
+ export * from './response.js';
3
+ export * from './types.js';
4
+ export * from './services/collection-service.js';
5
+ export * from './services/singleton-service.js';
@@ -0,0 +1,2 @@
1
+ import { ObservablePromiseLike } from './types.js';
2
+ export declare function observablePromise<T>(promise: Promise<T>): ObservablePromiseLike<T>;
@@ -0,0 +1,6 @@
1
+ import { from } from 'rxjs';
2
+ export function observablePromise(promise) {
3
+ const observable = from(promise);
4
+ observable.then = (...args) => promise.then(...args);
5
+ return observable;
6
+ }
@@ -0,0 +1,8 @@
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
+ }
@@ -0,0 +1,18 @@
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
+ }
@@ -0,0 +1,7 @@
1
+ import { CollectionDeleteManyQueryOptions } from '@opra/schema';
2
+ import { Expression } from '@opra/url';
3
+ export declare class CollectionDeleteManyRequest {
4
+ protected _options: CollectionDeleteManyQueryOptions;
5
+ constructor(_options?: CollectionDeleteManyQueryOptions);
6
+ filter(value: string | Expression): this;
7
+ }
@@ -0,0 +1,10 @@
1
+ export class CollectionDeleteManyRequest {
2
+ _options;
3
+ constructor(_options = {}) {
4
+ this._options = _options;
5
+ }
6
+ filter(value) {
7
+ this._options.filter = value;
8
+ return this;
9
+ }
10
+ }
@@ -0,0 +1,8 @@
1
+ import { CollectionGetQueryOptions } from '@opra/schema';
2
+ export declare class CollectionGetRequest {
3
+ protected _options: CollectionGetQueryOptions;
4
+ constructor(_options?: CollectionGetQueryOptions);
5
+ omit(...fields: (string | string[])[]): this;
6
+ pick(...fields: (string | string[])[]): this;
7
+ include(...fields: (string | string[])[]): this;
8
+ }
@@ -0,0 +1,18 @@
1
+ export class CollectionGetRequest {
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
+ }
@@ -0,0 +1,15 @@
1
+ import { CollectionSearchQueryOptions } from '@opra/schema';
2
+ import { Expression } from '@opra/url';
3
+ export declare class CollectionSearchRequest {
4
+ protected _options: CollectionSearchQueryOptions;
5
+ constructor(_options?: CollectionSearchQueryOptions);
6
+ omit(...fields: (string | string[])[]): this;
7
+ pick(...fields: (string | string[])[]): this;
8
+ include(...fields: (string | string[])[]): this;
9
+ limit(value: number): this;
10
+ skip(value: number): this;
11
+ count(value?: boolean): this;
12
+ distinct(value: boolean): this;
13
+ sort(...fields: (string | string[])[]): this;
14
+ filter(value: string | Expression): this;
15
+ }
@@ -0,0 +1,42 @@
1
+ export class CollectionSearchRequest {
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
+ limit(value) {
19
+ this._options.limit = value;
20
+ return this;
21
+ }
22
+ skip(value) {
23
+ this._options.skip = value;
24
+ return this;
25
+ }
26
+ count(value = true) {
27
+ this._options.count = value;
28
+ return this;
29
+ }
30
+ distinct(value) {
31
+ this._options.distinct = value;
32
+ return this;
33
+ }
34
+ sort(...fields) {
35
+ this._options.sort = fields.flat();
36
+ return this;
37
+ }
38
+ filter(value) {
39
+ this._options.filter = value;
40
+ return this;
41
+ }
42
+ }
@@ -0,0 +1,7 @@
1
+ import { CollectionUpdateManyQueryOptions } from '@opra/schema';
2
+ import { Expression } from '@opra/url';
3
+ export declare class CollectionUpdateManyRequest {
4
+ protected _options: CollectionUpdateManyQueryOptions;
5
+ constructor(_options?: CollectionUpdateManyQueryOptions);
6
+ filter(value: string | Expression): this;
7
+ }
@@ -0,0 +1,10 @@
1
+ export class CollectionUpdateManyRequest {
2
+ _options;
3
+ constructor(_options = {}) {
4
+ this._options = _options;
5
+ }
6
+ filter(value) {
7
+ this._options.filter = value;
8
+ return this;
9
+ }
10
+ }
@@ -0,0 +1,8 @@
1
+ import { CollectionUpdateQueryOptions } from '@opra/schema';
2
+ export declare class CollectionUpdateRequest {
3
+ protected _options: CollectionUpdateQueryOptions;
4
+ constructor(_options?: CollectionUpdateQueryOptions);
5
+ omit(...fields: (string | string[])[]): this;
6
+ pick(...fields: (string | string[])[]): this;
7
+ include(...fields: (string | string[])[]): this;
8
+ }
@@ -0,0 +1,18 @@
1
+ export class CollectionUpdateRequest {
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
+ }
@@ -0,0 +1,8 @@
1
+ import { SingletonGetQueryOptions } from '@opra/schema';
2
+ export declare class SingletonGetRequest {
3
+ protected _options: SingletonGetQueryOptions;
4
+ constructor(_options?: SingletonGetQueryOptions);
5
+ omit(...fields: (string | string[])[]): this;
6
+ pick(...fields: (string | string[])[]): this;
7
+ include(...fields: (string | string[])[]): this;
8
+ }
@@ -0,0 +1,18 @@
1
+ export class SingletonGetRequest {
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
+ }
@@ -0,0 +1,9 @@
1
+ import { ResponsiveMap } from '@opra/common';
2
+ import { ResponseHeaders } from './types.js';
3
+ export declare type OpraResponse<T = any> = {
4
+ status: number;
5
+ statusText: string;
6
+ rawHeaders: ResponseHeaders;
7
+ headers: ResponsiveMap<string, string | string[]>;
8
+ data?: T;
9
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,31 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+ import { CollectionCreateQueryOptions, CollectionDeleteManyQueryOptions, CollectionGetQueryOptions, CollectionResourceInfo, CollectionSearchQueryOptions, CollectionUpdateManyQueryOptions, CollectionUpdateQueryOptions, OpraDocument } from '@opra/schema';
3
+ import { CollectionCreateRequest } from '../requests/collection-create-request.js';
4
+ import { CollectionDeleteManyRequest } from '../requests/collection-delete-many-request.js';
5
+ import { CollectionGetRequest } from '../requests/collection-get-request.js';
6
+ import { CollectionSearchRequest } from '../requests/collection-search-request.js';
7
+ import { CollectionUpdateManyRequest } from '../requests/collection-update-many-request.js';
8
+ import { CollectionUpdateRequest } from '../requests/collection-update-request.js';
9
+ import { OpraResponse } from '../response.js';
10
+ import { ObservablePromiseLike, PartialInput, RequestConfig } from '../types.js';
11
+ export declare class CollectionService<T, TResponse extends OpraResponse<T>> {
12
+ protected _serviceUrl: string;
13
+ protected _document: OpraDocument;
14
+ protected _handler: (req: AxiosRequestConfig) => Promise<any>;
15
+ protected _resource: CollectionResourceInfo;
16
+ constructor(_serviceUrl: string, _document: OpraDocument, _handler: (req: AxiosRequestConfig) => Promise<any>, _resource: CollectionResourceInfo);
17
+ create(data: PartialInput<T>, options?: CollectionCreateQueryOptions | ((req: CollectionCreateRequest) => void)): ObservablePromiseLike<TResponse>;
18
+ delete(keyValue: any): ObservablePromiseLike<TResponse>;
19
+ deleteMany(options?: CollectionDeleteManyQueryOptions | ((req: CollectionDeleteManyRequest) => void)): ObservablePromiseLike<TResponse>;
20
+ get(keyValue: any, options?: CollectionGetQueryOptions | ((req: CollectionGetRequest) => void)): ObservablePromiseLike<TResponse>;
21
+ search(options?: CollectionSearchQueryOptions | ((req: CollectionSearchRequest) => void)): ObservablePromiseLike<TResponse>;
22
+ update(keyValue: any, data: PartialInput<T>, options?: CollectionUpdateQueryOptions | ((req: CollectionUpdateRequest) => void)): ObservablePromiseLike<TResponse>;
23
+ updateMany(data: PartialInput<T>, options?: CollectionUpdateManyQueryOptions | ((req: CollectionUpdateManyRequest) => void)): ObservablePromiseLike<TResponse>;
24
+ protected _prepareCreateRequest(data: any, options: CollectionCreateQueryOptions): AxiosRequestConfig;
25
+ protected _prepareDeleteRequest(keyValue: any): AxiosRequestConfig;
26
+ protected _prepareDeleteManyRequest(options: CollectionDeleteManyQueryOptions): AxiosRequestConfig;
27
+ protected _prepareGetRequest(keyValue: any, options: CollectionGetQueryOptions): RequestConfig;
28
+ protected _prepareSearchRequest(options: CollectionSearchQueryOptions): AxiosRequestConfig;
29
+ protected _prepareUpdateRequest(keyValue: any, data: any, options: CollectionUpdateQueryOptions): RequestConfig;
30
+ protected _prepareUpdateManyRequest(data: any, options: CollectionUpdateManyQueryOptions): RequestConfig;
31
+ }
@@ -0,0 +1,188 @@
1
+ import { OpraURL } from '@opra/url';
2
+ import { observablePromise } from '../observable-promise.js';
3
+ import { CollectionCreateRequest } from '../requests/collection-create-request.js';
4
+ import { CollectionDeleteManyRequest } from '../requests/collection-delete-many-request.js';
5
+ import { CollectionGetRequest } from '../requests/collection-get-request.js';
6
+ import { CollectionSearchRequest } from '../requests/collection-search-request.js';
7
+ import { CollectionUpdateManyRequest } from '../requests/collection-update-many-request.js';
8
+ import { CollectionUpdateRequest } from '../requests/collection-update-request.js';
9
+ export class CollectionService {
10
+ _serviceUrl;
11
+ _document;
12
+ _handler;
13
+ _resource;
14
+ constructor(_serviceUrl, _document, _handler, _resource) {
15
+ this._serviceUrl = _serviceUrl;
16
+ this._document = _document;
17
+ this._handler = _handler;
18
+ this._resource = _resource;
19
+ }
20
+ create(data, options) {
21
+ const requestOptions = options && typeof options === 'object' ? options : {};
22
+ const requestWrapper = new CollectionCreateRequest(requestOptions);
23
+ if (typeof options === 'function')
24
+ options(requestWrapper);
25
+ const req = this._prepareCreateRequest(data, requestOptions);
26
+ const promise = this._handler(req);
27
+ return observablePromise(promise);
28
+ }
29
+ delete(keyValue) {
30
+ const req = this._prepareDeleteRequest(keyValue);
31
+ const promise = this._handler(req);
32
+ return observablePromise(promise);
33
+ }
34
+ deleteMany(options) {
35
+ const requestOptions = options && typeof options === 'object' ? options : {};
36
+ const requestWrapper = new CollectionDeleteManyRequest(requestOptions);
37
+ if (typeof options === 'function')
38
+ options(requestWrapper);
39
+ const req = this._prepareDeleteManyRequest(requestOptions);
40
+ const promise = this._handler(req);
41
+ return observablePromise(promise);
42
+ }
43
+ get(keyValue, options) {
44
+ const requestOptions = options && typeof options === 'object' ? options : {};
45
+ const requestWrapper = new CollectionGetRequest(requestOptions);
46
+ if (typeof options === 'function')
47
+ options(requestWrapper);
48
+ const req = this._prepareGetRequest(keyValue, requestOptions);
49
+ const promise = this._handler(req);
50
+ return observablePromise(promise);
51
+ }
52
+ search(options) {
53
+ const requestOptions = options && typeof options === 'object' ? options : {};
54
+ const requestWrapper = new CollectionSearchRequest(requestOptions);
55
+ if (typeof options === 'function')
56
+ options(requestWrapper);
57
+ const req = this._prepareSearchRequest(requestOptions);
58
+ const promise = this._handler(req);
59
+ return observablePromise(promise);
60
+ }
61
+ update(keyValue, data, options) {
62
+ const requestOptions = options && typeof options === 'object' ? options : {};
63
+ const requestWrapper = new CollectionUpdateRequest(requestOptions);
64
+ if (typeof options === 'function')
65
+ options(requestWrapper);
66
+ const req = this._prepareUpdateRequest(keyValue, data, requestOptions);
67
+ const promise = this._handler(req);
68
+ return observablePromise(promise);
69
+ }
70
+ updateMany(data, options) {
71
+ const requestOptions = options && typeof options === 'object' ? options : {};
72
+ const requestWrapper = new CollectionUpdateManyRequest(requestOptions);
73
+ if (typeof options === 'function')
74
+ options(requestWrapper);
75
+ const req = this._prepareUpdateManyRequest(data, requestOptions);
76
+ const promise = this._handler(req);
77
+ return observablePromise(promise);
78
+ }
79
+ _prepareCreateRequest(data, options) {
80
+ const url = new OpraURL(this._serviceUrl);
81
+ url.path.join(this._resource.name);
82
+ if (options.include)
83
+ url.searchParams.set('$include', options.include);
84
+ if (options.pick)
85
+ url.searchParams.set('$pick', options.pick);
86
+ if (options.omit)
87
+ url.searchParams.set('$omit', options.omit);
88
+ return {
89
+ method: 'POST',
90
+ url: url.address,
91
+ data,
92
+ params: url.searchParams
93
+ };
94
+ }
95
+ _prepareDeleteRequest(keyValue) {
96
+ const url = new OpraURL(this._serviceUrl);
97
+ url.path.join(this._resource.name);
98
+ url.path.get(url.path.size - 1).key = keyValue;
99
+ return {
100
+ method: 'DELETE',
101
+ url: url.address,
102
+ params: url.searchParams
103
+ };
104
+ }
105
+ _prepareDeleteManyRequest(options) {
106
+ const url = new OpraURL(this._serviceUrl);
107
+ url.path.join(this._resource.name);
108
+ if (options.filter)
109
+ url.searchParams.set('$filter', options.filter);
110
+ return {
111
+ method: 'DELETE',
112
+ url: url.address,
113
+ params: url.searchParams
114
+ };
115
+ }
116
+ _prepareGetRequest(keyValue, options) {
117
+ const url = new OpraURL(this._serviceUrl);
118
+ url.path.join(this._resource.name);
119
+ url.path.get(url.path.size - 1).key = keyValue;
120
+ if (options.include)
121
+ url.searchParams.set('$include', options.include);
122
+ if (options.pick)
123
+ url.searchParams.set('$pick', options.pick);
124
+ if (options.omit)
125
+ url.searchParams.set('$omit', options.omit);
126
+ return {
127
+ method: 'GET',
128
+ url: url.address,
129
+ params: url.searchParams
130
+ };
131
+ }
132
+ _prepareSearchRequest(options) {
133
+ const url = new OpraURL(this._serviceUrl);
134
+ url.path.join(this._resource.name);
135
+ if (options.include)
136
+ url.searchParams.set('$include', options.include);
137
+ if (options.pick)
138
+ url.searchParams.set('$pick', options.pick);
139
+ if (options.omit)
140
+ url.searchParams.set('$omit', options.omit);
141
+ if (options.sort)
142
+ url.searchParams.set('$sort', options.sort);
143
+ if (options.filter)
144
+ url.searchParams.set('$filter', options.filter);
145
+ if (options.limit != null)
146
+ url.searchParams.set('$limit', options.limit);
147
+ if (options.skip != null)
148
+ url.searchParams.set('$skip', options.skip);
149
+ if (options.count != null)
150
+ url.searchParams.set('$count', options.count);
151
+ if (options.distinct != null)
152
+ url.searchParams.set('$distinct', options.distinct);
153
+ return {
154
+ method: 'GET',
155
+ url: url.address,
156
+ params: url.searchParams
157
+ };
158
+ }
159
+ _prepareUpdateRequest(keyValue, data, options) {
160
+ const url = new OpraURL(this._serviceUrl);
161
+ url.path.join(this._resource.name);
162
+ url.path.get(url.path.size - 1).key = keyValue;
163
+ if (options.include)
164
+ url.searchParams.set('$include', options.include);
165
+ if (options.pick)
166
+ url.searchParams.set('$pick', options.pick);
167
+ if (options.omit)
168
+ url.searchParams.set('$omit', options.omit);
169
+ return {
170
+ method: 'PATCH',
171
+ url: url.address,
172
+ data,
173
+ params: url.searchParams
174
+ };
175
+ }
176
+ _prepareUpdateManyRequest(data, options) {
177
+ const url = new OpraURL(this._serviceUrl);
178
+ url.path.join(this._resource.name);
179
+ if (options.filter)
180
+ url.searchParams.set('$filter', options.filter);
181
+ return {
182
+ method: 'PATCH',
183
+ url: url.address,
184
+ data,
185
+ params: url.searchParams
186
+ };
187
+ }
188
+ }
@@ -0,0 +1,14 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+ import { OpraDocument, SingletonGetQueryOptions, SingletonResourceInfo } from '@opra/schema';
3
+ import { SingletonGetRequest } from '../requests/singleton-get-request.js';
4
+ import { OpraResponse } from '../response.js';
5
+ import { ObservablePromiseLike, RequestConfig } from '../types.js';
6
+ export declare class SingletonService<T, TResponse extends OpraResponse<T> = OpraResponse<T>> {
7
+ protected _serviceUrl: string;
8
+ protected _document: OpraDocument;
9
+ protected _handler: (req: AxiosRequestConfig) => Promise<any>;
10
+ protected _resource: SingletonResourceInfo;
11
+ constructor(_serviceUrl: string, _document: OpraDocument, _handler: (req: AxiosRequestConfig) => Promise<any>, _resource: SingletonResourceInfo);
12
+ get(options?: SingletonGetQueryOptions | ((req: SingletonGetRequest) => void)): ObservablePromiseLike<TResponse>;
13
+ protected _prepareGetRequest(options: SingletonGetQueryOptions): RequestConfig;
14
+ }
@@ -0,0 +1,39 @@
1
+ import { OpraURL } from '@opra/url';
2
+ import { observablePromise } from '../observable-promise.js';
3
+ import { SingletonGetRequest } from '../requests/singleton-get-request.js';
4
+ export class SingletonService {
5
+ _serviceUrl;
6
+ _document;
7
+ _handler;
8
+ _resource;
9
+ constructor(_serviceUrl, _document, _handler, _resource) {
10
+ this._serviceUrl = _serviceUrl;
11
+ this._document = _document;
12
+ this._handler = _handler;
13
+ this._resource = _resource;
14
+ }
15
+ get(options) {
16
+ const requestOptions = options && typeof options === 'object' ? options : {};
17
+ const requestWrapper = new SingletonGetRequest(requestOptions);
18
+ if (typeof options === 'function')
19
+ options(requestWrapper);
20
+ const req = this._prepareGetRequest(requestOptions);
21
+ const promise = this._handler(req);
22
+ return observablePromise(promise);
23
+ }
24
+ _prepareGetRequest(options) {
25
+ const url = new OpraURL(this._serviceUrl);
26
+ url.path.join(this._resource.name);
27
+ if (options.include)
28
+ url.searchParams.set('$include', options.include);
29
+ if (options.pick)
30
+ url.searchParams.set('$pick', options.pick);
31
+ if (options.omit)
32
+ url.searchParams.set('$omit', options.omit);
33
+ return {
34
+ method: 'GET',
35
+ url: url.address,
36
+ params: url.searchParams
37
+ };
38
+ }
39
+ }
package/esm/types.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+ import { Observable } from 'rxjs';
3
+ export declare type ObservablePromiseLike<T> = Observable<T> & PromiseLike<T>;
4
+ export { PartialInput, PartialOutput } from '@opra/common';
5
+ export declare type ResponseHeaders = Partial<Record<string, string | string[]>>;
6
+ export declare type CommonRequestOptions = {
7
+ headers?: Record<string, string>;
8
+ validateStatus?: boolean | ((status: number) => boolean);
9
+ };
10
+ export declare type RequestConfig = AxiosRequestConfig & CommonRequestOptions;
package/esm/types.js ADDED
@@ -0,0 +1,10 @@
1
+ export {};
2
+ // export type CollectionCreateRequestOptions = CollectionCreateQueryOptions & CommonRequestOptions;
3
+ // export type CollectionDeleteRequestOptions = CommonRequestOptions;
4
+ // export type CollectionDeleteManyRequestOptions = CollectionDeleteManyQueryOptions & CommonRequestOptions;
5
+ // export type CollectionGetRequestOptions = CollectionGetQueryOptions & CommonRequestOptions;
6
+ // export type CollectionUpdateRequestOptions = CollectionUpdateQueryOptions & CommonRequestOptions;
7
+ // export type CollectionUpdateManyRequestOptions = CollectionUpdateManyQueryOptions & CommonRequestOptions;
8
+ // export type CollectionSearchRequestOptions = CollectionSearchQueryOptions & CommonRequestOptions;
9
+ //
10
+ // export type SingletonGetRequestOptions = SingletonGetQueryOptions & CommonRequestOptions;