File tree 5 files changed +31
-3
lines changed 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>
179
179
type Err = FetchErrorType <typeof findPetsByStatus >
180
180
` ` `
181
181
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
+
182
193
Happy fetching! 👍
Original file line number Diff line number Diff line change 1
1
import { Fetcher } from './fetcher'
2
+ import { arrayRequestBody } from './utils'
2
3
3
4
import type {
4
5
ApiResponse ,
@@ -28,4 +29,4 @@ export type {
28
29
TypedFetch ,
29
30
}
30
31
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 1
1
import 'whatwg-fetch'
2
2
3
3
import { server } from './mocks/server'
4
- import { ApiError , Fetcher } from '../src'
4
+ import { ApiError , arrayRequestBody , Fetcher } from '../src'
5
5
import { Data , paths } from './paths'
6
6
7
7
beforeAll ( ( ) => server . listen ( ) )
@@ -68,7 +68,7 @@ describe('fetch', () => {
68
68
it ( `${ method . toUpperCase ( ) } /bodyarray/{id}` , async ( ) => {
69
69
const fun = fetcher . path ( '/bodyarray/{id}' ) . method ( method ) . create ( )
70
70
71
- const { data } = await fun ( Object . assign ( [ 'b' , 'c' ] , { id : 1 } ) )
71
+ const { data } = await fun ( arrayRequestBody ( [ 'b' , 'c' ] , { id : 1 } ) )
72
72
73
73
expect ( data . params ) . toEqual ( { id : '1' } )
74
74
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