Skip to content

Commit 3242f7a

Browse files
attempting a persistParams option to fix path/body key deletion
1 parent d5fed4a commit 3242f7a

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/fetcher.ts

Lines changed: 36 additions & 18 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,11 +118,16 @@ function mergeRequestInit(
108118
return { ...first, ...second, headers }
109119
}
110120

111-
function getFetchParams(request: Request) {
121+
function getFetchParams(request: Request, persistParams: string[]) {
112122
const payload = { ...request.payload } // clone payload
113123

114-
const path = getPath(request.path, payload)
115-
const query = getQuery(request.method, payload, request.queryParams)
124+
const path = getPath(request.path, payload, persistParams)
125+
const query = getQuery(
126+
request.method,
127+
payload,
128+
request.queryParams,
129+
persistParams,
130+
)
116131
const headers = getHeaders(request.init?.headers)
117132
const url = request.baseUrl + path + query
118133

@@ -181,8 +196,8 @@ function wrapMiddlewares(middlewares: Middleware[], fetch: Fetch): Fetch {
181196
return (url, init) => handler(0, url, init)
182197
}
183198

184-
async function fetchUrl<R>(request: Request) {
185-
const { url, init } = getFetchParams(request)
199+
async function fetchUrl<R>(request: Request, persistParams: string[]) {
200+
const { url, init } = getFetchParams(request, persistParams)
186201

187202
const response = await request.fetch(url, init)
188203

@@ -234,16 +249,19 @@ function fetcher<Paths>() {
234249
path: <P extends keyof Paths>(path: P) => ({
235250
method: <M extends keyof Paths[P]>(method: M) => ({
236251
create: ((queryParams?: Record<string, true | 1>) =>
237-
createFetch((payload, init) =>
238-
fetchUrl({
239-
baseUrl: baseUrl || '',
240-
path: path as string,
241-
method: method as Method,
242-
queryParams: Object.keys(queryParams || {}),
243-
payload,
244-
init: mergeRequestInit(defaultInit, init),
245-
fetch,
246-
}),
252+
createFetch((payload, init, persistParams) =>
253+
fetchUrl(
254+
{
255+
baseUrl: baseUrl || '',
256+
path: path as string,
257+
method: method as Method,
258+
queryParams: Object.keys(queryParams || {}),
259+
payload,
260+
init: mergeRequestInit(defaultInit, init),
261+
fetch,
262+
},
263+
persistParams,
264+
),
247265
)) as CreateFetch<M, Paths[P][M]>,
248266
}),
249267
}),

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export type Fetch = (
8989
export type _TypedFetch<OP> = (
9090
arg: OpArgType<OP>,
9191
init?: RequestInit,
92+
persistParams?: string[],
9293
) => Promise<ApiResponse<OpReturnType<OP>>>
9394

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

0 commit comments

Comments
 (0)