Skip to content

Allow credentials option to be set #57

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

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ When provided this value will be sent in the `Content-Type` header. When not pro

Adds additional headers to the request. `X-CSRF-Token` and `Content-Type` are automatically included.

##### credentials

Specifies the `credentials` option. Default is `same-origin`.

##### query

Appends query parameters to the URL. Query params in the URL are preserved and merged with the query options.
Expand Down
7 changes: 3 additions & 4 deletions __tests__/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,13 @@ describe('header handling', () => {
expect(request.fetchOptions.signal).toBe("signal")
})

test('has fixed credentials setting which cannot be changed', () => {
test('has credentials setting which can be changed', () => {
let request
request = new FetchRequest("get", "localhost")
expect(request.fetchOptions.credentials).toBe('same-origin')

// has no effect
request = new FetchRequest("get", "localhost", { credentials: "omit"})
expect(request.fetchOptions.credentials).toBe('same-origin')
request = new FetchRequest("get", "localhost", { credentials: "include"})
expect(request.fetchOptions.credentials).toBe('include')
})

describe('csrf token inclusion', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class FetchRequest {
headers: this.headers,
body: this.formattedBody,
signal: this.signal,
credentials: 'same-origin',
credentials: this.credentials,
redirect: this.redirect
}
}
Expand Down Expand Up @@ -147,6 +147,10 @@ export class FetchRequest {
return this.options.redirect || 'follow'
}

get credentials () {
return this.options.credentials || 'same-origin'
}

get additionalHeaders () {
return this.options.headers || {}
}
Expand Down