I understand that the following code is not valid in Angular, but I am using it for visual demonstration purposes.
My goal is to enhance the Angular HTTP client by adding custom headers.
I envision creating a class like this, where I extend the Angular http client and set a default header in the constructor:
import { Injectable } from '@angular/core'
import { HttpClient, HttpHandler } from '@angular/common/http'
import { AuthenticationService } from './authentication.service.ts'
@Injectable()
export class AuthenticatedHTTPClient extends HttpClient {
constructor(
handler: HttpHandler,
auth: AuthenticationService
){
super(handler)
this.headers.set('Authorization', this.auth.accessToken)
}
}
When consuming it with something like:
constructor(
authHttp: AuthenticatedHTTPClient
){ }
...
this.authHttp.get('/api/something')
The authHttp
client being used will already have the header set.
Is this feasible?