@livequery/rest 1.0.86 → 2.0.6
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 +10 -10
- package/build/RestTransporter.js +35 -45
- package/package.json +5 -5
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { Transporter, QueryOption, QueryStream } from '@livequery/types';
|
|
1
|
+
import { Transporter, QueryOption, QueryStream, Response } from '@livequery/types';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
2
3
|
type MaybePromise<T> = T | Promise<T>;
|
|
4
|
+
export type Request = {
|
|
5
|
+
url: RequestInfo | URL | string;
|
|
6
|
+
options?: RequestInit;
|
|
7
|
+
};
|
|
3
8
|
export type RestTransporterConfig = {
|
|
4
9
|
websocket_url: () => MaybePromise<string>;
|
|
5
10
|
base_url: () => MaybePromise<string>;
|
|
6
11
|
headers?: () => Promise<{
|
|
7
12
|
[key: string]: string;
|
|
8
13
|
}>;
|
|
9
|
-
|
|
10
|
-
url: string;
|
|
11
|
-
headers: Headers;
|
|
12
|
-
body: any;
|
|
13
|
-
status: number;
|
|
14
|
-
}) => void;
|
|
14
|
+
hooks?: (o: Observable<Request>) => Observable<Request>;
|
|
15
15
|
};
|
|
16
16
|
export declare class RestTransporter implements Transporter {
|
|
17
17
|
#private;
|
|
@@ -20,7 +20,7 @@ export declare class RestTransporter implements Transporter {
|
|
|
20
20
|
constructor(config: RestTransporterConfig);
|
|
21
21
|
query<T extends {
|
|
22
22
|
id: string;
|
|
23
|
-
}>(ref: string, options?: Partial<QueryOption<T>>):
|
|
23
|
+
}>(ref: string, options?: Partial<QueryOption<T>>): Observable<QueryStream<T> | {
|
|
24
24
|
error: any;
|
|
25
25
|
}> & {
|
|
26
26
|
reload: () => void;
|
|
@@ -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
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { of } from 'rxjs';
|
|
1
|
+
import { of, lastValueFrom } from 'rxjs';
|
|
2
2
|
import { catchError, map, mergeMap } from 'rxjs/operators';
|
|
3
|
-
import { merge, Subject } from 'rxjs';
|
|
3
|
+
import { merge, Subject, BehaviorSubject } 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 ? {
|
|
@@ -59,48 +59,38 @@ export class RestTransporter {
|
|
|
59
59
|
...this.socket ? { socket_id: this.socket.socket_session } : {}
|
|
60
60
|
};
|
|
61
61
|
try {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
catch (e) {
|
|
78
|
-
this.config.onResponse?.({
|
|
79
|
-
body: null,
|
|
80
|
-
headers: response.headers,
|
|
81
|
-
status: response.status,
|
|
82
|
-
url
|
|
83
|
-
});
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
62
|
+
const request = {
|
|
63
|
+
url: `${this.config.base_url()}/${url}${this.#encode_query(query)}`,
|
|
64
|
+
options: {
|
|
65
|
+
method,
|
|
66
|
+
headers: headers,
|
|
67
|
+
...payload ? { body: JSON.stringify(payload) } : {},
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const mapped = this.config.hooks ? await lastValueFrom(this.config.hooks(new BehaviorSubject(request))) : request;
|
|
71
|
+
const response = await fetch(mapped.url, mapped.options);
|
|
72
|
+
const body = await response.json();
|
|
73
|
+
if (body.error)
|
|
74
|
+
throw body.error;
|
|
75
|
+
return body;
|
|
86
76
|
}
|
|
87
77
|
catch (error) {
|
|
88
78
|
throw error;
|
|
89
79
|
}
|
|
90
80
|
}
|
|
91
|
-
|
|
92
|
-
return
|
|
81
|
+
get(ref, query = {}) {
|
|
82
|
+
return this.call(ref, 'GET', null, query);
|
|
93
83
|
}
|
|
94
|
-
|
|
95
|
-
return
|
|
84
|
+
add(ref, data) {
|
|
85
|
+
return this.call(ref, 'POST', data, {});
|
|
96
86
|
}
|
|
97
|
-
|
|
98
|
-
return
|
|
87
|
+
update(ref, data, method = 'PATCH') {
|
|
88
|
+
return this.call(ref, method, data, {});
|
|
99
89
|
}
|
|
100
|
-
|
|
101
|
-
return
|
|
90
|
+
remove(ref) {
|
|
91
|
+
return this.call(ref, 'DELETE');
|
|
102
92
|
}
|
|
103
|
-
|
|
104
|
-
return
|
|
93
|
+
trigger(ref, name, payload, query) {
|
|
94
|
+
return this.call(`${ref}/~${name}`, 'POST', payload, query);
|
|
105
95
|
}
|
|
106
96
|
}
|
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.6",
|
|
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": {
|