@nestia/fetcher 2.5.0-dev.20240131 → 2.5.0

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/src/HttpError.ts CHANGED
@@ -1,85 +1,85 @@
1
- /**
2
- * HTTP Error.
3
- *
4
- * `HttpError` is a type of error class who've been thrown by the remote HTTP server.
5
- *
6
- * @author Jeongho Nam - https://github.com/samchon
7
- */
8
- export class HttpError extends Error {
9
- /**
10
- * @internal
11
- */
12
- private body_: any = NOT_YET;
13
-
14
- /**
15
- * Initializer Constructor.
16
- *
17
- * @param method Method of the HTTP request.
18
- * @param path Path of the HTTP request.
19
- * @param status Status code from the remote HTTP server.
20
- * @param message Error message from the remote HTTP server.
21
- */
22
- public constructor(
23
- public readonly method:
24
- | "GET"
25
- | "DELETE"
26
- | "POST"
27
- | "PUT"
28
- | "PATCH"
29
- | "HEAD",
30
- public readonly path: string,
31
- public readonly status: number,
32
- public readonly headers: Record<string, string | string[]>,
33
- message: string,
34
- ) {
35
- super(message);
36
-
37
- // INHERITANCE POLYFILL
38
- const proto: HttpError = new.target.prototype;
39
- if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
40
- else (this as any).__proto__ = proto;
41
- }
42
-
43
- /**
44
- * `HttpError` to JSON.
45
- *
46
- * When you call `JSON.stringify()` function on current `HttpError` instance,
47
- * this `HttpError.toJSON()` method would be automatically called.
48
- *
49
- * Also, if response body from the remote HTTP server forms a JSON object,
50
- * this `HttpError.toJSON()` method would be useful because it returns the
51
- * parsed JSON object about the {@link message} property.
52
- *
53
- * @template T Expected type of the response body.
54
- * @returns JSON object of the `HttpError`.
55
- */
56
- public toJSON<T>(): HttpError.IProps<T> {
57
- if (this.body_ === NOT_YET)
58
- try {
59
- this.body_ = JSON.parse(this.message);
60
- } catch {
61
- this.body_ = this.message;
62
- }
63
- return {
64
- method: this.method,
65
- path: this.path,
66
- status: this.status,
67
- headers: this.headers,
68
- message: this.body_,
69
- };
70
- }
71
- }
72
- export namespace HttpError {
73
- /**
74
- * Returned type of {@link HttpError.toJSON} method.
75
- */
76
- export interface IProps<T> {
77
- method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
78
- path: string;
79
- status: number;
80
- headers: Record<string, string | string[]>;
81
- message: T;
82
- }
83
- }
84
-
85
- const NOT_YET = {} as any;
1
+ /**
2
+ * HTTP Error.
3
+ *
4
+ * `HttpError` is a type of error class who've been thrown by the remote HTTP server.
5
+ *
6
+ * @author Jeongho Nam - https://github.com/samchon
7
+ */
8
+ export class HttpError extends Error {
9
+ /**
10
+ * @internal
11
+ */
12
+ private body_: any = NOT_YET;
13
+
14
+ /**
15
+ * Initializer Constructor.
16
+ *
17
+ * @param method Method of the HTTP request.
18
+ * @param path Path of the HTTP request.
19
+ * @param status Status code from the remote HTTP server.
20
+ * @param message Error message from the remote HTTP server.
21
+ */
22
+ public constructor(
23
+ public readonly method:
24
+ | "GET"
25
+ | "DELETE"
26
+ | "POST"
27
+ | "PUT"
28
+ | "PATCH"
29
+ | "HEAD",
30
+ public readonly path: string,
31
+ public readonly status: number,
32
+ public readonly headers: Record<string, string | string[]>,
33
+ message: string,
34
+ ) {
35
+ super(message);
36
+
37
+ // INHERITANCE POLYFILL
38
+ const proto: HttpError = new.target.prototype;
39
+ if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
40
+ else (this as any).__proto__ = proto;
41
+ }
42
+
43
+ /**
44
+ * `HttpError` to JSON.
45
+ *
46
+ * When you call `JSON.stringify()` function on current `HttpError` instance,
47
+ * this `HttpError.toJSON()` method would be automatically called.
48
+ *
49
+ * Also, if response body from the remote HTTP server forms a JSON object,
50
+ * this `HttpError.toJSON()` method would be useful because it returns the
51
+ * parsed JSON object about the {@link message} property.
52
+ *
53
+ * @template T Expected type of the response body.
54
+ * @returns JSON object of the `HttpError`.
55
+ */
56
+ public toJSON<T>(): HttpError.IProps<T> {
57
+ if (this.body_ === NOT_YET)
58
+ try {
59
+ this.body_ = JSON.parse(this.message);
60
+ } catch {
61
+ this.body_ = this.message;
62
+ }
63
+ return {
64
+ method: this.method,
65
+ path: this.path,
66
+ status: this.status,
67
+ headers: this.headers,
68
+ message: this.body_,
69
+ };
70
+ }
71
+ }
72
+ export namespace HttpError {
73
+ /**
74
+ * Returned type of {@link HttpError.toJSON} method.
75
+ */
76
+ export interface IProps<T> {
77
+ method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
78
+ path: string;
79
+ status: number;
80
+ headers: Record<string, string | string[]>;
81
+ message: T;
82
+ }
83
+ }
84
+
85
+ const NOT_YET = {} as any;
@@ -1,50 +1,50 @@
1
- import { IConnection } from "./IConnection";
2
-
3
- /**
4
- * Encryption password.
5
- *
6
- * `IEncryptionPassword` is a type of interface who represents encryption password used by
7
- * the {@link Fetcher} with AES-128/256 algorithm. If your encryption password is not fixed
8
- * but changes according to the input content, you can utilize the
9
- * {@link IEncryptionPassword.Closure} function type.
10
- *
11
- * @author Jeongho Nam - https://github.com/samchon
12
- */
13
- export interface IEncryptionPassword {
14
- /**
15
- * Secret key.
16
- */
17
- key: string;
18
-
19
- /**
20
- * Initialization Vector.
21
- */
22
- iv: string;
23
- }
24
- export namespace IEncryptionPassword {
25
- /**
26
- * Type of a closure function returning the {@link IEncryptionPassword} object.
27
- *
28
- * `IEncryptionPassword.Closure` is a type of closure function who are returning the
29
- * {@link IEncryptionPassword} object. It would be used when your encryption password
30
- * be changed according to the input content.
31
- */
32
- export interface Closure {
33
- /**
34
- * Encryption password getter.
35
- *
36
- * @param props Properties for predication
37
- * @returns Encryption password
38
- */
39
- (props: IProps): IEncryptionPassword;
40
- }
41
-
42
- /**
43
- * Properties for the closure.
44
- */
45
- export interface IProps {
46
- headers: Record<string, IConnection.HeaderValue | undefined>;
47
- body: string;
48
- direction: "encode" | "decode";
49
- }
50
- }
1
+ import { IConnection } from "./IConnection";
2
+
3
+ /**
4
+ * Encryption password.
5
+ *
6
+ * `IEncryptionPassword` is a type of interface who represents encryption password used by
7
+ * the {@link Fetcher} with AES-128/256 algorithm. If your encryption password is not fixed
8
+ * but changes according to the input content, you can utilize the
9
+ * {@link IEncryptionPassword.Closure} function type.
10
+ *
11
+ * @author Jeongho Nam - https://github.com/samchon
12
+ */
13
+ export interface IEncryptionPassword {
14
+ /**
15
+ * Secret key.
16
+ */
17
+ key: string;
18
+
19
+ /**
20
+ * Initialization Vector.
21
+ */
22
+ iv: string;
23
+ }
24
+ export namespace IEncryptionPassword {
25
+ /**
26
+ * Type of a closure function returning the {@link IEncryptionPassword} object.
27
+ *
28
+ * `IEncryptionPassword.Closure` is a type of closure function who are returning the
29
+ * {@link IEncryptionPassword} object. It would be used when your encryption password
30
+ * be changed according to the input content.
31
+ */
32
+ export interface Closure {
33
+ /**
34
+ * Encryption password getter.
35
+ *
36
+ * @param props Properties for predication
37
+ * @returns Encryption password
38
+ */
39
+ (props: IProps): IEncryptionPassword;
40
+ }
41
+
42
+ /**
43
+ * Properties for the closure.
44
+ */
45
+ export interface IProps {
46
+ headers: Record<string, IConnection.HeaderValue | undefined>;
47
+ body: string;
48
+ direction: "encode" | "decode";
49
+ }
50
+ }
@@ -1,38 +1,38 @@
1
- export interface IRandomGenerator {
2
- boolean(): boolean;
3
- integer(minimum?: number, maximum?: number): number;
4
- number(minimum?: number, maximum?: number): number;
5
- bigint(minimum?: bigint, maximum?: bigint): bigint;
6
- string(length?: number): string;
7
- array<T>(closure: (index: number) => T, count?: number): T[];
8
- length(): number;
9
-
10
- uuid(): string;
11
- email(): string;
12
- url(): string;
13
- ipv4(): string;
14
- ipv6(): string;
15
- pattern(regex: RegExp): string;
16
- date(minimum?: number, maximum?: number): string;
17
- datetime(minimum?: number, maximum?: number): string;
18
-
19
- customs?: IRandomGenerator.CustomMap;
20
- }
21
- export namespace IRandomGenerator {
22
- export type CustomMap = {
23
- [Type in keyof Customizable]?: (
24
- tags: ICommentTag[],
25
- ) => Customizable[Type] | undefined;
26
- };
27
-
28
- export type Customizable = {
29
- number: number;
30
- string: string;
31
- bigint: bigint;
32
- };
33
-
34
- export interface ICommentTag {
35
- name: string;
36
- value?: string;
37
- }
38
- }
1
+ export interface IRandomGenerator {
2
+ boolean(): boolean;
3
+ integer(minimum?: number, maximum?: number): number;
4
+ number(minimum?: number, maximum?: number): number;
5
+ bigint(minimum?: bigint, maximum?: bigint): bigint;
6
+ string(length?: number): string;
7
+ array<T>(closure: (index: number) => T, count?: number): T[];
8
+ length(): number;
9
+
10
+ uuid(): string;
11
+ email(): string;
12
+ url(): string;
13
+ ipv4(): string;
14
+ ipv6(): string;
15
+ pattern(regex: RegExp): string;
16
+ date(minimum?: number, maximum?: number): string;
17
+ datetime(minimum?: number, maximum?: number): string;
18
+
19
+ customs?: IRandomGenerator.CustomMap;
20
+ }
21
+ export namespace IRandomGenerator {
22
+ export type CustomMap = {
23
+ [Type in keyof Customizable]?: (
24
+ tags: ICommentTag[],
25
+ ) => Customizable[Type] | undefined;
26
+ };
27
+
28
+ export type Customizable = {
29
+ number: number;
30
+ string: string;
31
+ bigint: bigint;
32
+ };
33
+
34
+ export interface ICommentTag {
35
+ name: string;
36
+ value?: string;
37
+ }
38
+ }
@@ -0,0 +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,106 +1,106 @@
1
- import { IConnection } from "./IConnection";
2
- import { IPropagation } from "./IPropagation";
3
- import { Primitive } from "./Primitive";
4
- import { FetcherBase } from "./internal/FetcherBase";
5
- import { IFetchRoute } from "./internal/IFetchRoute";
6
-
7
- /**
8
- * Utility class for `fetch` functions used in `@nestia/sdk`.
9
- *
10
- * `PlainFetcher` is a utility class designed for SDK functions generated by
11
- * [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
12
- * HTTP sever API. In other words, this is a collection of dedicated `fetch()`
13
- * functions for `@nestia/sdk`.
14
- *
15
- * For reference, `PlainFetcher` class does not encrypt or decrypt the body data
16
- * at all. It just delivers plain data without any post processing. If you've
17
- * defined a controller method through `@EncryptedRoute` or `@EncryptedBody`
18
- * decorator, then {@liink EncryptedFetcher} class would be used instead.
19
- *
20
- * @author Jeongho Nam - https://github.com/samchon
21
- */
22
- export namespace PlainFetcher {
23
- /**
24
- * Fetch function only for `HEAD` method.
25
- *
26
- * @param connection Connection information for the remote HTTP server
27
- * @param route Route information about the target API
28
- * @return Nothing because of `HEAD` method
29
- */
30
- export function fetch(
31
- connection: IConnection,
32
- route: IFetchRoute<"HEAD">,
33
- ): Promise<void>;
34
-
35
- /**
36
- * Fetch function only for `GET` method.
37
- *
38
- * @param connection Connection information for the remote HTTP server
39
- * @param route Route information about the target API
40
- * @return Response body data from the remote API
41
- */
42
- export function fetch<Output>(
43
- connection: IConnection,
44
- route: IFetchRoute<"GET">,
45
- ): Promise<Primitive<Output>>;
46
-
47
- /**
48
- * Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
49
- *
50
- * @param connection Connection information for the remote HTTP server
51
- * @param route Route information about the target API
52
- * @return Response body data from the remote API
53
- */
54
- export function fetch<Input, Output>(
55
- connection: IConnection,
56
- route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
57
- input?: Input,
58
- stringify?: (input: Input) => string,
59
- ): Promise<Primitive<Output>>;
60
-
61
- export async function fetch<Input, Output>(
62
- connection: IConnection,
63
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
64
- input?: Input,
65
- stringify?: (input: Input) => string,
66
- ): Promise<Primitive<Output>> {
67
- if (route.request?.encrypted === true || route.response?.encrypted === true)
68
- throw new Error(
69
- "Error on PlainFetcher.fetch(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
70
- );
71
- return FetcherBase.fetch({
72
- className: "PlainFetcher",
73
- encode: (input) => input,
74
- decode: (input) => input,
75
- })(connection, route, input, stringify);
76
- }
77
-
78
- export function propagate<Output extends IPropagation<any, any>>(
79
- connection: IConnection,
80
- route: IFetchRoute<"GET" | "HEAD">,
81
- ): Promise<Output>;
82
-
83
- export function propagate<Input, Output extends IPropagation<any, any>>(
84
- connection: IConnection,
85
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
86
- input?: Input,
87
- stringify?: (input: Input) => string,
88
- ): Promise<Output>;
89
-
90
- export async function propagate<Input, Output extends IPropagation<any, any>>(
91
- connection: IConnection,
92
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
93
- input?: Input,
94
- stringify?: (input: Input) => string,
95
- ): Promise<Output> {
96
- if (route.request?.encrypted === true || route.response?.encrypted === true)
97
- throw new Error(
98
- "Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
99
- );
100
- return FetcherBase.propagate({
101
- className: "PlainFetcher",
102
- encode: (input) => input,
103
- decode: (input) => input,
104
- })(connection, route, input, stringify) as Promise<Output>;
105
- }
106
- }
1
+ import { IConnection } from "./IConnection";
2
+ import { IPropagation } from "./IPropagation";
3
+ import { Primitive } from "./Primitive";
4
+ import { FetcherBase } from "./internal/FetcherBase";
5
+ import { IFetchRoute } from "./internal/IFetchRoute";
6
+
7
+ /**
8
+ * Utility class for `fetch` functions used in `@nestia/sdk`.
9
+ *
10
+ * `PlainFetcher` is a utility class designed for SDK functions generated by
11
+ * [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
12
+ * HTTP sever API. In other words, this is a collection of dedicated `fetch()`
13
+ * functions for `@nestia/sdk`.
14
+ *
15
+ * For reference, `PlainFetcher` class does not encrypt or decrypt the body data
16
+ * at all. It just delivers plain data without any post processing. If you've
17
+ * defined a controller method through `@EncryptedRoute` or `@EncryptedBody`
18
+ * decorator, then {@liink EncryptedFetcher} class would be used instead.
19
+ *
20
+ * @author Jeongho Nam - https://github.com/samchon
21
+ */
22
+ export namespace PlainFetcher {
23
+ /**
24
+ * Fetch function only for `HEAD` method.
25
+ *
26
+ * @param connection Connection information for the remote HTTP server
27
+ * @param route Route information about the target API
28
+ * @return Nothing because of `HEAD` method
29
+ */
30
+ export function fetch(
31
+ connection: IConnection,
32
+ route: IFetchRoute<"HEAD">,
33
+ ): Promise<void>;
34
+
35
+ /**
36
+ * Fetch function only for `GET` method.
37
+ *
38
+ * @param connection Connection information for the remote HTTP server
39
+ * @param route Route information about the target API
40
+ * @return Response body data from the remote API
41
+ */
42
+ export function fetch<Output>(
43
+ connection: IConnection,
44
+ route: IFetchRoute<"GET">,
45
+ ): Promise<Primitive<Output>>;
46
+
47
+ /**
48
+ * Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
49
+ *
50
+ * @param connection Connection information for the remote HTTP server
51
+ * @param route Route information about the target API
52
+ * @return Response body data from the remote API
53
+ */
54
+ export function fetch<Input, Output>(
55
+ connection: IConnection,
56
+ route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
57
+ input?: Input,
58
+ stringify?: (input: Input) => string,
59
+ ): Promise<Primitive<Output>>;
60
+
61
+ export async function fetch<Input, Output>(
62
+ connection: IConnection,
63
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
64
+ input?: Input,
65
+ stringify?: (input: Input) => string,
66
+ ): Promise<Primitive<Output>> {
67
+ if (route.request?.encrypted === true || route.response?.encrypted === true)
68
+ throw new Error(
69
+ "Error on PlainFetcher.fetch(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
70
+ );
71
+ return FetcherBase.fetch({
72
+ className: "PlainFetcher",
73
+ encode: (input) => input,
74
+ decode: (input) => input,
75
+ })(connection, route, input, stringify);
76
+ }
77
+
78
+ export function propagate<Output extends IPropagation<any, any>>(
79
+ connection: IConnection,
80
+ route: IFetchRoute<"GET" | "HEAD">,
81
+ ): Promise<Output>;
82
+
83
+ export function propagate<Input, Output extends IPropagation<any, any>>(
84
+ connection: IConnection,
85
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
86
+ input?: Input,
87
+ stringify?: (input: Input) => string,
88
+ ): Promise<Output>;
89
+
90
+ export async function propagate<Input, Output extends IPropagation<any, any>>(
91
+ connection: IConnection,
92
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
93
+ input?: Input,
94
+ stringify?: (input: Input) => string,
95
+ ): Promise<Output> {
96
+ if (route.request?.encrypted === true || route.response?.encrypted === true)
97
+ throw new Error(
98
+ "Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
99
+ );
100
+ return FetcherBase.propagate({
101
+ className: "PlainFetcher",
102
+ encode: (input) => input,
103
+ decode: (input) => input,
104
+ })(connection, route, input, stringify) as Promise<Output>;
105
+ }
106
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- export * from "./IConnection";
2
- export * from "./IEncryptionPassword";
3
- export * from "./IPropagation";
4
- export * from "./IRandomGenerator";
5
- export * from "./HttpError";
6
- export * from "./Primitive";
7
- export * from "./Resolved";
1
+ export * from "./IConnection";
2
+ export * from "./IEncryptionPassword";
3
+ export * from "./IPropagation";
4
+ export * from "./IRandomGenerator";
5
+ export * from "./HttpError";
6
+ export * from "./Primitive";
7
+ export * from "./Resolved";