@livequery/rest 1.0.86 → 2.0.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/build/RestTransporter.d.ts +3 -3
- package/build/RestTransporter.js +19 -19
- package/package.json +5 -5
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Transporter, QueryOption, QueryStream } from '@livequery/types';
|
|
1
|
+
import { Transporter, QueryOption, QueryStream, Response } from '@livequery/types';
|
|
2
2
|
type MaybePromise<T> = T | Promise<T>;
|
|
3
3
|
export type RestTransporterConfig = {
|
|
4
4
|
websocket_url: () => MaybePromise<string>;
|
|
@@ -29,7 +29,7 @@ export declare class RestTransporter implements Transporter {
|
|
|
29
29
|
get<T>(ref: string, query?: any): Promise<T>;
|
|
30
30
|
add<T extends {} = {}>(ref: string, data: Partial<T>): Promise<any>;
|
|
31
31
|
update<T extends {} = {}>(ref: string, data: Partial<T>, method?: 'PATCH' | 'PUT'): Promise<any>;
|
|
32
|
-
remove(ref: string): Promise<
|
|
33
|
-
trigger<
|
|
32
|
+
remove<T>(ref: string): Promise<T>;
|
|
33
|
+
trigger<T extends {}>(ref: string, name: string, payload?: any, query?: any): Promise<Response<T>>;
|
|
34
34
|
}
|
|
35
35
|
export {};
|
package/build/RestTransporter.js
CHANGED
|
@@ -2,7 +2,7 @@ import { of } from 'rxjs';
|
|
|
2
2
|
import { catchError, map, mergeMap } from 'rxjs/operators';
|
|
3
3
|
import { merge, Subject } from 'rxjs';
|
|
4
4
|
import { Socket } from './Socket.js';
|
|
5
|
-
import
|
|
5
|
+
import qs from 'query-string';
|
|
6
6
|
export class RestTransporter {
|
|
7
7
|
config;
|
|
8
8
|
socket;
|
|
@@ -13,17 +13,17 @@ export class RestTransporter {
|
|
|
13
13
|
query(ref, options) {
|
|
14
14
|
const $on_reload = new Subject();
|
|
15
15
|
const http_request = merge(of(1), this.socket?.$reconnect || of(), $on_reload)
|
|
16
|
-
.pipe(mergeMap(() => this.call(ref, 'GET', options)), map(({ data, error }) => {
|
|
16
|
+
.pipe(mergeMap(() => this.call(ref, 'GET', undefined, options)), map(({ data, error }) => {
|
|
17
17
|
if (error)
|
|
18
18
|
throw error;
|
|
19
|
-
data.
|
|
20
|
-
// If collection
|
|
19
|
+
data.subscription_token && this.socket?.subscribe(data.subscription_token);
|
|
21
20
|
const collection = data;
|
|
21
|
+
// If collection
|
|
22
22
|
if (collection.items) {
|
|
23
23
|
return {
|
|
24
24
|
data: {
|
|
25
25
|
changes: collection.items.map(data => ({ data, type: 'added', ref })),
|
|
26
|
-
paging: { ...collection
|
|
26
|
+
paging: { ...collection, n: 0 }
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
}
|
|
@@ -32,11 +32,11 @@ export class RestTransporter {
|
|
|
32
32
|
return {
|
|
33
33
|
data: {
|
|
34
34
|
changes: [{ data: document.item, type: 'added', ref }],
|
|
35
|
-
paging: { n: 0 }
|
|
35
|
+
paging: { ...collection, n: 0 }
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
}), catchError(error => of({ error })));
|
|
39
|
-
const websocket_sync = (!this.socket || options
|
|
39
|
+
const websocket_sync = (!this.socket || options[':after'] || options[':before'] || options[':around']) ? of() : (this.socket.listen(ref).pipe(map((change) => ({ data: { changes: [change] } }))));
|
|
40
40
|
return Object.assign(merge(http_request, websocket_sync), {
|
|
41
41
|
reload: () => $on_reload.next()
|
|
42
42
|
});
|
|
@@ -48,9 +48,9 @@ export class RestTransporter {
|
|
|
48
48
|
...o,
|
|
49
49
|
[key]: typeof query[key] == 'object' ? JSON.stringify(query[key]) : query[key]
|
|
50
50
|
}), {});
|
|
51
|
-
return `?${stringify(encoded_query)}`;
|
|
51
|
+
return `?${qs.stringify(encoded_query)}`;
|
|
52
52
|
}
|
|
53
|
-
async call(url, method, query = {}
|
|
53
|
+
async call(url, method, payload, query = {}) {
|
|
54
54
|
const headers = {
|
|
55
55
|
...await this.config.headers?.() || {},
|
|
56
56
|
...payload ? {
|
|
@@ -88,19 +88,19 @@ export class RestTransporter {
|
|
|
88
88
|
throw error;
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
-
|
|
92
|
-
return
|
|
91
|
+
get(ref, query = {}) {
|
|
92
|
+
return this.call(ref, 'GET', null, query);
|
|
93
93
|
}
|
|
94
|
-
|
|
95
|
-
return
|
|
94
|
+
add(ref, data) {
|
|
95
|
+
return this.call(ref, 'POST', data, {});
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
return
|
|
97
|
+
update(ref, data, method = 'PATCH') {
|
|
98
|
+
return this.call(ref, method, data, {});
|
|
99
99
|
}
|
|
100
|
-
|
|
101
|
-
return
|
|
100
|
+
remove(ref) {
|
|
101
|
+
return this.call(ref, 'DELETE');
|
|
102
102
|
}
|
|
103
|
-
|
|
104
|
-
return
|
|
103
|
+
trigger(ref, name, payload, query) {
|
|
104
|
+
return this.call(`${ref}/~${name}`, 'POST', payload, query);
|
|
105
105
|
}
|
|
106
106
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"repository": {
|
|
4
4
|
"url": "https://github.com/livequery/rest"
|
|
5
5
|
},
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "2.0.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"description": "",
|
|
9
9
|
"main": "build/index.js",
|
|
@@ -12,14 +12,14 @@
|
|
|
12
12
|
"build/**/*"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@types/uuid": "^8.3.0",
|
|
16
15
|
"query-string": "^7.0.1",
|
|
17
|
-
"rxjs": "^7.1.0",
|
|
18
|
-
"typescript": "4.9.5",
|
|
19
16
|
"uuid": "^8.3.2"
|
|
20
17
|
},
|
|
21
18
|
"devDependencies": {
|
|
22
|
-
"
|
|
19
|
+
"rxjs": "^7.1.0",
|
|
20
|
+
"typescript": "4.9.5",
|
|
21
|
+
"@types/uuid": "^8.3.0",
|
|
22
|
+
"@livequery/types": "2.0.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {},
|
|
25
25
|
"scripts": {
|