[{"data":1,"prerenderedAt":593},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Finterceptors":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":592,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Finterceptors.md",{},"Add interceptors",{"type":8,"value":9,"toc":582},"minimark",[10,27,32,35,160,174,178,185,283,287,293,372,382,386,399,404,543,547,557],[11,12,13,14,18,19,22,23,26],"p",{},"Register handlers on ",[15,16,17],"code",{},"client.interceptors.request",", ",[15,20,21],{},".response",", or ",[15,24,25],{},".error"," to apply behavior across calls. Handlers receive native Axios objects.",[28,29,31],"h2",{"id":30},"run-before-the-send","Run before the send",[11,33,34],{},"Request handlers run after URL construction, serialization, and authentication. Return the modified request:",[36,37,41],"pre",{"language":38,"code":39,"class":40},"typescript","import { client } from '.\u002Fgen\u002F.kubb\u002Fclient'\n\nclient.interceptors.request.use((request) => {\n  request.headers.set('X-Request-ID', crypto.randomUUID())\n  return request\n})","highlights",[15,42,44,71,72,71,74,71,110,71,146,71,155],{"class":43},"language-typescript",[45,46,49,53,57,61,64,67],"span",{"class":47,"style":48},"line","display: inline",[45,50,52],{"style":51},"color:#d73a49;--hls-dark:#f97583","import ",[45,54,56],{"style":55},"color:#005cc5;--hls-dark:#79b8ff","{ ",[45,58,60],{"style":59},"color:#24292e;--hls-dark:#e1e4e8","client ",[45,62,63],{"style":55},"} ",[45,65,66],{"style":51},"from ",[45,68,70],{"style":69},"color:#032f62;--hls-dark:#9ecbff","'.\u002Fgen\u002F.kubb\u002Fclient'","\n",[45,73],{"class":47,"style":48},[45,75,76,79,82,85,87,90,92,96,99,101,104,107],{"class":47,"style":48},[45,77,78],{"style":59},"client",[45,80,81],{"style":51},".",[45,83,84],{"style":55},"interceptors",[45,86,81],{"style":51},[45,88,89],{"style":55},"request",[45,91,81],{"style":51},[45,93,95],{"style":94},"color:#6f42c1;--hls-dark:#b392f0","use",[45,97,98],{"style":55},"((",[45,100,89],{"style":59},[45,102,103],{"style":55},") ",[45,105,106],{"style":51},"=> ",[45,108,109],{"style":55},"{",[45,111,112,115,117,119,122,124,127,130,133,135,138,140,143],{"class":47,"style":48},[45,113,114],{"style":55},"  ",[45,116,89],{"style":59},[45,118,81],{"style":51},[45,120,121],{"style":55},"headers",[45,123,81],{"style":51},[45,125,126],{"style":94},"set",[45,128,129],{"style":55},"(",[45,131,132],{"style":69},"'X-Request-ID'",[45,134,18],{"style":51},[45,136,137],{"style":59},"crypto",[45,139,81],{"style":51},[45,141,142],{"style":94},"randomUUID",[45,144,145],{"style":55},"())",[45,147,148,150,153],{"class":47,"style":48},[45,149,114],{"style":55},[45,151,152],{"style":51},"return ",[45,154,89],{"style":59},[45,156,157],{"class":47,"style":48},[45,158,159],{"style":55},"})",[11,161,162,163,166,167,170,171,81],{},"The handler receives an ",[15,164,165],{},"InternalAxiosRequestConfig",", so headers go through ",[15,168,169],{},"AxiosHeaders"," and the\nbody is on ",[15,172,173],{},"data",[28,175,177],{"id":176},"run-after-the-send","Run after the send",[11,179,180,181,184],{},"Response handlers run before calls resolve. The handler receives an ",[15,182,183],{},"AxiosResponse",":",[36,186,188],{"language":38,"code":187,"class":40},"client.interceptors.response.use((response) => {\n  console.info(`${response.status} ${response.config.url}`)\n  return response\n})",[15,189,190,71,217,71,271,71,279],{"class":43},[45,191,192,194,196,198,200,203,205,207,209,211,213,215],{"class":47,"style":48},[45,193,78],{"style":59},[45,195,81],{"style":51},[45,197,84],{"style":55},[45,199,81],{"style":51},[45,201,202],{"style":55},"response",[45,204,81],{"style":51},[45,206,95],{"style":94},[45,208,98],{"style":55},[45,210,202],{"style":59},[45,212,103],{"style":55},[45,214,106],{"style":51},[45,216,109],{"style":55},[45,218,219,221,224,226,229,231,234,237,239,241,244,247,250,252,254,256,259,261,264,266,268],{"class":47,"style":48},[45,220,114],{"style":55},[45,222,223],{"style":59},"console",[45,225,81],{"style":51},[45,227,228],{"style":94},"info",[45,230,129],{"style":55},[45,232,233],{"style":69},"`",[45,235,236],{"style":59},"${",[45,238,202],{"style":59},[45,240,81],{"style":51},[45,242,243],{"style":55},"status",[45,245,246],{"style":59},"}",[45,248,249],{"style":69}," ",[45,251,236],{"style":59},[45,253,202],{"style":59},[45,255,81],{"style":51},[45,257,258],{"style":55},"config",[45,260,81],{"style":51},[45,262,263],{"style":55},"url",[45,265,246],{"style":59},[45,267,233],{"style":69},[45,269,270],{"style":55},")",[45,272,273,275,277],{"class":47,"style":48},[45,274,114],{"style":55},[45,276,152],{"style":51},[45,278,202],{"style":59},[45,280,281],{"class":47,"style":48},[45,282,159],{"style":55},[28,284,286],{"id":285},"react-to-an-error","React to an error",[11,288,289,290,184],{},"Use an error handler to react to HTTP errors on the throwing path. The handler receives an\n",[15,291,292],{},"AxiosError",[36,294,296],{"language":38,"code":295,"class":40},"client.interceptors.error.use((error) => {\n  if (error.response?.status === 401) scheduleTokenRefresh()\n  return error\n})",[15,297,298,71,325,71,360,71,368],{"class":43},[45,299,300,302,304,306,308,311,313,315,317,319,321,323],{"class":47,"style":48},[45,301,78],{"style":59},[45,303,81],{"style":51},[45,305,84],{"style":55},[45,307,81],{"style":51},[45,309,310],{"style":55},"error",[45,312,81],{"style":51},[45,314,95],{"style":94},[45,316,98],{"style":55},[45,318,310],{"style":59},[45,320,103],{"style":55},[45,322,106],{"style":51},[45,324,109],{"style":55},[45,326,327,329,332,334,336,338,340,343,346,349,352,354,357],{"class":47,"style":48},[45,328,114],{"style":55},[45,330,331],{"style":51},"if ",[45,333,129],{"style":55},[45,335,310],{"style":59},[45,337,81],{"style":51},[45,339,202],{"style":55},[45,341,342],{"style":51},"?.",[45,344,345],{"style":55},"status ",[45,347,348],{"style":51},"=== ",[45,350,351],{"style":55},"401",[45,353,103],{"style":55},[45,355,356],{"style":94},"scheduleTokenRefresh",[45,358,359],{"style":55},"()",[45,361,362,364,366],{"class":47,"style":48},[45,363,114],{"style":55},[45,365,152],{"style":51},[45,367,310],{"style":59},[45,369,370],{"class":47,"style":48},[45,371,159],{"style":55},[11,373,374,375,378,379,381],{},"This channel only fires on the throw path. When you read with ",[15,376,377],{},"throwOnError: false",", a documented\nnon-2xx response resolves and no error interceptor runs, so inspect the returned ",[15,380,310],{}," on the\nresult instead. A transport failure (no response) still throws, and the error handler runs for it.",[28,383,385],{"id":384},"add-replace-and-remove-handlers","Add, replace, and remove handlers",[11,387,388,390,391,394,395,398],{},[15,389,95],{}," registers a handler and returns an id. Pass that id to ",[15,392,393],{},"eject"," to remove it, or to ",[15,396,397],{},"update","\nto swap its function in place.",[400,401,403],"blockquote",{"as":402},"note","Response and error handlers run in the order you register them. Axios runs request handlers in\nreverse order.",[36,405,407],{"language":38,"code":406,"class":40},"const id = client.interceptors.request.use((request) => request)\n\nclient.interceptors.request.update(id, (request) => {\n  request.headers.set('X-Trace', 'on')\n  return request\n})\n\nclient.interceptors.request.eject(id)",[15,408,409,71,446,71,448,71,481,71,507,71,515,71,519,71,521],{"class":43},[45,410,411,414,417,420,422,424,426,428,430,432,434,436,438,440,442,444],{"class":47,"style":48},[45,412,413],{"style":51},"const ",[45,415,416],{"style":59},"id ",[45,418,419],{"style":51},"= ",[45,421,78],{"style":59},[45,423,81],{"style":51},[45,425,84],{"style":55},[45,427,81],{"style":51},[45,429,89],{"style":55},[45,431,81],{"style":51},[45,433,95],{"style":94},[45,435,98],{"style":55},[45,437,89],{"style":59},[45,439,103],{"style":55},[45,441,106],{"style":51},[45,443,89],{"style":59},[45,445,270],{"style":55},[45,447],{"class":47,"style":48},[45,449,450,452,454,456,458,460,462,464,466,469,471,473,475,477,479],{"class":47,"style":48},[45,451,78],{"style":59},[45,453,81],{"style":51},[45,455,84],{"style":55},[45,457,81],{"style":51},[45,459,89],{"style":55},[45,461,81],{"style":51},[45,463,397],{"style":94},[45,465,129],{"style":55},[45,467,468],{"style":59},"id",[45,470,18],{"style":51},[45,472,129],{"style":55},[45,474,89],{"style":59},[45,476,103],{"style":55},[45,478,106],{"style":51},[45,480,109],{"style":55},[45,482,483,485,487,489,491,493,495,497,500,502,505],{"class":47,"style":48},[45,484,114],{"style":55},[45,486,89],{"style":59},[45,488,81],{"style":51},[45,490,121],{"style":55},[45,492,81],{"style":51},[45,494,126],{"style":94},[45,496,129],{"style":55},[45,498,499],{"style":69},"'X-Trace'",[45,501,18],{"style":51},[45,503,504],{"style":69},"'on'",[45,506,270],{"style":55},[45,508,509,511,513],{"class":47,"style":48},[45,510,114],{"style":55},[45,512,152],{"style":51},[45,514,89],{"style":59},[45,516,517],{"class":47,"style":48},[45,518,159],{"style":55},[45,520],{"class":47,"style":48},[45,522,523,525,527,529,531,533,535,537,539,541],{"class":47,"style":48},[45,524,78],{"style":59},[45,526,81],{"style":51},[45,528,84],{"style":55},[45,530,81],{"style":51},[45,532,89],{"style":55},[45,534,81],{"style":51},[45,536,393],{"style":94},[45,538,129],{"style":55},[45,540,468],{"style":59},[45,542,270],{"style":55},[28,544,546],{"id":545},"where-to-set-them","Where to set them",[11,548,549,550,552,553,556],{},"Register handlers on the shared ",[15,551,78],{}," for all operations, or on a separate ",[15,554,555],{},"createClient"," instance for isolated calls. Interceptors are instance-level settings.",[11,558,559,560,563,564,567,568,570,571,573,574,577,578,581],{},"Interceptors registered through ",[15,561,562],{},"client.interceptors"," also follow a custom transport set later with\n",[15,565,566],{},"client.setConfig({ transport })",". When the transport changes, handlers detach from the old\ninstance and attach to the new one, and their IDs stay valid for ",[15,569,393],{}," and ",[15,572,397],{},". Clearing the\ntransport (",[15,575,576],{},"client.setConfig({ transport: undefined })",") moves them back to the client's base\ninstance. A per-call ",[15,579,580],{},"transport"," still bypasses client interceptors.",{"title":583,"searchDepth":584,"depth":584,"links":585},"",5,[586,588,589,590,591],{"id":30,"depth":587,"text":31},2,{"id":176,"depth":587,"text":177},{"id":285,"depth":587,"text":286},{"id":384,"depth":587,"text":385},{"id":545,"depth":587,"text":546},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios\u002Fguides\u002Finterceptors",1793639046843]