diff --git a/.changeset/soft-gorillas-travel.md b/.changeset/soft-gorillas-travel.md new file mode 100644 index 0000000..6eca2a6 --- /dev/null +++ b/.changeset/soft-gorillas-travel.md @@ -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. diff --git a/packages/fetch/src/request.js b/packages/fetch/src/request.js index 482882c..8752dca 100644 --- a/packages/fetch/src/request.js +++ b/packages/fetch/src/request.js @@ -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 * @@ -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 }; @@ -159,7 +161,7 @@ export default class Request extends Body { */ get credentials() { - return "same-origin" + return this[INTERNALS].credentials } /** diff --git a/packages/fetch/test/request.js b/packages/fetch/test/request.js index 9fa8cdc..a615be7 100644 --- a/packages/fetch/test/request.js +++ b/packages/fetch/test/request.js @@ -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);