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:
parent
a667da41c8
commit
62b14ae378
@ -52,7 +52,7 @@ export interface JsonApiConfig {
|
|||||||
apiBase: string;
|
apiBase: string;
|
||||||
serverAddress: string;
|
serverAddress: string;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
getRequestOptions?: () => RequestInit;
|
getRequestOptions?: () => Promise<RequestInit>;
|
||||||
}
|
}
|
||||||
export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
|
export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
|
||||||
static reqInitDefault: RequestInit = {
|
static reqInitDefault: RequestInit = {
|
||||||
@ -75,15 +75,20 @@ export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
|
|||||||
public onData = new EventEmitter<[D, Response]>();
|
public onData = new EventEmitter<[D, Response]>();
|
||||||
public onError = new EventEmitter<[JsonApiErrorParsed, Response]>();
|
public onError = new EventEmitter<[JsonApiErrorParsed, Response]>();
|
||||||
|
|
||||||
private getRequestOptions?: () => RequestInit;
|
private getRequestOptions?: () => Promise<RequestInit>;
|
||||||
|
|
||||||
get<T = D>(path: string, params?: P, reqInit: RequestInit = {}) {
|
get<T = D>(path: string, params?: P, reqInit: RequestInit = {}) {
|
||||||
return this.request<T>(path, params, { ...reqInit, method: "get" });
|
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}`;
|
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;
|
const { query } = params || {} as P;
|
||||||
|
|
||||||
if (!reqInit.method) {
|
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 = {}) {
|
protected async request<D>(path: string, params?: P, init: RequestInit = {}) {
|
||||||
let reqUrl = `${this.config.serverAddress}${this.config.apiBase}${path}`;
|
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;
|
const { data, query } = params || {} as P;
|
||||||
|
|
||||||
if (data && !reqInit.body) {
|
if (data && !reqInit.body) {
|
||||||
|
|||||||
@ -109,7 +109,7 @@ export interface IRemoteKubeApiConfig {
|
|||||||
skipTLSVerify?: boolean;
|
skipTLSVerify?: boolean;
|
||||||
}
|
}
|
||||||
user: {
|
user: {
|
||||||
token?: string | (() => string);
|
token?: string | (() => Promise<string>);
|
||||||
clientCertificateData?: string;
|
clientCertificateData?: string;
|
||||||
clientKeyData?: 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> {
|
export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiConfig, kubeClass: KubeObjectConstructor<T>): KubeApi<T> {
|
||||||
const reqInit: RequestInit = {};
|
const reqInit: RequestInit = {};
|
||||||
|
const token = config.user.token;
|
||||||
|
|
||||||
if (config.user.token) {
|
if (!isFunction(token)) {
|
||||||
reqInit.headers = {
|
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,
|
serverAddress: config.cluster.server,
|
||||||
apiBase: "",
|
apiBase: "",
|
||||||
debug: isDevelopment,
|
debug: isDevelopment,
|
||||||
...(config.user.token ? {
|
...(isFunction(token) ? {
|
||||||
getRequestOptions: () => ({
|
getRequestOptions: async () => ({
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${getToken(config.user.token)}`
|
"Authorization": `Bearer ${await token()}`
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} : {})
|
} : {})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user