@nestia/fetcher 6.0.6 → 7.0.0-dev.20250604

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,69 +1,69 @@
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
- /**
10
- * Method of the HTTP request.
11
- */
12
- method: Method;
13
-
14
- /**
15
- * Path of the HTTP request.
16
- */
17
- path: string;
18
-
19
- /**
20
- * Path template.
21
- *
22
- * Filled since 3.2.2 version.
23
- */
24
- template?: string;
25
-
26
- /**
27
- * Request body data info.
28
- */
29
- request: Method extends "DELETE" | "POST" | "PUT" | "PATCH"
30
- ? IFetchRoute.IBody | null
31
- : null;
32
-
33
- /**
34
- * Response body data info.
35
- */
36
- response: Method extends "HEAD" ? null : IFetchRoute.IBody;
37
-
38
- /**
39
- * When special status code being used.
40
- */
41
- status: number | null;
42
-
43
- /**
44
- * Parser of the query string.
45
- *
46
- * If content type of response body is `application/x-www-form-urlencoded`,
47
- * then this `parseQuery` function would be called.
48
- *
49
- * If you've forgotten to configuring this `parseQuery` property about the
50
- * `application/x-www-form-urlencoded` typed response body data, then
51
- * only the `URLSearchParams` typed instance would be returned instead.
52
- */
53
- parseQuery?(input: URLSearchParams): any;
54
- }
55
- export namespace IFetchRoute {
56
- /**
57
- * Metadata of body.
58
- *
59
- * Describes how content-type being used in body, and whether encrypted or not.
60
- */
61
- export interface IBody {
62
- type:
63
- | "application/json"
64
- | "application/x-www-form-urlencoded"
65
- | "multipart/form-data"
66
- | "text/plain";
67
- encrypted?: boolean;
68
- }
69
- }
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
+ /**
10
+ * Method of the HTTP request.
11
+ */
12
+ method: Method;
13
+
14
+ /**
15
+ * Path of the HTTP request.
16
+ */
17
+ path: string;
18
+
19
+ /**
20
+ * Path template.
21
+ *
22
+ * Filled since 3.2.2 version.
23
+ */
24
+ template?: string;
25
+
26
+ /**
27
+ * Request body data info.
28
+ */
29
+ request: Method extends "DELETE" | "POST" | "PUT" | "PATCH"
30
+ ? IFetchRoute.IBody | null
31
+ : null;
32
+
33
+ /**
34
+ * Response body data info.
35
+ */
36
+ response: Method extends "HEAD" ? null : IFetchRoute.IBody;
37
+
38
+ /**
39
+ * When special status code being used.
40
+ */
41
+ status: number | null;
42
+
43
+ /**
44
+ * Parser of the query string.
45
+ *
46
+ * If content type of response body is `application/x-www-form-urlencoded`,
47
+ * then this `parseQuery` function would be called.
48
+ *
49
+ * If you've forgotten to configuring this `parseQuery` property about the
50
+ * `application/x-www-form-urlencoded` typed response body data, then
51
+ * only the `URLSearchParams` typed instance would be returned instead.
52
+ */
53
+ parseQuery?(input: URLSearchParams): any;
54
+ }
55
+ export namespace IFetchRoute {
56
+ /**
57
+ * Metadata of body.
58
+ *
59
+ * Describes how content-type being used in body, and whether encrypted or not.
60
+ */
61
+ export interface IBody {
62
+ type:
63
+ | "application/json"
64
+ | "application/x-www-form-urlencoded"
65
+ | "multipart/form-data"
66
+ | "text/plain";
67
+ encrypted?: boolean;
68
+ }
69
+ }
@@ -1,100 +1,100 @@
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 by
28
- * `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be `true`.
29
- * In that case, SDK functions generated by `@nestia/sdk` no more returns response DTO
30
- * typed data directly, but returns this `IPropagation` typed object instead.
31
- *
32
- * @template StatusMap Map of status code and its body data type.
33
- * @template Success Default success status code.
34
- * @author Jeongho Nam - https://github.com/samchon
35
- */
36
- export type IPropagation<
37
- StatusMap extends {
38
- [P in IPropagation.Status]?: any;
39
- },
40
- Success extends number = 200 | 201,
41
- > =
42
- | {
43
- [P in keyof StatusMap]: IPropagation.IBranch<
44
- P extends Success ? true : false,
45
- P,
46
- StatusMap[P]
47
- >;
48
- }[keyof StatusMap]
49
- | IPropagation.IBranch<false, unknown, unknown>;
50
- export namespace IPropagation {
51
- /**
52
- * Type of configurable status codes.
53
- *
54
- * The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the range
55
- * of status codes. If `5XX` is specified, it means the status code is in the
56
- * range of `500` to `599`.
57
- */
58
- export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
59
-
60
- /**
61
- * Branch type of propagation.
62
- *
63
- * `IPropagation.IBranch` is a branch type composing `IPropagation` type,
64
- * which is gathering all possible status codes and their body data types
65
- * as a union type.
66
- */
67
- export interface IBranch<Success extends boolean, StatusValue, BodyData> {
68
- success: Success;
69
- status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
70
- ? StatusRange<StatusValue>
71
- : StatusValue extends number
72
- ? StatusValue
73
- : never;
74
- data: BodyData;
75
- headers: Record<string, string | string[]>;
76
- }
77
-
78
- /**
79
- * Range of status codes by the first digit.
80
- */
81
- export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> = T extends 0
82
- ? IntRange<200, 299>
83
- : T extends 3
84
- ? IntRange<300, 399>
85
- : T extends 4
86
- ? IntRange<400, 499>
87
- : IntRange<500, 599>;
88
-
89
- type IntRange<F extends number, T extends number> = Exclude<
90
- Enumerate<T>,
91
- Enumerate<F>
92
- >;
93
-
94
- type Enumerate<
95
- N extends number,
96
- Acc extends number[] = [],
97
- > = Acc["length"] extends N
98
- ? Acc[number]
99
- : Enumerate<N, [...Acc, Acc["length"]]>;
100
- }
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 by
28
+ * `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be `true`.
29
+ * In that case, SDK functions generated by `@nestia/sdk` no more returns response DTO
30
+ * typed data directly, but returns this `IPropagation` typed object instead.
31
+ *
32
+ * @template StatusMap Map of status code and its body data type.
33
+ * @template Success Default success status code.
34
+ * @author Jeongho Nam - https://github.com/samchon
35
+ */
36
+ export type IPropagation<
37
+ StatusMap extends {
38
+ [P in IPropagation.Status]?: any;
39
+ },
40
+ Success extends number = 200 | 201,
41
+ > =
42
+ | {
43
+ [P in keyof StatusMap]: IPropagation.IBranch<
44
+ P extends Success ? true : false,
45
+ P,
46
+ StatusMap[P]
47
+ >;
48
+ }[keyof StatusMap]
49
+ | IPropagation.IBranch<false, unknown, unknown>;
50
+ export namespace IPropagation {
51
+ /**
52
+ * Type of configurable status codes.
53
+ *
54
+ * The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the range
55
+ * of status codes. If `5XX` is specified, it means the status code is in the
56
+ * range of `500` to `599`.
57
+ */
58
+ export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
59
+
60
+ /**
61
+ * Branch type of propagation.
62
+ *
63
+ * `IPropagation.IBranch` is a branch type composing `IPropagation` type,
64
+ * which is gathering all possible status codes and their body data types
65
+ * as a union type.
66
+ */
67
+ export interface IBranch<Success extends boolean, StatusValue, BodyData> {
68
+ success: Success;
69
+ status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
70
+ ? StatusRange<StatusValue>
71
+ : StatusValue extends number
72
+ ? StatusValue
73
+ : never;
74
+ data: BodyData;
75
+ headers: Record<string, string | string[]>;
76
+ }
77
+
78
+ /**
79
+ * Range of status codes by the first digit.
80
+ */
81
+ export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> = T extends 0
82
+ ? IntRange<200, 299>
83
+ : T extends 3
84
+ ? IntRange<300, 399>
85
+ : T extends 4
86
+ ? IntRange<400, 499>
87
+ : IntRange<500, 599>;
88
+
89
+ type IntRange<F extends number, T extends number> = Exclude<
90
+ Enumerate<T>,
91
+ Enumerate<F>
92
+ >;
93
+
94
+ type Enumerate<
95
+ N extends number,
96
+ Acc extends number[] = [],
97
+ > = Acc["length"] extends N
98
+ ? Acc[number]
99
+ : Enumerate<N, [...Acc, Acc["length"]]>;
100
+ }
@@ -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
+ }