@livequery/rest 2.0.0 → 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 +7 -7
- package/build/RestTransporter.js +16 -26
- package/package.json +1 -1
|
@@ -1,17 +1,17 @@
|
|
|
1
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;
|
package/build/RestTransporter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
5
|
import qs from 'query-string';
|
|
6
6
|
export class RestTransporter {
|
|
@@ -59,30 +59,20 @@ 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;
|