Skip to content

Commit a7a78e3

Browse files
committed
Allow credentials option to be set
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`.
1 parent 4821f5c commit a7a78e3

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ When provided this value will be sent in the `Content-Type` header. When not pro
8888

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

91+
##### credentials
92+
93+
Specifies the `credentials` option. Default is `same-origin`.
94+
9195
##### query
9296

9397
Appends query parameters to the URL. Query params in the URL are preserved and merged with the query options.

__tests__/fetch_request.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,13 @@ describe('header handling', () => {
198198
expect(request.fetchOptions.signal).toBe("signal")
199199
})
200200

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

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

211210
describe('csrf token inclusion', () => {

src/fetch_request.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class FetchRequest {
5858
headers: this.headers,
5959
body: this.formattedBody,
6060
signal: this.signal,
61-
credentials: 'same-origin',
61+
credentials: this.credentials,
6262
redirect: this.redirect
6363
}
6464
}
@@ -147,6 +147,10 @@ export class FetchRequest {
147147
return this.options.redirect || 'follow'
148148
}
149149

150+
get credentials () {
151+
return this.options.credentials || 'same-origin'
152+
}
153+
150154
get additionalHeaders () {
151155
return this.options.headers || {}
152156
}

0 commit comments

Comments
 (0)