Skip to content

Commit cb02220

Browse files
authored
Merge pull request #3 from Tam2/fix/patch-put-bare-body-fallback
fix(runtime): forward a bare body on PATCH/PUT like POST
2 parents 1c0e745 + 5bc4d0b commit cb02220

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sveltekit-openapi-remote",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "Generate SvelteKit remote functions directly from OpenAPI/Swagger specs.",
55
"type": "module",
66
"license": "MIT",

src/runtime/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,23 @@ export function createRemoteHandlers(client: OpenapiClient) {
4343
async function handlePatchCommand(path: string, input: any) {
4444
const hasPath = input && typeof input === 'object' && 'path' in input;
4545
const hasBody = input && typeof input === 'object' && 'body' in input;
46+
// For a no-path endpoint the generated wrapper passes the body directly (no `body` key), so fall
47+
// back to the whole input as the body — matching handlePostCommand. Without this, a bare body was
48+
// silently dropped, sending an empty PATCH.
4649
const { data, error, response } = await client.PATCH(path, {
4750
...(hasPath && { params: { path: input.path } }),
48-
...(hasBody && { body: input.body }),
51+
body: hasBody ? input.body : input,
4952
});
5053
return handleResponse(response, error, data);
5154
}
5255

5356
async function handlePutCommand(path: string, input: any) {
5457
const hasPath = input && typeof input === 'object' && 'path' in input;
5558
const hasBody = input && typeof input === 'object' && 'body' in input;
59+
// Same bare-body fallback as handlePostCommand/handlePatchCommand.
5660
const { data, error, response } = await client.PUT(path, {
5761
...(hasPath && { params: { path: input.path } }),
58-
...(hasBody && { body: input.body }),
62+
body: hasBody ? input.body : input,
5963
});
6064
return handleResponse(response, error, data);
6165
}

test/runtime.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ describe('createRemoteHandlers', () => {
117117
});
118118
expect(result).toEqual({ updated: true });
119119
});
120+
121+
it('treats a bare body (no path/body key) as the request body, like POST', async () => {
122+
// A no-path endpoint's generated wrapper passes the body directly (e.g. patchUsersCommand({...})).
123+
// Regression: handlePatchCommand used to only forward input.body, silently dropping a bare body.
124+
const client = createMockClient();
125+
client.PATCH.mockResolvedValue({
126+
data: { updated: true },
127+
error: undefined,
128+
response: { ok: true, status: 200 },
129+
});
130+
const { handlePatchCommand } = createRemoteHandlers(client as any);
131+
const result = await handlePatchCommand('/users', { firstName: 'Tam', lastName: 'M' });
132+
expect(client.PATCH).toHaveBeenCalledWith('/users', {
133+
body: { firstName: 'Tam', lastName: 'M' },
134+
});
135+
expect(result).toEqual({ updated: true });
136+
});
120137
});
121138

122139
describe('handlePutCommand', () => {
@@ -138,6 +155,21 @@ describe('createRemoteHandlers', () => {
138155
});
139156
expect(result).toEqual({ id: 1, name: 'Replaced' });
140157
});
158+
159+
it('treats a bare body (no path/body key) as the request body, like POST', async () => {
160+
const client = createMockClient();
161+
client.PUT.mockResolvedValue({
162+
data: { replaced: true },
163+
error: undefined,
164+
response: { ok: true, status: 200 },
165+
});
166+
const { handlePutCommand } = createRemoteHandlers(client as any);
167+
const result = await handlePutCommand('/settings', { theme: 'dark' });
168+
expect(client.PUT).toHaveBeenCalledWith('/settings', {
169+
body: { theme: 'dark' },
170+
});
171+
expect(result).toEqual({ replaced: true });
172+
});
141173
});
142174

143175
describe('handleDeleteCommand', () => {

0 commit comments

Comments
 (0)