Skip to content

Commit 1da95bc

Browse files
MichaelDeBoeykettanaitojacob-ebey
authored
feat(fetch): support custom credentials in Request (#82)
* fix(Request): support custom "credentials" value (#21) * Create soft-gorillas-travel.md * Update soft-gorillas-travel.md * chore: fix changeset --------- Co-authored-by: Artem Zakharchenko <[email protected]> Co-authored-by: Jacob Ebey <[email protected]>
1 parent 89cb33a commit 1da95bc

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

.changeset/soft-gorillas-travel.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@web-std/fetch": feat
3+
---
4+
5+
Add support for custom `credentials` value. Nothing is done with them at the moment but they pass through for the consumer of the request to access if needed.

packages/fetch/src/request.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const isRequest = object => {
3737
* @property {string} method
3838
* @property {RequestRedirect} redirect
3939
* @property {globalThis.Headers} headers
40+
* @property {RequestCredentials} credentials
4041
* @property {URL} parsedURL
4142
* @property {AbortSignal|null} signal
4243
*
@@ -125,6 +126,7 @@ export default class Request extends Body {
125126
method,
126127
redirect: init.redirect || input.redirect || 'follow',
127128
headers,
129+
credentials: init.credentials || 'same-origin',
128130
parsedURL,
129131
signal: signal || null
130132
};
@@ -159,7 +161,7 @@ export default class Request extends Body {
159161
*/
160162

161163
get credentials() {
162-
return "same-origin"
164+
return this[INTERNALS].credentials
163165
}
164166

165167
/**

packages/fetch/test/request.js

+10
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ describe('Request', () => {
111111
expect(derivedRequest.signal).to.equal(null);
112112
});
113113

114+
it('should default to "same-origin" as credentials', () => {
115+
const request = new Request(base)
116+
expect(request.credentials).to.equal('same-origin');
117+
})
118+
119+
it('should respect custom credentials value', () => {
120+
expect(new Request(base, { credentials: 'omit'})).to.have.property('credentials', 'omit');
121+
expect(new Request(base, { credentials: 'include'})).to.have.property('credentials', 'include');
122+
})
123+
114124
it('should throw error with GET/HEAD requests with body', () => {
115125
expect(() => new Request(base, {body: ''}))
116126
.to.throw(TypeError);

0 commit comments

Comments
 (0)