Improving Backend Integrations: the Interceptor Pattern
In a single-page application (SPA), communication with the backend is one of the most common tasks. In Chapter 5, Angular Services and the Singleton Pattern, we learned that the Angular component that makes this communication is called Service
. However, many side tasks are common to all communications with the backend, such as header processing, authentication, and loading.
We could do this sides task on a service-by-service basis, but in addition to being an unproductive activity, the team might not be able to implement some control on the request due to the carelessness or ignorance of a new member of the team.
In order to simplify the development of side tasks for communicating with the backend, the Angular framework implements the interceptor design pattern, which we will explore in this chapter. Here, we will cover the following topics:
- Attaching the token to the request with an interceptor
- Changing the...