-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
browser-geturl.ts
44 lines (35 loc) · 1.51 KB
/
browser-geturl.ts
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
"use strict";
import { arrayify } from "@ethersproject/bytes";
import type { GetUrlResponse, Options } from "./types";
export { GetUrlResponse, Options };
export async function getUrl(href: string, options?: Options): Promise<GetUrlResponse> {
if (options == null) { options = { }; }
const request = {
method: (options.method || "GET"),
headers: (options.headers || { }),
body: (options.body || undefined),
mode: <RequestMode>"cors", // no-cors, cors, *same-origin
cache: <RequestCache>"no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: <RequestCredentials>"same-origin", // include, *same-origin, omit
redirect: <RequestRedirect>"follow", // manual, *follow, error
referrer: "client", // no-referrer, *client
};
const response = await fetch(href, request);
const body = await response.arrayBuffer();
const headers: { [ name: string ]: string } = { };
if (response.headers.forEach) {
response.headers.forEach((value, key) => {
headers[key.toLowerCase()] = value;
});
} else {
(<() => Array<string>>((<any>(response.headers)).keys))().forEach((key) => {
headers[key.toLowerCase()] = response.headers.get(key);
});
}
return {
headers: headers,
statusCode: response.status,
statusMessage: response.statusText,
body: arrayify(new Uint8Array(body)),
}
}