Skip to content

Commit

Permalink
Allow credentials option to be set
Browse files Browse the repository at this point in the history
Right now, the credentials option for the request object is locked to `same-origin`. `omit` and `include` are two additional valid options.

This allows the user to set the value through the options object. If not set, we still default to `same-origin`.
  • Loading branch information
t27duck committed May 20, 2023
1 parent 4821f5c commit 227f252
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
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
6 changes: 3 additions & 3 deletions __tests__/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ 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

0 comments on commit 227f252

Please sign in to comment.