@nestia/fetcher 2.5.0-dev.20240130-7 → 2.5.0-dev.20240201
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/lib/NestiaSimulator.d.ts +13 -0
- package/lib/NestiaSimulator.js +62 -0
- package/lib/NestiaSimulator.js.map +1 -0
- package/package.json +1 -1
- package/src/NestiaSimulator.ts +82 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare namespace NestiaSimulator {
|
|
2
|
+
interface IProps {
|
|
3
|
+
host: string;
|
|
4
|
+
path: string;
|
|
5
|
+
method: "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
|
|
6
|
+
contentType: string;
|
|
7
|
+
}
|
|
8
|
+
const assert: (props: IProps) => {
|
|
9
|
+
param: (name: string) => <T>(task: () => T) => void;
|
|
10
|
+
query: <T_1>(task: () => T_1) => void;
|
|
11
|
+
body: <T_2>(task: () => T_2) => void;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NestiaSimulator = void 0;
|
|
4
|
+
var HttpError_1 = require("./HttpError");
|
|
5
|
+
var NestiaSimulator;
|
|
6
|
+
(function (NestiaSimulator) {
|
|
7
|
+
NestiaSimulator.assert = function (props) {
|
|
8
|
+
return {
|
|
9
|
+
param: param(props),
|
|
10
|
+
query: query(props),
|
|
11
|
+
body: body(props),
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
var param = function (props) {
|
|
15
|
+
return function (name) {
|
|
16
|
+
return function (task) {
|
|
17
|
+
validate(function (exp) { return "URL parameter \"".concat(name, "\" is not ").concat(exp.expected, " type."); })(props)(task);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
var query = function (props) {
|
|
22
|
+
return function (task) {
|
|
23
|
+
return validate(function () { return "Request query parameters are not following the promised type."; })(props)(task);
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
var body = function (props) {
|
|
27
|
+
return function (task) {
|
|
28
|
+
return validate(function () { return "Request body is not following the promised type."; })(props)(task);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
var validate = function (message, path) {
|
|
32
|
+
return function (props) {
|
|
33
|
+
return function (task) {
|
|
34
|
+
try {
|
|
35
|
+
task();
|
|
36
|
+
}
|
|
37
|
+
catch (exp) {
|
|
38
|
+
if (isTypeGuardError(exp))
|
|
39
|
+
throw new HttpError_1.HttpError(props.method, props.host + props.path, 400, {
|
|
40
|
+
"Content-Type": props.contentType,
|
|
41
|
+
}, JSON.stringify({
|
|
42
|
+
method: exp.method,
|
|
43
|
+
path: path !== null && path !== void 0 ? path : exp.path,
|
|
44
|
+
expected: exp.expected,
|
|
45
|
+
value: exp.value,
|
|
46
|
+
message: message(exp),
|
|
47
|
+
}));
|
|
48
|
+
throw exp;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
})(NestiaSimulator || (exports.NestiaSimulator = NestiaSimulator = {}));
|
|
54
|
+
var isTypeGuardError = function (input) {
|
|
55
|
+
return "string" === typeof input.method &&
|
|
56
|
+
(undefined === input.path || "string" === typeof input.path) &&
|
|
57
|
+
"string" === typeof input.expected &&
|
|
58
|
+
"string" === typeof input.name &&
|
|
59
|
+
"string" === typeof input.message &&
|
|
60
|
+
(undefined === input.stack || "string" === typeof input.stack);
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=NestiaSimulator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NestiaSimulator.js","sourceRoot":"","sources":["../src/NestiaSimulator.ts"],"names":[],"mappings":";;;AAAA,yCAAwC;AAExC,IAAiB,eAAe,CAgE/B;AAhED,WAAiB,eAAe;IAQjB,sBAAM,GAAG,UAAC,KAAa;QAClC,OAAO;YACL,KAAK,EAAE,KAAK,CA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B,IAAI,EAAE,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,GAAG,CAAC,IAAI;4BACtB,QAAQ,EAAE,GAAG,CAAC,QAAQ;4BACtB,KAAK,EAAE,GAAG,CAAC,KAAK;4BAChB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC;yBACtB,CAAC,CACH,CAAC;oBACJ,MAAM,GAAG,CAAC;gBACZ,CAAC;YACH,CAAC;QAtBD,CAsBC;IAvBD,CAuBC,CAAC;AACN,CAAC,EAhEgB,eAAe,+BAAf,eAAe,QAgE/B;AAED,IAAM,gBAAgB,GAAG,UAAC,KAAU;IAClC,OAAA,QAAQ,KAAK,OAAO,KAAK,CAAC,MAAM;QAChC,CAAC,SAAS,KAAK,KAAK,CAAC,IAAI,IAAI,QAAQ,KAAK,OAAO,KAAK,CAAC,IAAI,CAAC;QAC5D,QAAQ,KAAK,OAAO,KAAK,CAAC,QAAQ;QAClC,QAAQ,KAAK,OAAO,KAAK,CAAC,IAAI;QAC9B,QAAQ,KAAK,OAAO,KAAK,CAAC,OAAO;QACjC,CAAC,SAAS,KAAK,KAAK,CAAC,KAAK,IAAI,QAAQ,KAAK,OAAO,KAAK,CAAC,KAAK,CAAC;AAL9D,CAK8D,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { HttpError } from "./HttpError";
|
|
2
|
+
|
|
3
|
+
export namespace NestiaSimulator {
|
|
4
|
+
export interface IProps {
|
|
5
|
+
host: string;
|
|
6
|
+
path: string;
|
|
7
|
+
method: "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
|
|
8
|
+
contentType: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const assert = (props: IProps) => {
|
|
12
|
+
return {
|
|
13
|
+
param: param(props),
|
|
14
|
+
query: query(props),
|
|
15
|
+
body: body(props),
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
const param =
|
|
19
|
+
(props: IProps) =>
|
|
20
|
+
(name: string) =>
|
|
21
|
+
<T>(task: () => T): void => {
|
|
22
|
+
validate((exp) => `URL parameter "${name}" is not ${exp.expected} type.`)(
|
|
23
|
+
props,
|
|
24
|
+
)(task);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const query =
|
|
28
|
+
(props: IProps) =>
|
|
29
|
+
<T>(task: () => T): void =>
|
|
30
|
+
validate(
|
|
31
|
+
() => "Request query parameters are not following the promised type.",
|
|
32
|
+
)(props)(task);
|
|
33
|
+
|
|
34
|
+
const body =
|
|
35
|
+
(props: IProps) =>
|
|
36
|
+
<T>(task: () => T): void =>
|
|
37
|
+
validate(() => "Request body is not following the promised type.")(props)(
|
|
38
|
+
task,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const validate =
|
|
42
|
+
(message: (exp: TypeGuardError) => string, path?: string) =>
|
|
43
|
+
(props: IProps) =>
|
|
44
|
+
<T>(task: () => T): void => {
|
|
45
|
+
try {
|
|
46
|
+
task();
|
|
47
|
+
} catch (exp) {
|
|
48
|
+
if (isTypeGuardError(exp))
|
|
49
|
+
throw new HttpError(
|
|
50
|
+
props.method,
|
|
51
|
+
props.host + props.path,
|
|
52
|
+
400,
|
|
53
|
+
{
|
|
54
|
+
"Content-Type": props.contentType,
|
|
55
|
+
},
|
|
56
|
+
JSON.stringify({
|
|
57
|
+
method: exp.method,
|
|
58
|
+
path: path ?? exp.path,
|
|
59
|
+
expected: exp.expected,
|
|
60
|
+
value: exp.value,
|
|
61
|
+
message: message(exp),
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
throw exp;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const isTypeGuardError = (input: any): input is TypeGuardError =>
|
|
70
|
+
"string" === typeof input.method &&
|
|
71
|
+
(undefined === input.path || "string" === typeof input.path) &&
|
|
72
|
+
"string" === typeof input.expected &&
|
|
73
|
+
"string" === typeof input.name &&
|
|
74
|
+
"string" === typeof input.message &&
|
|
75
|
+
(undefined === input.stack || "string" === typeof input.stack);
|
|
76
|
+
|
|
77
|
+
interface TypeGuardError extends Error {
|
|
78
|
+
method: string;
|
|
79
|
+
path: string | undefined;
|
|
80
|
+
expected: string;
|
|
81
|
+
value: any;
|
|
82
|
+
}
|