-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented non-RDF fetch #104
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1db923
Implemented non-RDF fetch
NSeydoux 197f5fc
Apply suggestions from code review
NSeydoux 7ccd667
Applied suggestions from PR#104
NSeydoux b518af6
Brought the non-rdf fetching closer to the fetch API
NSeydoux 09e865c
Implemented PR feedback
NSeydoux 209fd74
Renamed getFile into fetchFile
NSeydoux 3d0a492
Checks the returned response in tests
NSeydoux 8195e0e
Split between the RequestInit options and the the fetcher options
NSeydoux 489b059
Fixed the way options are passed to fetchFile
NSeydoux b811e17
Removed outdated comment
NSeydoux 41b82c0
Update src/nonRdfData.ts
NSeydoux File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { describe, it, expect } from "@jest/globals"; | ||
|
||
jest.mock("./fetcher", () => ({ | ||
fetch: jest | ||
.fn() | ||
.mockImplementation(() => | ||
Promise.resolve( | ||
new Response("Some data", { status: 200, statusText: "OK" }) | ||
) | ||
), | ||
})); | ||
|
||
import { fetchFile } from "./nonRdfData"; | ||
import { Headers, Response } from "cross-fetch"; | ||
|
||
describe("Non-RDF data fetch", () => { | ||
it("should GET a remote resource using cross-fetch if no other fetcher is available", async () => { | ||
const fetcher = jest.requireMock("./fetcher") as { | ||
fetch: jest.Mock< | ||
ReturnType<typeof window.fetch>, | ||
[RequestInfo, RequestInit?] | ||
>; | ||
}; | ||
|
||
fetcher.fetch.mockReturnValue( | ||
Promise.resolve( | ||
new Response("Some data", { status: 200, statusText: "OK" }) | ||
) | ||
); | ||
|
||
const response = await fetchFile("https://some.url"); | ||
|
||
expect(fetcher.fetch.mock.calls).toEqual([["https://some.url", undefined]]); | ||
expect(response).toEqual( | ||
new Response("Some data", { status: 200, statusText: "OK" }) | ||
); | ||
}); | ||
|
||
it("should GET a remote resource using the provided fetcher", async () => { | ||
const mockFetch = jest | ||
.fn(window.fetch) | ||
.mockReturnValue( | ||
Promise.resolve( | ||
new Response("Some data", { status: 200, statusText: "OK" }) | ||
) | ||
); | ||
|
||
const response = await fetchFile("https://some.url", { | ||
fetch: mockFetch, | ||
}); | ||
|
||
expect(mockFetch.mock.calls).toEqual([["https://some.url", undefined]]); | ||
expect(response).toEqual( | ||
new Response("Some data", { status: 200, statusText: "OK" }) | ||
); | ||
}); | ||
|
||
it("should pass the request headers through", async () => { | ||
NSeydoux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const mockFetch = jest | ||
.fn(window.fetch) | ||
.mockReturnValue( | ||
Promise.resolve( | ||
new Response("Some data", { status: 200, statusText: "OK" }) | ||
) | ||
); | ||
|
||
const response = await fetchFile("https://some.url", { | ||
init: { | ||
headers: new Headers({ Accept: "text/turtle" }), | ||
}, | ||
fetch: mockFetch, | ||
}); | ||
|
||
expect(mockFetch.mock.calls).toEqual([ | ||
[ | ||
"https://some.url", | ||
{ | ||
headers: new Headers({ Accept: "text/turtle" }), | ||
}, | ||
], | ||
]); | ||
expect(response).toEqual( | ||
new Response("Some data", { status: 200, statusText: "OK" }) | ||
); | ||
}); | ||
|
||
it("should return the response even on failure", async () => { | ||
const mockFetch = jest | ||
.fn(window.fetch) | ||
.mockReturnValue( | ||
Promise.resolve( | ||
new Response(undefined, { status: 400, statusText: "Bad request" }) | ||
) | ||
); | ||
|
||
const response = await fetchFile("https://some.url", { | ||
fetch: mockFetch, | ||
}); | ||
|
||
expect(mockFetch.mock.calls).toEqual([["https://some.url", undefined]]); | ||
expect(response).toEqual( | ||
new Response(undefined, { status: 400, statusText: "Bad request" }) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { fetch } from "./fetcher"; | ||
|
||
interface GetFileOptions { | ||
fetch: typeof window.fetch; | ||
init: RequestInit; | ||
} | ||
|
||
const defaultGetFileOptions = { | ||
fetch: fetch, | ||
}; | ||
|
||
/** | ||
* getFile fetches a file, and returns it as a blob of data. | ||
* | ||
* @param url The IRI of the fetched file | ||
* @param options Fetching options: a custom fetcher and/or headers. | ||
*/ | ||
export async function fetchFile( | ||
input: RequestInfo, | ||
options: Partial<GetFileOptions> = defaultGetFileOptions | ||
): Promise<Response> { | ||
const config = { | ||
...defaultGetFileOptions, | ||
...options, | ||
}; | ||
return config.fetch(input, config.init); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should also have a test that checks that the Response is actually returned to the caller?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in ee40efb