@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.
@@ -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(args: {
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
- }): import("rxjs").Observable<{
13
+ }): Observable<{
13
14
  timestamp: number;
14
- payload: any;
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":"AAMA,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,IAAI,EAAE;IAChC,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;;;GAsBA"}
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"}
@@ -13,14 +13,36 @@ class RequestNetworkError extends Error {
13
13
  }
14
14
  }
15
15
  exports.RequestNetworkError = RequestNetworkError;
16
- function withRequest(args) {
16
+ function withRequest({ method, url, headers, body }) {
17
17
  const { error } = (0, use_logger_1.useLogger)(withRequest.name);
18
- return (0, rxjs_1.defer)(() => (0, rxjs_1.from)((0, undici_1.request)(args.url, args)).pipe((0, rxjs_1.switchMap)(it => {
19
- if (it.statusCode !== 200) {
20
- error(`errored`, Object.assign(Object.assign({}, args), { statusCode: it.statusCode }));
21
- return (0, rxjs_1.throwError)(() => new RequestNetworkError(it.statusCode, () => it.body.json()));
22
- }
23
- return (0, rxjs_1.from)(it.body.json());
24
- }), (0, rxjs_1.map)(payload => ({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload }))));
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantform/core",
3
- "version": "0.7.0-beta.20",
3
+ "version": "0.7.0-beta.22",
4
4
  "license": "MIT",
5
5
  "author": "Mateusz Majchrzak",
6
6
  "description": "Node.js library for building systematic trading strategies in reactive way.",
@@ -1,4 +1,4 @@
1
- import { defer, from, map, switchMap, throwError } from 'rxjs';
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(args: {
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 defer(() =>
33
- from(request(args.url, args)).pipe(
34
- switchMap(it => {
35
- if (it.statusCode !== 200) {
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
- ...args,
38
- statusCode: it.statusCode
42
+ method,
43
+ url,
44
+ headers,
45
+ body,
46
+ statusCode
39
47
  });
40
48
 
41
- return throwError(
42
- () => new RequestNetworkError(it.statusCode, () => it.body.json())
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
- return from(it.body.json());
47
- }),
48
- map(payload => ({ timestamp: useTimestamp(), payload }))
49
- )
50
- );
63
+ subscriber.error(error);
64
+ })
65
+ .finally(() => subscriber.complete());
66
+ });
51
67
  }