Skip to content

Commit

Permalink
chore(core): refactor cognito identtiy clients to be factories
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Oct 3, 2024
1 parent 6f394c7 commit 78ee7df
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 273 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createGetCredentialsForIdentityClient } from '../../../src';
import { fetchTransferHandler } from '../../../src/clients/handlers/fetch';
import {
GetCredentialsForIdentityInput,
GetCredentialsForIdentityOutput,
getCredentialsForIdentity,
} from '../../../src/awsClients/cognitoIdentity';
} from '../../../src/foundation/factories/serviceClients/cognitoIdentity/types';
import { AmplifyUrl } from '../../../src/libraryUtils';
import {
cognitoIdentityHandlerOptions,
mockCredentials,
Expand Down Expand Up @@ -60,6 +61,14 @@ describe('CognitoIdentity - getCredentialsForIdentity', () => {
(fetchTransferHandler as jest.Mock).mockResolvedValue(
mockJsonResponse(succeedResponse),
);
const getCredentialsForIdentity = createGetCredentialsForIdentityClient({
endpointResolver: jest.fn(() => ({
url: new AmplifyUrl(
'https://cognito-identity.us-east-1.amazonaws.com/',
),
})),
});

const response = await getCredentialsForIdentity(
cognitoIdentityHandlerOptions,
params,
Expand Down Expand Up @@ -91,6 +100,13 @@ describe('CognitoIdentity - getCredentialsForIdentity', () => {
mockJsonResponse(failureResponse),
);
expect.assertions(1);
const getCredentialsForIdentity = createGetCredentialsForIdentityClient({
endpointResolver: jest.fn(() => ({
url: new AmplifyUrl(
'https://cognito-identity.us-east-1.amazonaws.com/',
),
})),
});
try {
await getCredentialsForIdentity(cognitoIdentityHandlerOptions, params);
fail('test should fail');
Expand Down
19 changes: 17 additions & 2 deletions packages/core/__tests__/awsClients/cognitoIdentity/getId.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createGetIdClient } from '../../../src';
import { fetchTransferHandler } from '../../../src/clients/handlers/fetch';
import {
GetIdInput,
GetIdOutput,
getId,
} from '../../../src/awsClients/cognitoIdentity';
} from '../../../src/foundation/factories/serviceClients/cognitoIdentity/types';
import { AmplifyUrl } from '../../../src/libraryUtils';
import {
cognitoIdentityHandlerOptions,
mockIdentityId,
Expand Down Expand Up @@ -57,6 +58,13 @@ describe('CognitoIdentity - getId', () => {
(fetchTransferHandler as jest.Mock).mockResolvedValue(
mockJsonResponse(succeedResponse),
);
const getId = createGetIdClient({
endpointResolver: jest.fn(() => ({
url: new AmplifyUrl(
'https://cognito-identity.us-east-1.amazonaws.com/',
),
})),
});
const response = await getId(cognitoIdentityHandlerOptions, params);
expect(response).toEqual(expectedOutput);
expect(fetchTransferHandler).toHaveBeenCalledWith(
Expand Down Expand Up @@ -85,6 +93,13 @@ describe('CognitoIdentity - getId', () => {
mockJsonResponse(failureResponse),
);
expect.assertions(1);
const getId = createGetIdClient({
endpointResolver: jest.fn(() => ({
url: new AmplifyUrl(
'https://cognito-identity.us-east-1.amazonaws.com/',
),
})),
});
try {
await getId(cognitoIdentityHandlerOptions, params);
fail('test should fail');
Expand Down
109 changes: 0 additions & 109 deletions packages/core/src/awsClients/cognitoIdentity/base.ts

This file was deleted.

This file was deleted.

61 changes: 0 additions & 61 deletions packages/core/src/awsClients/cognitoIdentity/getId.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/core/src/awsClients/cognitoIdentity/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { HttpRequest, HttpResponse, Middleware } from '../../../clients';

/**
* A Cognito Identity-specific middleware that disables caching for all requests.
*/
export const createDisableCacheMiddleware: Middleware<
HttpRequest,
HttpResponse,
Record<string, unknown>
> = () => next =>
async function disableCacheMiddleware(request) {
request.headers['cache-control'] = 'no-store';

return next(request);
};
4 changes: 4 additions & 0 deletions packages/core/src/foundation/factories/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export { createDisableCacheMiddleware } from './createDisableCacheMiddleware';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { EndpointResolverOptions, getDnsSuffix } from '../../../../clients';
import { AmplifyUrl } from '../../../../libraryUtils';

import { COGNITO_IDENTITY_SERVICE_NAME } from './constants';

export const cognitoIdentityPoolEndpointResolver = ({
region,
}: EndpointResolverOptions) => ({
url: new AmplifyUrl(
`https://${COGNITO_IDENTITY_SERVICE_NAME}.${region}.${getDnsSuffix(region)}`,
),
});
Loading

0 comments on commit 78ee7df

Please sign in to comment.