Skip to content

feat(api-rest): support "body" in delete API #14462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/api-rest/__tests__/apis/common/publicApis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions packages/api-rest/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export type GetInput = ApiInput<RestApiOptionsBase>;
export type PostInput = ApiInput<RestApiOptionsBase>;
export type PutInput = ApiInput<RestApiOptionsBase>;
export type PatchInput = ApiInput<RestApiOptionsBase>;
export type DeleteInput = ApiInput<Omit<RestApiOptionsBase, 'body'>>;
export type DeleteInput = ApiInput<RestApiOptionsBase>;
export type HeadInput = ApiInput<Omit<RestApiOptionsBase, 'body'>>;

export type GetOperation = Operation<RestApiResponse>;
export type PostOperation = Operation<RestApiResponse>;
export type PutOperation = Operation<RestApiResponse>;
export type PatchOperation = Operation<RestApiResponse>;
export type DeleteOperation = Operation<Omit<RestApiResponse, 'body'>>;
export type DeleteOperation = Operation<RestApiResponse>;
export type HeadOperation = Operation<Omit<RestApiResponse, 'body'>>;

/**
Expand Down
16 changes: 14 additions & 2 deletions packages/core/__tests__/clients/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,27 @@ 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(
{ ...mockRequest, method, body: 'Mock Body' },
{},
);
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');
},
);
});
2 changes: 1 addition & 1 deletion packages/core/src/clients/handlers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
Loading