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