@nestia/fetcher 2.5.14 → 2.5.15
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/package.json +2 -2
- package/src/EncryptedFetcher.ts +174 -174
- package/src/PlainFetcher.ts +105 -105
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestia/fetcher",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.15",
|
|
4
4
|
"description": "Fetcher library of Nestia SDK",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
|
31
31
|
"@typescript-eslint/parser": "^5.46.1",
|
|
32
32
|
"rimraf": "^3.0.2",
|
|
33
|
-
"typescript": "^5.
|
|
33
|
+
"typescript": "^5.4.2"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"typescript": ">= 4.8.0"
|
package/src/EncryptedFetcher.ts
CHANGED
|
@@ -1,174 +1,174 @@
|
|
|
1
|
-
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
|
-
import { IConnection } from "./IConnection";
|
|
3
|
-
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
|
-
import { IPropagation } from "./IPropagation";
|
|
5
|
-
import { FetcherBase } from "./internal/FetcherBase";
|
|
6
|
-
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
10
|
-
*
|
|
11
|
-
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
|
|
12
|
-
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
13
|
-
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a collection of
|
|
14
|
-
* dedicated `fetch()` functions for `@nestia/sdk` with encryption.
|
|
15
|
-
*
|
|
16
|
-
* For reference, `EncryptedFetcher` class being used only when target controller
|
|
17
|
-
* method is encrypting body data by `@EncryptedRoute` or `@EncryptedBody` decorators.
|
|
18
|
-
* If those decorators are not used, {@link PlainFetcher} class would be used instead.
|
|
19
|
-
*
|
|
20
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
21
|
-
*/
|
|
22
|
-
export namespace EncryptedFetcher {
|
|
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<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<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<Output> {
|
|
67
|
-
if (
|
|
68
|
-
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
69
|
-
connection.encryption === undefined
|
|
70
|
-
)
|
|
71
|
-
throw new Error(
|
|
72
|
-
"Error on EncryptedFetcher.fetch(): the encryption password has not been configured.",
|
|
73
|
-
);
|
|
74
|
-
const closure =
|
|
75
|
-
typeof connection.encryption === "function"
|
|
76
|
-
? (direction: "encode" | "decode") =>
|
|
77
|
-
(
|
|
78
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
79
|
-
body: string,
|
|
80
|
-
) =>
|
|
81
|
-
(connection.encryption as IEncryptionPassword.Closure)({
|
|
82
|
-
headers,
|
|
83
|
-
body,
|
|
84
|
-
direction,
|
|
85
|
-
})
|
|
86
|
-
: () => () => connection.encryption as IEncryptionPassword;
|
|
87
|
-
|
|
88
|
-
return FetcherBase.fetch({
|
|
89
|
-
className: "EncryptedFetcher",
|
|
90
|
-
encode:
|
|
91
|
-
route.request?.encrypted === true
|
|
92
|
-
? (input, headers) => {
|
|
93
|
-
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
94
|
-
return AesPkcs5.encrypt(
|
|
95
|
-
(stringify ?? JSON.stringify)(input),
|
|
96
|
-
p.key,
|
|
97
|
-
p.iv,
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
: (input) => input,
|
|
101
|
-
decode:
|
|
102
|
-
route.response?.encrypted === true
|
|
103
|
-
? (input, headers) => {
|
|
104
|
-
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
105
|
-
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
106
|
-
return s.length ? JSON.parse(s) : s;
|
|
107
|
-
}
|
|
108
|
-
: (input) => input,
|
|
109
|
-
})(connection, route, input, stringify);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function propagate<Output extends IPropagation<any, any>>(
|
|
113
|
-
connection: IConnection,
|
|
114
|
-
route: IFetchRoute<"GET" | "HEAD">,
|
|
115
|
-
): Promise<Output>;
|
|
116
|
-
|
|
117
|
-
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
118
|
-
connection: IConnection,
|
|
119
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
120
|
-
input?: Input,
|
|
121
|
-
stringify?: (input: Input) => string,
|
|
122
|
-
): Promise<Output>;
|
|
123
|
-
|
|
124
|
-
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
125
|
-
connection: IConnection,
|
|
126
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
127
|
-
input?: Input,
|
|
128
|
-
stringify?: (input: Input) => string,
|
|
129
|
-
): Promise<Output> {
|
|
130
|
-
if (
|
|
131
|
-
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
132
|
-
connection.encryption === undefined
|
|
133
|
-
)
|
|
134
|
-
throw new Error(
|
|
135
|
-
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
136
|
-
);
|
|
137
|
-
const closure =
|
|
138
|
-
typeof connection.encryption === "function"
|
|
139
|
-
? (direction: "encode" | "decode") =>
|
|
140
|
-
(
|
|
141
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
142
|
-
body: string,
|
|
143
|
-
) =>
|
|
144
|
-
(connection.encryption as IEncryptionPassword.Closure)({
|
|
145
|
-
headers,
|
|
146
|
-
body,
|
|
147
|
-
direction,
|
|
148
|
-
})
|
|
149
|
-
: () => () => connection.encryption as IEncryptionPassword;
|
|
150
|
-
|
|
151
|
-
return FetcherBase.propagate({
|
|
152
|
-
className: "EncryptedFetcher",
|
|
153
|
-
encode:
|
|
154
|
-
route.request?.encrypted === true
|
|
155
|
-
? (input, headers) => {
|
|
156
|
-
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
157
|
-
return AesPkcs5.encrypt(
|
|
158
|
-
(stringify ?? JSON.stringify)(input),
|
|
159
|
-
p.key,
|
|
160
|
-
p.iv,
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
: (input) => input,
|
|
164
|
-
decode:
|
|
165
|
-
route.response?.encrypted === true
|
|
166
|
-
? (input, headers) => {
|
|
167
|
-
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
168
|
-
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
169
|
-
return s.length ? JSON.parse(s) : s;
|
|
170
|
-
}
|
|
171
|
-
: (input) => input,
|
|
172
|
-
})(connection, route, input, stringify) as Promise<Output>;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
1
|
+
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
|
+
import { IConnection } from "./IConnection";
|
|
3
|
+
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
|
+
import { IPropagation } from "./IPropagation";
|
|
5
|
+
import { FetcherBase } from "./internal/FetcherBase";
|
|
6
|
+
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
10
|
+
*
|
|
11
|
+
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
|
|
12
|
+
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
13
|
+
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a collection of
|
|
14
|
+
* dedicated `fetch()` functions for `@nestia/sdk` with encryption.
|
|
15
|
+
*
|
|
16
|
+
* For reference, `EncryptedFetcher` class being used only when target controller
|
|
17
|
+
* method is encrypting body data by `@EncryptedRoute` or `@EncryptedBody` decorators.
|
|
18
|
+
* If those decorators are not used, {@link PlainFetcher} class would be used instead.
|
|
19
|
+
*
|
|
20
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
21
|
+
*/
|
|
22
|
+
export namespace EncryptedFetcher {
|
|
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<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<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<Output> {
|
|
67
|
+
if (
|
|
68
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
69
|
+
connection.encryption === undefined
|
|
70
|
+
)
|
|
71
|
+
throw new Error(
|
|
72
|
+
"Error on EncryptedFetcher.fetch(): the encryption password has not been configured.",
|
|
73
|
+
);
|
|
74
|
+
const closure =
|
|
75
|
+
typeof connection.encryption === "function"
|
|
76
|
+
? (direction: "encode" | "decode") =>
|
|
77
|
+
(
|
|
78
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
79
|
+
body: string,
|
|
80
|
+
) =>
|
|
81
|
+
(connection.encryption as IEncryptionPassword.Closure)({
|
|
82
|
+
headers,
|
|
83
|
+
body,
|
|
84
|
+
direction,
|
|
85
|
+
})
|
|
86
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
87
|
+
|
|
88
|
+
return FetcherBase.fetch({
|
|
89
|
+
className: "EncryptedFetcher",
|
|
90
|
+
encode:
|
|
91
|
+
route.request?.encrypted === true
|
|
92
|
+
? (input, headers) => {
|
|
93
|
+
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
94
|
+
return AesPkcs5.encrypt(
|
|
95
|
+
(stringify ?? JSON.stringify)(input),
|
|
96
|
+
p.key,
|
|
97
|
+
p.iv,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
: (input) => input,
|
|
101
|
+
decode:
|
|
102
|
+
route.response?.encrypted === true
|
|
103
|
+
? (input, headers) => {
|
|
104
|
+
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
105
|
+
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
106
|
+
return s.length ? JSON.parse(s) : s;
|
|
107
|
+
}
|
|
108
|
+
: (input) => input,
|
|
109
|
+
})(connection, route, input, stringify);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function propagate<Output extends IPropagation<any, any>>(
|
|
113
|
+
connection: IConnection,
|
|
114
|
+
route: IFetchRoute<"GET" | "HEAD">,
|
|
115
|
+
): Promise<Output>;
|
|
116
|
+
|
|
117
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
118
|
+
connection: IConnection,
|
|
119
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
120
|
+
input?: Input,
|
|
121
|
+
stringify?: (input: Input) => string,
|
|
122
|
+
): Promise<Output>;
|
|
123
|
+
|
|
124
|
+
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
125
|
+
connection: IConnection,
|
|
126
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
127
|
+
input?: Input,
|
|
128
|
+
stringify?: (input: Input) => string,
|
|
129
|
+
): Promise<Output> {
|
|
130
|
+
if (
|
|
131
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
132
|
+
connection.encryption === undefined
|
|
133
|
+
)
|
|
134
|
+
throw new Error(
|
|
135
|
+
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
136
|
+
);
|
|
137
|
+
const closure =
|
|
138
|
+
typeof connection.encryption === "function"
|
|
139
|
+
? (direction: "encode" | "decode") =>
|
|
140
|
+
(
|
|
141
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
142
|
+
body: string,
|
|
143
|
+
) =>
|
|
144
|
+
(connection.encryption as IEncryptionPassword.Closure)({
|
|
145
|
+
headers,
|
|
146
|
+
body,
|
|
147
|
+
direction,
|
|
148
|
+
})
|
|
149
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
150
|
+
|
|
151
|
+
return FetcherBase.propagate({
|
|
152
|
+
className: "EncryptedFetcher",
|
|
153
|
+
encode:
|
|
154
|
+
route.request?.encrypted === true
|
|
155
|
+
? (input, headers) => {
|
|
156
|
+
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
157
|
+
return AesPkcs5.encrypt(
|
|
158
|
+
(stringify ?? JSON.stringify)(input),
|
|
159
|
+
p.key,
|
|
160
|
+
p.iv,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
: (input) => input,
|
|
164
|
+
decode:
|
|
165
|
+
route.response?.encrypted === true
|
|
166
|
+
? (input, headers) => {
|
|
167
|
+
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
168
|
+
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
169
|
+
return s.length ? JSON.parse(s) : s;
|
|
170
|
+
}
|
|
171
|
+
: (input) => input,
|
|
172
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
173
|
+
}
|
|
174
|
+
}
|
package/src/PlainFetcher.ts
CHANGED
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
import { IConnection } from "./IConnection";
|
|
2
|
-
import { IPropagation } from "./IPropagation";
|
|
3
|
-
import { FetcherBase } from "./internal/FetcherBase";
|
|
4
|
-
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
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
|
-
* @return 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
|
-
* @return 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
|
-
* @return 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.fetch({
|
|
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 { IPropagation } from "./IPropagation";
|
|
3
|
+
import { FetcherBase } from "./internal/FetcherBase";
|
|
4
|
+
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
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
|
+
* @return 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
|
+
* @return 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
|
+
* @return 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.fetch({
|
|
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
|
+
}
|