Skip to content

Adding support for setting the fetch API credentials mode #1317

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

Merged
merged 3 commits into from
Jun 27, 2024
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
6 changes: 6 additions & 0 deletions .changeset/polite-fans-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smithy/fetch-http-handler": minor
"@smithy/types": minor
---

Adding support for setting the fetch API credentials mode
29 changes: 29 additions & 0 deletions packages/fetch-http-handler/src/fetch-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,35 @@ describe(FetchHttpHandler.name, () => {
expect(await blobToText(response.body)).toBe("FOO");
});

it.each(["include", "omit", "same-origin"])(
"will pass credentials mode '%s' from a provider to a request",
async (credentialsMode) => {
const mockResponse = {
headers: { entries: jest.fn().mockReturnValue([]) },
blob: jest.fn().mockResolvedValue(new Blob()),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);

(global as any).fetch = mockFetch;

const httpRequest = new HttpRequest({
headers: {},
hostname: "foo.amazonaws.com",
method: "GET",
path: "/",
body: "will be omitted",
});
const fetchHttpHandler = new FetchHttpHandler();
fetchHttpHandler.updateHttpClientConfig("credentials", credentialsMode as RequestCredentials);

await fetchHttpHandler.handle(httpRequest, {});

expect(mockFetch.mock.calls.length).toBe(1);
const requestCall = mockRequest.mock.calls[0];
expect(requestCall[1].credentials).toBe(credentialsMode);
}
);

describe("#destroy", () => {
it("should be callable and return nothing", () => {
const httpHandler = new FetchHttpHandler();
Expand Down
2 changes: 2 additions & 0 deletions packages/fetch-http-handler/src/fetch-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
}
const requestTimeoutInMs = this.config!.requestTimeout;
const keepAlive = this.config!.keepAlive === true;
const credentials = this.config!.credentials as RequestInit["credentials"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're already using RequestInit, so RequestCredentials is probably globally available too, but I'm being conservative here.


// if the request was already aborted, prevent doing extra work
if (abortSignal?.aborted) {
Expand Down Expand Up @@ -110,6 +111,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
body,
headers: new Headers(request.headers),
method: method,
credentials,
};
if (body) {
requestOptions.duplex = "half";
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/http/httpHandlerInitialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@ export interface FetchHttpHandlerOptions {
* these limitations before enabling keepalive.
*/
keepAlive?: boolean;

/**
* A string indicating whether credentials will be sent with the request always, never, or
* only when sent to a same-origin URL.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
*/
credentials?: "include" | "omit" | "same-origin" | undefined | string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe credentialsMode instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

credentials matches the property on the Fetch request itself. I think it makes sense to keep this property name consistent with how it's used on the request.

}
Loading