@quantform/core 0.7.0-beta.22 → 0.7.0-beta.24

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,5 +1,5 @@
1
1
  /// <reference types="jest" />
2
- import { Observable } from 'rxjs';
2
+ import { Observable, Subject } from 'rxjs';
3
3
  import { Dependency } from './module';
4
4
  export declare function makeTestModule(dependencies: Dependency[]): Promise<{
5
5
  act: <T>(func: () => T) => T;
@@ -9,5 +9,6 @@ type MockableFunction = (...args: any[]) => any;
9
9
  export declare const mockedFunc: <Func extends MockableFunction>(mockedFunc: Func) => jest.MockedFunction<Func>;
10
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
+ export declare function mockSubject<T extends jest.FunctionProperties<Required<T>>, M extends keyof jest.FunctionProperties<Required<T>>>(object: T, method: M): Subject<InferObservableType<ReturnType<jest.FunctionProperties<Required<T>>[M]>>>;
12
13
  export {};
13
14
  //# 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,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"}
1
+ {"version":3,"file":"make-test-module.d.ts","sourceRoot":"","sources":["../src/make-test-module.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG3C,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;AAE/E,wBAAgB,WAAW,CACzB,CAAC,SAAS,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAC9C,CAAC,SAAS,MAAM,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EACpD,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,qFAYrB"}
@@ -9,7 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.toArray = exports.mockedFunc = exports.makeTestModule = void 0;
12
+ exports.mockSubject = exports.toArray = exports.mockedFunc = exports.makeTestModule = void 0;
13
+ const rxjs_1 = require("rxjs");
13
14
  const core_1 = require("./core");
14
15
  const module_1 = require("./module");
15
16
  function makeTestModule(dependencies) {
@@ -46,3 +47,11 @@ function toArray(observable) {
46
47
  return array;
47
48
  }
48
49
  exports.toArray = toArray;
50
+ function mockSubject(object, method) {
51
+ const subject = new rxjs_1.Subject();
52
+ jest
53
+ .spyOn(object, method)
54
+ .mockReturnValue(subject.asObservable());
55
+ return subject;
56
+ }
57
+ exports.mockSubject = mockSubject;
@@ -18,7 +18,7 @@ function whenSocket(url, options = { pingInterval: 5000 }) {
18
18
  stream.error(it);
19
19
  };
20
20
  socket.onclose = () => {
21
- debug('errored', url);
21
+ debug('closed', url);
22
22
  clearInterval(interval);
23
23
  stream.error();
24
24
  };
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"with-request.d.ts","sourceRoot":"","sources":["../src/with-request.ts"],"names":[],"mappings":"AACA,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;GA6C5D"}
@@ -1,6 +1,16 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.withRequest = exports.RequestNetworkError = void 0;
13
+ const node_crypto_1 = require("node:crypto");
4
14
  const rxjs_1 = require("rxjs");
5
15
  const undici_1 = require("undici");
6
16
  const use_logger_1 = require("./use-logger");
@@ -14,10 +24,21 @@ class RequestNetworkError extends Error {
14
24
  }
15
25
  exports.RequestNetworkError = RequestNetworkError;
16
26
  function withRequest({ method, url, headers, body }) {
17
- const { error } = (0, use_logger_1.useLogger)(withRequest.name);
27
+ const { error, debug } = (0, use_logger_1.useLogger)(withRequest.name);
18
28
  return new rxjs_1.Observable(subscriber => {
29
+ const correlationId = (0, node_crypto_1.randomUUID)();
30
+ debug('requesting', { correlationId, method, url, headers, body });
19
31
  (0, undici_1.request)(url, { method, headers, body })
20
- .then(({ statusCode, body }) => {
32
+ .then(({ statusCode, body }) => __awaiter(this, void 0, void 0, function* () {
33
+ const json = yield body.json();
34
+ debug('received', {
35
+ correlationId,
36
+ method,
37
+ url,
38
+ headers,
39
+ body: json,
40
+ statusCode
41
+ });
21
42
  if (statusCode !== 200) {
22
43
  error(`errored`, {
23
44
  method,
@@ -26,12 +47,12 @@ function withRequest({ method, url, headers, body }) {
26
47
  body,
27
48
  statusCode
28
49
  });
29
- subscriber.error(new RequestNetworkError(statusCode, () => body.json()));
50
+ subscriber.error(new RequestNetworkError(statusCode, () => json));
30
51
  }
31
52
  else {
32
- subscriber.next({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload: body.json() });
53
+ subscriber.next({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload: json });
33
54
  }
34
- })
55
+ }))
35
56
  .catch((e) => {
36
57
  error(`errored`, {
37
58
  method,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantform/core",
3
- "version": "0.7.0-beta.22",
3
+ "version": "0.7.0-beta.24",
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 } from 'rxjs';
1
+ import { Observable, Subject } from 'rxjs';
2
2
 
3
3
  import { core } from '@lib/core';
4
4
  import { Dependency, Module } from '@lib/module';
@@ -47,3 +47,20 @@ export function toArray<T>(observable: Observable<T>) {
47
47
  }
48
48
 
49
49
  export type InferObservableType<T> = T extends Observable<infer U> ? U : never;
50
+
51
+ export function mockSubject<
52
+ T extends jest.FunctionProperties<Required<T>>,
53
+ M extends keyof jest.FunctionProperties<Required<T>>
54
+ >(object: T, method: M) {
55
+ const subject = new Subject<
56
+ InferObservableType<ReturnType<jest.FunctionProperties<Required<T>>[M]>>
57
+ >();
58
+
59
+ jest
60
+ .spyOn<T, M>(object, method)
61
+ .mockReturnValue(
62
+ subject.asObservable() as ReturnType<jest.FunctionProperties<Required<T>>[M]>
63
+ );
64
+
65
+ return subject;
66
+ }
@@ -23,7 +23,7 @@ export function whenSocket(
23
23
  stream.error(it);
24
24
  };
25
25
  socket.onclose = () => {
26
- debug('errored', url);
26
+ debug('closed', url);
27
27
  clearInterval(interval);
28
28
  stream.error();
29
29
  };
@@ -1,3 +1,4 @@
1
+ import { randomUUID } from 'node:crypto';
1
2
  import { Observable } from 'rxjs';
2
3
  import { request } from 'undici';
3
4
 
@@ -32,11 +33,26 @@ export function withRequest({
32
33
  headers?: Record<string, any>;
33
34
  body?: string;
34
35
  }) {
35
- const { error } = useLogger(withRequest.name);
36
+ const { error, debug } = useLogger(withRequest.name);
36
37
 
37
38
  return new Observable<{ timestamp: number; payload: unknown }>(subscriber => {
39
+ const correlationId = randomUUID();
40
+
41
+ debug('requesting', { correlationId, method, url, headers, body });
42
+
38
43
  request(url, { method, headers, body })
39
- .then(({ statusCode, body }) => {
44
+ .then(async ({ statusCode, body }) => {
45
+ const json = await body.json();
46
+
47
+ debug('received', {
48
+ correlationId,
49
+ method,
50
+ url,
51
+ headers,
52
+ body: json,
53
+ statusCode
54
+ });
55
+
40
56
  if (statusCode !== 200) {
41
57
  error(`errored`, {
42
58
  method,
@@ -46,9 +62,9 @@ export function withRequest({
46
62
  statusCode
47
63
  });
48
64
 
49
- subscriber.error(new RequestNetworkError(statusCode, () => body.json()));
65
+ subscriber.error(new RequestNetworkError(statusCode, () => json));
50
66
  } else {
51
- subscriber.next({ timestamp: useTimestamp(), payload: body.json() });
67
+ subscriber.next({ timestamp: useTimestamp(), payload: json });
52
68
  }
53
69
  })
54
70
  .catch((e: Error) => {