[{"data":1,"prerenderedAt":1001,"buildId":1002},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ftransport":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":1000,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ftransport.md",{},"Use a custom transport",{"type":8,"value":9,"toc":985},"minimark",[10,14,22,27,34,286,306,310,316,560,595,599,609,719,723,729,877,881,931,949,953,959],[11,12,13],"p",{},"Replace the network send of the generated axios client with your own axios instance or 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 AxiosRequestConfig\n stream?: boolean \u002F\u002F the call streams the response, so do not buffer the body\n}\n\ntype TransportResult = { webResponse: Response; request: AxiosRequestConfig; response: AxiosResponse }","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 AxiosRequestConfig",[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,284],{"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},"AxiosRequestConfig",[44,275,266],{"style":50},[44,277,278],{"style":61},"response",[44,280,69],{"style":65},[44,282,283],{"style":54},"AxiosResponse ",[44,285,242],{"style":61},[11,287,288,289,291,292,294,295,297,298,305],{},"Return a ",[18,290,93],{}," to set the native ",[18,293,66],{}," and ",[18,296,278],{}," that results and errors carry. See ",[299,300,302,303],"a",{"href":301},"#results-from-a-plain-response","Results from a plain ",[18,304,32],{},".",[23,307,309],{"id":308},"send-through-your-own-axios-instance","Send through your own axios instance",[11,311,312,315],{},[18,313,314],{},"createAxiosTransport(instance)"," turns any axios instance into a transport, with its defaults, adapter, and interceptors:",[35,317,319],{"language":37,"code":318,"class":39},"import axios from 'axios'\nimport { client, createAxiosTransport } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\nconst instance = axios.create({ timeout: 10_000, headers: { 'X-Client': 'kubb' } })\n\ninstance.interceptors.response.use((response) => {\n console.info(`${response.config.method?.toUpperCase()} ${response.config.url} -> ${response.status}`)\n return response\n})\n\nclient.setConfig({ transport: createAxiosTransport(instance) })",[18,320,321,97,336,97,358,97,360,97,408,97,410,97,440,97,518,97,527,97,532,97,534],{"class":42},[44,322,323,326,329,332],{"class":46,"style":47},[44,324,325],{"style":50},"import ",[44,327,328],{"style":65},"axios ",[44,330,331],{"style":50},"from ",[44,333,335],{"style":334},"color:#032f62;--hls-dark:#9ecbff","'axios'",[44,337,338,340,342,345,347,350,353,355],{"class":46,"style":47},[44,339,325],{"style":50},[44,341,256],{"style":61},[44,343,344],{"style":65},"client",[44,346,159],{"style":50},[44,348,349],{"style":65},"createAxiosTransport ",[44,351,352],{"style":61},"} ",[44,354,331],{"style":50},[44,356,357],{"style":334},"'.\u002Fgen\u002F.kubb\u002Fclient'",[44,359],{"class":46,"style":47},[44,361,362,365,368,370,373,375,378,381,384,386,389,391,393,395,397,400,402,405],{"class":46,"style":47},[44,363,364],{"style":50},"const ",[44,366,367],{"style":65},"instance ",[44,369,58],{"style":50},[44,371,372],{"style":65},"axios",[44,374,305],{"style":50},[44,376,377],{"style":54},"create",[44,379,380],{"style":61},"({ ",[44,382,383],{"style":61},"timeout",[44,385,69],{"style":50},[44,387,388],{"style":61},"10_000",[44,390,159],{"style":50},[44,392,147],{"style":61},[44,394,69],{"style":50},[44,396,256],{"style":61},[44,398,399],{"style":334},"'X-Client'",[44,401,69],{"style":50},[44,403,404],{"style":334},"'kubb' ",[44,406,407],{"style":61},"} })",[44,409],{"class":46,"style":47},[44,411,412,415,417,420,422,424,426,429,432,434,436,438],{"class":46,"style":47},[44,413,414],{"style":65},"instance",[44,416,305],{"style":50},[44,418,419],{"style":61},"interceptors",[44,421,305],{"style":50},[44,423,278],{"style":61},[44,425,305],{"style":50},[44,427,428],{"style":54},"use",[44,430,431],{"style":61},"((",[44,433,278],{"style":65},[44,435,75],{"style":61},[44,437,78],{"style":50},[44,439,111],{"style":61},[44,441,442,444,447,449,452,454,457,460,462,464,467,469,471,474,477,480,482,485,487,489,491,493,495,497,499,502,504,506,508,511,513,515],{"class":46,"style":47},[44,443,116],{"style":61},[44,445,446],{"style":65},"console",[44,448,305],{"style":50},[44,450,451],{"style":54},"info",[44,453,62],{"style":61},[44,455,456],{"style":334},"`",[44,458,459],{"style":65},"${",[44,461,278],{"style":65},[44,463,305],{"style":50},[44,465,466],{"style":61},"config",[44,468,305],{"style":50},[44,470,135],{"style":61},[44,472,473],{"style":50},"?.",[44,475,476],{"style":54},"toUpperCase",[44,478,479],{"style":61},"()",[44,481,242],{"style":65},[44,483,484],{"style":334}," ",[44,486,459],{"style":65},[44,488,278],{"style":65},[44,490,305],{"style":50},[44,492,466],{"style":61},[44,494,305],{"style":50},[44,496,119],{"style":61},[44,498,242],{"style":65},[44,500,501],{"style":334}," -> ",[44,503,459],{"style":65},[44,505,278],{"style":65},[44,507,305],{"style":50},[44,509,510],{"style":61},"status",[44,512,242],{"style":65},[44,514,456],{"style":334},[44,516,517],{"style":61},")",[44,519,520,522,525],{"class":46,"style":47},[44,521,116],{"style":61},[44,523,524],{"style":50},"return ",[44,526,278],{"style":65},[44,528,529],{"class":46,"style":47},[44,530,531],{"style":61},"})",[44,533],{"class":46,"style":47},[44,535,536,538,540,543,545,548,550,553,555,557],{"class":46,"style":47},[44,537,344],{"style":65},[44,539,305],{"style":50},[44,541,542],{"style":54},"setConfig",[44,544,380],{"style":61},[44,546,547],{"style":61},"transport",[44,549,69],{"style":50},[44,551,552],{"style":54},"createAxiosTransport",[44,554,62],{"style":61},[44,556,414],{"style":65},[44,558,559],{"style":61},") })",[561,562,564,565,159,567,159,569,159,571,159,574,159,577,159,580,583,584,587,588,590,591,594],"blockquote",{"as":563},"note","Kubb sets ",[18,566,119],{},[18,568,135],{},[18,570,147],{},[18,572,573],{},"data",[18,575,576],{},"transformRequest",[18,578,579],{},"transformResponse",[18,581,582],{},"responseType",", and ",[18,585,586],{},"validateStatus",", so leave those alone. For one call, pass ",[18,589,214],{},", such as ",[18,592,593],{},"{ timeout }",", instead of a new instance.",[23,596,598],{"id":597},"wrap-the-default-transport","Wrap the default transport",[11,600,601,604,605,608],{},[18,602,603],{},"defaultTransport"," sends through the instance ",[18,606,607],{},"getAxiosInstance()"," returns. Configure that instance, or wrap the transport to add tracing or timing:",[35,610,612],{"language":37,"code":611,"class":39},"import { client, defaultTransport, getAxiosInstance } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\ngetAxiosInstance().defaults.timeout = 10_000\n\nclient.setConfig({\n transport: (request) => withSpan(request, () => defaultTransport(request)),\n})",[18,613,614,97,637,97,639,97,660,97,662,97,673,97,715],{"class":42},[44,615,616,618,620,622,624,626,628,631,633,635],{"class":46,"style":47},[44,617,325],{"style":50},[44,619,256],{"style":61},[44,621,344],{"style":65},[44,623,159],{"style":50},[44,625,603],{"style":65},[44,627,159],{"style":50},[44,629,630],{"style":65},"getAxiosInstance ",[44,632,352],{"style":61},[44,634,331],{"style":50},[44,636,357],{"style":334},[44,638],{"class":46,"style":47},[44,640,641,644,646,648,651,653,656,658],{"class":46,"style":47},[44,642,643],{"style":54},"getAxiosInstance",[44,645,479],{"style":61},[44,647,305],{"style":50},[44,649,650],{"style":61},"defaults",[44,652,305],{"style":50},[44,654,655],{"style":61},"timeout ",[44,657,58],{"style":50},[44,659,388],{"style":61},[44,661],{"class":46,"style":47},[44,663,664,666,668,670],{"class":46,"style":47},[44,665,344],{"style":65},[44,667,305],{"style":50},[44,669,542],{"style":54},[44,671,672],{"style":61},"({",[44,674,675,677,679,681,683,685,687,689,692,694,696,698,701,703,705,707,709,712],{"class":46,"style":47},[44,676,116],{"style":61},[44,678,547],{"style":61},[44,680,69],{"style":50},[44,682,62],{"style":61},[44,684,66],{"style":65},[44,686,75],{"style":61},[44,688,78],{"style":50},[44,690,691],{"style":54},"withSpan",[44,693,62],{"style":61},[44,695,66],{"style":65},[44,697,159],{"style":50},[44,699,700],{"style":61},"() ",[44,702,78],{"style":50},[44,704,603],{"style":54},[44,706,62],{"style":61},[44,708,66],{"style":65},[44,710,711],{"style":61},"))",[44,713,714],{"style":50},",",[44,716,717],{"class":46,"style":47},[44,718,531],{"style":61},[23,720,722],{"id":721},"mock-the-network-in-tests","Mock the network in tests",[11,724,725,726,728],{},"Return a fixed ",[18,727,32],{}," from a separate client, and pass it to the call or to a query plugin:",[35,730,732],{"language":37,"code":731,"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,733,734,97,749,97,765,97,767,97,822,97,824,97,872],{"class":42},[44,735,736,738,740,743,745,747],{"class":46,"style":47},[44,737,325],{"style":50},[44,739,256],{"style":61},[44,741,742],{"style":65},"createClient ",[44,744,352],{"style":61},[44,746,331],{"style":50},[44,748,357],{"style":334},[44,750,751,753,755,758,760,762],{"class":46,"style":47},[44,752,325],{"style":50},[44,754,256],{"style":61},[44,756,757],{"style":65},"getPetById ",[44,759,352],{"style":61},[44,761,331],{"style":50},[44,763,764],{"style":334},"'.\u002Fgen\u002Fclients\u002FgetPetById'",[44,766],{"class":46,"style":47},[44,768,769,771,774,776,779,781,783,785,788,790,792,794,796,799,801,804,806,809,811,814,816,819],{"class":46,"style":47},[44,770,364],{"style":50},[44,772,773],{"style":65},"testClient ",[44,775,58],{"style":50},[44,777,778],{"style":54},"createClient",[44,780,380],{"style":61},[44,782,547],{"style":61},[44,784,69],{"style":50},[44,786,787],{"style":50},"async ",[44,789,700],{"style":61},[44,791,78],{"style":50},[44,793,32],{"style":54},[44,795,305],{"style":50},[44,797,798],{"style":54},"json",[44,800,380],{"style":61},[44,802,803],{"style":61},"id",[44,805,69],{"style":50},[44,807,808],{"style":61},"1",[44,810,159],{"style":50},[44,812,813],{"style":61},"name",[44,815,69],{"style":50},[44,817,818],{"style":334},"'Fluffy' ",[44,820,821],{"style":61},"}) })",[44,823],{"class":46,"style":47},[44,825,826,828,830,833,835,837,840,843,845,848,850,852,855,857,860,862,864,866,868,870],{"class":46,"style":47},[44,827,364],{"style":50},[44,829,256],{"style":61},[44,831,832],{"style":65},"data ",[44,834,352],{"style":61},[44,836,58],{"style":50},[44,838,839],{"style":50},"await ",[44,841,842],{"style":54},"getPetById",[44,844,380],{"style":61},[44,846,847],{"style":61},"path",[44,849,69],{"style":50},[44,851,256],{"style":61},[44,853,854],{"style":61},"petId",[44,856,69],{"style":50},[44,858,859],{"style":61},"1 ",[44,861,242],{"style":61},[44,863,159],{"style":50},[44,865,344],{"style":61},[44,867,69],{"style":50},[44,869,773],{"style":65},[44,871,531],{"style":61},[44,873,874],{"class":46,"style":47},[44,875,876],{"style":127},"\u002F\u002F ^ { id: 1, name: 'Fluffy' }",[23,878,880],{"id":879},"where-to-set-it","Where to set it",[882,883,884,897],"table",{},[885,886,887],"thead",{},[888,889,890,894],"tr",{},[891,892,893],"th",{},"Scope",[891,895,896],{},"How",[898,899,900,911,921],"tbody",{},[888,901,902,906],{},[903,904,905],"td",{},"Every call",[903,907,908],{},[18,909,910],{},"client.setConfig({ transport })",[888,912,913,916],{},[903,914,915],{},"A separate client",[903,917,918],{},[18,919,920],{},"createClient({ transport })",[888,922,923,926],{},[903,924,925],{},"One call",[903,927,928,930],{},[18,929,547],{}," on the call",[11,932,933,934,937,938,940,941,943,944,946,947,305],{},"A client from ",[18,935,936],{},"createClient()"," copies the shared ",[18,939,547],{}," at creation, so a later ",[18,942,910],{}," does not reach it. Without a ",[18,945,547],{},", it sends through ",[18,948,607],{},[23,950,952],{"id":951},"less-common-cases","Less common cases",[954,955,302,957],"h3",{"id":956},"results-from-a-plain-response",[18,958,32],{},[11,960,961,962,964,965,967,968,970,971,974,975,977,978,980,981,984],{},"When a transport returns a plain ",[18,963,32],{},", results and errors carry it as ",[18,966,278],{},". Their ",[18,969,66],{}," is a ",[18,972,973],{},"Request"," built from the resolved request without its body, or the resolved request itself when ",[18,976,973],{}," cannot parse the URL, such as a relative URL in Node.js. Return a ",[18,979,93],{}," to set both, as ",[299,982,26],{"href":983},"#write-a-transport-function"," shows.",{"title":986,"searchDepth":987,"depth":987,"links":988},"",5,[989,991,992,993,994,995],{"id":25,"depth":990,"text":26},2,{"id":308,"depth":990,"text":309},{"id":597,"depth":990,"text":598},{"id":721,"depth":990,"text":722},{"id":879,"depth":990,"text":880},{"id":951,"depth":990,"text":952,"children":996},[997],{"id":956,"depth":998,"text":999},3,"Results from a plain Response","\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ftransport",1795727683542,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]