Skip to content
Myr

fix(client): skip undefined values in form data serialization (#4732)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #4731

authored by Aiden Liu · committed by GitHub · parent 11a1942 · Browse files · 2 files+39 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+39 added−0 removed

  • tests:+36 added−0 removed
  • other:+3 added−0 removed

2 files+39 added−0 removed

modified:src/client/client.test.ts+36 added
445 unmodified lines
446 expect(await res.json()).toEqual(['foo', 'bar'])447 })448})449450describe('Form - Undefined Values', () => {451 const server = setupServer(452 http.post('http://localhost/form-undefined', async ({ request }) => {453 const data = await request.formData()454 return HttpResponse.json({455 keys: [...data.keys()],456 title: data.get('title'),457 optional: data.get('optional'),458 })459 })460 )461462 beforeAll(() => server.listen())463 afterEach(() => server.resetHandlers())464 afterAll(() => server.close())465466 const client = hc('http://localhost/')467468 it('Should skip undefined values in form data', async () => {469 // @ts-expect-error `client['form-undefined'].$post` is not typed470 const res = await client['form-undefined'].$post({471 form: {472 title: 'Hello',473 optional: undefined,474 },475 })476 expect(res.status).toBe(200)477 const json = await res.json()478 expect(json).toEqual({479 keys: ['title'],480 title: 'Hello',481 optional: null,482 })483 })484})485486describe('Infer the response/request type', () => {487 const app = new Hono()488 const route = app.get(489 '/',
1152 unmodified lines
modified:src/client/client.ts+3 added
62 unmodified lines
6364 if (args.form) {65 const form = new FormData()66 for (const [k, v] of Object.entries(args.form)) {67 if (v === undefined) {68 continue69 }70 if (Array.isArray(v)) {71 for (const v2 of v) {72 form.append(k, v2)73 }
147 unmodified lines