> For the complete documentation index, see [llms.txt](https://devworkshops.gitbook.io/masteringvuejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devworkshops.gitbook.io/masteringvuejs/consuming-apis/intercetors.md).

# Interceptors

The first thing we're going to use interceptors for is to show a very nice progress bar at the top of the page whenever we hit the server. For this example, we're going to use a package called **NProgress**, to install it just run `npm i nprogress`. Once it's installed, we're going to update the **NorthwindService.js** file to simply start and stop the progress bar.

{% code title="NorthwindService.js" %}

```javascript
import NProgress from 'nprogress'
...
apiClient.interceptors.request.use(
    config => {
        NProgress.start()
        return config
    }
)
apiClient.interceptors.response.use(
    config => {
        NProgress.done()
        return config
    }
)
...
```

{% endcode %}

Now we need to import the default stylesheet for NProgress

{% code title="main.js" %}

```javascript
import 'nprogress/nprogress.css'
...
```

{% endcode %}

If you do want to make some changes to the look and feel of the progress bar, you can do it by overwriting the styles for `#nprogress` like the below

```css
#nprogress .bar {
  background: purple;
}

#nprogress .spinner-icon {
  border-top-color: purple;
  border-left-color: purple;
}
```

A great way to test the progress bar is to slow down the server response, with **json-server** that's super simple, we can add a delay to response by include the flag `--delay 1000`

Now we have a problem, what if the request times out? We're currently not taking this into account, we're looking for the happy path only. So let's stop the progress bar if something goes wrong, otherwise we're going to get stuck.

{% code title="NorthwindService.js" %}

```javascript
...
apiClient.interceptors.request.use(
    config => {
        NProgress.start()
        return config
    },
    err => {
        NProgress.done()
    }
)
apiClient.interceptors.response.use(
    config => {
        NProgress.done()
        return config
    },
    err => {
        NProgress.done()
    }
)
...
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devworkshops.gitbook.io/masteringvuejs/consuming-apis/intercetors.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
