@nestia/fetcher 7.0.0-dev.20250608 → 7.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,245 +1,245 @@
1
- import { HttpError } from "../HttpError";
2
- import { IConnection } from "../IConnection";
3
- import { IFetchEvent } from "../IFetchEvent";
4
- import { IFetchRoute } from "../IFetchRoute";
5
- import { IPropagation } from "../IPropagation";
6
-
7
- /**
8
- * @internal
9
- */
10
- export namespace FetcherBase {
11
- export interface IProps {
12
- className: string;
13
- encode: (
14
- input: any,
15
- headers: Record<string, IConnection.HeaderValue | undefined>,
16
- ) => string;
17
- decode: (
18
- input: string,
19
- headers: Record<string, IConnection.HeaderValue | undefined>,
20
- ) => any;
21
- }
22
-
23
- export const request =
24
- (props: IProps) =>
25
- async <Input, Output>(
26
- connection: IConnection,
27
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
28
- input?: Input,
29
- stringify?: (input: Input) => string,
30
- ): Promise<Output> => {
31
- const result = await _Propagate("fetch")(props)(
32
- connection,
33
- route,
34
- input,
35
- stringify,
36
- );
37
- if ((result as any).success === false)
38
- throw new HttpError(
39
- route.method,
40
- route.path,
41
- result.status as any as number,
42
- result.headers,
43
- result.data as string,
44
- );
45
- return result.data as Output;
46
- };
47
-
48
- export const propagate =
49
- (props: IProps) =>
50
- async <Input>(
51
- connection: IConnection,
52
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
53
- input?: Input,
54
- stringify?: (input: Input) => string,
55
- ): Promise<IPropagation<any, any>> =>
56
- _Propagate("propagate")(props)(connection, route, input, stringify);
57
-
58
- /**
59
- * @internal
60
- */
61
- const _Propagate =
62
- (method: string) =>
63
- (props: IProps) =>
64
- async <Input>(
65
- connection: IConnection,
66
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
67
- input?: Input,
68
- stringify?: (input: Input) => string,
69
- ): Promise<IPropagation<any, any>> => {
70
- //----
71
- // REQUEST MESSAGE
72
- //----
73
- // METHOD & HEADERS
74
- const headers: Record<string, IConnection.HeaderValue | undefined> = {
75
- ...(connection.headers ?? {}),
76
- };
77
- if (input !== undefined) {
78
- if (route.request?.type === undefined)
79
- throw new Error(
80
- `Error on ${props.className}.fetch(): no content-type being configured.`,
81
- );
82
- else if (route.request.type !== "multipart/form-data")
83
- headers["Content-Type"] = route.request.type;
84
- } else if (input === undefined && headers["Content-Type"] !== undefined)
85
- delete headers["Content-Type"];
86
-
87
- // INIT REQUEST DATA
88
- const init: RequestInit = {
89
- ...(connection.options ?? {}),
90
- method: route.method,
91
- headers: (() => {
92
- const output: [string, string][] = [];
93
- for (const [key, value] of Object.entries(headers))
94
- if (value === undefined) continue;
95
- else if (Array.isArray(value))
96
- for (const v of value) output.push([key, String(v)]);
97
- else output.push([key, String(value)]);
98
- return output;
99
- })(),
100
- };
101
-
102
- // CONSTRUCT BODY DATA
103
- if (input !== undefined)
104
- init.body = props.encode(
105
- // BODY TRANSFORM
106
- route.request?.type === "application/x-www-form-urlencoded"
107
- ? request_query_body(input)
108
- : route.request?.type === "multipart/form-data"
109
- ? request_form_data_body(input as any)
110
- : route.request?.type !== "text/plain"
111
- ? (stringify ?? JSON.stringify)(input)
112
- : input,
113
- headers,
114
- );
115
-
116
- //----
117
- // RESPONSE MESSAGE
118
- //----
119
- // URL SPECIFICATION
120
- const path: string =
121
- connection.host[connection.host.length - 1] !== "/" &&
122
- route.path[0] !== "/"
123
- ? `/${route.path}`
124
- : route.path;
125
- const url: URL = new URL(`${connection.host}${path}`);
126
-
127
- // DO FETCH
128
- const event: IFetchEvent = {
129
- route,
130
- path,
131
- status: null,
132
- input,
133
- output: undefined,
134
- started_at: new Date(),
135
- respond_at: null,
136
- completed_at: null!,
137
- };
138
- try {
139
- // TRY FETCH
140
- const response: Response = await (connection.fetch ?? fetch)(
141
- url.href,
142
- init,
143
- );
144
- event.respond_at = new Date();
145
- event.status = response.status;
146
-
147
- // CONSTRUCT RESULT DATA
148
- const result: IPropagation<any, any> = {
149
- success:
150
- response.status === 200 ||
151
- response.status === 201 ||
152
- response.status === route.status,
153
- status: response.status,
154
- headers: response_headers_to_object(response.headers),
155
- data: undefined!,
156
- } as any;
157
- if ((result as any).success === false) {
158
- // WHEN FAILED
159
- result.data = await response.text();
160
- const type = response.headers.get("content-type");
161
- if (
162
- method !== "fetch" &&
163
- type &&
164
- type.indexOf("application/json") !== -1
165
- )
166
- try {
167
- result.data = JSON.parse(result.data);
168
- } catch {}
169
- } else {
170
- // WHEN SUCCESS
171
- if (route.method === "HEAD") result.data = undefined!;
172
- else if (route.response?.type === "application/json") {
173
- const text: string = await response.text();
174
- result.data = text.length ? JSON.parse(text) : undefined;
175
- } else if (
176
- route.response?.type === "application/x-www-form-urlencoded"
177
- ) {
178
- const query: URLSearchParams = new URLSearchParams(
179
- await response.text(),
180
- );
181
- result.data = route.parseQuery ? route.parseQuery(query) : query;
182
- } else
183
- result.data = props.decode(await response.text(), result.headers);
184
- }
185
- event.output = result.data;
186
- return result;
187
- } catch (exp) {
188
- throw exp;
189
- } finally {
190
- event.completed_at = new Date();
191
- if (connection.logger)
192
- try {
193
- await connection.logger(event);
194
- } catch {}
195
- }
196
- };
197
- }
198
-
199
- /**
200
- * @internal
201
- */
202
- const request_query_body = (input: any): URLSearchParams => {
203
- const q: URLSearchParams = new URLSearchParams();
204
- for (const [key, value] of Object.entries(input))
205
- if (value === undefined) continue;
206
- else if (Array.isArray(value))
207
- value.forEach((elem) => q.append(key, String(elem)));
208
- else q.set(key, String(value));
209
- return q;
210
- };
211
-
212
- /**
213
- * @internal
214
- */
215
- const request_form_data_body = (input: Record<string, any>): FormData => {
216
- const encoded: FormData = new FormData();
217
- const append = (key: string) => (value: any) => {
218
- if (value === undefined) return;
219
- else if (typeof File === "function" && value instanceof File)
220
- encoded.append(key, value, value.name);
221
- else encoded.append(key, value);
222
- };
223
- for (const [key, value] of Object.entries(input))
224
- if (Array.isArray(value)) value.map(append(key));
225
- else append(key)(value);
226
- return encoded;
227
- };
228
-
229
- /**
230
- * @internal
231
- */
232
- const response_headers_to_object = (
233
- headers: Headers,
234
- ): Record<string, string | string[]> => {
235
- const output: Record<string, string | string[]> = {};
236
- headers.forEach((value, key) => {
237
- if (key === "set-cookie") {
238
- output[key] ??= [];
239
- (output[key] as string[]).push(
240
- ...value.split(";").map((str) => str.trim()),
241
- );
242
- } else output[key] = value;
243
- });
244
- return output;
245
- };
1
+ import { HttpError } from "../HttpError";
2
+ import { IConnection } from "../IConnection";
3
+ import { IFetchEvent } from "../IFetchEvent";
4
+ import { IFetchRoute } from "../IFetchRoute";
5
+ import { IPropagation } from "../IPropagation";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export namespace FetcherBase {
11
+ export interface IProps {
12
+ className: string;
13
+ encode: (
14
+ input: any,
15
+ headers: Record<string, IConnection.HeaderValue | undefined>,
16
+ ) => string;
17
+ decode: (
18
+ input: string,
19
+ headers: Record<string, IConnection.HeaderValue | undefined>,
20
+ ) => any;
21
+ }
22
+
23
+ export const request =
24
+ (props: IProps) =>
25
+ async <Input, Output>(
26
+ connection: IConnection,
27
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
28
+ input?: Input,
29
+ stringify?: (input: Input) => string,
30
+ ): Promise<Output> => {
31
+ const result = await _Propagate("fetch")(props)(
32
+ connection,
33
+ route,
34
+ input,
35
+ stringify,
36
+ );
37
+ if ((result as any).success === false)
38
+ throw new HttpError(
39
+ route.method,
40
+ route.path,
41
+ result.status as any as number,
42
+ result.headers,
43
+ result.data as string,
44
+ );
45
+ return result.data as Output;
46
+ };
47
+
48
+ export const propagate =
49
+ (props: IProps) =>
50
+ async <Input>(
51
+ connection: IConnection,
52
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
53
+ input?: Input,
54
+ stringify?: (input: Input) => string,
55
+ ): Promise<IPropagation<any, any>> =>
56
+ _Propagate("propagate")(props)(connection, route, input, stringify);
57
+
58
+ /**
59
+ * @internal
60
+ */
61
+ const _Propagate =
62
+ (method: string) =>
63
+ (props: IProps) =>
64
+ async <Input>(
65
+ connection: IConnection,
66
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
67
+ input?: Input,
68
+ stringify?: (input: Input) => string,
69
+ ): Promise<IPropagation<any, any>> => {
70
+ //----
71
+ // REQUEST MESSAGE
72
+ //----
73
+ // METHOD & HEADERS
74
+ const headers: Record<string, IConnection.HeaderValue | undefined> = {
75
+ ...(connection.headers ?? {}),
76
+ };
77
+ if (input !== undefined) {
78
+ if (route.request?.type === undefined)
79
+ throw new Error(
80
+ `Error on ${props.className}.fetch(): no content-type being configured.`,
81
+ );
82
+ else if (route.request.type !== "multipart/form-data")
83
+ headers["Content-Type"] = route.request.type;
84
+ } else if (input === undefined && headers["Content-Type"] !== undefined)
85
+ delete headers["Content-Type"];
86
+
87
+ // INIT REQUEST DATA
88
+ const init: RequestInit = {
89
+ ...(connection.options ?? {}),
90
+ method: route.method,
91
+ headers: (() => {
92
+ const output: [string, string][] = [];
93
+ for (const [key, value] of Object.entries(headers))
94
+ if (value === undefined) continue;
95
+ else if (Array.isArray(value))
96
+ for (const v of value) output.push([key, String(v)]);
97
+ else output.push([key, String(value)]);
98
+ return output;
99
+ })(),
100
+ };
101
+
102
+ // CONSTRUCT BODY DATA
103
+ if (input !== undefined)
104
+ init.body = props.encode(
105
+ // BODY TRANSFORM
106
+ route.request?.type === "application/x-www-form-urlencoded"
107
+ ? request_query_body(input)
108
+ : route.request?.type === "multipart/form-data"
109
+ ? request_form_data_body(input as any)
110
+ : route.request?.type !== "text/plain"
111
+ ? (stringify ?? JSON.stringify)(input)
112
+ : input,
113
+ headers,
114
+ );
115
+
116
+ //----
117
+ // RESPONSE MESSAGE
118
+ //----
119
+ // URL SPECIFICATION
120
+ const path: string =
121
+ connection.host[connection.host.length - 1] !== "/" &&
122
+ route.path[0] !== "/"
123
+ ? `/${route.path}`
124
+ : route.path;
125
+ const url: URL = new URL(`${connection.host}${path}`);
126
+
127
+ // DO FETCH
128
+ const event: IFetchEvent = {
129
+ route,
130
+ path,
131
+ status: null,
132
+ input,
133
+ output: undefined,
134
+ started_at: new Date(),
135
+ respond_at: null,
136
+ completed_at: null!,
137
+ };
138
+ try {
139
+ // TRY FETCH
140
+ const response: Response = await (connection.fetch ?? fetch)(
141
+ url.href,
142
+ init,
143
+ );
144
+ event.respond_at = new Date();
145
+ event.status = response.status;
146
+
147
+ // CONSTRUCT RESULT DATA
148
+ const result: IPropagation<any, any> = {
149
+ success:
150
+ response.status === 200 ||
151
+ response.status === 201 ||
152
+ response.status === route.status,
153
+ status: response.status,
154
+ headers: response_headers_to_object(response.headers),
155
+ data: undefined!,
156
+ } as any;
157
+ if ((result as any).success === false) {
158
+ // WHEN FAILED
159
+ result.data = await response.text();
160
+ const type = response.headers.get("content-type");
161
+ if (
162
+ method !== "fetch" &&
163
+ type &&
164
+ type.indexOf("application/json") !== -1
165
+ )
166
+ try {
167
+ result.data = JSON.parse(result.data);
168
+ } catch {}
169
+ } else {
170
+ // WHEN SUCCESS
171
+ if (route.method === "HEAD") result.data = undefined!;
172
+ else if (route.response?.type === "application/json") {
173
+ const text: string = await response.text();
174
+ result.data = text.length ? JSON.parse(text) : undefined;
175
+ } else if (
176
+ route.response?.type === "application/x-www-form-urlencoded"
177
+ ) {
178
+ const query: URLSearchParams = new URLSearchParams(
179
+ await response.text(),
180
+ );
181
+ result.data = route.parseQuery ? route.parseQuery(query) : query;
182
+ } else
183
+ result.data = props.decode(await response.text(), result.headers);
184
+ }
185
+ event.output = result.data;
186
+ return result;
187
+ } catch (exp) {
188
+ throw exp;
189
+ } finally {
190
+ event.completed_at = new Date();
191
+ if (connection.logger)
192
+ try {
193
+ await connection.logger(event);
194
+ } catch {}
195
+ }
196
+ };
197
+ }
198
+
199
+ /**
200
+ * @internal
201
+ */
202
+ const request_query_body = (input: any): URLSearchParams => {
203
+ const q: URLSearchParams = new URLSearchParams();
204
+ for (const [key, value] of Object.entries(input))
205
+ if (value === undefined) continue;
206
+ else if (Array.isArray(value))
207
+ value.forEach((elem) => q.append(key, String(elem)));
208
+ else q.set(key, String(value));
209
+ return q;
210
+ };
211
+
212
+ /**
213
+ * @internal
214
+ */
215
+ const request_form_data_body = (input: Record<string, any>): FormData => {
216
+ const encoded: FormData = new FormData();
217
+ const append = (key: string) => (value: any) => {
218
+ if (value === undefined) return;
219
+ else if (typeof File === "function" && value instanceof File)
220
+ encoded.append(key, value, value.name);
221
+ else encoded.append(key, value);
222
+ };
223
+ for (const [key, value] of Object.entries(input))
224
+ if (Array.isArray(value)) value.map(append(key));
225
+ else append(key)(value);
226
+ return encoded;
227
+ };
228
+
229
+ /**
230
+ * @internal
231
+ */
232
+ const response_headers_to_object = (
233
+ headers: Headers,
234
+ ): Record<string, string | string[]> => {
235
+ const output: Record<string, string | string[]> = {};
236
+ headers.forEach((value, key) => {
237
+ if (key === "set-cookie") {
238
+ output[key] ??= [];
239
+ (output[key] as string[]).push(
240
+ ...value.split(";").map((str) => str.trim()),
241
+ );
242
+ } else output[key] = value;
243
+ });
244
+ return output;
245
+ };