@quantform/core 0.7.0-beta.20 → 0.7.0-beta.22
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/dist/with-request.d.ts +4 -3
- package/dist/with-request.d.ts.map +1 -1
- package/dist/with-request.js +30 -8
- package/package.json +1 -1
- package/src/with-request.ts +32 -16
package/dist/with-request.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
1
2
|
export type RequestMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
|
|
2
3
|
export declare class RequestNetworkError extends Error {
|
|
3
4
|
readonly statusCode: number;
|
|
4
5
|
readonly json: () => Promise<string>;
|
|
5
6
|
constructor(statusCode: number, json: () => Promise<string>);
|
|
6
7
|
}
|
|
7
|
-
export declare function withRequest(
|
|
8
|
+
export declare function withRequest({ method, url, headers, body }: {
|
|
8
9
|
method: RequestMethod;
|
|
9
10
|
url: string;
|
|
10
11
|
headers?: Record<string, any>;
|
|
11
12
|
body?: string;
|
|
12
|
-
}):
|
|
13
|
+
}): Observable<{
|
|
13
14
|
timestamp: number;
|
|
14
|
-
payload:
|
|
15
|
+
payload: unknown;
|
|
15
16
|
}>;
|
|
16
17
|
//# sourceMappingURL=with-request.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-request.d.ts","sourceRoot":"","sources":["../src/with-request.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"with-request.d.ts","sourceRoot":"","sources":["../src/with-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAMlC,MAAM,MAAM,aAAa,GACrB,KAAK,GACL,MAAM,GACN,MAAM,GACN,KAAK,GACL,QAAQ,GACR,SAAS,GACT,SAAS,GACT,OAAO,GACP,OAAO,CAAC;AAEZ,qBAAa,mBAAoB,SAAQ,KAAK;IAChC,QAAQ,CAAC,UAAU,EAAE,MAAM;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC;gBAAxD,UAAU,EAAE,MAAM,EAAW,IAAI,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC;CAG9E;AAED,wBAAgB,WAAW,CAAC,EAC1B,MAAM,EACN,GAAG,EACH,OAAO,EACP,IAAI,EACL,EAAE;IACD,MAAM,EAAE,aAAa,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;eAGoC,MAAM;aAAW,OAAO;GA8B5D"}
|
package/dist/with-request.js
CHANGED
|
@@ -13,14 +13,36 @@ class RequestNetworkError extends Error {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
exports.RequestNetworkError = RequestNetworkError;
|
|
16
|
-
function withRequest(
|
|
16
|
+
function withRequest({ method, url, headers, body }) {
|
|
17
17
|
const { error } = (0, use_logger_1.useLogger)(withRequest.name);
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
return new rxjs_1.Observable(subscriber => {
|
|
19
|
+
(0, undici_1.request)(url, { method, headers, body })
|
|
20
|
+
.then(({ statusCode, body }) => {
|
|
21
|
+
if (statusCode !== 200) {
|
|
22
|
+
error(`errored`, {
|
|
23
|
+
method,
|
|
24
|
+
url,
|
|
25
|
+
headers,
|
|
26
|
+
body,
|
|
27
|
+
statusCode
|
|
28
|
+
});
|
|
29
|
+
subscriber.error(new RequestNetworkError(statusCode, () => body.json()));
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
subscriber.next({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload: body.json() });
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
.catch((e) => {
|
|
36
|
+
error(`errored`, {
|
|
37
|
+
method,
|
|
38
|
+
url,
|
|
39
|
+
headers,
|
|
40
|
+
body,
|
|
41
|
+
error: e
|
|
42
|
+
});
|
|
43
|
+
subscriber.error(error);
|
|
44
|
+
})
|
|
45
|
+
.finally(() => subscriber.complete());
|
|
46
|
+
});
|
|
25
47
|
}
|
|
26
48
|
exports.withRequest = withRequest;
|
package/package.json
CHANGED
package/src/with-request.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
2
|
import { request } from 'undici';
|
|
3
3
|
|
|
4
4
|
import { useLogger } from './use-logger';
|
|
@@ -21,7 +21,12 @@ export class RequestNetworkError extends Error {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function withRequest(
|
|
24
|
+
export function withRequest({
|
|
25
|
+
method,
|
|
26
|
+
url,
|
|
27
|
+
headers,
|
|
28
|
+
body
|
|
29
|
+
}: {
|
|
25
30
|
method: RequestMethod;
|
|
26
31
|
url: string;
|
|
27
32
|
headers?: Record<string, any>;
|
|
@@ -29,23 +34,34 @@ export function withRequest(args: {
|
|
|
29
34
|
}) {
|
|
30
35
|
const { error } = useLogger(withRequest.name);
|
|
31
36
|
|
|
32
|
-
return
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (
|
|
37
|
+
return new Observable<{ timestamp: number; payload: unknown }>(subscriber => {
|
|
38
|
+
request(url, { method, headers, body })
|
|
39
|
+
.then(({ statusCode, body }) => {
|
|
40
|
+
if (statusCode !== 200) {
|
|
36
41
|
error(`errored`, {
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
method,
|
|
43
|
+
url,
|
|
44
|
+
headers,
|
|
45
|
+
body,
|
|
46
|
+
statusCode
|
|
39
47
|
});
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
);
|
|
49
|
+
subscriber.error(new RequestNetworkError(statusCode, () => body.json()));
|
|
50
|
+
} else {
|
|
51
|
+
subscriber.next({ timestamp: useTimestamp(), payload: body.json() });
|
|
44
52
|
}
|
|
53
|
+
})
|
|
54
|
+
.catch((e: Error) => {
|
|
55
|
+
error(`errored`, {
|
|
56
|
+
method,
|
|
57
|
+
url,
|
|
58
|
+
headers,
|
|
59
|
+
body,
|
|
60
|
+
error: e
|
|
61
|
+
});
|
|
45
62
|
|
|
46
|
-
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
);
|
|
63
|
+
subscriber.error(error);
|
|
64
|
+
})
|
|
65
|
+
.finally(() => subscriber.complete());
|
|
66
|
+
});
|
|
51
67
|
}
|