authInterceptor function

Interceptor authInterceptor(
  1. AuthConfig config
)

Builds an Interceptor that fetches a credential from AuthConfig.source and writes it into AuthConfig.headerName before delegating to next.

Composition order per RFC 0002: auth sits innermost so each retry attempt re-runs through this interceptor and a refreshed token is acquired before the next network call.

Implementation

Interceptor authInterceptor(AuthConfig config) {
  return <I extends Object, O extends Object>(AnyFn<I, O> next) {
    return (Request<I, O> req) async {
      if (!config.overrideExisting && req.headers[config.headerName] != null) {
        return next(req);
      }
      final token = await config.source.token();
      req.headers[config.headerName] = '${config.prefix}$token';
      return next(req);
    };
  };
}