Skip to content

Commit f5f01ab

Browse files
committed
fix: top level array body with params
1 parent e2957cc commit f5f01ab

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

src/fetcher.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function getHeaders(init?: HeadersInit) {
8686
return headers
8787
}
8888

89-
function getBody(method: Method, payload: Record<string, unknown>) {
89+
function getBody(method: Method, payload: any) {
9090
const body = sendBody(method) ? JSON.stringify(payload) : undefined
9191
// if delete don't send body if empty
9292
return method === 'delete' && body === '{}' ? undefined : body
@@ -109,7 +109,14 @@ function mergeRequestInit(
109109
}
110110

111111
function getFetchParams(request: Request) {
112-
const payload = Array.isArray(request.payload) ? [...request.payload] : { ...request.payload }
112+
// clone payload
113+
// if body is a top level array [ 'a', 'b', param: value ] with param values
114+
// using spread [ ...payload ] returns [ 'a', 'b' ] and skips custom keys
115+
// cloning with Object.assign() preserves all keys
116+
const payload = Object.assign(
117+
Array.isArray(request.payload) ? [] : {},
118+
request.payload,
119+
)
113120

114121
const path = getPath(request.path, payload)
115122
const query = getQuery(request.method, payload, request.queryParams)

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export type Request = {
136136
method: Method
137137
path: string
138138
queryParams: string[] // even if a post these will be sent in query
139-
payload: Record<string, unknown>
139+
payload: any
140140
init?: RequestInit
141141
fetch: Fetch
142142
}

test/fetch.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ describe('fetch', () => {
6464
})
6565
})
6666

67+
methods.forEach((method) => {
68+
it(`${method.toUpperCase()} /bodyarray/{id}`, async () => {
69+
const fun = fetcher.path('/bodyarray/{id}').method(method).create()
70+
71+
const { data } = await fun(Object.assign(['b', 'c'], { id: 1 }))
72+
73+
expect(data.params).toEqual({ id: '1' })
74+
expect(data.body).toEqual(['b', 'c'])
75+
expect(data.query).toEqual({})
76+
expect(data.headers).toEqual(expectedHeaders)
77+
})
78+
})
79+
6780
methods.forEach((method) => {
6881
it(`${method.toUpperCase()} /bodyquery/{id}`, async () => {
6982
const fun = fetcher

test/mocks/handlers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const methods = {
4343
withBody: ['post', 'put', 'patch', 'delete'].map((method) => {
4444
return (rest as any)[method](`${HOST}/body/:id`, getResult)
4545
}),
46+
withBodyArray: ['post', 'put', 'patch', 'delete'].map((method) => {
47+
return (rest as any)[method](`${HOST}/bodyarray/:id`, getResult)
48+
}),
4649
withBodyAndQuery: ['post', 'put', 'patch', 'delete'].map((method) => {
4750
return (rest as any)[method](`${HOST}/bodyquery/:id`, getResult)
4851
}),
@@ -73,6 +76,7 @@ const methods = {
7376
export const handlers = [
7477
...methods.withQuery,
7578
...methods.withBody,
79+
...methods.withBodyArray,
7680
...methods.withBodyAndQuery,
7781
...methods.withError,
7882
]

test/paths.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export type Data = {
33
params: string[]
44
headers: Record<string, string>
55
query: Record<string, string | string[]>
6-
body: Record<string, any>
6+
body: any
77
}
88

99
type Query = {
@@ -22,6 +22,14 @@ type Body = {
2222
responses: { 200: { schema: Data } }
2323
}
2424

25+
type BodyArray = {
26+
parameters: {
27+
path: { id: number }
28+
body: { payload: string[] }
29+
}
30+
responses: { 200: { schema: Data } }
31+
}
32+
2533
type BodyAndQuery = {
2634
parameters: {
2735
path: { id: number }
@@ -41,6 +49,12 @@ export type paths = {
4149
patch: Body
4250
delete: Body
4351
}
52+
'/bodyarray/{id}': {
53+
post: BodyArray
54+
put: BodyArray
55+
patch: BodyArray
56+
delete: BodyArray
57+
}
4458
'/bodyquery/{id}': {
4559
post: BodyAndQuery
4660
put: BodyAndQuery

0 commit comments

Comments
 (0)