Skip to content

Commit a45be79

Browse files
committed
fix: address feedbacks
1 parent 29fcc9a commit a45be79

27 files changed

+197
-144
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"publish:next": "lerna publish --canary --force-publish \"*\" --yes --dist-tag=next --preid=next${PREID_HASH_SUFFIX} --exact --no-verify-access",
2929
"publish:release": "lerna publish --conventional-commits --yes --message 'chore(release): Publish [ci skip]' --no-verify-access",
3030
"publish:verdaccio": "lerna publish --no-push --canary minor --dist-tag=unstable --preid=unstable --exact --force-publish --yes --no-verify-access",
31-
"publish:custom-clients": "lerna publish --canary --force-publish \"*\" --yes --dist-tag=v5-custom-clients --preid=v5-custom-clients${PREID_HASH_SUFFIX} --exact --no-verify-access",
31+
"publish:v5/custom-clients": "lerna publish --canary --force-publish \"*\" --yes --dist-tag=v5-custom-clients --preid=v5-custom-clients${PREID_HASH_SUFFIX} --exact --no-verify-access",
3232
"ts-coverage": "lerna run ts-coverage"
3333
},
3434
"husky": {

packages/api-graphql/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"name": "API (GraphQL client)",
6565
"path": "./lib-esm/index.js",
6666
"import": "{ Amplify, GraphQLAPI }",
67-
"limit": "86.1 kB"
67+
"limit": "86.2 kB"
6868
}
6969
],
7070
"jest": {

packages/api-rest/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"name": "API (rest client)",
5656
"path": "./lib-esm/index.js",
5757
"import": "{ Amplify, RestAPI }",
58-
"limit": "28.7 kB"
58+
"limit": "28.8 kB"
5959
}
6060
],
6161
"jest": {

packages/auth/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"name": "Auth (top-level class)",
5959
"path": "./lib-esm/index.js",
6060
"import": "{ Amplify, Auth }",
61-
"limit": "53.3 kB"
61+
"limit": "53.4 kB"
6262
}
6363
],
6464
"jest": {

packages/core/__tests__/clients/composeApiHandler-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe(composeServiceApi.name, () => {
1818
};
1919
const api = composeServiceApi(
2020
mockTransferHandler,
21-
async input => defaultRequest,
21+
input => defaultRequest,
2222
async output => ({
2323
Result: 'from API',
2424
}),

packages/core/__tests__/clients/retry-middleware-test.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { HttpResponse, MiddlewareHandler } from '../../src/clients/types';
22
import { composeTransferHandler } from '../../src/clients/internal/composeTransferHandler';
3-
import { retry, RetryOptions } from '../../src/clients/middleware/retry';
3+
import {
4+
retryMiddleware,
5+
RetryOptions,
6+
} from '../../src/clients/middleware/retry';
47

58
jest.spyOn(global, 'setTimeout');
69
jest.spyOn(global, 'clearTimeout');
710

8-
describe(`${retry.name} middleware`, () => {
11+
describe(`${retryMiddleware.name} middleware`, () => {
912
beforeEach(() => {
1013
jest.clearAllMocks();
1114
});
1215

1316
const defaultRetryOptions = {
14-
retryDecider: () => true,
17+
retryDecider: async () => true,
1518
computeDelay: () => 1,
1619
};
1720
const defaultRequest = { url: new URL('https://a.b') };
@@ -21,7 +24,7 @@ describe(`${retry.name} middleware`, () => {
2124
headers: {},
2225
};
2326
const getRetryableHandler = (nextHandler: MiddlewareHandler<any, any>) =>
24-
composeTransferHandler<[RetryOptions]>(nextHandler, [retry]);
27+
composeTransferHandler<[RetryOptions]>(nextHandler, [retryMiddleware]);
2528

2629
test('should retry specified times', async () => {
2730
const nextHandler = jest.fn().mockResolvedValue(defaultResponse);
@@ -122,7 +125,7 @@ describe(`${retry.name} middleware`, () => {
122125
const nextHandler = jest.fn().mockResolvedValue(defaultResponse);
123126
const retryableHandler = getRetryableHandler(nextHandler);
124127
const controller = new AbortController();
125-
const retryDecider = () => true;
128+
const retryDecider = async () => true;
126129
const computeDelay = jest.fn().mockImplementation(attempt => {
127130
if (attempt === 1) {
128131
setTimeout(() => controller.abort(), 100);
@@ -161,7 +164,7 @@ describe(`${retry.name} middleware`, () => {
161164

162165
const doubleRetryableHandler = composeTransferHandler<
163166
[RetryOptions, {}, RetryOptions]
164-
>(coreHandler, [retry, betweenRetryMiddleware, retry]);
167+
>(coreHandler, [retryMiddleware, betweenRetryMiddleware, retryMiddleware]);
165168

166169
const retryDecider = jest
167170
.fn()

packages/core/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@
100100
"name": "Core (Credentials)",
101101
"path": "./lib-esm/index.js",
102102
"import": "{ Credentials }",
103-
"limit": "11.5 kB"
103+
"limit": "11.51 kB"
104104
},
105105
{
106106
"name": "Custom clients (retry middleware)",
107107
"path": "./lib-esm/clients/middleware/retry/middleware.js",
108-
"import": "{ retry }",
108+
"import": "{ retryMiddleware }",
109109
"limit": "1.24 kB"
110110
},
111111
{
@@ -117,7 +117,7 @@
117117
{
118118
"name": "Custom clients (unauthenticated handler)",
119119
"path": "./lib-esm/clients/handlers/unauth.js",
120-
"import": "{ unAuthenticatedHandler }",
120+
"import": "{ unauthenticatedHandler }",
121121
"limit": "2.75 kB"
122122
}
123123
],
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,83 @@
1-
import { Middleware, HttpRequest, HttpResponse } from '../../clients/types';
1+
import type {
2+
Endpoint,
3+
Headers,
4+
HttpRequest,
5+
HttpResponse,
6+
Middleware,
7+
} from '../../clients/types';
28
import { composeTransferHandler } from '../../clients/internal/composeTransferHandler';
3-
import { unAuthenticatedHandler } from '../../clients/handlers/unauth';
9+
import { unauthenticatedHandler } from '../../clients/handlers/unauth';
410
import {
511
jitteredBackoff,
612
getRetryDecider,
713
} from '../../clients/middleware/retry';
8-
import { loadErrorCode } from '../../clients/serde/json';
14+
import { loadJsonErrorCode } from '../../clients/serde/json';
15+
16+
/**
17+
* The service name used to sign requests if the API requires authentication.
18+
*/
19+
const SERVICE_NAME = 'cognito-identity';
920

21+
/**
22+
* The endpoint resolver function that returns the endpoint URL for a given region.
23+
*/
24+
const endpointResolver = (endpointOptions: { region: string }) => ({
25+
url: new URL(
26+
`https://cognito-identity.${endpointOptions.region}.amazonaws.com`
27+
),
28+
});
29+
30+
/**
31+
* A Cognito Identity-specific middleware that disables caching for all requests.
32+
*/
1033
const disableCacheMiddleware: Middleware<HttpRequest, HttpResponse, {}> =
1134
() => (next, context) =>
1235
async function disableCacheMiddleware(request) {
1336
request.headers['cache-control'] = 'no-store';
1437
return next(request);
1538
};
1639

40+
/**
41+
* A Cognito Identity-specific transfer handler that does NOT sign requests, and
42+
* disables caching.
43+
*
44+
* @internal
45+
*/
1746
export const cognitoIdentityTransferHandler = composeTransferHandler<
18-
[unknown],
47+
[Parameters<typeof disableCacheMiddleware>[0]],
1948
HttpRequest,
2049
HttpResponse,
21-
typeof unAuthenticatedHandler
22-
>(unAuthenticatedHandler, [disableCacheMiddleware]);
50+
typeof unauthenticatedHandler
51+
>(unauthenticatedHandler, [disableCacheMiddleware]);
2352

53+
/**
54+
* @internal
55+
*/
2456
export const defaultConfigs = {
25-
service: 'cognito-identity',
26-
endpointResolver: (endpointOptions: { region: string }) => ({
27-
url: new URL(
28-
`https://cognito-identity.${endpointOptions.region}.amazonaws.com`
29-
),
30-
}),
31-
retryDecider: getRetryDecider(loadErrorCode),
57+
service: SERVICE_NAME,
58+
endpointResolver,
59+
retryDecider: getRetryDecider(loadJsonErrorCode),
3260
computeDelay: jitteredBackoff,
3361
};
62+
63+
/**
64+
* @internal
65+
*/
66+
export const sharedHeaders = (operation: string): Headers => ({
67+
'content-type': 'application/x-amz-json-1.1',
68+
'x-amz-target': `AWSCognitoIdentityService.${operation}`,
69+
});
70+
71+
/**
72+
* @internal
73+
*/
74+
export const buildHttpRpcRequest = (
75+
{ url }: Endpoint,
76+
headers: Headers,
77+
body: any
78+
): HttpRequest => ({
79+
headers,
80+
url,
81+
body,
82+
method: 'POST',
83+
});

packages/core/src/AwsClients/CognitoIdentity/getCredentialsForIdentity.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,37 @@ import type {
22
GetCredentialsForIdentityCommandInput,
33
GetCredentialsForIdentityCommandOutput,
44
} from '@aws-sdk/client-cognito-identity';
5-
import { cognitoIdentityTransferHandler, defaultConfigs } from './base';
5+
import {
6+
buildHttpRpcRequest,
7+
cognitoIdentityTransferHandler,
8+
defaultConfigs,
9+
sharedHeaders,
10+
} from './base';
611
import { composeServiceApi } from '../../clients/internal/composeApiHandler';
712
import { Endpoint, HttpRequest, HttpResponse } from '../../clients/types';
8-
import { parseBody, throwError } from '../../clients/serde';
13+
import { parseJsonBody, throwJsonError } from '../../clients/serde';
914

1015
export type {
1116
GetCredentialsForIdentityCommandInput,
1217
GetCredentialsForIdentityCommandOutput,
1318
} from '@aws-sdk/client-cognito-identity';
1419

15-
const getCredentialsForIdentitySerializer = async (
20+
const getCredentialsForIdentitySerializer = (
1621
input: GetCredentialsForIdentityCommandInput,
1722
endpoint: Endpoint
18-
): Promise<HttpRequest> => {
19-
return {
20-
headers: {
21-
'content-type': 'application/x-amz-json-1.1',
22-
'x-amz-target': 'AWSCognitoIdentityService.GetCredentialsForIdentity',
23-
},
24-
method: 'POST',
25-
url: endpoint.url,
26-
body: JSON.stringify(input),
27-
};
23+
): HttpRequest => {
24+
const headers = sharedHeaders('GetCredentialsForIdentity');
25+
const body = JSON.stringify(input);
26+
return buildHttpRpcRequest(endpoint, headers, body);
2827
};
2928

3029
const getCredentialsForIdentityDeserializer = async (
3130
response: HttpResponse
3231
): Promise<GetCredentialsForIdentityCommandOutput> => {
3332
if (response.statusCode >= 300) {
34-
throw await throwError(response);
33+
await throwJsonError(response);
3534
} else {
36-
const body = await parseBody(response);
35+
const body = await parseJsonBody(response);
3736
return {
3837
...body,
3938
Credentials: {

packages/core/src/AwsClients/CognitoIdentity/getId.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,37 @@ import type {
22
GetIdCommandInput,
33
GetIdCommandOutput,
44
} from '@aws-sdk/client-cognito-identity';
5-
import { cognitoIdentityTransferHandler, defaultConfigs } from './base';
5+
import {
6+
buildHttpRpcRequest,
7+
cognitoIdentityTransferHandler,
8+
defaultConfigs,
9+
sharedHeaders,
10+
} from './base';
611
import { composeServiceApi } from '../../clients/internal/composeApiHandler';
712
import { Endpoint, HttpRequest, HttpResponse } from '../../clients/types';
8-
import { parseBody, throwError } from '../../clients/serde';
13+
import { parseJsonBody, throwJsonError } from '../../clients/serde';
914

1015
export type {
1116
GetIdCommandInput,
1217
GetIdCommandOutput,
1318
} from '@aws-sdk/client-cognito-identity';
1419

15-
const getIdSerializer = async (
20+
const getIdSerializer = (
1621
input: GetIdCommandInput,
1722
endpoint: Endpoint
18-
): Promise<HttpRequest> => {
19-
return {
20-
headers: {
21-
'content-type': 'application/x-amz-json-1.1',
22-
'x-amz-target': 'AWSCognitoIdentityService.GetId',
23-
},
24-
method: 'POST',
25-
url: endpoint.url,
26-
body: JSON.stringify(input),
27-
};
23+
): HttpRequest => {
24+
const headers = sharedHeaders('GetId');
25+
const body = JSON.stringify(input);
26+
return buildHttpRpcRequest(endpoint, headers, body);
2827
};
2928

3029
const getIdDeserializer = async (
3130
response: HttpResponse
3231
): Promise<GetIdCommandOutput> => {
3332
if (response.statusCode >= 300) {
34-
throw await throwError(response);
33+
await throwJsonError(response);
3534
} else {
36-
const body = await parseBody(response);
35+
const body = await parseJsonBody(response);
3736
return body as GetIdCommandOutput;
3837
}
3938
};
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
export * from './getId';
2-
export * from './getCredentialsForIdentity';
1+
export { getId, GetIdCommandInput, GetIdCommandOutput } from './getId';
2+
export {
3+
getCredentialsForIdentity,
4+
GetCredentialsForIdentityCommandInput,
5+
GetCredentialsForIdentityCommandOutput,
6+
} from './getCredentialsForIdentity';

packages/core/src/Credentials.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export class CredentialsClass {
280280

281281
const identityId = (this._identityId = await this._getGuestIdentityId());
282282

283-
const cognitoConfig = { region: identityPoolRegion || region };
283+
const cognitoConfig = { region: identityPoolRegion ?? region };
284284

285285
const credentialsProvider = async () => {
286286
if (!identityId) {
@@ -381,7 +381,7 @@ export class CredentialsClass {
381381
);
382382
}
383383

384-
const cognitoConfig = { region: identityPoolRegion || region };
384+
const cognitoConfig = { region: identityPoolRegion ?? region };
385385

386386
const credentialsProvider = async () => {
387387
if (!identity_id) {
@@ -430,7 +430,7 @@ export class CredentialsClass {
430430
const logins = {};
431431
logins[key] = idToken;
432432

433-
const cognitoConfig = { region: identityPoolRegion || region };
433+
const cognitoConfig = { region: identityPoolRegion ?? region };
434434

435435
/*
436436
Retreiving identityId with GetIdCommand to mimic the behavior in the following code in aws-sdk-v3:
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { retry, RetryOptions } from '../middleware/retry';
1+
import { retryMiddleware, RetryOptions } from '../middleware/retry';
2+
import { userAgentMiddleware, UserAgentOptions } from '../middleware/userAgent';
23
import { composeTransferHandler } from '../internal/composeTransferHandler';
34
import { fetchTransferHandler } from './fetch';
45
import { HttpRequest, HttpResponse } from '../types';
5-
import { userAgent, UserAgentOptions } from '../middleware/user-agent';
66

7-
export const unAuthenticatedHandler = composeTransferHandler<
7+
export const unauthenticatedHandler = composeTransferHandler<
88
[UserAgentOptions, RetryOptions<HttpResponse>],
99
HttpRequest,
1010
HttpResponse
11-
>(fetchTransferHandler, [userAgent, retry]);
11+
>(fetchTransferHandler, [userAgentMiddleware, retryMiddleware]);

0 commit comments

Comments
 (0)