2020-04-18 09:46:52 +03:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-31 00:03:36 +03:00
|
|
|
import { arrayify } from "@ethersproject/bytes";
|
|
|
|
|
|
|
|
import type { GetUrlResponse, Options } from "./types";
|
|
|
|
|
|
|
|
export { GetUrlResponse, Options };
|
2020-04-18 09:46:52 +03:00
|
|
|
|
|
|
|
export async function getUrl(href: string, options?: Options): Promise<GetUrlResponse> {
|
|
|
|
if (options == null) { options = { }; }
|
|
|
|
|
2021-10-05 23:57:51 +03:00
|
|
|
const request: RequestInit = {
|
2020-04-18 09:46:52 +03:00
|
|
|
method: (options.method || "GET"),
|
|
|
|
headers: (options.headers || { }),
|
|
|
|
body: (options.body || undefined),
|
2021-10-05 23:57:51 +03:00
|
|
|
};
|
2020-04-18 09:46:52 +03:00
|
|
|
|
2021-10-05 23:57:51 +03:00
|
|
|
if (options.skipFetchSetup !== true) {
|
|
|
|
request.mode = <RequestMode>"cors"; // no-cors, cors, *same-origin
|
|
|
|
request.cache = <RequestCache>"no-cache"; // *default, no-cache, reload, force-cache, only-if-cached
|
|
|
|
request.credentials = <RequestCredentials>"same-origin"; // include, *same-origin, omit
|
|
|
|
request.redirect = <RequestRedirect>"follow"; // manual, *follow, error
|
|
|
|
request.referrer = "client"; // no-referrer, *client
|
2020-04-18 09:46:52 +03:00
|
|
|
};
|
|
|
|
|
2022-06-30 23:25:07 +03:00
|
|
|
if (options.fetchOptions != null) {
|
|
|
|
const opts = options.fetchOptions;
|
|
|
|
if (opts.mode) { request.mode = <RequestMode>(opts.mode); }
|
|
|
|
if (opts.cache) { request.cache = <RequestCache>(opts.cache); }
|
|
|
|
if (opts.credentials) { request.credentials = <RequestCredentials>(opts.credentials); }
|
|
|
|
if (opts.redirect) { request.redirect = <RequestRedirect>(opts.redirect); }
|
|
|
|
if (opts.referrer) { request.referrer = opts.referrer; }
|
|
|
|
}
|
|
|
|
|
2020-04-18 09:46:52 +03:00
|
|
|
const response = await fetch(href, request);
|
2020-07-31 00:03:36 +03:00
|
|
|
const body = await response.arrayBuffer();
|
2020-04-18 09:46:52 +03: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-31 00:03:36 +03:00
|
|
|
body: arrayify(new Uint8Array(body)),
|
2020-04-18 09:46:52 +03:00
|
|
|
}
|
|
|
|
}
|