Skip to content

Commit

Permalink
Split between the RequestInit options and the the fetcher options
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Jun 3, 2020
1 parent ee40efb commit d1a2fc8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
25 changes: 16 additions & 9 deletions src/nonRdfData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("Non-RDF data fetch", () => {

const response = await fetchFile("https://some.url");

expect(fetcher.fetch.mock.calls).toEqual([["https://some.url", {}]]);
expect(fetcher.fetch.mock.calls).toEqual([["https://some.url", undefined]]);
expect(response).toEqual(
new Response("Some data", { status: 200, statusText: "OK" })
);
Expand All @@ -48,9 +48,11 @@ describe("Non-RDF data fetch", () => {
)
);

const response = await fetchFile("https://some.url", { fetch: mockFetch });
const response = await fetchFile("https://some.url", undefined, {
fetch: mockFetch,
});

expect(mockFetch.mock.calls).toEqual([["https://some.url", {}]]);
expect(mockFetch.mock.calls).toEqual([["https://some.url", undefined]]);
expect(response).toEqual(
new Response("Some data", { status: 200, statusText: "OK" })
);
Expand All @@ -65,10 +67,15 @@ describe("Non-RDF data fetch", () => {
)
);

const response = await fetchFile("https://some.url", {
fetch: mockFetch,
headers: new Headers({ Accept: "text/turtle" }),
});
const response = await fetchFile(
"https://some.url",
{
headers: new Headers({ Accept: "text/turtle" }),
},
{
fetch: mockFetch,
}
);

expect(mockFetch.mock.calls).toEqual([
[
Expand All @@ -92,11 +99,11 @@ describe("Non-RDF data fetch", () => {
)
);

const response = await fetchFile("https://some.url", {
const response = await fetchFile("https://some.url", undefined, {
fetch: mockFetch,
});

expect(mockFetch.mock.calls).toEqual([["https://some.url", {}]]);
expect(mockFetch.mock.calls).toEqual([["https://some.url", undefined]]);
expect(response).toEqual(
new Response(undefined, { status: 400, statusText: "Bad request" })
);
Expand Down
10 changes: 4 additions & 6 deletions src/nonRdfData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetch } from "./fetcher";

interface GetFileOptions extends RequestInit {
interface GetFileOptions {
fetch: typeof window.fetch;
}

Expand All @@ -16,14 +16,12 @@ const defaultGetFileOptions = {
*/
export async function fetchFile(
input: RequestInfo,
init: Partial<GetFileOptions> = defaultGetFileOptions
init?: RequestInit,
options: Partial<GetFileOptions> = defaultGetFileOptions
): Promise<Response> {
const config = {
...defaultGetFileOptions,
...init,
...options,
};
// The `fetch` field is not part of the original RequestInit, and it is no longer
// needed in the init object.
delete init.fetch;
return config.fetch(input, init);
}

0 comments on commit d1a2fc8

Please sign in to comment.