Just in case anyone has this issue in the future, this is what I ended up doing.
I needed to add STATIC headers to an HttpRequest but this solution can be easily modified to allow more dynamic headers.
I made an Attribute Adding Interceptor with the following:
attribute.interceptor.ts
@Injectable()
export class AttributesInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const attributes = environment.attributes;
let attributeReq = req;
for (let i = 0; i !== attributes.length; i++) {
const attribute = attributes[i];
if (this.urlMatches(attribute.urls, req)) {
// The url matches so let's apply the attribute
attributeReq = attributeReq.clone({ headers: req.headers.set(attribute.header, attribute.value) });
}
}
return next.handle(attributeReq);
}
urlMatches(urls: string[], req: HttpRequest<any>): boolean {
for (let i = 0; i !== urls.length; i++) {
if (req.url.includes(urls[i])) {
return true;
}
}
return false;
}
}
And defined my static attributes in the environment file:
environment.local.ts
export const endpoints = {
midway: 'http://localhost:8080',
builds: 'http://localhost:7245'
};
export const environment = {
production: false,
logLevel: LogSeverity.INFO,
endpoints: endpoints,
attributes: <Attribute[]> [
{
header: 'X-COMPANY',
value: 'SECRET-TOKEN',
urls: [ endpoints.midway ]
}
]
};
Where an Attribute is defined as:
export class Attribute {
header: string;
value: string;
urls: string[];
}
So now I can add specific static headers to any service call and just append the this attribute array with the service base url and the header / value.
This solution can be extended to serve a lot more complicated use cases but this simple solutions does exactly what I need!