@nestia/fetcher 10.0.2 → 11.0.0-dev.20260305
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/LICENSE +21 -0
- package/README.md +93 -93
- package/package.json +20 -13
- package/src/AesPkcs5.ts +41 -49
- package/src/EncryptedFetcher.ts +176 -176
- package/src/FormDataInput.ts +80 -80
- package/src/HttpError.ts +1 -1
- package/src/IConnection.ts +241 -241
- package/src/IEncryptionPassword.ts +44 -44
- package/src/IFetchEvent.ts +31 -31
- package/src/IFetchRoute.ts +60 -60
- package/src/IPropagation.ts +99 -99
- package/src/NestiaSimulator.ts +82 -82
- package/src/PlainFetcher.ts +105 -105
- package/src/index.ts +12 -7
- package/src/internal/FetcherBase.ts +235 -235
- package/lib/AesPkcs5.d.ts +0 -30
- package/lib/AesPkcs5.js +0 -49
- package/lib/AesPkcs5.js.map +0 -1
- package/lib/EncryptedFetcher.d.ts +0 -47
- package/lib/EncryptedFetcher.js +0 -139
- package/lib/EncryptedFetcher.js.map +0 -1
- package/lib/FormDataInput.d.ts +0 -70
- package/lib/FormDataInput.js +0 -3
- package/lib/FormDataInput.js.map +0 -1
- package/lib/HttpError.d.ts +0 -1
- package/lib/HttpError.js +0 -6
- package/lib/HttpError.js.map +0 -1
- package/lib/IConnection.d.ts +0 -165
- package/lib/IConnection.js +0 -3
- package/lib/IConnection.js.map +0 -1
- package/lib/IEncryptionPassword.d.ts +0 -41
- package/lib/IEncryptionPassword.js +0 -3
- package/lib/IEncryptionPassword.js.map +0 -1
- package/lib/IFetchEvent.d.ts +0 -11
- package/lib/IFetchEvent.js +0 -21
- package/lib/IFetchEvent.js.map +0 -1
- package/lib/IFetchRoute.d.ts +0 -46
- package/lib/IFetchRoute.js +0 -3
- package/lib/IFetchRoute.js.map +0 -1
- package/lib/IPropagation.d.ts +0 -69
- package/lib/IPropagation.js +0 -3
- package/lib/IPropagation.js.map +0 -1
- package/lib/MigrateFetcher.d.ts +0 -19
- package/lib/MigrateFetcher.js +0 -179
- package/lib/MigrateFetcher.js.map +0 -1
- package/lib/NestiaSimulator.d.ts +0 -13
- package/lib/NestiaSimulator.js +0 -62
- package/lib/NestiaSimulator.js.map +0 -1
- package/lib/PlainFetcher.d.ts +0 -46
- package/lib/PlainFetcher.js +0 -89
- package/lib/PlainFetcher.js.map +0 -1
- package/lib/index.d.ts +0 -7
- package/lib/index.js +0 -24
- package/lib/index.js.map +0 -1
- package/lib/internal/FetcherBase.d.ts +0 -1
- package/lib/internal/FetcherBase.js +0 -348
- package/lib/internal/FetcherBase.js.map +0 -1
- package/src/MigrateFetcher.ts +0 -118
package/src/IFetchRoute.ts
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Properties of remote API route.
|
|
3
|
-
*
|
|
4
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
5
|
-
*/
|
|
6
|
-
export interface IFetchRoute<
|
|
7
|
-
Method extends "HEAD" | "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
|
|
8
|
-
> {
|
|
9
|
-
/** Method of the HTTP request. */
|
|
10
|
-
method: Method;
|
|
11
|
-
|
|
12
|
-
/** Path of the HTTP request. */
|
|
13
|
-
path: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Path template.
|
|
17
|
-
*
|
|
18
|
-
* Filled since 3.2.2 version.
|
|
19
|
-
*/
|
|
20
|
-
template?: string;
|
|
21
|
-
|
|
22
|
-
/** Request body data info. */
|
|
23
|
-
request: Method extends "DELETE" | "POST" | "PUT" | "PATCH"
|
|
24
|
-
? IFetchRoute.IBody | null
|
|
25
|
-
: null;
|
|
26
|
-
|
|
27
|
-
/** Response body data info. */
|
|
28
|
-
response: Method extends "HEAD" ? null : IFetchRoute.IBody;
|
|
29
|
-
|
|
30
|
-
/** When special status code being used. */
|
|
31
|
-
status: number | null;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Parser of the query string.
|
|
35
|
-
*
|
|
36
|
-
* If content type of response body is `application/x-www-form-urlencoded`,
|
|
37
|
-
* then this `parseQuery` function would be called.
|
|
38
|
-
*
|
|
39
|
-
* If you've forgotten to configuring this `parseQuery` property about the
|
|
40
|
-
* `application/x-www-form-urlencoded` typed response body data, then only the
|
|
41
|
-
* `URLSearchParams` typed instance would be returned instead.
|
|
42
|
-
*/
|
|
43
|
-
parseQuery?(input: URLSearchParams): any;
|
|
44
|
-
}
|
|
45
|
-
export namespace IFetchRoute {
|
|
46
|
-
/**
|
|
47
|
-
* Metadata of body.
|
|
48
|
-
*
|
|
49
|
-
* Describes how content-type being used in body, and whether encrypted or
|
|
50
|
-
* not.
|
|
51
|
-
*/
|
|
52
|
-
export interface IBody {
|
|
53
|
-
type:
|
|
54
|
-
| "application/json"
|
|
55
|
-
| "application/x-www-form-urlencoded"
|
|
56
|
-
| "multipart/form-data"
|
|
57
|
-
| "text/plain";
|
|
58
|
-
encrypted?: boolean;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Properties of remote API route.
|
|
3
|
+
*
|
|
4
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
5
|
+
*/
|
|
6
|
+
export interface IFetchRoute<
|
|
7
|
+
Method extends "HEAD" | "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
|
|
8
|
+
> {
|
|
9
|
+
/** Method of the HTTP request. */
|
|
10
|
+
method: Method;
|
|
11
|
+
|
|
12
|
+
/** Path of the HTTP request. */
|
|
13
|
+
path: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Path template.
|
|
17
|
+
*
|
|
18
|
+
* Filled since 3.2.2 version.
|
|
19
|
+
*/
|
|
20
|
+
template?: string;
|
|
21
|
+
|
|
22
|
+
/** Request body data info. */
|
|
23
|
+
request: Method extends "DELETE" | "POST" | "PUT" | "PATCH"
|
|
24
|
+
? IFetchRoute.IBody | null
|
|
25
|
+
: null;
|
|
26
|
+
|
|
27
|
+
/** Response body data info. */
|
|
28
|
+
response: Method extends "HEAD" ? null : IFetchRoute.IBody;
|
|
29
|
+
|
|
30
|
+
/** When special status code being used. */
|
|
31
|
+
status: number | null;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Parser of the query string.
|
|
35
|
+
*
|
|
36
|
+
* If content type of response body is `application/x-www-form-urlencoded`,
|
|
37
|
+
* then this `parseQuery` function would be called.
|
|
38
|
+
*
|
|
39
|
+
* If you've forgotten to configuring this `parseQuery` property about the
|
|
40
|
+
* `application/x-www-form-urlencoded` typed response body data, then only the
|
|
41
|
+
* `URLSearchParams` typed instance would be returned instead.
|
|
42
|
+
*/
|
|
43
|
+
parseQuery?(input: URLSearchParams): any;
|
|
44
|
+
}
|
|
45
|
+
export namespace IFetchRoute {
|
|
46
|
+
/**
|
|
47
|
+
* Metadata of body.
|
|
48
|
+
*
|
|
49
|
+
* Describes how content-type being used in body, and whether encrypted or
|
|
50
|
+
* not.
|
|
51
|
+
*/
|
|
52
|
+
export interface IBody {
|
|
53
|
+
type:
|
|
54
|
+
| "application/json"
|
|
55
|
+
| "application/x-www-form-urlencoded"
|
|
56
|
+
| "multipart/form-data"
|
|
57
|
+
| "text/plain";
|
|
58
|
+
encrypted?: boolean;
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/IPropagation.ts
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Propagation type.
|
|
3
|
-
*
|
|
4
|
-
* `IPropagation` is a type gathering all possible status codes and their body
|
|
5
|
-
* data types as a discriminated union type. You can specify the status code and
|
|
6
|
-
* its body data type just by using conditional statement like below.
|
|
7
|
-
*
|
|
8
|
-
* ```typescript
|
|
9
|
-
* type Output = IPropagation<{
|
|
10
|
-
* 200: ISeller.IAuthorized;
|
|
11
|
-
* 400: TypeGuardError.IProps;
|
|
12
|
-
* >};
|
|
13
|
-
*
|
|
14
|
-
* const output: Output = await sdk.sellers.authenticate.join(input);
|
|
15
|
-
* if (output.success) {
|
|
16
|
-
* // automatically casted to "ISeller.IAuthorized" type
|
|
17
|
-
* const authorized: ISeller.IAuthorized = output.data;
|
|
18
|
-
* } else if (output.status === 400) {
|
|
19
|
-
* // automatically casted to "TypeGuardError.IProps" type
|
|
20
|
-
* const error: TypeGuardError.IProps = output.data;
|
|
21
|
-
* } else {
|
|
22
|
-
* // unknown type when out of pre-defined status codes
|
|
23
|
-
* const result: unknown = output.data;
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* For reference, this `IPropagation` type is utilized by SDK library generated
|
|
28
|
-
* by `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be
|
|
29
|
-
* `true`. In that case, SDK functions generated by `@nestia/sdk` no more
|
|
30
|
-
* returns response DTO typed data directly, but returns this `IPropagation`
|
|
31
|
-
* typed object instead.
|
|
32
|
-
*
|
|
33
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
34
|
-
* @template StatusMap Map of status code and its body data type.
|
|
35
|
-
* @template Success Default success status code.
|
|
36
|
-
*/
|
|
37
|
-
export type IPropagation<
|
|
38
|
-
StatusMap extends {
|
|
39
|
-
[P in IPropagation.Status]?: any;
|
|
40
|
-
},
|
|
41
|
-
Success extends number = 200 | 201,
|
|
42
|
-
> =
|
|
43
|
-
| {
|
|
44
|
-
[P in keyof StatusMap]: IPropagation.IBranch<
|
|
45
|
-
P extends Success ? true : false,
|
|
46
|
-
P,
|
|
47
|
-
StatusMap[P]
|
|
48
|
-
>;
|
|
49
|
-
}[keyof StatusMap]
|
|
50
|
-
| IPropagation.IBranch<false, unknown, unknown>;
|
|
51
|
-
export namespace IPropagation {
|
|
52
|
-
/**
|
|
53
|
-
* Type of configurable status codes.
|
|
54
|
-
*
|
|
55
|
-
* The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the
|
|
56
|
-
* range of status codes. If `5XX` is specified, it means the status code is
|
|
57
|
-
* in the range of `500` to `599`.
|
|
58
|
-
*/
|
|
59
|
-
export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Branch type of propagation.
|
|
63
|
-
*
|
|
64
|
-
* `IPropagation.IBranch` is a branch type composing `IPropagation` type,
|
|
65
|
-
* which is gathering all possible status codes and their body data types as a
|
|
66
|
-
* union type.
|
|
67
|
-
*/
|
|
68
|
-
export interface IBranch<Success extends boolean, StatusValue, BodyData> {
|
|
69
|
-
success: Success;
|
|
70
|
-
status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
|
|
71
|
-
? StatusRange<StatusValue>
|
|
72
|
-
: StatusValue extends number
|
|
73
|
-
? StatusValue
|
|
74
|
-
: never;
|
|
75
|
-
data: BodyData;
|
|
76
|
-
headers: Record<string, string | string[]>;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Range of status codes by the first digit. */
|
|
80
|
-
export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> = T extends 0
|
|
81
|
-
? IntRange<200, 299>
|
|
82
|
-
: T extends 3
|
|
83
|
-
? IntRange<300, 399>
|
|
84
|
-
: T extends 4
|
|
85
|
-
? IntRange<400, 499>
|
|
86
|
-
: IntRange<500, 599>;
|
|
87
|
-
|
|
88
|
-
type IntRange<F extends number, T extends number> = Exclude<
|
|
89
|
-
Enumerate<T>,
|
|
90
|
-
Enumerate<F>
|
|
91
|
-
>;
|
|
92
|
-
|
|
93
|
-
type Enumerate<
|
|
94
|
-
N extends number,
|
|
95
|
-
Acc extends number[] = [],
|
|
96
|
-
> = Acc["length"] extends N
|
|
97
|
-
? Acc[number]
|
|
98
|
-
: Enumerate<N, [...Acc, Acc["length"]]>;
|
|
99
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Propagation type.
|
|
3
|
+
*
|
|
4
|
+
* `IPropagation` is a type gathering all possible status codes and their body
|
|
5
|
+
* data types as a discriminated union type. You can specify the status code and
|
|
6
|
+
* its body data type just by using conditional statement like below.
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* type Output = IPropagation<{
|
|
10
|
+
* 200: ISeller.IAuthorized;
|
|
11
|
+
* 400: TypeGuardError.IProps;
|
|
12
|
+
* >};
|
|
13
|
+
*
|
|
14
|
+
* const output: Output = await sdk.sellers.authenticate.join(input);
|
|
15
|
+
* if (output.success) {
|
|
16
|
+
* // automatically casted to "ISeller.IAuthorized" type
|
|
17
|
+
* const authorized: ISeller.IAuthorized = output.data;
|
|
18
|
+
* } else if (output.status === 400) {
|
|
19
|
+
* // automatically casted to "TypeGuardError.IProps" type
|
|
20
|
+
* const error: TypeGuardError.IProps = output.data;
|
|
21
|
+
* } else {
|
|
22
|
+
* // unknown type when out of pre-defined status codes
|
|
23
|
+
* const result: unknown = output.data;
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* For reference, this `IPropagation` type is utilized by SDK library generated
|
|
28
|
+
* by `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be
|
|
29
|
+
* `true`. In that case, SDK functions generated by `@nestia/sdk` no more
|
|
30
|
+
* returns response DTO typed data directly, but returns this `IPropagation`
|
|
31
|
+
* typed object instead.
|
|
32
|
+
*
|
|
33
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
34
|
+
* @template StatusMap Map of status code and its body data type.
|
|
35
|
+
* @template Success Default success status code.
|
|
36
|
+
*/
|
|
37
|
+
export type IPropagation<
|
|
38
|
+
StatusMap extends {
|
|
39
|
+
[P in IPropagation.Status]?: any;
|
|
40
|
+
},
|
|
41
|
+
Success extends number = 200 | 201,
|
|
42
|
+
> =
|
|
43
|
+
| {
|
|
44
|
+
[P in keyof StatusMap]: IPropagation.IBranch<
|
|
45
|
+
P extends Success ? true : false,
|
|
46
|
+
P,
|
|
47
|
+
StatusMap[P]
|
|
48
|
+
>;
|
|
49
|
+
}[keyof StatusMap]
|
|
50
|
+
| IPropagation.IBranch<false, unknown, unknown>;
|
|
51
|
+
export namespace IPropagation {
|
|
52
|
+
/**
|
|
53
|
+
* Type of configurable status codes.
|
|
54
|
+
*
|
|
55
|
+
* The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the
|
|
56
|
+
* range of status codes. If `5XX` is specified, it means the status code is
|
|
57
|
+
* in the range of `500` to `599`.
|
|
58
|
+
*/
|
|
59
|
+
export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Branch type of propagation.
|
|
63
|
+
*
|
|
64
|
+
* `IPropagation.IBranch` is a branch type composing `IPropagation` type,
|
|
65
|
+
* which is gathering all possible status codes and their body data types as a
|
|
66
|
+
* union type.
|
|
67
|
+
*/
|
|
68
|
+
export interface IBranch<Success extends boolean, StatusValue, BodyData> {
|
|
69
|
+
success: Success;
|
|
70
|
+
status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
|
|
71
|
+
? StatusRange<StatusValue>
|
|
72
|
+
: StatusValue extends number
|
|
73
|
+
? StatusValue
|
|
74
|
+
: never;
|
|
75
|
+
data: BodyData;
|
|
76
|
+
headers: Record<string, string | string[]>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Range of status codes by the first digit. */
|
|
80
|
+
export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> = T extends 0
|
|
81
|
+
? IntRange<200, 299>
|
|
82
|
+
: T extends 3
|
|
83
|
+
? IntRange<300, 399>
|
|
84
|
+
: T extends 4
|
|
85
|
+
? IntRange<400, 499>
|
|
86
|
+
: IntRange<500, 599>;
|
|
87
|
+
|
|
88
|
+
type IntRange<F extends number, T extends number> = Exclude<
|
|
89
|
+
Enumerate<T>,
|
|
90
|
+
Enumerate<F>
|
|
91
|
+
>;
|
|
92
|
+
|
|
93
|
+
type Enumerate<
|
|
94
|
+
N extends number,
|
|
95
|
+
Acc extends number[] = [],
|
|
96
|
+
> = Acc["length"] extends N
|
|
97
|
+
? Acc[number]
|
|
98
|
+
: Enumerate<N, [...Acc, Acc["length"]]>;
|
|
99
|
+
}
|
package/src/NestiaSimulator.ts
CHANGED
|
@@ -1,82 +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
|
-
}
|
|
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
|
+
}
|