2020-04-18 02:46:52 -04:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-30 17:03:36 -04:00
|
|
|
import { arrayify } from "@ethersproject/bytes";
|
|
|
|
|
|
|
|
import type { GetUrlResponse, Options } from "./types";
|
|
|
|
|
|
|
|
export { GetUrlResponse, Options };
|
2020-04-18 02:46:52 -04:00
|
|
|
|
|
|
|
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);
|
2020-07-30 17:03:36 -04:00
|
|
|
const body = await response.arrayBuffer();
|
2020-04-18 02:46:52 -04:00
|
|
|
|
|
|
|
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,
|
2020-07-30 17:03:36 -04:00
|
|
|
body: arrayify(new Uint8Array(body)),
|
2020-04-18 02:46:52 -04:00
|
|
|
}
|
|
|
|
}
|