Skip to content

Auth & Interceptors

Bearer token

ts
import { CrispOquentConfig } from '@bir-tan/crisp-oquent';

CrispOquentConfig.setBearerToken('eyJhbGc...');
// → Authorization: Bearer eyJhbGc...

CrispOquentConfig.setBearerToken(null);
// removes the Authorization header

Custom headers

ts
CrispOquentConfig.setHeader('X-Tenant-Id', 'acme');
CrispOquentConfig.setHeader('Accept-Language', 'tr');
CrispOquentConfig.removeHeader('X-Tenant-Id');

Request interceptor

Mutate every outgoing request. Useful for tracing, CSRF tokens, dynamic auth:

ts
CrispOquentConfig.addRequestInterceptor((ctx) => ({
  ...ctx,
  init: {
    ...ctx.init,
    headers: {
      ...ctx.init.headers,
      'X-Trace-Id': crypto.randomUUID(),
    },
  },
}));

Interceptors run in registration order. Each receives the previous one's output.

Response interceptor

Inspect or transform responses. Common use case: silent token refresh on 401.

ts
CrispOquentConfig.addResponseInterceptor(async (response, ctx) => {
  if (response.status === 401 && !ctx.url.includes('/auth/refresh')) {
    await refreshToken();
    return CrispOquentConfig.options.fetch(ctx.url, ctx.init);
  }
  return response;
});

Custom fetch

Want to swap the underlying fetch (e.g. for undici, cross-fetch, instrumentation)?

ts
import { fetch as undiciFetch } from 'undici';

CrispOquentConfig.initialize({
  baseUri: 'https://api.example.com',
  fetch: undiciFetch,
});