Skip to content

Commit 06b782c

Browse files
attempting a persistParams option to fix path/body key deletion
1 parent cda7665 commit 06b782c

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/fetcher.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,17 @@ function queryString(params: Record<string, unknown>): string {
4545
return ''
4646
}
4747

48-
function getPath(path: string, payload: Record<string, any>) {
48+
function getPath(
49+
path: string,
50+
payload: Record<string, any>,
51+
persistParams: string[],
52+
) {
4953
return path.replace(/\{([^}]+)\}/g, (_, key) => {
5054
const value = encodeURI(payload[key])
51-
delete payload[key]
55+
56+
if (!persistParams.includes(key)) {
57+
delete payload[key]
58+
}
5259
return value
5360
})
5461
}
@@ -57,13 +64,16 @@ function getQuery(
5764
method: Method,
5865
payload: Record<string, any>,
5966
query: string[],
67+
persistParams: string[],
6068
) {
6169
let queryObj = {} as any
6270

6371
if (sendBody(method)) {
6472
query.forEach((key) => {
6573
queryObj[key] = payload[key]
66-
delete payload[key]
74+
if (!persistParams.includes(key)) {
75+
delete payload[key]
76+
}
6777
})
6878
} else {
6979
queryObj = { ...payload }
@@ -108,18 +118,19 @@ function mergeRequestInit(
108118
return { ...first, ...second, headers }
109119
}
110120

111-
function getFetchParams(request: Request) {
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
121+
function getFetchParams(request: Request, persistParams: string[]) {
116122
const payload = Object.assign(
117123
Array.isArray(request.payload) ? [] : {},
118124
request.payload,
119125
)
120126

121-
const path = getPath(request.path, payload)
122-
const query = getQuery(request.method, payload, request.queryParams)
127+
const path = getPath(request.path, payload, persistParams)
128+
const query = getQuery(
129+
request.method,
130+
payload,
131+
request.queryParams,
132+
persistParams,
133+
)
123134
const headers = getHeaders(request.init?.headers)
124135
const url = request.baseUrl + path + query
125136

@@ -188,8 +199,8 @@ function wrapMiddlewares(middlewares: Middleware[], fetch: Fetch): Fetch {
188199
return (url, init) => handler(0, url, init)
189200
}
190201

191-
async function fetchUrl<R>(request: Request) {
192-
const { url, init } = getFetchParams(request)
202+
async function fetchUrl<R>(request: Request, persistParams: string[]) {
203+
const { url, init } = getFetchParams(request, persistParams)
193204

194205
const response = await request.fetch(url, init)
195206

@@ -241,16 +252,19 @@ function fetcher<Paths>() {
241252
path: <P extends keyof Paths>(path: P) => ({
242253
method: <M extends keyof Paths[P]>(method: M) => ({
243254
create: ((queryParams?: Record<string, true | 1>) =>
244-
createFetch((payload, init) =>
245-
fetchUrl({
246-
baseUrl: baseUrl || '',
247-
path: path as string,
248-
method: method as Method,
249-
queryParams: Object.keys(queryParams || {}),
250-
payload,
251-
init: mergeRequestInit(defaultInit, init),
252-
fetch,
253-
}),
255+
createFetch((payload, init, persistParams) =>
256+
fetchUrl(
257+
{
258+
baseUrl: baseUrl || '',
259+
path: path as string,
260+
method: method as Method,
261+
queryParams: Object.keys(queryParams || {}),
262+
payload,
263+
init: mergeRequestInit(defaultInit, init),
264+
fetch,
265+
},
266+
persistParams || [],
267+
),
254268
)) as CreateFetch<M, Paths[P][M]>,
255269
}),
256270
}),

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export type Fetch = (
9191
export type _TypedFetch<OP> = (
9292
arg: OpArgType<OP>,
9393
init?: RequestInit,
94+
persistParams?: string[],
9495
) => Promise<ApiResponse<OpReturnType<OP>>>
9596

9697
export type TypedFetch<OP> = _TypedFetch<OP> & {

0 commit comments

Comments
 (0)