Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4af7cb4

Browse files
authored
fix: append search params (#673)
1 parent8c91cfb commit4af7cb4

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
constcreateFetcher=(method:'GET'|'POST')=>async(params:Input)=>{
150150
constheaders=newHeaders(params.headers)
151-
letqueryParams=``
151+
letsearchParams:URLSearchParams|null=null
152152
letbody=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
constinit:RequestInit={ method, headers, body, ...params.fetchOptions}
169169

170-
leturlResolved=params.url
170+
leturl=newURL(params.url)
171171
letinitResolved=init
172+
172173
if(params.middleware){
173174
constresult=awaitPromise.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=newURL(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-
returnawait$fetch(urlResolved,initResolved)
194+
returnawait$fetch(url,initResolved)
190195
}
191196

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

209-
constbuildQueryParams=(params:Input):string=>{
214+
constbuildQueryParams=(params:Input):URLSearchParams=>{
210215
const$jsonSerializer=params.fetchOptions.jsonSerializer??defaultJsonSerializer
216+
constsearchParams=newURLSearchParams()
217+
211218
if(params.request._tag===`Single`){
212-
constsearch: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-
returnsearch.join(`&`)
226+
returnsearchParams
220227
}elseif(params.request._tag===`Batch`){
221228
constvariablesSerialized=params.request.variables?.map((v)=>$jsonSerializer.stringify(v))??[]
222229
constqueriesCleaned=params.request.query.map(cleanQuery)
223230
constpayload=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
throwcasesExhausted(params.request)
230237
}
238+
239+
returnsearchParams
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
awaitclient.request(gql`foo`).catch(()=>{
1414
/* ignore */
1515
})
16-
expect(fetch.mock.calls).toMatchObject([[expect.stringMatching(`.*`),{next:{revalidate:1}}]])
16+
expect(fetch.mock.calls).toMatchObject([[newURL(`https://foobar`),{next:{revalidate:1}}]])
1717
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp