I use nswag
npm package to generate http services, interfaces, etc.
The typescript code for a typical service proxy looks as follows:
@Injectable()
export class TenantsServiceProxy {
...
constructor(@Inject(HttpClient) http: HttpClient, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
...
getTenantId(subdomain: string | null): Observable<number | null> {
...
let options_ : any = {
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json"
})
};
return this.http.request("get", url_, options_).flatMap((response_ : any) => {
return this.processGetTenantId(response_);
}).catch((response_: any) => {
...
Regarding the bit where HTTP Headers
are detailed:
I wonder if there's a way to tell the nswag
tool to add an extra header (Authorization
for JWT Bearer in my case) automatically?
Of course there's sort of a hacky workaround to replace the headers bit using text editor with the following code:
headers: new HttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json",
'Authorization': 'Bearer ' + localStorage.getItem('token')
})
But I suspect there might be a way to include additional headers.
Maybe someone has already solved this issue?
Enable UseTransformOptionsMethod, set a ClientBaseClass and define the method in the base class with extension code...