-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (233 loc) · 7.22 KB
/
index.js
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
222
223
224
225
226
227
228
229
230
231
232
233
"use strict";
class FetchBuilder {
static buildUrl(strings, ...p) {
let r = "";
for (let index = 0; index < strings.length; index++) {
const element = strings[index];
r += element;
if (index < p.length) {
r += encodeURIComponent(p[index]);
}
}
return r;
}
static get(url) {
return this.method(url, "GET");
}
static put(url) {
return this.method(url, "PUT");
}
static post(url) {
return this.method(url, "POST");
}
static delete(url) {
return this.method(url, "DELETE");
}
static url(url) {
return this.method(url, "GET");
}
static header(name, value) {
return new FetchBuilder({ headers: { url: "", method: "POST", [name]: value } });
}
static method(url, method) {
return new FetchBuilder({ url, method });
}
constructor(request) {
this.request = request;
}
log(logger) {
return this.append({ log: logger });
}
logWhenFailed(logger) {
return this.append({ logError: logger });
}
get(url) {
return this.method(url, "GET");
}
put(url) {
return this.method(url, "PUT");
}
post(url) {
return this.method(url, "POST");
}
delete(url) {
return this.method(url, "DELETE");
}
method(url, method) {
return this.append({ url, method });
}
// public cancelToken(cancelToken: CancelToken) {
// const ac = new AbortController();
// cancelToken.registerForCancel(() => ac.abort());
// return this.signal(ac.signal);
// }
signal(signal) {
return this.append({
signal
});
}
form(name, value, fileName) {
if (value === void 0) {
return this;
}
const body = this.request.body ?? new FormData();
if (fileName) {
if (typeof value === "string") {
throw new Error("value must be a blob with content type set correctly.");
}
body.append(name, value, fileName);
}
else {
body.append(name, value);
}
return this.append({ body });
}
jsonBody(body, encode = true) {
if (encode) {
body = JSON.stringify(body);
}
const headers = this.request.headers ?? {};
headers["content-type"] = "application/json";
return this.append({ body, headers });
}
header(name, value) {
const headers = this.request.headers ?? {};
headers[name] = value;
return this.append({ headers });
}
path(name, value, encode = true) {
let url = this.request.url;
if (encode) {
value = encodeURIComponent(value);
}
url = url.replace(name, value);
return this.append({ url });
}
query(name, value, encode = true) {
if (value === void 0) {
return this;
}
let url = this.request.url;
if (encode) {
value = encodeURIComponent(value);
}
name = encodeURIComponent(name);
if (url.indexOf("?") === -1) {
url += `?${name}=${value}`;
}
else {
url += `&${name}=${value}`;
}
return this.append({ url });
}
async asText(ensureSuccess = true) {
const { result } = await this.asTextResponse(ensureSuccess);
return result;
}
async asBlob(ensureSuccess = true) {
const { result } = await this.asBlobResponse(ensureSuccess);
return result;
}
async asJson(ensureSuccess = true) {
const { result } = await this.asJsonResponse(ensureSuccess);
return result;
}
async asJsonResponse(ensureSuccess = true) {
return this.execute(ensureSuccess, (x) => x.json());
}
async asTextResponse(ensureSuccess = true) {
return this.execute(ensureSuccess, (x) => x.text());
}
asBlobResponse(ensureSuccess = true) {
return this.execute(ensureSuccess, (x) => x.blob());
}
async execute(ensureSuccess = true, postProcessor) {
let { log, logError } = this.request;
try {
const { headers } = this.request;
const r = await fetch(this.request.url, this.request);
if (ensureSuccess) {
if (r.status > 300) {
log = logError;
log?.(`fetch: ${this.request.method ?? "GET"} ${this.request.url}`);
if (log && headers) {
for (const key in headers) {
if ((Object.hasOwn && Object.hasOwn(headers, key)) || headers.hasOwnProperty(key)) {
log?.(`${key}: ${headers[key]}`);
}
}
}
log?.(`${r.status} ${r.statusText || "Http Error"}`);
const type = r.headers.get("content-type");
if (/\/json/i.test(type)) {
const json = await r.json();
log?.(json);
const message = json.title
?? json.detail
?? json.message
?? json.exceptionMessage
?? "Json Server Error";
log = null;
logError = null;
throw new JsonError(message, json);
}
const text = await r.text();
log?.(text);
log = null;
logError = null;
throw new Error(`Failed for ${this.request.url}\n${text}`);
}
}
log?.(`${this.request.method ?? "GET"} ${this.request.url}`);
if (log && headers) {
for (const key in headers) {
if ((Object.hasOwn && Object.hasOwn(headers, key)) || headers.hasOwnProperty(key)) {
log?.(`${key}: ${headers[key]}`);
}
}
}
const result = await postProcessor(r);
if (log) {
log(`${r.status} ${r.statusText || "OK"}`);
log(result);
}
return { result, headers: r.headers, status: r.status };
}
catch (error) {
log?.(error);
throw error;
}
}
append(r) {
// we will try to merge url here..
let { url } = this.request;
const { url: newUrl } = r;
if (newUrl) {
if (!url) {
url = newUrl;
}
else {
if (/^https?\:\/\//i.test(newUrl)) {
url = newUrl;
}
else {
url = (new URL(newUrl, url)).toString();
}
}
}
return new FetchBuilder({
...this.request,
...r,
url
});
}
}
class JsonError extends Error {
constructor(message, json) {
super(message);
this.json = json;
}
}
FetchBuilder.JsonError = JsonError;
module.exports = FetchBuilder;
//# sourceMappingURL=index.js.map