Skip to content

Commit

Permalink
feat: disable request caching for cognito APIs (#12020)
Browse files Browse the repository at this point in the history
Co-authored-by: Jim Blanchard <[email protected]>
  • Loading branch information
AllanZhengYP and jimblanc authored Sep 14, 2023
1 parent 0e5556d commit 6ba700b
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 40 deletions.
10 changes: 2 additions & 8 deletions packages/analytics/src/providers/pinpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export {
record,
identifyUser
} from './apis';
export {
RecordInput,
IdentifyUserInput
} from './types/inputs';
export { record, identifyUser } from './apis';
export { RecordInput, IdentifyUserInput } from './types/inputs';
5 changes: 1 addition & 4 deletions packages/analytics/src/providers/pinpoint/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
// SPDX-License-Identifier: Apache-2.0

export { UpdateEndpointException } from './errors';
export {
RecordInput,
IdentifyUserInput
} from './inputs';
export { RecordInput, IdentifyUserInput } from './inputs';
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const defaultConfig = {
retryDecider: getRetryDecider(parseJsonError),
computeDelay: jitteredBackoff,
userAgentValue: getAmplifyUserAgent(),
cache: 'no-store',
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/__tests__/clients/composeApiHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe(composeServiceApi.name, () => {
jest.clearAllMocks();
});

test('should call transfer handler with resolved config', async () => {
test('should call transfer handler with resolved config including default config values', async () => {
const mockTransferHandler = jest.fn().mockResolvedValue(defaultResponse);
const config = {
...defaultConfig,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/__tests__/clients/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ describe(fetchTransferHandler.name, () => {
);
});

test('should configure cache', async () => {
const cacheMode = 'no-store';
await fetchTransferHandler(mockRequest, { cache: cacheMode });
expect(mockFetch).toBeCalledTimes(1);
expect(mockFetch.mock.calls[0][1]).toEqual(
expect.objectContaining({ cache: cacheMode })
);
});

test('should support headers', async () => {
mockFetchResponse.headers.forEach.mockImplementation((cb: any) => {
cb('foo', 'bar');
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/AwsClients/CognitoIdentity/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '../../clients/middleware/retry';
import { getAmplifyUserAgent } from '../../Platform';
import { observeFrameworkChanges } from '../../Platform/detectFramework';
import { DefaultConfigOptions } from '../../clients/types';

/**
* The service name used to sign requests if the API requires authentication.
Expand Down Expand Up @@ -59,12 +58,13 @@ export const cognitoIdentityTransferHandler = composeTransferHandler<
/**
* @internal
*/
export const defaultConfig: DefaultConfigOptions = {
export const defaultConfig = {
service: SERVICE_NAME,
endpointResolver,
retryDecider: getRetryDecider(parseJsonError),
computeDelay: jitteredBackoff,
userAgentValue: getAmplifyUserAgent(),
cache: 'no-store',
};

observeFrameworkChanges(() => {
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/AwsClients/Pinpoint/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import {
getRetryDecider,
} from '../../clients/middleware/retry';
import { parseJsonError } from '../../clients/serde/json';
import type {
DefaultConfigOptions,
EndpointResolverOptions,
Headers,
} from '../../clients/types';
import type { EndpointResolverOptions, Headers } from '../../clients/types';
import { getAmplifyUserAgent } from '../../Platform';

/**
Expand All @@ -29,7 +25,7 @@ const endpointResolver = ({ region }: EndpointResolverOptions) => ({
/**
* @internal
*/
export const defaultConfig: DefaultConfigOptions = {
export const defaultConfig = {
service: SERVICE_NAME,
endpointResolver,
retryDecider: getRetryDecider(parseJsonError),
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/clients/handlers/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { HttpRequest, HttpResponse } from '../types';
export const authenticatedHandler = composeTransferHandler<
[UserAgentOptions, RetryOptions<HttpResponse>, SigningOptions],
HttpRequest,
HttpResponse
HttpResponse,
typeof fetchTransferHandler
>(fetchTransferHandler, [
userAgentMiddleware,
retryMiddleware,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/clients/handlers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ export const fetchTransferHandler: TransferHandler<
HttpRequest,
HttpResponse,
HttpTransferOptions
> = async ({ url, method, headers, body }, { abortSignal }) => {
> = async ({ url, method, headers, body }, { abortSignal, cache }) => {
let resp: Response;
try {
resp = await fetch(url, {
method,
headers,
body: shouldSendBody(method) ? body : undefined,
signal: abortSignal,
cache,
});
} catch (e) {
// TODO: needs to revise error handling in v6
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/clients/handlers/unauthenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import { HttpRequest, HttpResponse } from '../types';
export const unauthenticatedHandler = composeTransferHandler<
[UserAgentOptions, RetryOptions<HttpResponse>],
HttpRequest,
HttpResponse
HttpResponse,
typeof fetchTransferHandler
>(fetchTransferHandler, [userAgentMiddleware, retryMiddleware]);
13 changes: 10 additions & 3 deletions packages/core/src/clients/internal/composeServiceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const composeServiceApi = <
ServiceClientOptions &
Partial<DefaultConfig> &
InferEndpointResolverOptionType<DefaultConfig>,
keyof DefaultConfig
DefaultConfig
>,
input: Input
) => {
Expand All @@ -54,8 +54,15 @@ export const composeServiceApi = <
};
};

type OptionalizeKey<T, K extends keyof T> = Omit<T, K> & {
[P in K & keyof T]?: T[P];
type OptionalizeKey<InputType, InputDefaultsType> = {
[KeyWithDefaultValue in keyof InputDefaultsType]?: KeyWithDefaultValue extends keyof InputType
? InputType[KeyWithDefaultValue]
: never;
} & {
[KeyWithoutDefaultValue in keyof Omit<
InputType,
keyof InputDefaultsType
>]: InputType[KeyWithoutDefaultValue];
};

type InferEndpointResolverOptionType<T> = T extends {
Expand Down
10 changes: 0 additions & 10 deletions packages/core/src/clients/types/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,3 @@ export interface ServiceClientOptions {
export type ErrorParser = (
response?: HttpResponse
) => Promise<(Error & MetadataBearer) | undefined>;

/**
* Default config options for the `composeServiceApi`.
*/
export type DefaultConfigOptions<
TransferHandlerOptions extends Record<string, unknown> = Record<
string,
unknown
>
> = Partial<TransferHandlerOptions & ServiceClientOptions>;
8 changes: 8 additions & 0 deletions packages/core/src/clients/types/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export interface HttpResponse extends Response {

export interface HttpTransferOptions {
abortSignal?: AbortSignal;
/**
* Cache mode for the request. Note that this is only effective when the underlying HTTP handler is fetch.
* For XHR handler, or Node.js `"http(s)"` module or running on React Native, this option is ignored.
* Instead, you can configure the `Cache-Control` headers to achieve similar effects.
* @default 'default'
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/cache}
*/
cache?: RequestCache;
}

export type HttpTransferHandler = TransferHandler<
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/clients/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ export {
EndpointResolverOptions,
ErrorParser,
ServiceClientOptions,
DefaultConfigOptions,
} from './aws';
2 changes: 1 addition & 1 deletion packages/core/src/providers/pinpoint/types/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PinpointAnalyticsEvent, PinpointSession } from './pinpoint';
export type EventBufferConfig = {
appId: string;
bufferSize: number;
credentials: AuthSession['credentials'];
credentials: Required<AuthSession>['credentials'];
identityId: AuthSession['identityId'];
flushInterval: number;
flushSize: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/providers/pinpoint/types/pinpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type PinpointAnalyticsEvent = {
type PinpointCommonParameters = {
appId: string;
category: SupportedCategory;
credentials: AuthSession['credentials'];
credentials: Required<AuthSession>['credentials'];
identityId?: AuthSession['identityId'];
region: string;
userAgentValue?: string;
Expand Down

0 comments on commit 6ba700b

Please sign in to comment.