@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,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
+ }
@@ -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
+ }
@@ -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
+ }