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