@nestia/fetcher 9.0.0-dev.20251107-3 → 9.0.1

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,118 +1,118 @@
1
- import { IHttpMigrateRoute } from "@samchon/openapi";
2
-
3
- import { IConnection } from "./IConnection";
4
- import { IPropagation } from "./IPropagation";
5
- import { PlainFetcher } from "./PlainFetcher";
6
-
7
- /**
8
- * Use `HttpMigration.execute()` function of `@samchon/openapi` instead.
9
- *
10
- * This module would be removed in the next major update.
11
- *
12
- * @deprecated
13
- */
14
- export namespace MigrateFetcher {
15
- export interface IProps {
16
- route: IHttpMigrateRoute;
17
- connection: IConnection;
18
- arguments: any[];
19
- }
20
-
21
- export async function request(props: IProps): Promise<any> {
22
- const length: number =
23
- props.route.parameters.length +
24
- (props.route.query ? 1 : 0) +
25
- (props.route.body ? 1 : 0);
26
- if (props.arguments.length !== length)
27
- throw new Error(
28
- `Error on MigrateFetcher.request(): arguments length is not matched with the route (expected: ${length}, actual: ${props.arguments.length}).`,
29
- );
30
- else if (
31
- props.route.body?.["x-nestia-encrypted"] === true ||
32
- props.route.success?.["x-nestia-encrypted"] === true
33
- )
34
- throw new Error(
35
- `Error on MigrateFetcher.request(): encrypted API is not supported yet.`,
36
- );
37
- return PlainFetcher.fetch(
38
- props.connection,
39
- {
40
- method: props.route.method.toUpperCase() as "POST",
41
- path: getPath(props),
42
- template: props.route.path,
43
- status: null,
44
- request: props.route.body
45
- ? {
46
- encrypted: false,
47
- type: props.route.body.type,
48
- }
49
- : null,
50
- response: {
51
- encrypted: false,
52
- type: props.route.success?.type ?? "application/json",
53
- },
54
- },
55
- props.route.body ? props.arguments.at(-1) : undefined,
56
- );
57
- }
58
-
59
- export async function propagate(
60
- props: IProps,
61
- ): Promise<IPropagation.IBranch<boolean, number, any>> {
62
- const length: number =
63
- props.route.parameters.length +
64
- (props.route.query ? 1 : 0) +
65
- (props.route.body ? 1 : 0);
66
- if (props.arguments.length !== length)
67
- throw new Error(
68
- `Error on MigrateFetcher.propagate(): arguments length is not matched with the route (expected: ${length}, actual: ${props.arguments.length}).`,
69
- );
70
- else if (
71
- props.route.body?.["x-nestia-encrypted"] === true ||
72
- props.route.success?.["x-nestia-encrypted"] === true
73
- )
74
- throw new Error(
75
- `Error on MigrateFetcher.propagate(): encrypted API is not supported yet.`,
76
- );
77
- return PlainFetcher.propagate(
78
- props.connection,
79
- {
80
- method: props.route.method.toUpperCase() as "POST",
81
- path: getPath(props),
82
- template: props.route.path,
83
- status: null,
84
- request: props.route.body
85
- ? {
86
- encrypted: false,
87
- type: props.route.body.type,
88
- }
89
- : null,
90
- response: {
91
- encrypted: false,
92
- type: props.route.success?.type ?? "application/json",
93
- },
94
- },
95
- props.route.body ? props.arguments.at(-1) : undefined,
96
- ) as Promise<IPropagation.IBranch<boolean, number, any>>;
97
- }
98
-
99
- function getPath(props: Pick<IProps, "arguments" | "route">): string {
100
- let path: string = props.route.emendedPath;
101
- props.route.parameters.forEach((p, i) => {
102
- path = path.replace(`:${p.key}`, props.arguments[i]);
103
- });
104
- if (props.route.query)
105
- path += getQueryPath(props.arguments[props.route.parameters.length]);
106
- return path;
107
- }
108
-
109
- function getQueryPath(query: Record<string, any>): string {
110
- const variables = new URLSearchParams();
111
- for (const [key, value] of Object.entries(query))
112
- if (undefined === value) continue;
113
- else if (Array.isArray(value))
114
- value.forEach((elem: any) => variables.append(key, String(elem)));
115
- else variables.set(key, String(value));
116
- return 0 === variables.size ? "" : `?${variables.toString()}`;
117
- }
118
- }
1
+ import { IHttpMigrateRoute } from "@samchon/openapi";
2
+
3
+ import { IConnection } from "./IConnection";
4
+ import { IPropagation } from "./IPropagation";
5
+ import { PlainFetcher } from "./PlainFetcher";
6
+
7
+ /**
8
+ * Use `HttpMigration.execute()` function of `@samchon/openapi` instead.
9
+ *
10
+ * This module would be removed in the next major update.
11
+ *
12
+ * @deprecated
13
+ */
14
+ export namespace MigrateFetcher {
15
+ export interface IProps {
16
+ route: IHttpMigrateRoute;
17
+ connection: IConnection;
18
+ arguments: any[];
19
+ }
20
+
21
+ export async function request(props: IProps): Promise<any> {
22
+ const length: number =
23
+ props.route.parameters.length +
24
+ (props.route.query ? 1 : 0) +
25
+ (props.route.body ? 1 : 0);
26
+ if (props.arguments.length !== length)
27
+ throw new Error(
28
+ `Error on MigrateFetcher.request(): arguments length is not matched with the route (expected: ${length}, actual: ${props.arguments.length}).`,
29
+ );
30
+ else if (
31
+ props.route.body?.["x-nestia-encrypted"] === true ||
32
+ props.route.success?.["x-nestia-encrypted"] === true
33
+ )
34
+ throw new Error(
35
+ `Error on MigrateFetcher.request(): encrypted API is not supported yet.`,
36
+ );
37
+ return PlainFetcher.fetch(
38
+ props.connection,
39
+ {
40
+ method: props.route.method.toUpperCase() as "POST",
41
+ path: getPath(props),
42
+ template: props.route.path,
43
+ status: null,
44
+ request: props.route.body
45
+ ? {
46
+ encrypted: false,
47
+ type: props.route.body.type,
48
+ }
49
+ : null,
50
+ response: {
51
+ encrypted: false,
52
+ type: props.route.success?.type ?? "application/json",
53
+ },
54
+ },
55
+ props.route.body ? props.arguments.at(-1) : undefined,
56
+ );
57
+ }
58
+
59
+ export async function propagate(
60
+ props: IProps,
61
+ ): Promise<IPropagation.IBranch<boolean, number, any>> {
62
+ const length: number =
63
+ props.route.parameters.length +
64
+ (props.route.query ? 1 : 0) +
65
+ (props.route.body ? 1 : 0);
66
+ if (props.arguments.length !== length)
67
+ throw new Error(
68
+ `Error on MigrateFetcher.propagate(): arguments length is not matched with the route (expected: ${length}, actual: ${props.arguments.length}).`,
69
+ );
70
+ else if (
71
+ props.route.body?.["x-nestia-encrypted"] === true ||
72
+ props.route.success?.["x-nestia-encrypted"] === true
73
+ )
74
+ throw new Error(
75
+ `Error on MigrateFetcher.propagate(): encrypted API is not supported yet.`,
76
+ );
77
+ return PlainFetcher.propagate(
78
+ props.connection,
79
+ {
80
+ method: props.route.method.toUpperCase() as "POST",
81
+ path: getPath(props),
82
+ template: props.route.path,
83
+ status: null,
84
+ request: props.route.body
85
+ ? {
86
+ encrypted: false,
87
+ type: props.route.body.type,
88
+ }
89
+ : null,
90
+ response: {
91
+ encrypted: false,
92
+ type: props.route.success?.type ?? "application/json",
93
+ },
94
+ },
95
+ props.route.body ? props.arguments.at(-1) : undefined,
96
+ ) as Promise<IPropagation.IBranch<boolean, number, any>>;
97
+ }
98
+
99
+ function getPath(props: Pick<IProps, "arguments" | "route">): string {
100
+ let path: string = props.route.emendedPath;
101
+ props.route.parameters.forEach((p, i) => {
102
+ path = path.replace(`:${p.key}`, props.arguments[i]);
103
+ });
104
+ if (props.route.query)
105
+ path += getQueryPath(props.arguments[props.route.parameters.length]);
106
+ return path;
107
+ }
108
+
109
+ function getQueryPath(query: Record<string, any>): string {
110
+ const variables = new URLSearchParams();
111
+ for (const [key, value] of Object.entries(query))
112
+ if (undefined === value) continue;
113
+ else if (Array.isArray(value))
114
+ value.forEach((elem: any) => variables.append(key, String(elem)));
115
+ else variables.set(key, String(value));
116
+ return 0 === variables.size ? "" : `?${variables.toString()}`;
117
+ }
118
+ }
@@ -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";