diff --git a/packages/api-rest/__tests__/apis/common/publicApis.test.ts b/packages/api-rest/__tests__/apis/common/publicApis.test.ts index 58fa393e4dc..014e12e9b30 100644 --- a/packages/api-rest/__tests__/apis/common/publicApis.test.ts +++ b/packages/api-rest/__tests__/apis/common/publicApis.test.ts @@ -192,6 +192,36 @@ describe('public APIs', () => { ); }); + if (!['HEAD'].includes(method.toUpperCase())) { + it('should support body', async () => { + await fn(mockAmplifyInstance, { + apiName: 'restApi1', + path: '/items', + options: { + body: { + message: 'body', + }, + }, + }).response; + expect(mockAuthenticatedHandler).toHaveBeenCalledWith( + { + url: new URL( + 'https://123.execute-api.us-west-2.amazonaws.com/development/items', + ), + method, + headers: { + 'content-type': 'application/json; charset=UTF-8', + }, + body: '{"message":"body"}', + }, + expect.objectContaining({ + region: 'us-west-2', + service: 'execute-api', + }), + ); + }); + } + it('should support path parameters', async () => { await fn(mockAmplifyInstance, { apiName: 'restApi1', diff --git a/packages/api-rest/src/types/index.ts b/packages/api-rest/src/types/index.ts index 3949925f008..f011f717a94 100644 --- a/packages/api-rest/src/types/index.ts +++ b/packages/api-rest/src/types/index.ts @@ -6,14 +6,14 @@ export type GetInput = ApiInput; export type PostInput = ApiInput; export type PutInput = ApiInput; export type PatchInput = ApiInput; -export type DeleteInput = ApiInput>; +export type DeleteInput = ApiInput; export type HeadInput = ApiInput>; export type GetOperation = Operation; export type PostOperation = Operation; export type PutOperation = Operation; export type PatchOperation = Operation; -export type DeleteOperation = Operation>; +export type DeleteOperation = Operation; export type HeadOperation = Operation>; /** diff --git a/packages/core/__tests__/clients/fetch.test.ts b/packages/core/__tests__/clients/fetch.test.ts index 0c271aae1d3..f0ba1c6b8a8 100644 --- a/packages/core/__tests__/clients/fetch.test.ts +++ b/packages/core/__tests__/clients/fetch.test.ts @@ -108,7 +108,7 @@ describe(fetchTransferHandler.name, () => { expect(mockBody.json).toHaveBeenCalledTimes(1); // test caching }); - test.each(['GET', 'HEAD', 'DELETE'])( + test.each(['GET', 'HEAD'])( 'should ignore request payload for %s request', async method => { await fetchTransferHandler( @@ -116,7 +116,19 @@ describe(fetchTransferHandler.name, () => { {}, ); expect(mockFetch).toHaveBeenCalledTimes(1); - expect(mockFetch.mock.calls[0][0].body).toBeUndefined(); + expect(mockFetch.mock.calls[0][1].body).toBeUndefined(); + }, + ); + + test.each(['POST', 'PUT', 'DELETE', 'PATCH'])( + 'should include request payload for %s request', + async method => { + await fetchTransferHandler( + { ...mockRequest, method, body: 'Mock Body' }, + {}, + ); + expect(mockFetch).toHaveBeenCalledTimes(1); + expect(mockFetch.mock.calls[0][1].body).toBe('Mock Body'); }, ); }); diff --git a/packages/core/src/clients/handlers/fetch.ts b/packages/core/src/clients/handlers/fetch.ts index 30a37f210e2..1e942342edb 100644 --- a/packages/core/src/clients/handlers/fetch.ts +++ b/packages/core/src/clients/handlers/fetch.ts @@ -8,7 +8,7 @@ import { withMemoization } from '../utils/memoization'; import { AmplifyErrorCode } from '../../types'; const shouldSendBody = (method: string) => - !['HEAD', 'GET', 'DELETE'].includes(method.toUpperCase()); + !['HEAD', 'GET'].includes(method.toUpperCase()); // TODO[AllanZhengYP]: we need to provide isCanceledError utility export const fetchTransferHandler: TransferHandler<