[{"data":1,"prerenderedAt":1068,"buildId":1069},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Fcalling-operations":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":1067,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Fcalling-operations.md",{},"Call operations",{"type":8,"value":9,"toc":1051},"minimark",[10,14,19,41,232,258,282,286,296,425,435,547,551,558,605,658,668,672,688,796,800,827,871,893,897,903,992,1003,1007,1012,1028,1031],[11,12,13],"p",{},"Call a generated function with grouped request options, read the typed result, and pass content types or native fetch 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: Request \u002F\u002F the native request\n response: Response \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},"Request ",[51,404,405],{"style":143},"\u002F\u002F the native request",[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},"Response ",[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-fetch\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-fetch-options","Pass native fetch options",[11,801,802,803,806,807,810,811,26,814,26,817,26,820,823,824,295],{},"Pass ",[23,804,805],{},"options"," for ",[23,808,809],{},"fetch"," settings such as ",[23,812,813],{},"cache",[23,815,816],{},"mode",[23,818,819],{},"redirect",[23,821,822],{},"keepalive",", or Next.js's ",[23,825,826],{},"next",[42,828,830],{"language":44,"code":829,"class":46},"await getPetById({ path: { petId: 1 }, options: { cache: 'no-store' } })",[23,831,832],{"class":49},[51,833,834,836,838,840,842,844,846,848,850,852,854,856,858,860,862,864,866,869],{"class":53,"style":54},[51,835,113],{"style":57},[51,837,117],{"style":116},[51,839,120],{"style":61},[51,841,25],{"style":61},[51,843,125],{"style":57},[51,845,62],{"style":61},[51,847,130],{"style":61},[51,849,125],{"style":57},[51,851,135],{"style":61},[51,853,170],{"style":61},[51,855,26],{"style":57},[51,857,805],{"style":61},[51,859,125],{"style":57},[51,861,62],{"style":61},[51,863,813],{"style":61},[51,865,125],{"style":57},[51,867,868],{"style":75},"'no-store' ",[51,870,138],{"style":61},[11,872,873,876,877,879,880,883,884,888,889,892],{},[23,874,875],{},"client.setConfig({ options })"," sets shared defaults, and per-call options win. Kubb sets the method, headers, body, and signal over them, and keeps control of status handling. ",[23,878,809],{}," rejects the ",[23,881,882],{},"TRACE"," method, so see ",[244,885,887],{"href":886},"#trace-operations","TRACE operations"," for a ",[23,890,891],{},"trace"," operation.",[15,894,896],{"id":895},"build-a-url-without-sending","Build a URL without sending",[11,898,899,902],{},[23,900,901],{},"client.getUrl"," builds the URL a request would use, with the same base URL and serialization:",[42,904,906],{"language":44,"code":905,"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,907,908,77,924,77,926,77,987],{"class":49},[51,909,910,912,914,917,919,921],{"class":53,"style":54},[51,911,58],{"style":57},[51,913,62],{"style":61},[51,915,916],{"style":65},"client ",[51,918,69],{"style":61},[51,920,72],{"style":57},[51,922,923],{"style":75},"'.\u002Fgen\u002F.kubb\u002Fclient'",[51,925],{"class":53,"style":54},[51,927,928,930,933,935,938,940,943,945,948,950,953,955,957,959,961,963,965,967,969,971,973,975,977,980,982,985],{"class":53,"style":54},[51,929,100],{"style":57},[51,931,932],{"style":65},"url ",[51,934,110],{"style":57},[51,936,937],{"style":65},"client",[51,939,281],{"style":57},[51,941,942],{"style":116},"getUrl",[51,944,120],{"style":61},[51,946,947],{"style":61},"url",[51,949,125],{"style":57},[51,951,952],{"style":75},"'\u002Fpets\u002F{petId}'",[51,954,26],{"style":57},[51,956,25],{"style":61},[51,958,125],{"style":57},[51,960,62],{"style":61},[51,962,130],{"style":61},[51,964,125],{"style":57},[51,966,135],{"style":61},[51,968,170],{"style":61},[51,970,26],{"style":57},[51,972,29],{"style":61},[51,974,125],{"style":57},[51,976,62],{"style":61},[51,978,979],{"style":61},"fields",[51,981,125],{"style":57},[51,983,984],{"style":75},"'name' ",[51,986,138],{"style":61},[51,988,989],{"class":53,"style":54},[51,990,991],{"style":143},"\u002F\u002F https:\u002F\u002Fpetstore.swagger.io\u002Fv2\u002Fpets\u002F1?fields=name",[11,993,994,995,999,1000,281],{},"Next, ",[244,996,998],{"href":997},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Ferror-handling","handle errors",", including ",[23,1001,1002],{},"throwOnError: false",[15,1004,1006],{"id":1005},"less-common-cases","Less common cases",[1008,1009,1011],"h3",{"id":1010},"path-parameter-casing","Path parameter casing",[11,1013,1014,1015,1019,1020,1023,1024,1027],{},"When you set ",[244,1016,1017],{"href":246},[23,1018,249],{},", the HTTP request stays the same, because path parameter names never reach the wire. The exception is the ",[23,1021,1022],{},"matrix"," style, which sends the name (",[23,1025,1026],{},";group-id=42","), so an operation with a matrix path parameter keeps its spec names.",[1008,1029,887],{"id":1030},"trace-operations",[11,1032,1033,1034,1036,1037,879,1039,1041,1042,1045,1046,1050],{},"When the spec has a ",[23,1035,891],{}," operation, Kubb generates a call for it, but ",[23,1038,809],{},[23,1040,882],{}," method. Use ",[23,1043,1044],{},"@kubb\u002Fplugin-axios"," or a ",[244,1047,1049],{"href":1048},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Ftransport","custom transport"," for it.",{"title":1052,"searchDepth":1053,"depth":1053,"links":1054},"",5,[1055,1057,1058,1059,1060,1061,1062],{"id":17,"depth":1056,"text":18},2,{"id":284,"depth":1056,"text":285},{"id":549,"depth":1056,"text":550},{"id":670,"depth":1056,"text":671},{"id":798,"depth":1056,"text":799},{"id":895,"depth":1056,"text":896},{"id":1005,"depth":1056,"text":1006,"children":1063},[1064,1066],{"id":1010,"depth":1065,"text":1011},3,{"id":1030,"depth":1065,"text":887},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch\u002Fguides\u002Fcalling-operations",1795727684459,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]