File tree Expand file tree Collapse file tree 5 files changed +31
-3
lines changed Expand file tree Collapse file tree 5 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -179,4 +179,15 @@ type Ret = FetchReturnType<typeof findPetsByStatus>
179179type Err = FetchErrorType <typeof findPetsByStatus >
180180` ` `
181181
182+ ### Utility Methods
183+
184+ - ` arrayRequestBody ` - Helper to merge params when request body is an array
185+
186+ ` ` ` ts
187+
188+ const body = arrayRequestBody ([{ item: 1 }], { param: 2 })
189+
190+ // body type is { item: number }[] & { param: number }
191+ ```
192+
182193Happy fetching! 👍
Original file line number Diff line number Diff line change 11import { Fetcher } from './fetcher'
2+ import { arrayRequestBody } from './utils'
23
34import type {
45 ApiResponse ,
@@ -28,4 +29,4 @@ export type {
2829 TypedFetch ,
2930}
3031
31- export { Fetcher , ApiError }
32+ export { Fetcher , ApiError , arrayRequestBody }
Original file line number Diff line number Diff line change 1+ /**
2+ * Helper to merge params when request body is an array
3+ */
4+ export function arrayRequestBody < T , O > ( array : T [ ] , params ?: O ) : T [ ] & O {
5+ return Object . assign ( [ ...array ] , params )
6+ }
Original file line number Diff line number Diff line change 11import 'whatwg-fetch'
22
33import { server } from './mocks/server'
4- import { ApiError , Fetcher } from '../src'
4+ import { ApiError , arrayRequestBody , Fetcher } from '../src'
55import { Data , paths } from './paths'
66
77beforeAll ( ( ) => server . listen ( ) )
@@ -68,7 +68,7 @@ describe('fetch', () => {
6868 it ( `${ method . toUpperCase ( ) } /bodyarray/{id}` , async ( ) => {
6969 const fun = fetcher . path ( '/bodyarray/{id}' ) . method ( method ) . create ( )
7070
71- const { data } = await fun ( Object . assign ( [ 'b' , 'c' ] , { id : 1 } ) )
71+ const { data } = await fun ( arrayRequestBody ( [ 'b' , 'c' ] , { id : 1 } ) )
7272
7373 expect ( data . params ) . toEqual ( { id : '1' } )
7474 expect ( data . body ) . toEqual ( [ 'b' , 'c' ] )
Original file line number Diff line number Diff line change 1+ import { arrayRequestBody } from '../src'
2+
3+ describe ( 'utils' , ( ) => {
4+ it ( 'array request body with params' , ( ) => {
5+ const body = arrayRequestBody ( [ { item : 2 } ] , { param : 3 } )
6+ expect ( body . length ) . toEqual ( 1 )
7+ expect ( body [ 0 ] . item ) . toEqual ( 2 )
8+ expect ( body . param ) . toEqual ( 3 )
9+ } )
10+ } )
You can’t perform that action at this time.
0 commit comments