From 9f917b76cdb65bbdd0f2da1035c9ed2a3e5ee4cf Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 8 Dec 2025 13:26:10 +0000 Subject: [PATCH] Fix interceptor documentation: use LIFO instead of FILO and clarify execution order Co-authored-by: arthurfiorette <47537704+arthurfiorette@users.noreply.github.com> --- docs/src/guide/interceptors.md | 13 +++-- test-interceptor-order.mjs | 101 --------------------------------- 2 files changed, 7 insertions(+), 107 deletions(-) delete mode 100644 test-interceptor-order.mjs diff --git a/docs/src/guide/interceptors.md b/docs/src/guide/interceptors.md index 7135b75..988092f 100644 --- a/docs/src/guide/interceptors.md +++ b/docs/src/guide/interceptors.md @@ -5,17 +5,18 @@ inconsistencies. Which is explained in the next section. ## TL;DR -- **Request** interceptors registered before `setupCache()` are ran before and - registrations made after are ran after. -- **Response** interceptors registered before `setupCache()` are ran **after** and - registrations made after are ran **before**. +- **Request** interceptors registered **before** `setupCache()` run **before** the cache + interceptor; those registered **after** `setupCache()` run **after** the cache interceptor. +- **Response** interceptors registered **before** `setupCache()` run **after** the cache + interceptor; those registered **after** `setupCache()` run **before** the cache + interceptor. ## Explanation Axios interceptors are ran differently for the request and response ones. -- **Request interceptors** are FILO _(First In Last Out)_ -- **Response interceptors** are FIFO _(First In First Out)_ +- **Request interceptors** are executed in **reverse order** - the last interceptor added runs first (LIFO - _Last In First Out_) +- **Response interceptors** are executed in **normal order** - the first interceptor added runs first (FIFO - _First In First Out_) As explained better in the [Axios documentation](https://github.com/axios/axios#interceptors) and in diff --git a/test-interceptor-order.mjs b/test-interceptor-order.mjs deleted file mode 100644 index 86b1d63..0000000 --- a/test-interceptor-order.mjs +++ /dev/null @@ -1,101 +0,0 @@ -// Test to verify the actual order of axios interceptors -import Axios from 'axios'; - -const testAxios = () => { - const axios = Axios.create(); - const order = []; - - // Mock adapter that just returns a response - axios.defaults.adapter = async (config) => { - order.push('ADAPTER'); - return { - data: { success: true }, - status: 200, - statusText: 'OK', - headers: {}, - config - }; - }; - - // Add interceptors BEFORE setupCache - axios.interceptors.request.use((config) => { - order.push('req-1-before'); - return config; - }); - - axios.interceptors.request.use((config) => { - order.push('req-2-before'); - return config; - }); - - axios.interceptors.response.use((response) => { - order.push('res-1-before'); - return response; - }); - - axios.interceptors.response.use((response) => { - order.push('res-2-before'); - return response; - }); - - // Simulate setupCache by adding interceptors - axios.interceptors.request.use((config) => { - order.push('req-CACHE'); - return config; - }); - - axios.interceptors.response.use((response) => { - order.push('res-CACHE'); - return response; - }); - - // Add interceptors AFTER setupCache - axios.interceptors.request.use((config) => { - order.push('req-3-after'); - return config; - }); - - axios.interceptors.request.use((config) => { - order.push('req-4-after'); - return config; - }); - - axios.interceptors.response.use((response) => { - order.push('res-3-after'); - return response; - }); - - axios.interceptors.response.use((response) => { - order.push('res-4-after'); - return response; - }); - - return { axios, order }; -}; - -// Run the test -(async () => { - const { axios, order } = testAxios(); - - await axios.get('http://test.com'); - - console.log('\nActual execution order:'); - console.log(order.join(' -> ')); - - console.log('\n\nRequest interceptors order:'); - const reqOrder = order.filter(x => x.startsWith('req-')); - console.log(reqOrder.join(' -> ')); - - console.log('\nResponse interceptors order:'); - const resOrder = order.filter(x => x.startsWith('res-')); - console.log(resOrder.join(' -> ')); - - console.log('\n\nConclusion:'); - console.log('Request interceptors are executed in REVERSE order (Last In First Out)'); - console.log(' - req-4-after runs FIRST (added last)'); - console.log(' - req-1-before runs LAST (added first)'); - console.log(''); - console.log('Response interceptors are executed in NORMAL order (First In First Out)'); - console.log(' - res-1-before runs FIRST (added first)'); - console.log(' - res-4-after runs LAST (added last)'); -})();