Skip to content

Commit d6f1422

Browse files
authored
feat(api-rest): support "body" in delete API (#14462)
* feat(api-rest): support "body" in delete API
1 parent e5a8569 commit d6f1422

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,36 @@ describe('public APIs', () => {
192192
);
193193
});
194194

195+
if (!['HEAD'].includes(method.toUpperCase())) {
196+
it('should support body', async () => {
197+
await fn(mockAmplifyInstance, {
198+
apiName: 'restApi1',
199+
path: '/items',
200+
options: {
201+
body: {
202+
message: 'body',
203+
},
204+
},
205+
}).response;
206+
expect(mockAuthenticatedHandler).toHaveBeenCalledWith(
207+
{
208+
url: new URL(
209+
'https://123.execute-api.us-west-2.amazonaws.com/development/items',
210+
),
211+
method,
212+
headers: {
213+
'content-type': 'application/json; charset=UTF-8',
214+
},
215+
body: '{"message":"body"}',
216+
},
217+
expect.objectContaining({
218+
region: 'us-west-2',
219+
service: 'execute-api',
220+
}),
221+
);
222+
});
223+
}
224+
195225
it('should support path parameters', async () => {
196226
await fn(mockAmplifyInstance, {
197227
apiName: 'restApi1',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ export type GetInput = ApiInput<RestApiOptionsBase>;
66
export type PostInput = ApiInput<RestApiOptionsBase>;
77
export type PutInput = ApiInput<RestApiOptionsBase>;
88
export type PatchInput = ApiInput<RestApiOptionsBase>;
9-
export type DeleteInput = ApiInput<Omit<RestApiOptionsBase, 'body'>>;
9+
export type DeleteInput = ApiInput<RestApiOptionsBase>;
1010
export type HeadInput = ApiInput<Omit<RestApiOptionsBase, 'body'>>;
1111

1212
export type GetOperation = Operation<RestApiResponse>;
1313
export type PostOperation = Operation<RestApiResponse>;
1414
export type PutOperation = Operation<RestApiResponse>;
1515
export type PatchOperation = Operation<RestApiResponse>;
16-
export type DeleteOperation = Operation<Omit<RestApiResponse, 'body'>>;
16+
export type DeleteOperation = Operation<RestApiResponse>;
1717
export type HeadOperation = Operation<Omit<RestApiResponse, 'body'>>;
1818

1919
/**

packages/core/__tests__/clients/fetch.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,27 @@ describe(fetchTransferHandler.name, () => {
108108
expect(mockBody.json).toHaveBeenCalledTimes(1); // test caching
109109
});
110110

111-
test.each(['GET', 'HEAD', 'DELETE'])(
111+
test.each(['GET', 'HEAD'])(
112112
'should ignore request payload for %s request',
113113
async method => {
114114
await fetchTransferHandler(
115115
{ ...mockRequest, method, body: 'Mock Body' },
116116
{},
117117
);
118118
expect(mockFetch).toHaveBeenCalledTimes(1);
119-
expect(mockFetch.mock.calls[0][0].body).toBeUndefined();
119+
expect(mockFetch.mock.calls[0][1].body).toBeUndefined();
120+
},
121+
);
122+
123+
test.each(['POST', 'PUT', 'DELETE', 'PATCH'])(
124+
'should include request payload for %s request',
125+
async method => {
126+
await fetchTransferHandler(
127+
{ ...mockRequest, method, body: 'Mock Body' },
128+
{},
129+
);
130+
expect(mockFetch).toHaveBeenCalledTimes(1);
131+
expect(mockFetch.mock.calls[0][1].body).toBe('Mock Body');
120132
},
121133
);
122134
});

packages/core/src/clients/handlers/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { withMemoization } from '../utils/memoization';
88
import { AmplifyErrorCode } from '../../types';
99

1010
const shouldSendBody = (method: string) =>
11-
!['HEAD', 'GET', 'DELETE'].includes(method.toUpperCase());
11+
!['HEAD', 'GET'].includes(method.toUpperCase());
1212

1313
// TODO[AllanZhengYP]: we need to provide isCanceledError utility
1414
export const fetchTransferHandler: TransferHandler<

0 commit comments

Comments
 (0)