Skip to content

Commit 4784fb9

Browse files
qnesinghkuhe
andauthored
Adding support for setting the fetch API credentials mode (#1317)
* feat(fetch-http-handler): formatting and types --------- Co-authored-by: George Fu <[email protected]>
1 parent b69b742 commit 4784fb9

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

.changeset/polite-fans-fly.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/fetch-http-handler": minor
3+
"@smithy/types": minor
4+
---
5+
6+
Adding support for setting the fetch API credentials mode

packages/fetch-http-handler/src/fetch-http-handler.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,35 @@ describe(FetchHttpHandler.name, () => {
353353
expect(await blobToText(response.body)).toBe("FOO");
354354
});
355355

356+
it.each(["include", "omit", "same-origin"])(
357+
"will pass credentials mode '%s' from a provider to a request",
358+
async (credentialsMode) => {
359+
const mockResponse = {
360+
headers: { entries: jest.fn().mockReturnValue([]) },
361+
blob: jest.fn().mockResolvedValue(new Blob()),
362+
};
363+
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
364+
365+
(global as any).fetch = mockFetch;
366+
367+
const httpRequest = new HttpRequest({
368+
headers: {},
369+
hostname: "foo.amazonaws.com",
370+
method: "GET",
371+
path: "/",
372+
body: "will be omitted",
373+
});
374+
const fetchHttpHandler = new FetchHttpHandler();
375+
fetchHttpHandler.updateHttpClientConfig("credentials", credentialsMode as RequestCredentials);
376+
377+
await fetchHttpHandler.handle(httpRequest, {});
378+
379+
expect(mockFetch.mock.calls.length).toBe(1);
380+
const requestCall = mockRequest.mock.calls[0];
381+
expect(requestCall[1].credentials).toBe(credentialsMode);
382+
}
383+
);
384+
356385
describe("#destroy", () => {
357386
it("should be callable and return nothing", () => {
358387
const httpHandler = new FetchHttpHandler();

packages/fetch-http-handler/src/fetch-http-handler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
7777
}
7878
const requestTimeoutInMs = this.config!.requestTimeout;
7979
const keepAlive = this.config!.keepAlive === true;
80+
const credentials = this.config!.credentials as RequestInit["credentials"];
8081

8182
// if the request was already aborted, prevent doing extra work
8283
if (abortSignal?.aborted) {
@@ -110,6 +111,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
110111
body,
111112
headers: new Headers(request.headers),
112113
method: method,
114+
credentials,
113115
};
114116
if (body) {
115117
requestOptions.duplex = "half";

packages/types/src/http/httpHandlerInitialization.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,11 @@ export interface FetchHttpHandlerOptions {
9090
* these limitations before enabling keepalive.
9191
*/
9292
keepAlive?: boolean;
93+
94+
/**
95+
* A string indicating whether credentials will be sent with the request always, never, or
96+
* only when sent to a same-origin URL.
97+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
98+
*/
99+
credentials?: "include" | "omit" | "same-origin" | undefined | string;
93100
}

0 commit comments

Comments
 (0)