[{"data":1,"prerenderedAt":907},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-ky\u002Fguides\u002Ftransport":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":906,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-ky\u002Fguides\u002Ftransport.md",{},"Use a custom transport",{"type":8,"value":9,"toc":890},"minimark",[10,14,22,27,34,285,305,309,315,465,507,511,517,601,605,611,758,762,812,830,834,840,852,877,881],[11,12,13],"p",{},"Send the generated client through your own Ky instance, or replace the network send with 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 Ky options\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 Ky options",[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},"use-your-own-ky-instance","Use your own Ky instance",[11,310,311,314],{},[18,312,313],{},"createKyTransport"," sends through a Ky instance and keeps its defaults and hooks:",[35,316,318],{"language":37,"code":317,"class":39},"import ky from 'ky'\nimport { client, createKyTransport } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\nconst instance = ky.create({ timeout: 5000, hooks: { beforeRequest: [({ request }) => request.headers.set('traceparent', currentTraceparent())] } })\n\nclient.setConfig({ transport: createKyTransport(instance) })",[18,319,320,97,335,97,357,97,359,97,437,97,439],{"class":42},[44,321,322,325,328,331],{"class":46,"style":47},[44,323,324],{"style":50},"import ",[44,326,327],{"style":65},"ky ",[44,329,330],{"style":50},"from ",[44,332,334],{"style":333},"color:#032f62;--hls-dark:#9ecbff","'ky'",[44,336,337,339,341,344,346,349,352,354],{"class":46,"style":47},[44,338,324],{"style":50},[44,340,256],{"style":61},[44,342,343],{"style":65},"client",[44,345,159],{"style":50},[44,347,348],{"style":65},"createKyTransport ",[44,350,351],{"style":61},"} ",[44,353,330],{"style":50},[44,355,356],{"style":333},"'.\u002Fgen\u002F.kubb\u002Fclient'",[44,358],{"class":46,"style":47},[44,360,361,364,367,369,372,374,377,380,383,385,388,390,393,395,397,400,402,405,408,411,413,415,417,419,421,424,426,429,431,434],{"class":46,"style":47},[44,362,363],{"style":50},"const ",[44,365,366],{"style":65},"instance ",[44,368,58],{"style":50},[44,370,371],{"style":65},"ky",[44,373,304],{"style":50},[44,375,376],{"style":54},"create",[44,378,379],{"style":61},"({ ",[44,381,382],{"style":61},"timeout",[44,384,69],{"style":50},[44,386,387],{"style":61},"5000",[44,389,159],{"style":50},[44,391,392],{"style":61},"hooks",[44,394,69],{"style":50},[44,396,256],{"style":61},[44,398,399],{"style":61},"beforeRequest",[44,401,69],{"style":50},[44,403,404],{"style":61},"[({ ",[44,406,407],{"style":65},"request ",[44,409,410],{"style":61},"}) ",[44,412,78],{"style":50},[44,414,66],{"style":65},[44,416,304],{"style":50},[44,418,147],{"style":61},[44,420,304],{"style":50},[44,422,423],{"style":54},"set",[44,425,62],{"style":61},[44,427,428],{"style":333},"'traceparent'",[44,430,159],{"style":50},[44,432,433],{"style":54},"currentTraceparent",[44,435,436],{"style":61},"())] } })",[44,438],{"class":46,"style":47},[44,440,441,443,445,448,450,453,455,457,459,462],{"class":46,"style":47},[44,442,343],{"style":65},[44,444,304],{"style":50},[44,446,447],{"style":54},"setConfig",[44,449,379],{"style":61},[44,451,452],{"style":61},"transport",[44,454,69],{"style":50},[44,456,313],{"style":54},[44,458,62],{"style":61},[44,460,461],{"style":65},"instance",[44,463,464],{"style":61},") })",[466,467,469,470,159,473,159,476,479,480,483,484,490,491,494,495,498,499,502,503,506],"blockquote",{"as":468},"note","Leave ",[18,471,472],{},"prefix",[18,474,475],{},"baseUrl",[18,477,478],{},"json",", and ",[18,481,482],{},"searchParams"," off the instance, and set the base URL with ",[298,485,487],{"href":486},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-ky\u002Fguides\u002Fconfigure-requests#set-the-base-url",[18,488,489],{},"baseURL",". The transport sets ",[18,492,493],{},"throwHttpErrors: false",". Your instance keeps Ky's own ",[18,496,497],{},"retry"," default of 2, so pass ",[18,500,501],{},"retry: 0"," to ",[18,504,505],{},"ky.create"," to send each call once, as the default client does.",[23,508,510],{"id":509},"wrap-the-default-transport","Wrap the default transport",[11,512,513,516],{},[18,514,515],{},"defaultTransport"," is the Ky send the client uses when no transport is set. Wrap it to add tracing, timing, or headers:",[35,518,520],{"language":37,"code":519,"class":39},"import { client, defaultTransport } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\nclient.setConfig({\n  transport: (request) => withSpan(request, () => defaultTransport(request)),\n})",[18,521,522,97,541,97,543,97,554,97,596],{"class":42},[44,523,524,526,528,530,532,535,537,539],{"class":46,"style":47},[44,525,324],{"style":50},[44,527,256],{"style":61},[44,529,343],{"style":65},[44,531,159],{"style":50},[44,533,534],{"style":65},"defaultTransport ",[44,536,351],{"style":61},[44,538,330],{"style":50},[44,540,356],{"style":333},[44,542],{"class":46,"style":47},[44,544,545,547,549,551],{"class":46,"style":47},[44,546,343],{"style":65},[44,548,304],{"style":50},[44,550,447],{"style":54},[44,552,553],{"style":61},"({",[44,555,556,558,560,562,564,566,568,570,573,575,577,579,582,584,586,588,590,593],{"class":46,"style":47},[44,557,116],{"style":61},[44,559,452],{"style":61},[44,561,69],{"style":50},[44,563,62],{"style":61},[44,565,66],{"style":65},[44,567,75],{"style":61},[44,569,78],{"style":50},[44,571,572],{"style":54},"withSpan",[44,574,62],{"style":61},[44,576,66],{"style":65},[44,578,159],{"style":50},[44,580,581],{"style":61},"() ",[44,583,78],{"style":50},[44,585,515],{"style":54},[44,587,62],{"style":61},[44,589,66],{"style":65},[44,591,592],{"style":61},"))",[44,594,595],{"style":50},",",[44,597,598],{"class":46,"style":47},[44,599,600],{"style":61},"})",[23,602,604],{"id":603},"mock-the-network-in-tests","Mock the network in tests",[11,606,607,608,610],{},"Return a fixed ",[18,609,32],{}," from a separate client, and pass it to the call or to a query plugin:",[35,612,614],{"language":37,"code":613,"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,615,616,97,631,97,647,97,649,97,703,97,705,97,753],{"class":42},[44,617,618,620,622,625,627,629],{"class":46,"style":47},[44,619,324],{"style":50},[44,621,256],{"style":61},[44,623,624],{"style":65},"createClient ",[44,626,351],{"style":61},[44,628,330],{"style":50},[44,630,356],{"style":333},[44,632,633,635,637,640,642,644],{"class":46,"style":47},[44,634,324],{"style":50},[44,636,256],{"style":61},[44,638,639],{"style":65},"getPetById ",[44,641,351],{"style":61},[44,643,330],{"style":50},[44,645,646],{"style":333},"'.\u002Fgen\u002Fclients\u002FgetPetById'",[44,648],{"class":46,"style":47},[44,650,651,653,656,658,661,663,665,667,670,672,674,676,678,680,682,685,687,690,692,695,697,700],{"class":46,"style":47},[44,652,363],{"style":50},[44,654,655],{"style":65},"testClient ",[44,657,58],{"style":50},[44,659,660],{"style":54},"createClient",[44,662,379],{"style":61},[44,664,452],{"style":61},[44,666,69],{"style":50},[44,668,669],{"style":50},"async ",[44,671,581],{"style":61},[44,673,78],{"style":50},[44,675,32],{"style":54},[44,677,304],{"style":50},[44,679,478],{"style":54},[44,681,379],{"style":61},[44,683,684],{"style":61},"id",[44,686,69],{"style":50},[44,688,689],{"style":61},"1",[44,691,159],{"style":50},[44,693,694],{"style":61},"name",[44,696,69],{"style":50},[44,698,699],{"style":333},"'Fluffy' ",[44,701,702],{"style":61},"}) })",[44,704],{"class":46,"style":47},[44,706,707,709,711,714,716,718,721,724,726,729,731,733,736,738,741,743,745,747,749,751],{"class":46,"style":47},[44,708,363],{"style":50},[44,710,256],{"style":61},[44,712,713],{"style":65},"data ",[44,715,351],{"style":61},[44,717,58],{"style":50},[44,719,720],{"style":50},"await ",[44,722,723],{"style":54},"getPetById",[44,725,379],{"style":61},[44,727,728],{"style":61},"path",[44,730,69],{"style":50},[44,732,256],{"style":61},[44,734,735],{"style":61},"petId",[44,737,69],{"style":50},[44,739,740],{"style":61},"1 ",[44,742,242],{"style":61},[44,744,159],{"style":50},[44,746,343],{"style":61},[44,748,69],{"style":50},[44,750,655],{"style":65},[44,752,600],{"style":61},[44,754,755],{"class":46,"style":47},[44,756,757],{"style":127},"\u002F\u002F      ^ { id: 1, name: 'Fluffy' }",[23,759,761],{"id":760},"where-to-set-it","Where to set it",[763,764,765,778],"table",{},[766,767,768],"thead",{},[769,770,771,775],"tr",{},[772,773,774],"th",{},"Scope",[772,776,777],{},"How",[779,780,781,792,802],"tbody",{},[769,782,783,787],{},[784,785,786],"td",{},"Every call",[784,788,789],{},[18,790,791],{},"client.setConfig({ transport })",[769,793,794,797],{},[784,795,796],{},"A separate client",[784,798,799],{},[18,800,801],{},"createClient({ transport })",[769,803,804,807],{},[784,805,806],{},"One call",[784,808,809,811],{},[18,810,452],{}," on the call",[11,813,814,815,818,819,821,822,824,825,827,828,304],{},"A client from ",[18,816,817],{},"createClient()"," copies the shared ",[18,820,452],{}," at creation, so a later ",[18,823,791],{}," does not reach it. Without a ",[18,826,452],{},", it sends with ",[18,829,371],{},[23,831,833],{"id":832},"less-common-cases","Less common cases",[835,836,301,838],"h3",{"id":837},"results-from-a-plain-response",[18,839,32],{},[11,841,842,843,845,846,848,849,851],{},"The default transport and ",[18,844,313],{}," return a plain ",[18,847,32],{},", so ",[18,850,66],{}," on a result or an error lacks the headers that instance defaults or Ky hooks add.",[11,853,854,855,857,858,860,861,863,864,866,867,869,870,872,873,876],{},"When a transport returns a plain ",[18,856,32],{},", results and errors carry it as ",[18,859,278],{},". Their ",[18,862,66],{}," is a ",[18,865,273],{}," built from the resolved request without its body, or the resolved request itself when ",[18,868,273],{}," cannot parse the URL, such as a relative URL in Node.js. Return a ",[18,871,93],{}," to set both, as ",[298,874,26],{"href":875},"#write-a-transport-function"," shows.",[835,878,880],{"id":879},"what-kys-timeout-covers","What Ky's timeout covers",[11,882,883,884,886,887,304],{},"Ky's ",[18,885,382],{}," only waits for the response headers and never ends a started body or stream. To bound the whole call, pass ",[18,888,889],{},"signal: AbortSignal.timeout(ms)",{"title":891,"searchDepth":892,"depth":892,"links":893},"",5,[894,896,897,898,899,900],{"id":25,"depth":895,"text":26},2,{"id":307,"depth":895,"text":308},{"id":509,"depth":895,"text":510},{"id":603,"depth":895,"text":604},{"id":760,"depth":895,"text":761},{"id":832,"depth":895,"text":833,"children":901},[902,905],{"id":837,"depth":903,"text":904},3,"Results from a plain Response",{"id":879,"depth":903,"text":880},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-ky\u002Fguides\u002Ftransport",1795091984977]