@livequery/rest 1.0.85 → 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 +9 -3
- package/build/RestTransporter.js +36 -23
- package/build/index.d.ts +2 -2
- package/build/index.js +2 -2
- 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>;
|
|
@@ -6,6 +6,12 @@ export type RestTransporterConfig = {
|
|
|
6
6
|
headers?: () => Promise<{
|
|
7
7
|
[key: string]: string;
|
|
8
8
|
}>;
|
|
9
|
+
onResponse?: (data: {
|
|
10
|
+
url: string;
|
|
11
|
+
headers: Headers;
|
|
12
|
+
body: any;
|
|
13
|
+
status: number;
|
|
14
|
+
}) => void;
|
|
9
15
|
};
|
|
10
16
|
export declare class RestTransporter implements Transporter {
|
|
11
17
|
#private;
|
|
@@ -23,7 +29,7 @@ export declare class RestTransporter implements Transporter {
|
|
|
23
29
|
get<T>(ref: string, query?: any): Promise<T>;
|
|
24
30
|
add<T extends {} = {}>(ref: string, data: Partial<T>): Promise<any>;
|
|
25
31
|
update<T extends {} = {}>(ref: string, data: Partial<T>, method?: 'PATCH' | 'PUT'): Promise<any>;
|
|
26
|
-
remove(ref: string): Promise<
|
|
27
|
-
trigger<
|
|
32
|
+
remove<T>(ref: string): Promise<T>;
|
|
33
|
+
trigger<T extends {}>(ref: string, name: string, payload?: any, query?: any): Promise<Response<T>>;
|
|
28
34
|
}
|
|
29
35
|
export {};
|
package/build/RestTransporter.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { of } from 'rxjs';
|
|
2
2
|
import { catchError, map, mergeMap } from 'rxjs/operators';
|
|
3
3
|
import { merge, Subject } from 'rxjs';
|
|
4
|
-
import { Socket } from './Socket';
|
|
5
|
-
import
|
|
4
|
+
import { Socket } from './Socket.js';
|
|
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 ? {
|
|
@@ -59,15 +59,28 @@ export class RestTransporter {
|
|
|
59
59
|
...this.socket ? { socket_id: this.socket.socket_session } : {}
|
|
60
60
|
};
|
|
61
61
|
try {
|
|
62
|
-
const
|
|
62
|
+
const response = await fetch(`${this.config.base_url()}/${url}${this.#encode_query(query)}`, {
|
|
63
63
|
method,
|
|
64
64
|
headers: headers,
|
|
65
65
|
...payload ? { body: JSON.stringify(payload) } : {},
|
|
66
|
-
})
|
|
66
|
+
});
|
|
67
67
|
try {
|
|
68
|
-
|
|
68
|
+
const body = await response.json();
|
|
69
|
+
this.config.onResponse?.({
|
|
70
|
+
body,
|
|
71
|
+
headers: response.headers,
|
|
72
|
+
status: response.status,
|
|
73
|
+
url
|
|
74
|
+
});
|
|
75
|
+
return body;
|
|
69
76
|
}
|
|
70
77
|
catch (e) {
|
|
78
|
+
this.config.onResponse?.({
|
|
79
|
+
body: null,
|
|
80
|
+
headers: response.headers,
|
|
81
|
+
status: response.status,
|
|
82
|
+
url
|
|
83
|
+
});
|
|
71
84
|
return null;
|
|
72
85
|
}
|
|
73
86
|
}
|
|
@@ -75,19 +88,19 @@ export class RestTransporter {
|
|
|
75
88
|
throw error;
|
|
76
89
|
}
|
|
77
90
|
}
|
|
78
|
-
|
|
79
|
-
return
|
|
91
|
+
get(ref, query = {}) {
|
|
92
|
+
return this.call(ref, 'GET', null, query);
|
|
80
93
|
}
|
|
81
|
-
|
|
82
|
-
return
|
|
94
|
+
add(ref, data) {
|
|
95
|
+
return this.call(ref, 'POST', data, {});
|
|
83
96
|
}
|
|
84
|
-
|
|
85
|
-
return
|
|
97
|
+
update(ref, data, method = 'PATCH') {
|
|
98
|
+
return this.call(ref, method, data, {});
|
|
86
99
|
}
|
|
87
|
-
|
|
88
|
-
return
|
|
100
|
+
remove(ref) {
|
|
101
|
+
return this.call(ref, 'DELETE');
|
|
89
102
|
}
|
|
90
|
-
|
|
91
|
-
return
|
|
103
|
+
trigger(ref, name, payload, query) {
|
|
104
|
+
return this.call(`${ref}/~${name}`, 'POST', payload, query);
|
|
92
105
|
}
|
|
93
106
|
}
|
package/build/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { RestTransporter } from './RestTransporter';
|
|
2
|
-
export { Socket } from './Socket';
|
|
1
|
+
export { RestTransporter } from './RestTransporter.js';
|
|
2
|
+
export { Socket } from './Socket.js';
|
package/build/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { RestTransporter } from './RestTransporter';
|
|
2
|
-
export { Socket } from './Socket';
|
|
1
|
+
export { RestTransporter } from './RestTransporter.js';
|
|
2
|
+
export { Socket } from './Socket.js';
|
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": {
|