[{"data":1,"prerenderedAt":772,"buildId":773},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-swr\u002Fguides\u002Fcalling-operations":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":771,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-swr\u002Fguides\u002Fcalling-operations.md",{},"Call operations",{"type":8,"value":9,"toc":761},"minimark",[10,23,28,35,120,124,149,497,523,527,547,651,655,678,727,731,741],[11,12,13,14,18,19,22],"p",{},"Use the generated ",[15,16,17],"code",{},"useSWR"," and ",[15,20,21],{},"useSWRMutation"," hooks, and skip or freeze a request with the Kubb options.",[24,25,27],"h2",{"id":26},"what-each-plugin-generates","What each plugin generates",[11,29,30,31,34],{},"Each hook and its helpers live in a file named after the hook, such as ",[15,32,33],{},"src\u002Fgen\u002Fhooks\u002FuseGetPetById.ts",".",[36,37,38,51],"table",{},[39,40,41],"thead",{},[42,43,44,48],"tr",{},[45,46,47],"th",{},"Operation",[45,49,50],{},"Exports",[52,53,54,79,104,112],"tbody",{},[42,55,56,68],{},[57,58,59,60,63,64,67],"td",{},"Query (",[15,61,62],{},"GET",", ",[15,65,66],{},"QUERY",")",[57,69,70,63,73,63,76],{},[15,71,72],{},"getPetByIdQueryKey",[15,74,75],{},"getPetByIdQueryOptions",[15,77,78],{},"useGetPetById",[42,80,81,96],{},[57,82,83,84,63,87,63,90,63,93,67],{},"Mutation (",[15,85,86],{},"POST",[15,88,89],{},"PUT",[15,91,92],{},"PATCH",[15,94,95],{},"DELETE",[57,97,98,63,101],{},[15,99,100],{},"updatePetMutationKey",[15,102,103],{},"useUpdatePet",[42,105,106,109],{},[57,107,108],{},"Infinite or suspense",[57,110,111],{},"Not generated",[42,113,114,117],{},[57,115,116],{},"Server-Sent Events or JSON Lines",[57,118,119],{},"Nothing. Call the client function directly.",[24,121,123],{"id":122},"call-a-hook","Call a hook",[11,125,126,127,134,135,138,139,142,143,34],{},"The examples set ",[128,129,131],"a",{"href":130},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fadapter-oas\u002Foptions#integertype",[15,132,133],{},"integerType: 'number'"," on the adapter so ",[15,136,137],{},"petId"," is a ",[15,140,141],{},"number",". Path keys keep their spec names unless you set ",[128,144,146],{"href":145},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fadapter-oas\u002Foptions#pathparamscasing",[15,147,148],{},"pathParamsCasing",[150,151,152,332],"tabs",{},[153,154,156,190],"tabs-item",{"label":155},"Query",[11,157,158,159,63,162,63,165,168,169,172,173,175,176,179,180,183,184,18,187,34],{},"The first argument takes the request groups: ",[15,160,161],{},"path",[15,163,164],{},"query",[15,166,167],{},"headers",". The second takes SWR options (",[15,170,171],{},"SWRConfiguration",") in ",[15,174,164],{},", request config such as ",[15,177,178],{},"baseURL"," in ",[15,181,182],{},"client",", and the Kubb options ",[15,185,186],{},"shouldFetch",[15,188,189],{},"immutable",[191,192,196],"pre",{"language":193,"code":194,"class":195},"typescript","import { useGetPetById } from '.\u002Fgen\u002Fhooks\u002FuseGetPetById'\n\nconst { data, error, isLoading } = useGetPetById(\n { path: { petId: 1 } },\n { query: { refreshInterval: 30_000 }, client: { baseURL: 'https:\u002F\u002Fapi.example.com\u002Fv1' } },\n)","highlights",[15,197,199,226,227,226,229,226,260,226,285,226,328],{"class":198},"language-typescript",[200,201,204,208,212,216,219,222],"span",{"class":202,"style":203},"line","display: inline",[200,205,207],{"style":206},"color:#d73a49;--hls-dark:#f97583","import ",[200,209,211],{"style":210},"color:#005cc5;--hls-dark:#79b8ff","{ ",[200,213,215],{"style":214},"color:#24292e;--hls-dark:#e1e4e8","useGetPetById ",[200,217,218],{"style":210},"} ",[200,220,221],{"style":206},"from ",[200,223,225],{"style":224},"color:#032f62;--hls-dark:#9ecbff","'.\u002Fgen\u002Fhooks\u002FuseGetPetById'","\n",[200,228],{"class":202,"style":203},[200,230,231,234,236,239,241,244,246,249,251,254,257],{"class":202,"style":203},[200,232,233],{"style":206},"const ",[200,235,211],{"style":210},[200,237,238],{"style":214},"data",[200,240,63],{"style":206},[200,242,243],{"style":214},"error",[200,245,63],{"style":206},[200,247,248],{"style":214},"isLoading ",[200,250,218],{"style":210},[200,252,253],{"style":206},"= ",[200,255,78],{"style":256},"color:#6f42c1;--hls-dark:#b392f0",[200,258,259],{"style":210},"(",[200,261,262,265,267,270,272,274,276,279,282],{"class":202,"style":203},[200,263,264],{"style":210}," { ",[200,266,161],{"style":210},[200,268,269],{"style":206},": ",[200,271,211],{"style":210},[200,273,137],{"style":210},[200,275,269],{"style":206},[200,277,278],{"style":210},"1 ",[200,280,281],{"style":210},"} }",[200,283,284],{"style":206},",",[200,286,287,290,292,294,296,298,301,303,306,309,311,313,315,317,319,321,324,326],{"class":202,"style":203},[200,288,289],{"style":206}," ",[200,291,211],{"style":210},[200,293,164],{"style":210},[200,295,269],{"style":206},[200,297,211],{"style":210},[200,299,300],{"style":210},"refreshInterval",[200,302,269],{"style":206},[200,304,305],{"style":210},"30_000 ",[200,307,308],{"style":210},"}",[200,310,63],{"style":206},[200,312,182],{"style":210},[200,314,269],{"style":206},[200,316,211],{"style":210},[200,318,178],{"style":210},[200,320,269],{"style":206},[200,322,323],{"style":224},"'https:\u002F\u002Fapi.example.com\u002Fv1' ",[200,325,281],{"style":210},[200,327,284],{"style":206},[200,329,330],{"class":202,"style":203},[200,331,67],{"style":210},[153,333,335,350],{"label":334},"Mutation",[11,336,337,338,63,341,343,344,346,347,34],{},"A mutation hook takes only the options object, with ",[15,339,340],{},"mutation",[15,342,182],{},", and ",[15,345,186],{},". Pass the request groups to ",[15,348,349],{},"trigger",[191,351,353],{"language":193,"code":352,"class":195},"import { mutate } from 'swr'\nimport { useCreatePet } from '.\u002Fgen\u002Fhooks\u002FuseCreatePet'\nimport { getPetsQueryKey } from '.\u002Fgen\u002Fhooks\u002FuseGetPets'\n\nconst { trigger, isMutating } = useCreatePet({\n mutation: { onSuccess: () => mutate(getPetsQueryKey()) },\n})\n\nawait trigger({ body: { name: 'Fluffy' } })",[15,354,355,226,371,226,387,226,403,226,405,226,428,226,462,226,467,226,469],{"class":198},[200,356,357,359,361,364,366,368],{"class":202,"style":203},[200,358,207],{"style":206},[200,360,211],{"style":210},[200,362,363],{"style":214},"mutate ",[200,365,218],{"style":210},[200,367,221],{"style":206},[200,369,370],{"style":224},"'swr'",[200,372,373,375,377,380,382,384],{"class":202,"style":203},[200,374,207],{"style":206},[200,376,211],{"style":210},[200,378,379],{"style":214},"useCreatePet ",[200,381,218],{"style":210},[200,383,221],{"style":206},[200,385,386],{"style":224},"'.\u002Fgen\u002Fhooks\u002FuseCreatePet'",[200,388,389,391,393,396,398,400],{"class":202,"style":203},[200,390,207],{"style":206},[200,392,211],{"style":210},[200,394,395],{"style":214},"getPetsQueryKey ",[200,397,218],{"style":210},[200,399,221],{"style":206},[200,401,402],{"style":224},"'.\u002Fgen\u002Fhooks\u002FuseGetPets'",[200,404],{"class":202,"style":203},[200,406,407,409,411,413,415,418,420,422,425],{"class":202,"style":203},[200,408,233],{"style":206},[200,410,211],{"style":210},[200,412,349],{"style":214},[200,414,63],{"style":206},[200,416,417],{"style":214},"isMutating ",[200,419,218],{"style":210},[200,421,253],{"style":206},[200,423,424],{"style":256},"useCreatePet",[200,426,427],{"style":210},"({",[200,429,430,432,434,436,438,441,443,446,449,452,454,457,460],{"class":202,"style":203},[200,431,289],{"style":210},[200,433,340],{"style":210},[200,435,269],{"style":206},[200,437,211],{"style":210},[200,439,440],{"style":210},"onSuccess",[200,442,269],{"style":206},[200,444,445],{"style":210},"() ",[200,447,448],{"style":206},"=> ",[200,450,451],{"style":256},"mutate",[200,453,259],{"style":210},[200,455,456],{"style":256},"getPetsQueryKey",[200,458,459],{"style":210},"()) }",[200,461,284],{"style":206},[200,463,464],{"class":202,"style":203},[200,465,466],{"style":210},"})",[200,468],{"class":202,"style":203},[200,470,471,474,476,479,482,484,486,489,491,494],{"class":202,"style":203},[200,472,473],{"style":206},"await ",[200,475,349],{"style":256},[200,477,478],{"style":210},"({ ",[200,480,481],{"style":210},"body",[200,483,269],{"style":206},[200,485,211],{"style":210},[200,487,488],{"style":210},"name",[200,490,269],{"style":206},[200,492,493],{"style":224},"'Fluffy' ",[200,495,496],{"style":210},"} })",[11,498,499,500,503,504,506,507,511,512,18,517,34],{},"Pass a ",[15,501,502],{},"queryKey"," helper to SWR's ",[15,505,451],{}," to update or revalidate that entry. ",[128,508,510],{"href":509},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-swr\u002Fguides\u002Fquery-keys","SWR keys"," shows the default keys. To change them, set ",[128,513,515],{"href":514},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-swr\u002Foptions#querykey",[15,516,502],{},[128,518,520],{"href":519},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-swr\u002Foptions#mutationkey",[15,521,522],{},"mutationKey",[24,524,526],{"id":525},"skip-requests-until-ready","Skip requests until ready",[11,528,529,532,533,536,537,540,541,543,544,546],{},[15,530,531],{},"shouldFetch: false"," gives SWR a ",[15,534,535],{},"null"," key, so the request waits until you pass ",[15,538,539],{},"true",". On a mutation hook, ",[15,542,531],{}," makes ",[15,545,349],{}," throw a missing-key error.",[191,548,550],{"language":193,"code":549,"class":195},"import { useGetPetById } from '.\u002Fgen\u002Fhooks\u002FuseGetPetById'\n\nexport function useSelectedPet(petId: number | undefined) {\n return useGetPetById({ path: { petId: petId ?? 0 } }, { shouldFetch: petId != null })\n}",[15,551,552,226,566,226,568,226,597,226,647],{"class":198},[200,553,554,556,558,560,562,564],{"class":202,"style":203},[200,555,207],{"style":206},[200,557,211],{"style":210},[200,559,215],{"style":214},[200,561,218],{"style":210},[200,563,221],{"style":206},[200,565,225],{"style":224},[200,567],{"class":202,"style":203},[200,569,570,573,576,579,581,583,585,588,591,594],{"class":202,"style":203},[200,571,572],{"style":206},"export ",[200,574,575],{"style":206},"function ",[200,577,578],{"style":256},"useSelectedPet",[200,580,259],{"style":210},[200,582,137],{"style":214},[200,584,269],{"style":214},[200,586,587],{"style":256},"number ",[200,589,590],{"style":206},"| ",[200,592,593],{"style":210},"undefined",[200,595,596],{"style":210},") {",[200,598,599,601,604,606,608,610,612,614,616,618,621,624,627,629,631,633,635,637,639,642,645],{"class":202,"style":203},[200,600,289],{"style":210},[200,602,603],{"style":206},"return ",[200,605,78],{"style":256},[200,607,478],{"style":210},[200,609,161],{"style":210},[200,611,269],{"style":206},[200,613,211],{"style":210},[200,615,137],{"style":210},[200,617,269],{"style":206},[200,619,620],{"style":214},"petId ",[200,622,623],{"style":206},"?? ",[200,625,626],{"style":210},"0 ",[200,628,281],{"style":210},[200,630,63],{"style":206},[200,632,211],{"style":210},[200,634,186],{"style":210},[200,636,269],{"style":206},[200,638,620],{"style":214},[200,640,641],{"style":206},"!= ",[200,643,644],{"style":210},"null ",[200,646,466],{"style":210},[200,648,649],{"class":202,"style":203},[200,650,308],{"style":210},[24,652,654],{"id":653},"fetch-immutable-data","Fetch immutable data",[11,656,657,660,661,63,664,343,667,670,671,674,675,677],{},[15,658,659],{},"immutable: true"," on a query hook sets ",[15,662,663],{},"revalidateIfStale",[15,665,666],{},"revalidateOnFocus",[15,668,669],{},"revalidateOnReconnect"," to ",[15,672,673],{},"false",". Options in ",[15,676,164],{}," still win.",[191,679,681],{"language":193,"code":680,"class":195},"const { data } = useGetPetById({ path: { petId: 1 } }, { immutable: true })",[15,682,683],{"class":198},[200,684,685,687,689,692,694,696,698,700,702,704,706,708,710,712,714,716,718,720,722,725],{"class":202,"style":203},[200,686,233],{"style":206},[200,688,211],{"style":210},[200,690,691],{"style":214},"data ",[200,693,218],{"style":210},[200,695,253],{"style":206},[200,697,78],{"style":256},[200,699,478],{"style":210},[200,701,161],{"style":210},[200,703,269],{"style":206},[200,705,211],{"style":210},[200,707,137],{"style":210},[200,709,269],{"style":206},[200,711,278],{"style":210},[200,713,281],{"style":210},[200,715,63],{"style":206},[200,717,211],{"style":210},[200,719,189],{"style":210},[200,721,269],{"style":206},[200,723,724],{"style":210},"true ",[200,726,466],{"style":210},[24,728,730],{"id":729},"handle-errors","Handle errors",[11,732,733,734,737,738,740],{},"Each hook calls the client with ",[15,735,736],{},"throwOnError: true",", so a failed request sets SWR's ",[15,739,243],{},", typed from the spec's error responses.",[11,742,743,744,63,748,63,752,756,757,34],{},"Next, set the base URL, auth, and interceptors on your client plugin: ",[128,745,747],{"href":746},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch","plugin-fetch",[128,749,751],{"href":750},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios","plugin-axios",[128,753,755],{"href":754},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-ky","plugin-ky",", or your own module with ",[128,758,760],{"href":759},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-client\u002Fguides\u002Fwrite-your-client","plugin-client",{"title":762,"searchDepth":763,"depth":763,"links":764},"",5,[765,767,768,769,770],{"id":26,"depth":766,"text":27},2,{"id":122,"depth":766,"text":123},{"id":525,"depth":766,"text":526},{"id":653,"depth":766,"text":654},{"id":729,"depth":766,"text":730},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-swr\u002Fguides\u002Fcalling-operations",1795727686305,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]