@quantform/core 0.7.0-beta.10 → 0.7.0-beta.12

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.
@@ -7,7 +7,7 @@ export declare function makeTestModule(dependencies: Dependency[]): Promise<{
7
7
  }>;
8
8
  type MockableFunction = (...args: any[]) => any;
9
9
  export declare const mockedFunc: <Func extends MockableFunction>(mockedFunc: Func) => jest.MockedFunction<Func>;
10
- export declare function toArray<T>(observable: Observable<T>): T[];
10
+ export declare function toArray<T>(observable: Observable<T>): (Error | T)[];
11
11
  export type InferObservableType<T> = T extends Observable<infer U> ? U : never;
12
12
  export {};
13
13
  //# sourceMappingURL=make-test-module.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"make-test-module.d.ts","sourceRoot":"","sources":["../src/make-test-module.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAO,MAAM,MAAM,CAAC;AAGvC,OAAO,EAAE,UAAU,EAAU,MAAM,aAAa,CAAC;AAEjD,wBAAsB,cAAc,CAAC,YAAY,EAAE,UAAU,EAAE;;;GAS9D;AAED,KAAK,gBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAEhD,eAAO,MAAM,UAAU,gFAC+B,CAAC;AAEvD,wBAAgB,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,OAsBnD;AAED,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC"}
1
+ {"version":3,"file":"make-test-module.d.ts","sourceRoot":"","sources":["../src/make-test-module.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAGlC,OAAO,EAAE,UAAU,EAAU,MAAM,aAAa,CAAC;AAEjD,wBAAsB,cAAc,CAAC,YAAY,EAAE,UAAU,EAAE;;;GAS9D;AAED,KAAK,gBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAEhD,eAAO,MAAM,UAAU,gFAC+B,CAAC;AAEvD,wBAAgB,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,iBAyBnD;AAED,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC"}
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.toArray = exports.mockedFunc = exports.makeTestModule = void 0;
13
- const rxjs_1 = require("rxjs");
14
13
  const core_1 = require("./core");
15
14
  const module_1 = require("./module");
16
15
  function makeTestModule(dependencies) {
@@ -40,7 +39,10 @@ function toArray(observable) {
40
39
  }
41
40
  return it;
42
41
  };
43
- observable.pipe((0, rxjs_1.tap)(it => array.push(clone(it)))).subscribe();
42
+ observable.subscribe({
43
+ next: it => array.push(clone(it)),
44
+ error: it => array.push(it)
45
+ });
44
46
  return array;
45
47
  }
46
48
  exports.toArray = toArray;
@@ -31,7 +31,7 @@ exports.useLogger = (0, use_1.use)((context, tint) => {
31
31
  ? console.warn(`${prefix()}: ${message}`, params)
32
32
  : console.warn(`${prefix()}: ${message}`),
33
33
  error: (message, ...params) => (params === null || params === void 0 ? void 0 : params.length)
34
- ? console.error(`${prefix()}: ${message}`, params)
35
- : console.error(`${prefix()}: ${message}`)
34
+ ? console.error(`${prefix()}: ${chalk_1.default.red(message)}`, params)
35
+ : console.error(`${prefix()}: ${chalk_1.default.red(message)}`)
36
36
  };
37
37
  });
@@ -1 +1 @@
1
- {"version":3,"file":"use-request.d.ts","sourceRoot":"","sources":["../src/use-request.ts"],"names":[],"mappings":"AAKA,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,UAAU,CAAC,IAAI,EAAE;IAC/B,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;;;GAmBA"}
1
+ {"version":3,"file":"use-request.d.ts","sourceRoot":"","sources":["../src/use-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,UAAU,CAAC,IAAI,EAAE;IAC/B,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;;;GA8BA"}
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useRequest = exports.RequestNetworkError = void 0;
4
4
  const rxjs_1 = require("rxjs");
5
5
  const undici_1 = require("undici");
6
+ const use_logger_1 = require("./use-logger");
6
7
  const use_timestamp_1 = require("./use-timestamp");
7
8
  class RequestNetworkError extends Error {
8
9
  constructor(statusCode, json) {
@@ -13,15 +14,20 @@ class RequestNetworkError extends Error {
13
14
  }
14
15
  exports.RequestNetworkError = RequestNetworkError;
15
16
  function useRequest(args) {
16
- return (0, rxjs_1.from)((0, undici_1.request)(args.url, {
17
- method: args.method,
18
- headers: args.headers,
19
- body: args.body
20
- })).pipe((0, rxjs_1.switchMap)(it => {
21
- if (it.statusCode !== 200) {
22
- return (0, rxjs_1.throwError)(() => new RequestNetworkError(it.statusCode, () => it.body.json()));
23
- }
24
- return (0, rxjs_1.from)(it.body.json());
25
- }), (0, rxjs_1.map)(payload => ({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload })));
17
+ const { info, error } = (0, use_logger_1.useLogger)(useRequest.name);
18
+ return (0, rxjs_1.defer)(() => {
19
+ info('requested', args);
20
+ return (0, rxjs_1.from)((0, undici_1.request)(args.url, {
21
+ method: args.method,
22
+ headers: args.headers,
23
+ body: args.body
24
+ })).pipe((0, rxjs_1.switchMap)(it => {
25
+ if (it.statusCode !== 200) {
26
+ error(`errored`, Object.assign(Object.assign({}, args), { statusCode: it.statusCode }));
27
+ return (0, rxjs_1.throwError)(() => new RequestNetworkError(it.statusCode, () => it.body.json()));
28
+ }
29
+ return (0, rxjs_1.from)(it.body.json());
30
+ }), (0, rxjs_1.map)(payload => ({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload })));
31
+ });
26
32
  }
27
33
  exports.useRequest = useRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantform/core",
3
- "version": "0.7.0-beta.10",
3
+ "version": "0.7.0-beta.12",
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 { Observable, tap } from 'rxjs';
1
+ import { Observable } from 'rxjs';
2
2
 
3
3
  import { core } from '@lib/core';
4
4
  import { Dependency, Module } from '@lib/module';
@@ -20,9 +20,9 @@ export const mockedFunc = <Func extends MockableFunction>(mockedFunc: Func) =>
20
20
  mockedFunc as jest.MockedFunction<typeof mockedFunc>;
21
21
 
22
22
  export function toArray<T>(observable: Observable<T>) {
23
- const array = Array.of<T>();
23
+ const array = Array.of<T | Error>();
24
24
 
25
- const clone = (it: T): T => {
25
+ const clone = (it: T | Error): T | Error => {
26
26
  if (typeof it === 'symbol') {
27
27
  return it;
28
28
  }
@@ -38,7 +38,10 @@ export function toArray<T>(observable: Observable<T>) {
38
38
  return it;
39
39
  };
40
40
 
41
- observable.pipe(tap(it => array.push(clone(it)))).subscribe();
41
+ observable.subscribe({
42
+ next: it => array.push(clone(it)),
43
+ error: it => array.push(it)
44
+ });
42
45
 
43
46
  return array;
44
47
  }
package/src/use-logger.ts CHANGED
@@ -41,7 +41,7 @@ export const useLogger = use((context: string, tint?: string) => {
41
41
 
42
42
  error: (message: any, ...params: unknown[]) =>
43
43
  params?.length
44
- ? console.error(`${prefix()}: ${message}`, params)
45
- : console.error(`${prefix()}: ${message}`)
44
+ ? console.error(`${prefix()}: ${chalk.red(message)}`, params)
45
+ : console.error(`${prefix()}: ${chalk.red(message)}`)
46
46
  };
47
47
  });
@@ -1,6 +1,7 @@
1
- import { from, map, switchMap, throwError } from 'rxjs';
1
+ import { defer, from, map, switchMap, throwError } from 'rxjs';
2
2
  import { request } from 'undici';
3
3
 
4
+ import { useLogger } from './use-logger';
4
5
  import { useTimestamp } from './use-timestamp';
5
6
 
6
7
  export type RequestMethod =
@@ -26,22 +27,33 @@ export function useRequest(args: {
26
27
  headers?: Record<string, any>;
27
28
  body?: string;
28
29
  }) {
29
- return from(
30
- request(args.url, {
31
- method: args.method,
32
- headers: args.headers,
33
- body: args.body
34
- })
35
- ).pipe(
36
- switchMap(it => {
37
- if (it.statusCode !== 200) {
38
- return throwError(
39
- () => new RequestNetworkError(it.statusCode, () => it.body.json())
40
- );
41
- }
42
-
43
- return from(it.body.json());
44
- }),
45
- map(payload => ({ timestamp: useTimestamp(), payload }))
46
- );
30
+ const { info, error } = useLogger(useRequest.name);
31
+
32
+ return defer(() => {
33
+ info('requested', args);
34
+
35
+ return from(
36
+ request(args.url, {
37
+ method: args.method,
38
+ headers: args.headers,
39
+ body: args.body
40
+ })
41
+ ).pipe(
42
+ switchMap(it => {
43
+ if (it.statusCode !== 200) {
44
+ error(`errored`, {
45
+ ...args,
46
+ statusCode: it.statusCode
47
+ });
48
+
49
+ return throwError(
50
+ () => new RequestNetworkError(it.statusCode, () => it.body.json())
51
+ );
52
+ }
53
+
54
+ return from(it.body.json());
55
+ }),
56
+ map(payload => ({ timestamp: useTimestamp(), payload }))
57
+ );
58
+ });
47
59
  }