@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.
- package/dist/make-test-module.d.ts +2 -1
- package/dist/make-test-module.d.ts.map +1 -1
- package/dist/make-test-module.js +10 -1
- package/dist/when-socket.js +1 -1
- package/dist/with-request.d.ts.map +1 -1
- package/dist/with-request.js +26 -5
- package/package.json +1 -1
- package/src/make-test-module.ts +18 -1
- package/src/when-socket.ts +1 -1
- package/src/with-request.ts +20 -4
|
@@ -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;
|
|
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"}
|
package/dist/make-test-module.js
CHANGED
|
@@ -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;
|
package/dist/when-socket.js
CHANGED
|
@@ -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":"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"}
|
package/dist/with-request.js
CHANGED
|
@@ -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, () =>
|
|
50
|
+
subscriber.error(new RequestNetworkError(statusCode, () => json));
|
|
30
51
|
}
|
|
31
52
|
else {
|
|
32
|
-
subscriber.next({ timestamp: (0, use_timestamp_1.useTimestamp)(), payload:
|
|
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
package/src/make-test-module.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/when-socket.ts
CHANGED
package/src/with-request.ts
CHANGED
|
@@ -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, () =>
|
|
65
|
+
subscriber.error(new RequestNetworkError(statusCode, () => json));
|
|
50
66
|
} else {
|
|
51
|
-
subscriber.next({ timestamp: useTimestamp(), payload:
|
|
67
|
+
subscriber.next({ timestamp: useTimestamp(), payload: json });
|
|
52
68
|
}
|
|
53
69
|
})
|
|
54
70
|
.catch((e: Error) => {
|