Skip to content

Commit

Permalink
fix: not passing config.init to fetch
Browse files Browse the repository at this point in the history
Fixes a bug causing `config.init` not to be passed as the `init` parameter to `fetch`.
  • Loading branch information
kleinfreund committed Jul 16, 2023
1 parent 514b3e6 commit 4139f4f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/retrieve.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions src/retrieve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,52 @@ describe('retrieve', () => {
})

describe('init', () => {
test('config.init parameters are passed to fetch', async () => {
vi.spyOn(global, 'fetch').mockImplementation((...fetchParams: OriginalFetchParams) => {
assertInitEquality(fetchParams[1], {
body: 'body',
cache: 'default',
credentials: 'same-origin',
headers: new Headers({
'x-test-header': 'header-value',
'x-requested-with': 'XMLHttpRequest',
}),
integrity: 'hash',
keepalive: true,
method: 'POST',
mode: 'same-origin',
redirect: 'follow',
referrer: 'ref',
signal: null,
window: null,
})

return Promise.resolve(new Response('OK'))
})

await retrieve({
url: 'http://example.org',
init: {
body: 'body',
cache: 'default',
credentials: 'same-origin',
headers: {
'x-test-header': 'header-value',
},
integrity: 'hash',
keepalive: true,
method: 'POST',
mode: 'same-origin',
redirect: 'follow',
referrer: 'ref',
signal: null,
window: null,
},
})

expect(global.fetch).toHaveBeenCalled()
})

describe('method', () => {
test.each([
[
Expand Down
2 changes: 1 addition & 1 deletion src/retrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ function createUrl(config: RetrieveConfig): URL {

function createInit(config: RetrieveConfig): RequestInit {
const originalInit: RequestInit = config.init ?? {}
const init: RequestInit = {}
const init: RequestInit = { ...originalInit }

// Process request method
init.method = (originalInit.method ?? 'GET').toUpperCase()
Expand Down

0 comments on commit 4139f4f

Please sign in to comment.