如何将授权头传递给NSwag生成的获取客户端?



有一个通过NSwag自动生成的API代理获取客户端。MSBuild包,它有这样的构造函数。我需要通过init传递授权头参数。


export class CustomerClient implements ICustomerClient {
private http: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> };
private baseUrl: string;
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
constructor(baseUrl?: string, http?: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> }) {
this.http = http ? http : <any>window;
this.baseUrl = baseUrl !== undefined && baseUrl !== null ? baseUrl : "";
}
...
...
...
}

传递它的可能方法是什么?

也许这对你有帮助

const apiClient = new CustomerClient(`http://localhost:5000`, {
async fetch(url: RequestInfo, init: RequestInit) {
const accessToken = await acquireAccessToken();
if (accessToken) init.headers['Authorization'] = `Bearer ${accessToken}`;
return fetch(url, init);
},
});

最新更新