-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequest.ts
More file actions
221 lines (201 loc) · 5.88 KB
/
Copy pathRequest.ts
File metadata and controls
221 lines (201 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { EventEmitter } from "events";
import reduce from "lodash/reduce";
import querystring from "qs";
import { Readable } from "stream";
import { v4 as uuid } from "uuid";
import { RequestError, RequestTimeoutError } from "./errors";
import Response, { IFetchResponse } from "./Response";
export interface IRequestOptions {
method?: string;
uri: string;
body?: any;
json?: boolean;
multipart?: boolean;
responseAsJson?: boolean;
qs?: any;
headers?: { [key: string]: any };
timeout?: number;
disableRetry?: boolean;
retries?: number;
retryDelay?: number;
}
export default class Request extends EventEmitter {
public protectedKeys = ["hash"];
public readonly uuid: string;
public readonly method?: string;
public readonly uri: string;
public readonly body?: any;
public readonly json?: boolean;
public readonly multipart?: boolean;
public readonly responseAsJson?: boolean;
public readonly qs: string;
public readonly headers: { [key: string]: any };
public readonly timeout?: number;
public readonly meta: {
start: Date;
finish: Date | null;
time?: number;
};
public readonly retries: number;
public readonly retryDelay: number;
public response?: Response = undefined;
constructor({
method,
uri,
body,
json,
multipart,
responseAsJson,
qs,
headers,
timeout,
disableRetry = false,
retries = -1,
retryDelay = 5000,
}: IRequestOptions) {
super();
this.uuid = uuid();
this.method = method;
this.uri = uri;
this.body = body;
this.json = json;
this.multipart = multipart;
this.responseAsJson = responseAsJson;
this.qs = qs;
this.headers = {
...headers,
"User-Agent": `node-omnipartners/2.0.0`,
"X-Omnipartners-Request-Id": this.uuid,
...(this.json && {
Accept: "application/json",
}),
...(this.json &&
this.body && {
"Content-Type": "application/json",
}),
...(this.multipart &&
this.body && {
"Content-Type": "multipart/form-data",
}),
};
this.timeout = timeout;
this.meta = {
finish: null,
start: new Date(),
};
this.retries = disableRetry ? -1 : retries;
this.retryDelay = retryDelay;
}
public async fetch() {
this.emit("fetch");
let uri = this.uri;
if (this.qs) {
uri +=
(uri.indexOf("?") >= 0 ? "&" : "?") + querystring.stringify(this.qs);
}
let fetchRes: IFetchResponse;
try {
const headers = { ...this.headers };
let body: BodyInit | undefined;
if (this.multipart && this.body) {
const formData = new FormData();
for (const [key, value] of Object.entries(
this.body as Record<string, unknown>,
)) {
if (value === undefined || value === null) continue;
if (typeof value === "string") {
formData.append(key, value);
} else if (value instanceof Readable) {
const chunks: Buffer[] = [];
for await (const chunk of value) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
formData.append(key, new Blob([Buffer.concat(chunks)]));
} else if (
value &&
typeof value === "object" &&
"value" in value &&
"options" in value
) {
const { value: buf, options } = value as {
value: Buffer;
options: { filename: string };
};
formData.append(key, new Blob([buf]), options.filename);
} else {
formData.append(key, String(value));
}
}
// Remove Content-Type so fetch sets it automatically with the correct boundary
delete headers["Content-Type"];
body = formData;
} else {
body = this.json ? JSON.stringify(this.body) : this.body;
}
const controller = new AbortController();
const timeoutId = this.timeout
? setTimeout(() => controller.abort(), this.timeout)
: undefined;
const rawRes = await fetch(uri, {
body,
headers,
method: this.method,
signal: controller.signal,
}).finally(() => {
if (timeoutId !== undefined) clearTimeout(timeoutId);
});
const flatHeaders: { [key: string]: string } = {};
rawRes.headers.forEach((value: string, name: string) => {
flatHeaders[name] = value;
});
fetchRes = {
status: rawRes.status,
text: () => rawRes.text(),
headers: flatHeaders,
ok: rawRes.ok,
statusText: rawRes.statusText,
};
} catch (e) {
this.emit("fetchError", e);
const err = e as { name?: string; code?: string; cause?: { code?: string } };
if (err.name === "TimeoutError" || err.name === "AbortError") {
throw new RequestTimeoutError({ request: this });
} else if (
err.code === "ECONNRESET" ||
err.cause?.code === "ECONNRESET"
) {
throw new RequestError(this);
} else {
throw e;
}
}
this.meta.finish = new Date();
this.meta.time = this.meta.finish.getTime() - this.meta.start.getTime();
this.response = new Response(this, fetchRes);
this.response.checkRequestStatus();
this.emit("fetchSuccess");
return this.response;
}
public toJSON() {
const filterSensitiveData = (obj: any) =>
reduce(
obj,
(res, v, k) => ({
...res,
[k]: this.protectedKeys.indexOf(k) >= 0 ? "[FILTERED]" : v,
}),
{},
);
return {
body: this.body && filterSensitiveData(this.body),
headers: this.headers,
meta: this.meta,
method: this.method,
qs: this.qs && filterSensitiveData(this.qs),
response: this.response && this.response.toJSON(),
timeout: this.timeout,
uri: this.uri,
uuid: this.uuid,
};
}
}