From 6582ede1ce46be0b3abafb120e052b95a2d172b3 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Tue, 5 Oct 2021 16:57:51 -0400 Subject: [PATCH] Add support for Cloudflare Workers (#1886). --- packages/web/src.ts/browser-geturl.ts | 14 ++++++++------ packages/web/src.ts/types.ts | 9 +++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/web/src.ts/browser-geturl.ts b/packages/web/src.ts/browser-geturl.ts index 03c751509..7770da79a 100644 --- a/packages/web/src.ts/browser-geturl.ts +++ b/packages/web/src.ts/browser-geturl.ts @@ -9,16 +9,18 @@ export { GetUrlResponse, Options }; export async function getUrl(href: string, options?: Options): Promise { if (options == null) { options = { }; } - const request = { + const request: RequestInit = { method: (options.method || "GET"), headers: (options.headers || { }), body: (options.body || undefined), + }; - mode: "cors", // no-cors, cors, *same-origin - cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached - credentials: "same-origin", // include, *same-origin, omit - redirect: "follow", // manual, *follow, error - referrer: "client", // no-referrer, *client + if (options.skipFetchSetup !== true) { + request.mode = "cors"; // no-cors, cors, *same-origin + request.cache = "no-cache"; // *default, no-cache, reload, force-cache, only-if-cached + request.credentials = "same-origin"; // include, *same-origin, omit + request.redirect = "follow"; // manual, *follow, error + request.referrer = "client"; // no-referrer, *client }; const response = await fetch(href, request); diff --git a/packages/web/src.ts/types.ts b/packages/web/src.ts/types.ts index 1596e2f0d..3c80dc8ee 100644 --- a/packages/web/src.ts/types.ts +++ b/packages/web/src.ts/types.ts @@ -1,16 +1,17 @@ "use strict"; export type GetUrlResponse = { - statusCode: number, + statusCode: number; statusMessage: string; headers: { [ key: string] : string }; body: Uint8Array; }; export type Options = { - method?: string, + method?: string; allowGzip?: boolean; - body?: Uint8Array - headers?: { [ key: string] : string }, + body?: Uint8Array; + headers?: { [ key: string] : string }; + skipFetchSetup?: boolean; };