Skip to content

Commit 4af7cb4

Browse files
authored
fix: append search params (#673)
1 parent 8c91cfb commit 4af7cb4

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/legacy/helpers/runRequest.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const parseResultFromResponse = async (response: Response, jsonSerializer: JsonS
148148

149149
const createFetcher = (method: 'GET' | 'POST') => async (params: Input) => {
150150
const headers = new Headers(params.headers)
151-
let queryParams = ``
151+
let searchParams: URLSearchParams | null = null
152152
let body = undefined
153153

154154
if (!headers.has(ACCEPT_HEADER)) {
@@ -162,13 +162,14 @@ const createFetcher = (method: 'GET' | 'POST') => async (params: Input) => {
162162
headers.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON)
163163
}
164164
} else {
165-
queryParams = buildQueryParams(params)
165+
searchParams = buildQueryParams(params)
166166
}
167167

168168
const init: RequestInit = { method, headers, body, ...params.fetchOptions }
169169

170-
let urlResolved = params.url
170+
let url = new URL(params.url)
171171
let initResolved = init
172+
172173
if (params.middleware) {
173174
const result = await Promise.resolve(
174175
params.middleware({
@@ -179,14 +180,18 @@ const createFetcher = (method: 'GET' | 'POST') => async (params: Input) => {
179180
}),
180181
)
181182
const { url: urlNew, ...initNew } = result
182-
urlResolved = urlNew
183+
url = new URL(urlNew)
183184
initResolved = initNew
184185
}
185-
if (queryParams) {
186-
urlResolved = `${urlResolved}?${queryParams}`
186+
187+
if (searchParams) {
188+
searchParams.forEach((value, name) => {
189+
url.searchParams.append(name, value)
190+
})
187191
}
192+
188193
const $fetch = params.fetch ?? fetch
189-
return await $fetch(urlResolved, initResolved)
194+
return await $fetch(url, initResolved)
190195
}
191196

192197
const buildBody = (params: Input) => {
@@ -206,26 +211,30 @@ const buildBody = (params: Input) => {
206211
}
207212
}
208213

209-
const buildQueryParams = (params: Input): string => {
214+
const buildQueryParams = (params: Input): URLSearchParams => {
210215
const $jsonSerializer = params.fetchOptions.jsonSerializer ?? defaultJsonSerializer
216+
const searchParams = new URLSearchParams()
217+
211218
if (params.request._tag === `Single`) {
212-
const search: string[] = [`query=${encodeURIComponent(cleanQuery(params.request.document.expression))}`]
219+
searchParams.append(`query`, cleanQuery(params.request.document.expression))
213220
if (params.request.variables) {
214-
search.push(`variables=${encodeURIComponent($jsonSerializer.stringify(params.request.variables))}`)
221+
searchParams.append(`variables`, $jsonSerializer.stringify(params.request.variables))
215222
}
216223
if (params.request.document.operationName) {
217-
search.push(`operationName=${encodeURIComponent(params.request.document.operationName)}`)
224+
searchParams.append(`operationName`, params.request.document.operationName)
218225
}
219-
return search.join(`&`)
226+
return searchParams
220227
} else if (params.request._tag === `Batch`) {
221228
const variablesSerialized = params.request.variables?.map((v) => $jsonSerializer.stringify(v)) ?? []
222229
const queriesCleaned = params.request.query.map(cleanQuery)
223230
const payload = zip(queriesCleaned, variablesSerialized).map(([query, variables]) => ({
224231
query,
225232
variables,
226233
}))
227-
return `query=${encodeURIComponent($jsonSerializer.stringify(payload))}`
234+
searchParams.append(`query`, $jsonSerializer.stringify(payload))
228235
} else {
229236
throw casesExhausted(params.request)
230237
}
238+
239+
return searchParams
231240
}

tests/fetch.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ test(`custom fetch configuration is passed through`, async () => {
1313
await client.request(gql`foo`).catch(() => {
1414
/* ignore */
1515
})
16-
expect(fetch.mock.calls).toMatchObject([[expect.stringMatching(`.*`), { next: { revalidate: 1 } }]])
16+
expect(fetch.mock.calls).toMatchObject([[new URL(`https://foobar`), { next: { revalidate: 1 } }]])
1717
})

0 commit comments

Comments
 (0)