Interceptors
Hook into the request and response lifecycle
Interceptors are async functions that run before the request is sent (request) or after the response is received (response). Both sync and async are supported, and they execute in order — each one receives the output of the previous.
Request Interceptors
type RequestInterceptor = (request: HttpRequest, context: HttpRequest) => HttpRequest | Promise<HttpRequest>request— the current request state (may already be modified by a previous interceptor)context— a snapshot of the original request before any interceptors ran; useful for logging or error correlation
import type { } from '@crm/plasma'
const : = async (, ) => {
const = .('token')
if () ..('Authorization', `Bearer ${}`)
return
}The HttpRequest shape:
| Property | Type | Description |
|---|---|---|
url | string | Full resolved URL (serverUrl + path + query) |
method | 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE' | HTTP method |
headers | Headers | Mutable headers object |
body | BodyInit | null | undefined | Serialized request body |
Response Interceptors
type ResponseInterceptor = (response: Response, context: HttpRequest) => Response | Promise<Response>response— the current response (may have been modified by a previous interceptor)context— the originalHttpRequestthat generated this response; useful for logging, retries, or redirects
import type { } from '@crm/plasma'
const : = async (, ) => {
if (. === 401) {
.(`Unauthorized on ${.} ${.}`)
.('token')
.. = '/login'
}
return
}Use the context parameter to act on the original request inside a response interceptor. The following example logs and redirects forbidden access attempts:
import type { ResponseInterceptor } from "@crm/plasma";
import { redirect } from "@tanstack/react-router";
import { getUserProfile } from "@/core/users/services/get-user-profile";
export const forbiddenInterceptor: ResponseInterceptor = async (response, context) => {
if (response.status === 403) {
const user = await getUserProfile({ data: { userId: "" } });
console.warn(
`User **${user.name}** with id **${user.id}** tried to access the resource [${context.method}] **${context.url}**. \nLogged out by forbidden interceptor.`,
);
throw redirect({
to: "/login",
});
}
return response;
};Wiring interceptors
import { } from '@crm/plasma'
import { } from './routes'
import { , , } from './interceptors'
export const = ({
: ..,
: ,
: {
: [],
: [, ],
},
})