[{"data":1,"prerenderedAt":1028,"buildId":1029},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Fcalling-operations":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":1027,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Fcalling-operations.md",{},"Call operations",{"type":8,"value":9,"toc":1012},"minimark",[10,14,19,41,232,258,282,286,296,425,435,547,551,558,605,658,668,672,688,796,800,822,866,877,881,887,976,987,991,996],[11,12,13],"p",{},"Call a generated function with grouped request options, read the typed result, and pass content types or native axios settings for one call.",[15,16,18],"h2",{"id":17},"call-an-operation","Call an operation",[11,20,21,22,26,27,26,30,26,33,36,37,40],{},"Pass each request part under its own key: ",[23,24,25],"code",{},"path",", ",[23,28,29],{},"query",[23,31,32],{},"headers",[23,34,35],{},"cookies",", or ",[23,38,39],{},"body",". A key exists only when the operation declares that part:",[42,43,47],"pre",{"language":44,"code":45,"class":46},"typescript","import { getPetById } from '.\u002Fgen\u002Fclients\u002FgetPetById'\nimport { updatePet } from '.\u002Fgen\u002Fclients\u002FupdatePet'\n\nconst { data } = await getPetById({ path: { petId: 1 } })\n\u002F\u002F ^ the parsed pet, typed from the 200 response\n\nawait updatePet({ path: { petId: 1 }, headers: { 'X-Request-ID': 'r-1' }, body: { name: 'Rex', status: 'sold' } })\n\nawait getStatus() \u002F\u002F no required parameters, no argument","highlights",[23,48,50,77,78,77,94,77,96,77,139,77,145,77,147,77,217,77,219],{"class":49},"language-typescript",[51,52,55,59,63,67,70,73],"span",{"class":53,"style":54},"line","display: inline",[51,56,58],{"style":57},"color:#d73a49;--hls-dark:#f97583","import ",[51,60,62],{"style":61},"color:#005cc5;--hls-dark:#79b8ff","{ ",[51,64,66],{"style":65},"color:#24292e;--hls-dark:#e1e4e8","getPetById ",[51,68,69],{"style":61},"} ",[51,71,72],{"style":57},"from ",[51,74,76],{"style":75},"color:#032f62;--hls-dark:#9ecbff","'.\u002Fgen\u002Fclients\u002FgetPetById'","\n",[51,79,80,82,84,87,89,91],{"class":53,"style":54},[51,81,58],{"style":57},[51,83,62],{"style":61},[51,85,86],{"style":65},"updatePet ",[51,88,69],{"style":61},[51,90,72],{"style":57},[51,92,93],{"style":75},"'.\u002Fgen\u002Fclients\u002FupdatePet'",[51,95],{"class":53,"style":54},[51,97,98,101,103,106,108,111,114,118,121,123,126,128,131,133,136],{"class":53,"style":54},[51,99,100],{"style":57},"const ",[51,102,62],{"style":61},[51,104,105],{"style":65},"data ",[51,107,69],{"style":61},[51,109,110],{"style":57},"= ",[51,112,113],{"style":57},"await ",[51,115,117],{"style":116},"color:#6f42c1;--hls-dark:#b392f0","getPetById",[51,119,120],{"style":61},"({ ",[51,122,25],{"style":61},[51,124,125],{"style":57},": ",[51,127,62],{"style":61},[51,129,130],{"style":61},"petId",[51,132,125],{"style":57},[51,134,135],{"style":61},"1 ",[51,137,138],{"style":61},"} })",[51,140,141],{"class":53,"style":54},[51,142,144],{"style":143},"color:#6a737d;--hls-dark:#6a737d","\u002F\u002F ^ the parsed pet, typed from the 200 response",[51,146],{"class":53,"style":54},[51,148,149,151,154,156,158,160,162,164,166,168,171,173,175,177,179,182,184,187,189,191,193,195,197,200,202,205,207,210,212,215],{"class":53,"style":54},[51,150,113],{"style":57},[51,152,153],{"style":116},"updatePet",[51,155,120],{"style":61},[51,157,25],{"style":61},[51,159,125],{"style":57},[51,161,62],{"style":61},[51,163,130],{"style":61},[51,165,125],{"style":57},[51,167,135],{"style":61},[51,169,170],{"style":61},"}",[51,172,26],{"style":57},[51,174,32],{"style":61},[51,176,125],{"style":57},[51,178,62],{"style":61},[51,180,181],{"style":75},"'X-Request-ID'",[51,183,125],{"style":57},[51,185,186],{"style":75},"'r-1' ",[51,188,170],{"style":61},[51,190,26],{"style":57},[51,192,39],{"style":61},[51,194,125],{"style":57},[51,196,62],{"style":61},[51,198,199],{"style":61},"name",[51,201,125],{"style":57},[51,203,204],{"style":75},"'Rex'",[51,206,26],{"style":57},[51,208,209],{"style":61},"status",[51,211,125],{"style":57},[51,213,214],{"style":75},"'sold' ",[51,216,138],{"style":61},[51,218],{"class":53,"style":54},[51,220,221,223,226,229],{"class":53,"style":54},[51,222,113],{"style":57},[51,224,225],{"style":116},"getStatus",[51,227,228],{"style":61},"() ",[51,230,231],{"style":143},"\u002F\u002F no required parameters, no argument",[11,233,234,235,238,239,242,243,250,251,254,255,257],{},"Path keys keep the spec's names, so ",[23,236,237],{},"{pet-id}"," takes ",[23,240,241],{},"path: { 'pet-id': 1 }",". Set ",[244,245,247],"a",{"href":246},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fadapter-oas\u002Foptions#pathparamscasing",[23,248,249],{},"pathParamsCasing: 'camelcase'"," on the adapter (",[23,252,253],{},"@kubb\u002Fadapter-oas"," 5.20.0 or later) to pass ",[23,256,130],{}," instead.",[259,260,262,263,269,270,273,274,277,278,281],"blockquote",{"as":261},"note","The examples set ",[244,264,266],{"href":265},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fadapter-oas\u002Foptions#integertype",[23,267,268],{},"integerType: 'number'",". With the default ",[23,271,272],{},"bigint",", an ",[23,275,276],{},"int64"," id takes ",[23,279,280],{},"1n",".",[15,283,285],{"id":284},"read-the-result","Read the result",[11,287,288,289,292,293,295],{},"A call resolves to a ",[23,290,291],{},"RequestResult",", a union narrowed by the numeric ",[23,294,209],{},":",[42,297,299],{"language":44,"code":298,"class":46},"type RequestResult = {\n status: number\n data: TData \u002F\u002F the parsed success body, undefined on an error result\n error: TError \u002F\u002F the parsed error body, undefined on a success result\n contentType: string | undefined \u002F\u002F the response media type, without parameters\n headers: Headers \u002F\u002F the response headers\n request: AxiosRequestConfig \u002F\u002F the native request config\n response: AxiosResponse \u002F\u002F the native response\n}",[23,300,301,77,314,77,326,77,341,77,356,77,377,77,391,77,406,77,421],{"class":49},[51,302,303,306,309,311],{"class":53,"style":54},[51,304,305],{"style":57},"type ",[51,307,308],{"style":116},"RequestResult ",[51,310,110],{"style":57},[51,312,313],{"style":61},"{",[51,315,316,319,321,323],{"class":53,"style":54},[51,317,318],{"style":61}," ",[51,320,209],{"style":61},[51,322,125],{"style":65},[51,324,325],{"style":116},"number",[51,327,328,330,333,335,338],{"class":53,"style":54},[51,329,318],{"style":116},[51,331,332],{"style":65},"data",[51,334,125],{"style":65},[51,336,337],{"style":116},"TData ",[51,339,340],{"style":143},"\u002F\u002F the parsed success body, undefined on an error result",[51,342,343,345,348,350,353],{"class":53,"style":54},[51,344,318],{"style":143},[51,346,347],{"style":65},"error",[51,349,125],{"style":65},[51,351,352],{"style":116},"TError ",[51,354,355],{"style":143},"\u002F\u002F the parsed error body, undefined on a success result",[51,357,358,360,363,365,368,371,374],{"class":53,"style":54},[51,359,318],{"style":143},[51,361,362],{"style":65},"contentType",[51,364,125],{"style":65},[51,366,367],{"style":116},"string ",[51,369,370],{"style":57},"| ",[51,372,373],{"style":61},"undefined ",[51,375,376],{"style":143},"\u002F\u002F the response media type, without parameters",[51,378,379,381,383,385,388],{"class":53,"style":54},[51,380,318],{"style":143},[51,382,32],{"style":65},[51,384,125],{"style":65},[51,386,387],{"style":116},"Headers ",[51,389,390],{"style":143},"\u002F\u002F the response headers",[51,392,393,395,398,400,403],{"class":53,"style":54},[51,394,318],{"style":143},[51,396,397],{"style":65},"request",[51,399,125],{"style":65},[51,401,402],{"style":116},"AxiosRequestConfig ",[51,404,405],{"style":143},"\u002F\u002F the native request config",[51,407,408,410,413,415,418],{"class":53,"style":54},[51,409,318],{"style":143},[51,411,412],{"style":65},"response",[51,414,125],{"style":65},[51,416,417],{"style":116},"AxiosResponse ",[51,419,420],{"style":143},"\u002F\u002F the native response",[51,422,423],{"class":53,"style":54},[51,424,170],{"style":61},[11,426,427,428,431,432,434],{},"With the default ",[23,429,430],{},"throwOnError: true",", a resolved call is always a success. Narrow on ",[23,433,209],{}," when an operation has several success statuses:",[42,436,438],{"language":44,"code":437,"class":46},"const result = await getPetById({ path: { petId: 1 } })\n\nconsole.info(result.headers.get('x-ratelimit-remaining')) \u002F\u002F suggests the declared header names\nif (result.status === 200) console.info(result.data)",[23,439,440,77,469,77,471,77,507],{"class":49},[51,441,442,444,447,449,451,453,455,457,459,461,463,465,467],{"class":53,"style":54},[51,443,100],{"style":57},[51,445,446],{"style":65},"result ",[51,448,110],{"style":57},[51,450,113],{"style":57},[51,452,117],{"style":116},[51,454,120],{"style":61},[51,456,25],{"style":61},[51,458,125],{"style":57},[51,460,62],{"style":61},[51,462,130],{"style":61},[51,464,125],{"style":57},[51,466,135],{"style":61},[51,468,138],{"style":61},[51,470],{"class":53,"style":54},[51,472,473,476,478,481,484,487,489,491,493,496,498,501,504],{"class":53,"style":54},[51,474,475],{"style":65},"console",[51,477,281],{"style":57},[51,479,480],{"style":116},"info",[51,482,483],{"style":61},"(",[51,485,486],{"style":65},"result",[51,488,281],{"style":57},[51,490,32],{"style":61},[51,492,281],{"style":57},[51,494,495],{"style":116},"get",[51,497,483],{"style":61},[51,499,500],{"style":75},"'x-ratelimit-remaining'",[51,502,503],{"style":61},")) ",[51,505,506],{"style":143},"\u002F\u002F suggests the declared header names",[51,508,509,512,514,516,518,521,524,527,530,532,534,536,538,540,542,544],{"class":53,"style":54},[51,510,511],{"style":57},"if ",[51,513,483],{"style":61},[51,515,486],{"style":65},[51,517,281],{"style":57},[51,519,520],{"style":61},"status ",[51,522,523],{"style":57},"=== ",[51,525,526],{"style":61},"200",[51,528,529],{"style":61},") ",[51,531,475],{"style":65},[51,533,281],{"style":57},[51,535,480],{"style":116},[51,537,483],{"style":61},[51,539,486],{"style":65},[51,541,281],{"style":57},[51,543,332],{"style":61},[51,545,546],{"style":61},")",[15,548,550],{"id":549},"unwrap-the-success-body","Unwrap the success body",[11,552,553,554,557],{},"Call ",[23,555,556],{},".unwrap()"," to get only the success body:",[42,559,561],{"language":44,"code":560,"class":46},"const pet = await getPetById({ path: { petId: 1 } }).unwrap()\n\u002F\u002F ^ the parsed pet",[23,562,563,77,600],{"class":49},[51,564,565,567,570,572,574,576,578,580,582,584,586,588,590,592,594,597],{"class":53,"style":54},[51,566,100],{"style":57},[51,568,569],{"style":65},"pet ",[51,571,110],{"style":57},[51,573,113],{"style":57},[51,575,117],{"style":116},[51,577,120],{"style":61},[51,579,25],{"style":61},[51,581,125],{"style":57},[51,583,62],{"style":61},[51,585,130],{"style":61},[51,587,125],{"style":57},[51,589,135],{"style":61},[51,591,138],{"style":61},[51,593,281],{"style":57},[51,595,596],{"style":116},"unwrap",[51,598,599],{"style":61},"()",[51,601,602],{"class":53,"style":54},[51,603,604],{"style":143},"\u002F\u002F ^ the parsed pet",[606,607,608,625],"table",{},[609,610,611],"thead",{},[612,613,614,620],"tr",{},[615,616,617],"th",{},[23,618,619],{},"throwOnError",[615,621,622,623],{},"A non-2xx response makes ",[23,624,556],{},[626,627,628,648],"tbody",{},[612,629,630,637],{},[631,632,633,636],"td",{},[23,634,635],{},"true"," (default)",[631,638,639,640,643,644,647],{},"throw a ",[23,641,642],{},"ResponseError",", as a plain ",[23,645,646],{},"await"," does.",[612,649,650,655],{},[631,651,652],{},[23,653,654],{},"false",[631,656,657],{},"reject with the parsed error body.",[11,659,660,661,667],{},"To get the body from every call, set ",[244,662,664],{"href":663},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Foptions#returntype",[23,665,666],{},"returnType: 'data'"," on the plugin.",[15,669,671],{"id":670},"set-the-content-type","Set the content type",[11,673,674,675,677,678,681,682,684,685,295],{},"When an operation accepts or returns several content types, set ",[23,676,362],{}," on the call. A string sets ",[23,679,680],{},"Content-Type",", and ",[23,683,412],{}," sets ",[23,686,687],{},"Accept",[689,690,691,741],"code-group",{},[42,692,695],{"language":44,"filename":693,"code":694,"class":46},"Request only","await uploadAvatar({ path: { petId: 1 }, body: avatarBlob, contentType: 'image\u002Fpng' })",[23,696,697],{"class":49},[51,698,699,701,704,706,708,710,712,714,716,718,720,722,724,726,729,731,733,735,738],{"class":53,"style":54},[51,700,113],{"style":57},[51,702,703],{"style":116},"uploadAvatar",[51,705,120],{"style":61},[51,707,25],{"style":61},[51,709,125],{"style":57},[51,711,62],{"style":61},[51,713,130],{"style":61},[51,715,125],{"style":57},[51,717,135],{"style":61},[51,719,170],{"style":61},[51,721,26],{"style":57},[51,723,39],{"style":61},[51,725,125],{"style":57},[51,727,728],{"style":65},"avatarBlob",[51,730,26],{"style":57},[51,732,362],{"style":61},[51,734,125],{"style":57},[51,736,737],{"style":75},"'image\u002Fpng' ",[51,739,740],{"style":61},"})",[42,742,745],{"language":44,"filename":743,"code":744,"class":46},"Request and Accept","await getPet({ path: { petId: 1 }, contentType: { request: 'application\u002Fjson', response: 'application\u002Fxml' } })",[23,746,747],{"class":49},[51,748,749,751,754,756,758,760,762,764,766,768,770,772,774,776,778,780,782,785,787,789,791,794],{"class":53,"style":54},[51,750,113],{"style":57},[51,752,753],{"style":116},"getPet",[51,755,120],{"style":61},[51,757,25],{"style":61},[51,759,125],{"style":57},[51,761,62],{"style":61},[51,763,130],{"style":61},[51,765,125],{"style":57},[51,767,135],{"style":61},[51,769,170],{"style":61},[51,771,26],{"style":57},[51,773,362],{"style":61},[51,775,125],{"style":57},[51,777,62],{"style":61},[51,779,397],{"style":61},[51,781,125],{"style":57},[51,783,784],{"style":75},"'application\u002Fjson'",[51,786,26],{"style":57},[51,788,412],{"style":61},[51,790,125],{"style":57},[51,792,793],{"style":75},"'application\u002Fxml' ",[51,795,138],{"style":61},[15,797,799],{"id":798},"pass-native-axios-options","Pass native axios options",[11,801,802,803,806,807,26,810,26,813,26,816,36,819,295],{},"Pass ",[23,804,805],{},"options"," for axios settings such as ",[23,808,809],{},"timeout",[23,811,812],{},"proxy",[23,814,815],{},"maxRedirects",[23,817,818],{},"decompress",[23,820,821],{},"onUploadProgress",[42,823,825],{"language":44,"code":824,"class":46},"await getPetById({ path: { petId: 1 }, options: { timeout: 5_000 } })",[23,826,827],{"class":49},[51,828,829,831,833,835,837,839,841,843,845,847,849,851,853,855,857,859,861,864],{"class":53,"style":54},[51,830,113],{"style":57},[51,832,117],{"style":116},[51,834,120],{"style":61},[51,836,25],{"style":61},[51,838,125],{"style":57},[51,840,62],{"style":61},[51,842,130],{"style":61},[51,844,125],{"style":57},[51,846,135],{"style":61},[51,848,170],{"style":61},[51,850,26],{"style":57},[51,852,805],{"style":61},[51,854,125],{"style":57},[51,856,62],{"style":61},[51,858,809],{"style":61},[51,860,125],{"style":57},[51,862,863],{"style":61},"5_000 ",[51,865,138],{"style":61},[11,867,868,871,872,876],{},[23,869,870],{},"client.setConfig({ options })"," sets shared defaults, and per-call options win. Kubb keeps control of the URL, method, headers, body, and status handling, as ",[244,873,875],{"href":874},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ftransport","Use a custom transport"," lists.",[15,878,880],{"id":879},"build-a-url-without-sending","Build a URL without sending",[11,882,883,886],{},[23,884,885],{},"client.getUrl"," builds the URL a request would use, with the same base URL and serialization:",[42,888,890],{"language":44,"code":889,"class":46},"import { client } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\nconst url = client.getUrl({ url: '\u002Fpets\u002F{petId}', path: { petId: 1 }, query: { fields: 'name' } })\n\u002F\u002F https:\u002F\u002Fpetstore.swagger.io\u002Fv2\u002Fpets\u002F1?fields=name",[23,891,892,77,908,77,910,77,971],{"class":49},[51,893,894,896,898,901,903,905],{"class":53,"style":54},[51,895,58],{"style":57},[51,897,62],{"style":61},[51,899,900],{"style":65},"client ",[51,902,69],{"style":61},[51,904,72],{"style":57},[51,906,907],{"style":75},"'.\u002Fgen\u002F.kubb\u002Fclient'",[51,909],{"class":53,"style":54},[51,911,912,914,917,919,922,924,927,929,932,934,937,939,941,943,945,947,949,951,953,955,957,959,961,964,966,969],{"class":53,"style":54},[51,913,100],{"style":57},[51,915,916],{"style":65},"url ",[51,918,110],{"style":57},[51,920,921],{"style":65},"client",[51,923,281],{"style":57},[51,925,926],{"style":116},"getUrl",[51,928,120],{"style":61},[51,930,931],{"style":61},"url",[51,933,125],{"style":57},[51,935,936],{"style":75},"'\u002Fpets\u002F{petId}'",[51,938,26],{"style":57},[51,940,25],{"style":61},[51,942,125],{"style":57},[51,944,62],{"style":61},[51,946,130],{"style":61},[51,948,125],{"style":57},[51,950,135],{"style":61},[51,952,170],{"style":61},[51,954,26],{"style":57},[51,956,29],{"style":61},[51,958,125],{"style":57},[51,960,62],{"style":61},[51,962,963],{"style":61},"fields",[51,965,125],{"style":57},[51,967,968],{"style":75},"'name' ",[51,970,138],{"style":61},[51,972,973],{"class":53,"style":54},[51,974,975],{"style":143},"\u002F\u002F https:\u002F\u002Fpetstore.swagger.io\u002Fv2\u002Fpets\u002F1?fields=name",[11,977,978,979,983,984,281],{},"Next, ",[244,980,982],{"href":981},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Ferror-handling","handle errors",", including ",[23,985,986],{},"throwOnError: false",[15,988,990],{"id":989},"less-common-cases","Less common cases",[992,993,995],"h3",{"id":994},"path-parameter-casing","Path parameter casing",[11,997,998,999,1003,1004,1007,1008,1011],{},"When you set ",[244,1000,1001],{"href":246},[23,1002,249],{},", the HTTP request stays the same, because path parameter names never reach the wire. The exception is the ",[23,1005,1006],{},"matrix"," style, which sends the name (",[23,1009,1010],{},";group-id=42","), so an operation with a matrix path parameter keeps its spec names.",{"title":1013,"searchDepth":1014,"depth":1014,"links":1015},"",5,[1016,1018,1019,1020,1021,1022,1023],{"id":17,"depth":1017,"text":18},2,{"id":284,"depth":1017,"text":285},{"id":549,"depth":1017,"text":550},{"id":670,"depth":1017,"text":671},{"id":798,"depth":1017,"text":799},{"id":879,"depth":1017,"text":880},{"id":989,"depth":1017,"text":990,"children":1024},[1025],{"id":994,"depth":1026,"text":995},3,"\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Fcalling-operations",1795727683030,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]