@nestia/fetcher 8.1.0 → 9.0.0-dev.20251107

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,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
+ }
@@ -1,105 +1,105 @@
1
- import { IConnection } from "./IConnection";
2
- import { IFetchRoute } from "./IFetchRoute";
3
- import { IPropagation } from "./IPropagation";
4
- import { FetcherBase } from "./internal/FetcherBase";
5
-
6
- /**
7
- * Utility class for `fetch` functions used in `@nestia/sdk`.
8
- *
9
- * `PlainFetcher` is a utility class designed for SDK functions generated by
10
- * [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
11
- * HTTP sever API. In other words, this is a collection of dedicated `fetch()`
12
- * functions for `@nestia/sdk`.
13
- *
14
- * For reference, `PlainFetcher` class does not encrypt or decrypt the body data
15
- * at all. It just delivers plain data without any post processing. If you've
16
- * defined a controller method through `@EncryptedRoute` or `@EncryptedBody`
17
- * decorator, then {@liink EncryptedFetcher} class would be used instead.
18
- *
19
- * @author Jeongho Nam - https://github.com/samchon
20
- */
21
- export namespace PlainFetcher {
22
- /**
23
- * Fetch function only for `HEAD` method.
24
- *
25
- * @param connection Connection information for the remote HTTP server
26
- * @param route Route information about the target API
27
- * @returns Nothing because of `HEAD` method
28
- */
29
- export function fetch(
30
- connection: IConnection,
31
- route: IFetchRoute<"HEAD">,
32
- ): Promise<void>;
33
-
34
- /**
35
- * Fetch function only for `GET` method.
36
- *
37
- * @param connection Connection information for the remote HTTP server
38
- * @param route Route information about the target API
39
- * @returns Response body data from the remote API
40
- */
41
- export function fetch<Output>(
42
- connection: IConnection,
43
- route: IFetchRoute<"GET">,
44
- ): Promise<Output>;
45
-
46
- /**
47
- * Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
48
- *
49
- * @param connection Connection information for the remote HTTP server
50
- * @param route Route information about the target API
51
- * @returns Response body data from the remote API
52
- */
53
- export function fetch<Input, Output>(
54
- connection: IConnection,
55
- route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
56
- input?: Input,
57
- stringify?: (input: Input) => string,
58
- ): Promise<Output>;
59
-
60
- export async function fetch<Input, Output>(
61
- connection: IConnection,
62
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
63
- input?: Input,
64
- stringify?: (input: Input) => string,
65
- ): Promise<Output> {
66
- if (route.request?.encrypted === true || route.response?.encrypted === true)
67
- throw new Error(
68
- "Error on PlainFetcher.fetch(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
69
- );
70
- return FetcherBase.request({
71
- className: "PlainFetcher",
72
- encode: (input) => input,
73
- decode: (input) => input,
74
- })(connection, route, input, stringify);
75
- }
76
-
77
- export function propagate<Output extends IPropagation<any, any>>(
78
- connection: IConnection,
79
- route: IFetchRoute<"GET" | "HEAD">,
80
- ): Promise<Output>;
81
-
82
- export function propagate<Input, Output extends IPropagation<any, any>>(
83
- connection: IConnection,
84
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
85
- input?: Input,
86
- stringify?: (input: Input) => string,
87
- ): Promise<Output>;
88
-
89
- export async function propagate<Input, Output extends IPropagation<any, any>>(
90
- connection: IConnection,
91
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
92
- input?: Input,
93
- stringify?: (input: Input) => string,
94
- ): Promise<Output> {
95
- if (route.request?.encrypted === true || route.response?.encrypted === true)
96
- throw new Error(
97
- "Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
98
- );
99
- return FetcherBase.propagate({
100
- className: "PlainFetcher",
101
- encode: (input) => input,
102
- decode: (input) => input,
103
- })(connection, route, input, stringify) as Promise<Output>;
104
- }
105
- }
1
+ import { IConnection } from "./IConnection";
2
+ import { IFetchRoute } from "./IFetchRoute";
3
+ import { IPropagation } from "./IPropagation";
4
+ import { FetcherBase } from "./internal/FetcherBase";
5
+
6
+ /**
7
+ * Utility class for `fetch` functions used in `@nestia/sdk`.
8
+ *
9
+ * `PlainFetcher` is a utility class designed for SDK functions generated by
10
+ * [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
11
+ * HTTP sever API. In other words, this is a collection of dedicated `fetch()`
12
+ * functions for `@nestia/sdk`.
13
+ *
14
+ * For reference, `PlainFetcher` class does not encrypt or decrypt the body data
15
+ * at all. It just delivers plain data without any post processing. If you've
16
+ * defined a controller method through `@EncryptedRoute` or `@EncryptedBody`
17
+ * decorator, then {@liink EncryptedFetcher} class would be used instead.
18
+ *
19
+ * @author Jeongho Nam - https://github.com/samchon
20
+ */
21
+ export namespace PlainFetcher {
22
+ /**
23
+ * Fetch function only for `HEAD` method.
24
+ *
25
+ * @param connection Connection information for the remote HTTP server
26
+ * @param route Route information about the target API
27
+ * @returns Nothing because of `HEAD` method
28
+ */
29
+ export function fetch(
30
+ connection: IConnection,
31
+ route: IFetchRoute<"HEAD">,
32
+ ): Promise<void>;
33
+
34
+ /**
35
+ * Fetch function only for `GET` method.
36
+ *
37
+ * @param connection Connection information for the remote HTTP server
38
+ * @param route Route information about the target API
39
+ * @returns Response body data from the remote API
40
+ */
41
+ export function fetch<Output>(
42
+ connection: IConnection,
43
+ route: IFetchRoute<"GET">,
44
+ ): Promise<Output>;
45
+
46
+ /**
47
+ * Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
48
+ *
49
+ * @param connection Connection information for the remote HTTP server
50
+ * @param route Route information about the target API
51
+ * @returns Response body data from the remote API
52
+ */
53
+ export function fetch<Input, Output>(
54
+ connection: IConnection,
55
+ route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
56
+ input?: Input,
57
+ stringify?: (input: Input) => string,
58
+ ): Promise<Output>;
59
+
60
+ export async function fetch<Input, Output>(
61
+ connection: IConnection,
62
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
63
+ input?: Input,
64
+ stringify?: (input: Input) => string,
65
+ ): Promise<Output> {
66
+ if (route.request?.encrypted === true || route.response?.encrypted === true)
67
+ throw new Error(
68
+ "Error on PlainFetcher.fetch(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
69
+ );
70
+ return FetcherBase.request({
71
+ className: "PlainFetcher",
72
+ encode: (input) => input,
73
+ decode: (input) => input,
74
+ })(connection, route, input, stringify);
75
+ }
76
+
77
+ export function propagate<Output extends IPropagation<any, any>>(
78
+ connection: IConnection,
79
+ route: IFetchRoute<"GET" | "HEAD">,
80
+ ): Promise<Output>;
81
+
82
+ export function propagate<Input, Output extends IPropagation<any, any>>(
83
+ connection: IConnection,
84
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
85
+ input?: Input,
86
+ stringify?: (input: Input) => string,
87
+ ): Promise<Output>;
88
+
89
+ export async function propagate<Input, Output extends IPropagation<any, any>>(
90
+ connection: IConnection,
91
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
92
+ input?: Input,
93
+ stringify?: (input: Input) => string,
94
+ ): Promise<Output> {
95
+ if (route.request?.encrypted === true || route.response?.encrypted === true)
96
+ throw new Error(
97
+ "Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
98
+ );
99
+ return FetcherBase.propagate({
100
+ className: "PlainFetcher",
101
+ encode: (input) => input,
102
+ decode: (input) => input,
103
+ })(connection, route, input, stringify) as Promise<Output>;
104
+ }
105
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- export * from "./FormDataInput";
2
- export * from "./HttpError";
3
- export * from "./IConnection";
4
- export * from "./IEncryptionPassword";
5
- export * from "./IFetchEvent";
6
- export * from "./IFetchRoute";
7
- export * from "./IPropagation";
1
+ export * from "./FormDataInput";
2
+ export * from "./HttpError";
3
+ export * from "./IConnection";
4
+ export * from "./IEncryptionPassword";
5
+ export * from "./IFetchEvent";
6
+ export * from "./IFetchRoute";
7
+ export * from "./IPropagation";