1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Make getRequestOptions async.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti 2021-10-21 14:30:36 +03:00
parent a667da41c8
commit 62b14ae378
2 changed files with 23 additions and 12 deletions

View File

@ -52,7 +52,7 @@ export interface JsonApiConfig {
apiBase: string;
serverAddress: string;
debug?: boolean;
getRequestOptions?: () => RequestInit;
getRequestOptions?: () => Promise<RequestInit>;
}
export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
static reqInitDefault: RequestInit = {
@ -75,15 +75,20 @@ export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
public onData = new EventEmitter<[D, Response]>();
public onError = new EventEmitter<[JsonApiErrorParsed, Response]>();
private getRequestOptions?: () => RequestInit;
private getRequestOptions?: () => Promise<RequestInit>;
get<T = D>(path: string, params?: P, reqInit: RequestInit = {}) {
return this.request<T>(path, params, { ...reqInit, method: "get" });
}
getResponse(path: string, params?: P, init: RequestInit = {}): Promise<Response> {
async getResponse(path: string, params?: P, init: RequestInit = {}): Promise<Response> {
let reqUrl = `${this.config.serverAddress}${this.config.apiBase}${path}`;
const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init);
const reqInit: RequestInit = merge(
{},
this.reqInit,
this.getRequestOptions ? (await this.getRequestOptions()) : {},
init
);
const { query } = params || {} as P;
if (!reqInit.method) {
@ -117,7 +122,12 @@ export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
protected async request<D>(path: string, params?: P, init: RequestInit = {}) {
let reqUrl = `${this.config.serverAddress}${this.config.apiBase}${path}`;
const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init);
const reqInit: RequestInit = merge(
{},
this.reqInit,
this.getRequestOptions ? (await this.getRequestOptions()) : {},
init
);
const { data, query } = params || {} as P;
if (data && !reqInit.body) {

View File

@ -109,7 +109,7 @@ export interface IRemoteKubeApiConfig {
skipTLSVerify?: boolean;
}
user: {
token?: string | (() => string);
token?: string | (() => Promise<string>);
clientCertificateData?: string;
clientKeyData?: string;
}
@ -133,14 +133,15 @@ export function forCluster<T extends KubeObject>(cluster: ILocalKubeApiConfig, k
});
}
const getToken = (token: string | (() => string)) => isFunction(token) ? token() : token;
// const getToken = (token: string | (() => Promise<string>)) => isFunction(token) ? token() : token;
export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiConfig, kubeClass: KubeObjectConstructor<T>): KubeApi<T> {
const reqInit: RequestInit = {};
const token = config.user.token;
if (config.user.token) {
if (!isFunction(token)) {
reqInit.headers = {
"Authorization": `Bearer ${getToken(config.user.token)}`
"Authorization": `Bearer ${token}`
};
}
@ -170,10 +171,10 @@ export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiCon
serverAddress: config.cluster.server,
apiBase: "",
debug: isDevelopment,
...(config.user.token ? {
getRequestOptions: () => ({
...(isFunction(token) ? {
getRequestOptions: async () => ({
headers: {
"Authorization": `Bearer ${getToken(config.user.token)}`
"Authorization": `Bearer ${await token()}`
}
})
} : {})