Skip to content

Commit fa206e1

Browse files
authored
release(required): fix retryStrategy parameter location (#14321)
1 parent 0f68139 commit fa206e1

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

packages/api-rest/__tests__/apis/common/publicApis.test.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ const mockSuccessResponse = {
7878
text: jest.fn(),
7979
},
8080
};
81-
const mockGetRetryDecider = getRetryDecider as jest.Mock;
82-
const mockRetryResponse = { retryable: true };
83-
const mockNoRetryResponse = { retryable: false };
84-
const mockRetryDeciderResponse = () => Promise.resolve(mockRetryResponse);
81+
const mockGetRetryDecider = jest.mocked(getRetryDecider);
82+
const mockRetryDeciderResponse = () => Promise.resolve({ retryable: true });
8583

8684
describe('public APIs', () => {
8785
beforeEach(() => {
@@ -431,45 +429,51 @@ describe('public APIs', () => {
431429
});
432430

433431
it('should not retry when retry is set to "no-retry"', async () => {
434-
expect.assertions(2);
432+
expect.assertions(3);
435433
await fn(mockAmplifyInstance, {
436434
apiName: 'restApi1',
437435
path: '/items',
438-
retryStrategy: {
439-
strategy: 'no-retry',
436+
options: {
437+
retryStrategy: {
438+
strategy: 'no-retry',
439+
},
440440
},
441441
}).response;
442442
expect(mockAuthenticatedHandler).toHaveBeenCalledWith(
443443
expect.any(Object),
444444
expect.objectContaining({ retryDecider: expect.any(Function) }),
445445
);
446446
const callArgs = mockAuthenticatedHandler.mock.calls[0];
447+
expect(mockGetRetryDecider).not.toHaveBeenCalled();
447448
const { retryDecider } = callArgs[1];
448449
const result = await retryDecider();
449-
expect(result).toEqual(mockNoRetryResponse);
450+
expect(result).toEqual({ retryable: false });
450451
});
451452

452453
it('should retry when retry is set to "jittered-exponential-backoff"', async () => {
453-
expect.assertions(2);
454+
expect.assertions(3);
454455
await fn(mockAmplifyInstance, {
455456
apiName: 'restApi1',
456457
path: '/items',
457-
retryStrategy: {
458-
strategy: 'jittered-exponential-backoff',
458+
options: {
459+
retryStrategy: {
460+
strategy: 'jittered-exponential-backoff',
461+
},
459462
},
460463
}).response;
461464
expect(mockAuthenticatedHandler).toHaveBeenCalledWith(
462465
expect.any(Object),
463466
expect.objectContaining({ retryDecider: expect.any(Function) }),
464467
);
465468
const callArgs = mockAuthenticatedHandler.mock.calls[0];
469+
expect(mockGetRetryDecider).toHaveBeenCalled();
466470
const { retryDecider } = callArgs[1];
467471
const result = await retryDecider();
468-
expect(result).toEqual(mockRetryResponse);
472+
expect(result).toEqual({ retryable: true });
469473
});
470474

471475
it('should retry when retry strategy is not provided', async () => {
472-
expect.assertions(2);
476+
expect.assertions(3);
473477
await fn(mockAmplifyInstance, {
474478
apiName: 'restApi1',
475479
path: '/items',
@@ -479,13 +483,14 @@ describe('public APIs', () => {
479483
expect.objectContaining({ retryDecider: expect.any(Function) }),
480484
);
481485
const callArgs = mockAuthenticatedHandler.mock.calls[0];
486+
expect(mockGetRetryDecider).toHaveBeenCalled();
482487
const { retryDecider } = callArgs[1];
483488
const result = await retryDecider();
484-
expect(result).toEqual(mockRetryResponse);
489+
expect(result).toEqual({ retryable: true });
485490
});
486491

487492
it('should retry and prefer the individual retry strategy over the library options', async () => {
488-
expect.assertions(2);
493+
expect.assertions(3);
489494
const mockAmplifyInstanceWithNoRetry = {
490495
...mockAmplifyInstance,
491496
libraryOptions: {
@@ -501,8 +506,10 @@ describe('public APIs', () => {
501506
await fn(mockAmplifyInstanceWithNoRetry, {
502507
apiName: 'restApi1',
503508
path: 'items',
504-
retryStrategy: {
505-
strategy: 'jittered-exponential-backoff',
509+
options: {
510+
retryStrategy: {
511+
strategy: 'jittered-exponential-backoff',
512+
},
506513
},
507514
}).response;
508515

@@ -511,13 +518,14 @@ describe('public APIs', () => {
511518
expect.objectContaining({ retryDecider: expect.any(Function) }),
512519
);
513520
const callArgs = mockAuthenticatedHandler.mock.calls[0];
521+
expect(mockGetRetryDecider).toHaveBeenCalled();
514522
const { retryDecider } = callArgs[1];
515523
const result = await retryDecider();
516-
expect(result).toEqual(mockRetryResponse);
524+
expect(result).toEqual({ retryable: true });
517525
});
518526

519527
it('should not retry and prefer the individual retry strategy over the library options', async () => {
520-
expect.assertions(2);
528+
expect.assertions(3);
521529
const mockAmplifyInstanceWithRetry = {
522530
...mockAmplifyInstance,
523531
libraryOptions: {
@@ -533,8 +541,10 @@ describe('public APIs', () => {
533541
await fn(mockAmplifyInstanceWithRetry, {
534542
apiName: 'restApi1',
535543
path: 'items',
536-
retryStrategy: {
537-
strategy: 'no-retry',
544+
options: {
545+
retryStrategy: {
546+
strategy: 'no-retry',
547+
},
538548
},
539549
}).response;
540550

@@ -543,13 +553,14 @@ describe('public APIs', () => {
543553
expect.objectContaining({ retryDecider: expect.any(Function) }),
544554
);
545555
const callArgs = mockAuthenticatedHandler.mock.calls[0];
556+
expect(mockGetRetryDecider).not.toHaveBeenCalled();
546557
const { retryDecider } = callArgs[1];
547558
const result = await retryDecider();
548-
expect(result).toEqual(mockNoRetryResponse);
559+
expect(result).toEqual({ retryable: false });
549560
});
550561

551562
it('should not retry when configured through library options', async () => {
552-
expect.assertions(2);
563+
expect.assertions(3);
553564
const mockAmplifyInstanceWithRetry = {
554565
...mockAmplifyInstance,
555566
libraryOptions: {
@@ -572,9 +583,10 @@ describe('public APIs', () => {
572583
expect.objectContaining({ retryDecider: expect.any(Function) }),
573584
);
574585
const callArgs = mockAuthenticatedHandler.mock.calls[0];
586+
expect(mockGetRetryDecider).not.toHaveBeenCalled();
575587
const { retryDecider } = callArgs[1];
576588
const result = await retryDecider();
577-
expect(result).toEqual(mockNoRetryResponse);
589+
expect(result).toEqual({ retryable: false });
578590
});
579591
});
580592
});

packages/api-rest/src/apis/common/publicApis.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ const publicHandler = (
3535
method: string,
3636
) =>
3737
createCancellableOperation(async abortSignal => {
38-
const {
39-
apiName,
40-
options: apiOptions = {},
41-
path: apiPath,
42-
retryStrategy,
43-
} = options;
38+
const { apiName, options: apiOptions = {}, path: apiPath } = options;
4439
const url = resolveApiUrl(
4540
amplify,
4641
apiName,
@@ -76,7 +71,6 @@ const publicHandler = (
7671
method,
7772
headers,
7873
abortSignal,
79-
retryStrategy,
8074
},
8175
isIamAuthApplicableForRest,
8276
signingServiceInfo,

packages/api-rest/src/types/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export interface RestApiOptionsBase {
3535
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials}
3636
*/
3737
withCredentials?: boolean;
38+
/**
39+
* Retry strategy for the REST API calls. It will take precedence over REST `retryStrategy` in Amplify configuration libraryOptions.
40+
*
41+
* @default ` { strategy: 'jittered-exponential-backoff' } `
42+
*/
43+
retryStrategy?: RetryStrategy;
3844
}
3945

4046
type Headers = Record<string, string>;
@@ -82,12 +88,6 @@ export interface ApiInput<Options> {
8288
* Options to overwrite the REST API call behavior.
8389
*/
8490
options?: Options;
85-
/**
86-
* Retry strategy for the REST API calls. It will take precedence over REST `retryStrategy` in Amplify configuration libraryOptions.
87-
*
88-
* @default ` { strategy: 'jittered-exponential-backoff' } `
89-
*/
90-
retryStrategy?: RetryStrategy;
9191
}
9292

9393
/**
@@ -97,7 +97,7 @@ export interface ApiInput<Options> {
9797
export interface InternalPostInput {
9898
// Resolved GraphQl endpoint url
9999
url: URL;
100-
options?: RestApiOptionsBase & {
100+
options?: Omit<RestApiOptionsBase, 'retryStrategy'> & {
101101
/**
102102
* Internal-only option for GraphQL client to provide the IAM signing service and region.
103103
* * If auth mode is 'iam', you MUST set this value.

0 commit comments

Comments
 (0)