I utilize the nswag
npm package to generate HTTP services, interfaces, and more.
A sample TypeScript code for a typical service proxy is shown below:
@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) => {
...
Concerning the section on HTTP Headers
:
I'm curious if there's a way to instruct the nswag
tool to automatically include an extra header (specifically Authorization
for JWT Bearer in my case).
While a workaround involves manually editing the headers section with the following code:
headers: new HttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json",
'Authorization': 'Bearer ' + localStorage.getItem('token')
})
I suspect there might be a better method to add additional headers.
Perhaps someone has already found a solution to this dilemma?