Skip to content
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

feat(fetch): support custom credentials in Request #82

Merged
merged 4 commits into from
Aug 28, 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
5 changes: 5 additions & 0 deletions .changeset/soft-gorillas-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": feat
---

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.
4 changes: 3 additions & 1 deletion packages/fetch/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const isRequest = object => {
* @property {string} method
* @property {RequestRedirect} redirect
* @property {globalThis.Headers} headers
* @property {RequestCredentials} credentials
* @property {URL} parsedURL
* @property {AbortSignal|null} signal
*
Expand Down Expand Up @@ -125,6 +126,7 @@ export default class Request extends Body {
method,
redirect: init.redirect || input.redirect || 'follow',
headers,
credentials: init.credentials || 'same-origin',
parsedURL,
signal: signal || null
};
Expand Down Expand Up @@ -159,7 +161,7 @@ export default class Request extends Body {
*/

get credentials() {
return "same-origin"
return this[INTERNALS].credentials
}

/**
Expand Down
10 changes: 10 additions & 0 deletions packages/fetch/test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ describe('Request', () => {
expect(derivedRequest.signal).to.equal(null);
});

it('should default to "same-origin" as credentials', () => {
const request = new Request(base)
expect(request.credentials).to.equal('same-origin');
})

it('should respect custom credentials value', () => {
expect(new Request(base, { credentials: 'omit'})).to.have.property('credentials', 'omit');
expect(new Request(base, { credentials: 'include'})).to.have.property('credentials', 'include');
})

it('should throw error with GET/HEAD requests with body', () => {
expect(() => new Request(base, {body: ''}))
.to.throw(TypeError);
Expand Down