Skip to content

Commit c309df8

Browse files
committedJun 30, 2022
Allow browser fetch option overrides (#3096).

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed
 

‎packages/web/src.ts/browser-geturl.ts

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ export async function getUrl(href: string, options?: Options): Promise<GetUrlRes
2323
request.referrer = "client"; // no-referrer, *client
2424
};
2525

26+
if (options.fetchOptions != null) {
27+
const opts = options.fetchOptions;
28+
if (opts.mode) { request.mode = <RequestMode>(opts.mode); }
29+
if (opts.cache) { request.cache = <RequestCache>(opts.cache); }
30+
if (opts.credentials) { request.credentials = <RequestCredentials>(opts.credentials); }
31+
if (opts.redirect) { request.redirect = <RequestRedirect>(opts.redirect); }
32+
if (opts.referrer) { request.referrer = opts.referrer; }
33+
}
34+
2635
const response = await fetch(href, request);
2736
const body = await response.arrayBuffer();
2837

‎packages/web/src.ts/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type ConnectionInfo = {
5050
throttleCallback?: (attempt: number, url: string) => Promise<boolean>,
5151

5252
skipFetchSetup?: boolean;
53+
fetchOptions?: Record<string, string>;
5354
errorPassThrough?: boolean;
5455

5556
timeout?: number,
@@ -158,7 +159,12 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,
158159
if (connection.skipFetchSetup != null) {
159160
options.skipFetchSetup = !!connection.skipFetchSetup;
160161
}
162+
163+
if (connection.fetchOptions != null) {
164+
options.fetchOptions = shallowCopy(connection.fetchOptions);
165+
}
161166
}
167+
162168
const reData = new RegExp("^data:([a-z0-9-]+/[a-z0-9-]+);base64,(.*)$", "i");
163169
const dataMatch = ((url) ? url.match(reData): null);
164170
if (dataMatch) {

‎packages/web/src.ts/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export type Options = {
1313
body?: Uint8Array;
1414
headers?: { [ key: string] : string };
1515
skipFetchSetup?: boolean;
16+
fetchOptions?: Record<string, string>;
1617
};
1718

0 commit comments

Comments
 (0)
Please sign in to comment.