[{"data":1,"prerenderedAt":898,"buildId":899},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Ftransport":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":897,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Ftransport.md",{},"Use a custom transport",{"type":8,"value":9,"toc":881},"minimark",[10,14,22,27,34,285,305,309,339,479,490,494,503,584,588,594,744,748,798,816,820,826,851,855,866],[11,12,13],"p",{},"Replace the network send of the generated fetch client with another HTTP library or your own function.",[11,15,16,17,21],{},"The client still builds the URL, serializes parameters, resolves auth, runs interceptors, and decodes the result. ",[18,19,20],"code",{},"client.interceptors"," handlers keep running, whatever the transport.",[23,24,26],"h2",{"id":25},"write-a-transport-function","Write a transport function",[11,28,29,30,33],{},"A transport gets the resolved request and returns a web ",[18,31,32],{},"Response"," with its body unread:",[35,36,40],"pre",{"language":37,"code":38,"class":39},"typescript","type Transport = (request: ResolvedRequest) => Promise\u003CResponse | TransportResult>\n\ntype ResolvedRequest = {\n url: string \u002F\u002F built, with the query\n method: string\n headers: Record\u003Cstring, string> \u002F\u002F auth headers included\n body?: RequestBody \u002F\u002F already encoded\n signal?: AbortSignal\n credentials?: RequestCredentials\n options?: NativeOptions \u002F\u002F RequestInit plus Next.js's `next`\n stream?: boolean \u002F\u002F the call streams the response, so do not buffer the body\n}\n\ntype TransportResult = { webResponse: Response; request: Request; response: Response }","highlights",[18,41,43,97,98,97,100,97,112,97,129,97,141,97,168,97,184,97,196,97,208,97,223,97,238,97,243,97,245],{"class":42},"language-typescript",[44,45,48,52,56,59,63,67,70,73,76,79,82,85,88,91,94],"span",{"class":46,"style":47},"line","display: inline",[44,49,51],{"style":50},"color:#d73a49;--hls-dark:#f97583","type ",[44,53,55],{"style":54},"color:#6f42c1;--hls-dark:#b392f0","Transport ",[44,57,58],{"style":50},"= ",[44,60,62],{"style":61},"color:#005cc5;--hls-dark:#79b8ff","(",[44,64,66],{"style":65},"color:#24292e;--hls-dark:#e1e4e8","request",[44,68,69],{"style":65},": ",[44,71,72],{"style":54},"ResolvedRequest",[44,74,75],{"style":61},") ",[44,77,78],{"style":50},"=> ",[44,80,81],{"style":54},"Promise",[44,83,84],{"style":50},"\u003C",[44,86,87],{"style":54},"Response ",[44,89,90],{"style":50},"| ",[44,92,93],{"style":54},"TransportResult",[44,95,96],{"style":50},">","\n",[44,99],{"class":46,"style":47},[44,101,102,104,107,109],{"class":46,"style":47},[44,103,51],{"style":50},[44,105,106],{"style":54},"ResolvedRequest ",[44,108,58],{"style":50},[44,110,111],{"style":61},"{",[44,113,114,117,120,122,125],{"class":46,"style":47},[44,115,116],{"style":61}," ",[44,118,119],{"style":61},"url",[44,121,69],{"style":65},[44,123,124],{"style":54},"string ",[44,126,128],{"style":127},"color:#6a737d;--hls-dark:#6a737d","\u002F\u002F built, with the query",[44,130,131,133,136,138],{"class":46,"style":47},[44,132,116],{"style":127},[44,134,135],{"style":65},"method",[44,137,69],{"style":65},[44,139,140],{"style":54},"string",[44,142,143,145,148,150,153,155,157,160,162,165],{"class":46,"style":47},[44,144,116],{"style":54},[44,146,147],{"style":65},"headers",[44,149,69],{"style":65},[44,151,152],{"style":54},"Record",[44,154,84],{"style":50},[44,156,140],{"style":65},[44,158,159],{"style":50},", ",[44,161,140],{"style":65},[44,163,164],{"style":50},"> ",[44,166,167],{"style":127},"\u002F\u002F auth headers included",[44,169,170,172,175,178,181],{"class":46,"style":47},[44,171,116],{"style":127},[44,173,174],{"style":65},"body",[44,176,177],{"style":65},"?: ",[44,179,180],{"style":54},"RequestBody ",[44,182,183],{"style":127},"\u002F\u002F already encoded",[44,185,186,188,191,193],{"class":46,"style":47},[44,187,116],{"style":127},[44,189,190],{"style":65},"signal",[44,192,177],{"style":65},[44,194,195],{"style":54},"AbortSignal",[44,197,198,200,203,205],{"class":46,"style":47},[44,199,116],{"style":54},[44,201,202],{"style":65},"credentials",[44,204,177],{"style":65},[44,206,207],{"style":54},"RequestCredentials",[44,209,210,212,215,217,220],{"class":46,"style":47},[44,211,116],{"style":54},[44,213,214],{"style":65},"options",[44,216,177],{"style":65},[44,218,219],{"style":54},"NativeOptions ",[44,221,222],{"style":127},"\u002F\u002F RequestInit plus Next.js's `next`",[44,224,225,227,230,232,235],{"class":46,"style":47},[44,226,116],{"style":127},[44,228,229],{"style":65},"stream",[44,231,177],{"style":65},[44,233,234],{"style":54},"boolean ",[44,236,237],{"style":127},"\u002F\u002F the call streams the response, so do not buffer the body",[44,239,240],{"class":46,"style":47},[44,241,242],{"style":61},"}",[44,244],{"class":46,"style":47},[44,246,247,249,252,254,257,260,262,264,267,269,271,274,276,279,281,283],{"class":46,"style":47},[44,248,51],{"style":50},[44,250,251],{"style":54},"TransportResult ",[44,253,58],{"style":50},[44,255,256],{"style":61},"{ ",[44,258,259],{"style":61},"webResponse",[44,261,69],{"style":65},[44,263,32],{"style":54},[44,265,266],{"style":50},"; ",[44,268,66],{"style":61},[44,270,69],{"style":65},[44,272,273],{"style":54},"Request",[44,275,266],{"style":50},[44,277,278],{"style":61},"response",[44,280,69],{"style":65},[44,282,87],{"style":54},[44,284,242],{"style":61},[11,286,287,288,290,291,293,294,296,297,304],{},"Return a ",[18,289,93],{}," to set the native ",[18,292,66],{}," and ",[18,295,278],{}," that results and errors carry. See ",[298,299,301,302],"a",{"href":300},"#results-from-a-plain-response","Results from a plain ",[18,303,32],{},".",[23,306,308],{"id":307},"send-with-another-library","Send with another library",[11,310,311,312,318,319,321,322,326,327,330,331,334,335,338],{},"For Ky with its retries and hooks, use ",[298,313,315],{"href":314},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-ky",[18,316,317],{},"@kubb\u002Fplugin-ky"," instead of this plugin. A library that returns a ",[18,320,32],{},", such as ",[298,323,325],{"href":324},"https:\u002F\u002Fgithub.com\u002Fsindresorhus\u002Fky","ky",", needs only the send. ",[18,328,329],{},"toRequestInit(request)"," gives the ",[18,332,333],{},"RequestInit"," that ",[18,336,337],{},"fetch"," and ky take:",[35,340,342],{"language":37,"code":341,"class":39},"import ky from 'ky'\nimport { client, toRequestInit } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\nclient.setConfig({\n transport: (request) => ky(request.url, { ...toRequestInit(request), throwHttpErrors: false, retry: 0, timeout: false }),\n})",[18,343,344,97,359,97,381,97,383,97,395,97,475],{"class":42},[44,345,346,349,352,355],{"class":46,"style":47},[44,347,348],{"style":50},"import ",[44,350,351],{"style":65},"ky ",[44,353,354],{"style":50},"from ",[44,356,358],{"style":357},"color:#032f62;--hls-dark:#9ecbff","'ky'",[44,360,361,363,365,368,370,373,376,378],{"class":46,"style":47},[44,362,348],{"style":50},[44,364,256],{"style":61},[44,366,367],{"style":65},"client",[44,369,159],{"style":50},[44,371,372],{"style":65},"toRequestInit ",[44,374,375],{"style":61},"} ",[44,377,354],{"style":50},[44,379,380],{"style":357},"'.\u002Fgen\u002F.kubb\u002Fclient'",[44,382],{"class":46,"style":47},[44,384,385,387,389,392],{"class":46,"style":47},[44,386,367],{"style":65},[44,388,304],{"style":50},[44,390,391],{"style":54},"setConfig",[44,393,394],{"style":61},"({",[44,396,397,399,402,404,406,408,410,412,414,416,418,420,422,424,426,429,432,434,436,439,441,444,446,449,451,454,456,459,461,464,466,469,472],{"class":46,"style":47},[44,398,116],{"style":61},[44,400,401],{"style":61},"transport",[44,403,69],{"style":50},[44,405,62],{"style":61},[44,407,66],{"style":65},[44,409,75],{"style":61},[44,411,78],{"style":50},[44,413,325],{"style":54},[44,415,62],{"style":61},[44,417,66],{"style":65},[44,419,304],{"style":50},[44,421,119],{"style":61},[44,423,159],{"style":50},[44,425,256],{"style":61},[44,427,428],{"style":50},"...",[44,430,431],{"style":54},"toRequestInit",[44,433,62],{"style":61},[44,435,66],{"style":65},[44,437,438],{"style":61},")",[44,440,159],{"style":50},[44,442,443],{"style":61},"throwHttpErrors",[44,445,69],{"style":50},[44,447,448],{"style":61},"false",[44,450,159],{"style":50},[44,452,453],{"style":61},"retry",[44,455,69],{"style":50},[44,457,458],{"style":61},"0",[44,460,159],{"style":50},[44,462,463],{"style":61},"timeout",[44,465,69],{"style":50},[44,467,468],{"style":61},"false ",[44,470,471],{"style":61},"})",[44,473,474],{"style":50},",",[44,476,477],{"class":46,"style":47},[44,478,471],{"style":61},[480,481,483,484,486,487,304],"blockquote",{"as":482},"note","The library must not throw or retry on an error status, and must hand back the body unread. ky's ",[18,485,463],{}," never ends a started body or stream, so turn it off and bound the call with ",[18,488,489],{},"signal: AbortSignal.timeout(ms)",[23,491,493],{"id":492},"wrap-the-default-transport","Wrap the default transport",[11,495,496,499,500,502],{},[18,497,498],{},"defaultTransport"," is the ",[18,501,337],{},"-based send the client uses when no transport is set. Wrap it to add tracing, timing, or headers:",[35,504,506],{"language":37,"code":505,"class":39},"import { client, defaultTransport } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\nclient.setConfig({\n transport: (request) => withSpan(request, () => defaultTransport(request)),\n})",[18,507,508,97,527,97,529,97,539,97,580],{"class":42},[44,509,510,512,514,516,518,521,523,525],{"class":46,"style":47},[44,511,348],{"style":50},[44,513,256],{"style":61},[44,515,367],{"style":65},[44,517,159],{"style":50},[44,519,520],{"style":65},"defaultTransport ",[44,522,375],{"style":61},[44,524,354],{"style":50},[44,526,380],{"style":357},[44,528],{"class":46,"style":47},[44,530,531,533,535,537],{"class":46,"style":47},[44,532,367],{"style":65},[44,534,304],{"style":50},[44,536,391],{"style":54},[44,538,394],{"style":61},[44,540,541,543,545,547,549,551,553,555,558,560,562,564,567,569,571,573,575,578],{"class":46,"style":47},[44,542,116],{"style":61},[44,544,401],{"style":61},[44,546,69],{"style":50},[44,548,62],{"style":61},[44,550,66],{"style":65},[44,552,75],{"style":61},[44,554,78],{"style":50},[44,556,557],{"style":54},"withSpan",[44,559,62],{"style":61},[44,561,66],{"style":65},[44,563,159],{"style":50},[44,565,566],{"style":61},"() ",[44,568,78],{"style":50},[44,570,498],{"style":54},[44,572,62],{"style":61},[44,574,66],{"style":65},[44,576,577],{"style":61},"))",[44,579,474],{"style":50},[44,581,582],{"class":46,"style":47},[44,583,471],{"style":61},[23,585,587],{"id":586},"mock-the-network-in-tests","Mock the network in tests",[11,589,590,591,593],{},"Return a fixed ",[18,592,32],{}," from a separate client, and pass it to the call or to a query plugin:",[35,595,597],{"language":37,"code":596,"class":39},"import { createClient } from '.\u002Fgen\u002F.kubb\u002Fclient'\nimport { getPetById } from '.\u002Fgen\u002Fclients\u002FgetPetById'\n\nconst testClient = createClient({ transport: async () => Response.json({ id: 1, name: 'Fluffy' }) })\n\nconst { data } = await getPetById({ path: { petId: 1 }, client: testClient })\n\u002F\u002F ^ { id: 1, name: 'Fluffy' }",[18,598,599,97,614,97,630,97,632,97,689,97,691,97,739],{"class":42},[44,600,601,603,605,608,610,612],{"class":46,"style":47},[44,602,348],{"style":50},[44,604,256],{"style":61},[44,606,607],{"style":65},"createClient ",[44,609,375],{"style":61},[44,611,354],{"style":50},[44,613,380],{"style":357},[44,615,616,618,620,623,625,627],{"class":46,"style":47},[44,617,348],{"style":50},[44,619,256],{"style":61},[44,621,622],{"style":65},"getPetById ",[44,624,375],{"style":61},[44,626,354],{"style":50},[44,628,629],{"style":357},"'.\u002Fgen\u002Fclients\u002FgetPetById'",[44,631],{"class":46,"style":47},[44,633,634,637,640,642,645,648,650,652,655,657,659,661,663,666,668,671,673,676,678,681,683,686],{"class":46,"style":47},[44,635,636],{"style":50},"const ",[44,638,639],{"style":65},"testClient ",[44,641,58],{"style":50},[44,643,644],{"style":54},"createClient",[44,646,647],{"style":61},"({ ",[44,649,401],{"style":61},[44,651,69],{"style":50},[44,653,654],{"style":50},"async ",[44,656,566],{"style":61},[44,658,78],{"style":50},[44,660,32],{"style":54},[44,662,304],{"style":50},[44,664,665],{"style":54},"json",[44,667,647],{"style":61},[44,669,670],{"style":61},"id",[44,672,69],{"style":50},[44,674,675],{"style":61},"1",[44,677,159],{"style":50},[44,679,680],{"style":61},"name",[44,682,69],{"style":50},[44,684,685],{"style":357},"'Fluffy' ",[44,687,688],{"style":61},"}) })",[44,690],{"class":46,"style":47},[44,692,693,695,697,700,702,704,707,710,712,715,717,719,722,724,727,729,731,733,735,737],{"class":46,"style":47},[44,694,636],{"style":50},[44,696,256],{"style":61},[44,698,699],{"style":65},"data ",[44,701,375],{"style":61},[44,703,58],{"style":50},[44,705,706],{"style":50},"await ",[44,708,709],{"style":54},"getPetById",[44,711,647],{"style":61},[44,713,714],{"style":61},"path",[44,716,69],{"style":50},[44,718,256],{"style":61},[44,720,721],{"style":61},"petId",[44,723,69],{"style":50},[44,725,726],{"style":61},"1 ",[44,728,242],{"style":61},[44,730,159],{"style":50},[44,732,367],{"style":61},[44,734,69],{"style":50},[44,736,639],{"style":65},[44,738,471],{"style":61},[44,740,741],{"class":46,"style":47},[44,742,743],{"style":127},"\u002F\u002F ^ { id: 1, name: 'Fluffy' }",[23,745,747],{"id":746},"where-to-set-it","Where to set it",[749,750,751,764],"table",{},[752,753,754],"thead",{},[755,756,757,761],"tr",{},[758,759,760],"th",{},"Scope",[758,762,763],{},"How",[765,766,767,778,788],"tbody",{},[755,768,769,773],{},[770,771,772],"td",{},"Every call",[770,774,775],{},[18,776,777],{},"client.setConfig({ transport })",[755,779,780,783],{},[770,781,782],{},"A separate client",[770,784,785],{},[18,786,787],{},"createClient({ transport })",[755,789,790,793],{},[770,791,792],{},"One call",[770,794,795,797],{},[18,796,401],{}," on the call",[11,799,800,801,804,805,807,808,810,811,813,814,304],{},"A client from ",[18,802,803],{},"createClient()"," copies the shared ",[18,806,401],{}," at creation, so a later ",[18,809,777],{}," does not reach it. Without a ",[18,812,401],{},", it sends with the global ",[18,815,337],{},[23,817,819],{"id":818},"less-common-cases","Less common cases",[821,822,301,824],"h3",{"id":823},"results-from-a-plain-response",[18,825,32],{},[11,827,828,829,831,832,834,835,837,838,840,841,843,844,846,847,850],{},"When a transport returns a plain ",[18,830,32],{},", results and errors carry it as ",[18,833,278],{},". Their ",[18,836,66],{}," is a ",[18,839,273],{}," built from the resolved request without its body, or the resolved request itself when ",[18,842,273],{}," cannot parse the URL, such as a relative URL in Node.js. Return a ",[18,845,93],{}," to set both, as ",[298,848,26],{"href":849},"#write-a-transport-function"," shows.",[821,852,854],{"id":853},"why-ky-needs-these-settings","Why ky needs these settings",[11,856,857,858,862,863,865],{},"When you ",[298,859,861],{"href":860},"#send-with-another-library","send with ky",", or another library that returns a ",[18,864,32],{},":",[867,868,869,873],"ul",{},[870,871,872],"li",{},"The client splits success from error itself, so the library must not throw or retry on an error status.",[870,874,875,876,878,879,304],{},"ky's ",[18,877,463],{}," only waits for the response headers and never ends a started body or stream. Turn it off and bound the whole call with ",[18,880,489],{},{"title":882,"searchDepth":883,"depth":883,"links":884},"",5,[885,887,888,889,890,891],{"id":25,"depth":886,"text":26},2,{"id":307,"depth":886,"text":308},{"id":492,"depth":886,"text":493},{"id":586,"depth":886,"text":587},{"id":746,"depth":886,"text":747},{"id":818,"depth":886,"text":819,"children":892},[893,896],{"id":823,"depth":894,"text":895},3,"Results from a plain Response",{"id":853,"depth":894,"text":854},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Ftransport",1795727685436,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]