mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Move getRequestOptions to JsonApi.
Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
parent
0fa1533fd8
commit
9efaac289b
@ -52,6 +52,7 @@ export interface JsonApiConfig {
|
||||
apiBase: string;
|
||||
serverAddress: string;
|
||||
debug?: boolean;
|
||||
getRequestOptions?: () => RequestInit;
|
||||
}
|
||||
export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
|
||||
static reqInitDefault: RequestInit = {
|
||||
@ -68,10 +69,13 @@ export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
|
||||
this.config = Object.assign({}, JsonApi.configDefault, config);
|
||||
this.reqInit = merge({}, JsonApi.reqInitDefault, reqInit);
|
||||
this.parseResponse = this.parseResponse.bind(this);
|
||||
this.getRequestOptions = config.getRequestOptions;
|
||||
}
|
||||
|
||||
public onData = new EventEmitter<[D, Response]>();
|
||||
public onError = new EventEmitter<[JsonApiErrorParsed, Response]>();
|
||||
|
||||
private getRequestOptions?: () => RequestInit;
|
||||
|
||||
get<T = D>(path: string, params?: P, reqInit: RequestInit = {}) {
|
||||
return this.request<T>(path, params, { ...reqInit, method: "get" });
|
||||
@ -79,7 +83,7 @@ export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
|
||||
|
||||
getResponse(path: string, params?: P, init: RequestInit = {}): Promise<Response> {
|
||||
let reqUrl = `${this.config.serverAddress}${this.config.apiBase}${path}`;
|
||||
const reqInit: RequestInit = merge({}, this.reqInit, init);
|
||||
const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init);
|
||||
const { query } = params || {} as P;
|
||||
|
||||
if (!reqInit.method) {
|
||||
@ -113,7 +117,7 @@ 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, init);
|
||||
const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init);
|
||||
const { data, query } = params || {} as P;
|
||||
|
||||
if (data && !reqInit.body) {
|
||||
|
||||
@ -109,6 +109,7 @@ export interface IRemoteKubeApiConfig {
|
||||
}
|
||||
user: {
|
||||
token?: string;
|
||||
getToken?: () => string;
|
||||
clientCertificateData?: string;
|
||||
clientKeyData?: string;
|
||||
}
|
||||
@ -135,9 +136,13 @@ export function forCluster<T extends KubeObject>(cluster: ILocalKubeApiConfig, k
|
||||
export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiConfig, kubeClass: KubeObjectConstructor<T>): KubeApi<T> {
|
||||
const reqInit: RequestInit = {};
|
||||
|
||||
if (config.user.token) {
|
||||
if (config.user.token && config.user.getToken) {
|
||||
throw new Error("Provide either user.token or user.getToken");
|
||||
}
|
||||
|
||||
if (config.user.token || config.user.getToken) {
|
||||
reqInit.headers = {
|
||||
"Authorization": `Bearer ${config.user.token}`
|
||||
"Authorization": `Bearer ${config.user.token ?? config.user.getToken()}`
|
||||
};
|
||||
}
|
||||
|
||||
@ -167,6 +172,13 @@ export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiCon
|
||||
serverAddress: config.cluster.server,
|
||||
apiBase: "",
|
||||
debug: isDevelopment,
|
||||
...(config.user.getToken ? {
|
||||
getRequestOptions: () => ({
|
||||
headers: {
|
||||
"Authorization": `Bearer ${config.user.getToken()}`
|
||||
}
|
||||
})
|
||||
} : {})
|
||||
}, reqInit);
|
||||
|
||||
return new KubeApi({
|
||||
@ -195,9 +207,6 @@ export type KubeApiWatchOptions = {
|
||||
abortController?: AbortController
|
||||
watchId?: string;
|
||||
retry?: boolean;
|
||||
|
||||
// Request options that are applied for the initial watch request and also subsequent retry requests
|
||||
getRequestOptions?: () => RequestInit;
|
||||
};
|
||||
|
||||
export class KubeApi<T extends KubeObject> {
|
||||
@ -489,12 +498,7 @@ export class KubeApi<T extends KubeObject> {
|
||||
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
|
||||
let errorReceived = false;
|
||||
let timedRetry: NodeJS.Timeout;
|
||||
const {
|
||||
abortController: { abort, signal } = new AbortController(),
|
||||
namespace,
|
||||
callback = noop,
|
||||
retry,
|
||||
getRequestOptions } = opts;
|
||||
const { abortController: { abort, signal } = new AbortController(), namespace, callback = noop, retry } = opts;
|
||||
const { watchId = `${this.kind.toLowerCase()}-${this.watchId++}` } = opts;
|
||||
|
||||
signal.addEventListener("abort", () => {
|
||||
@ -503,11 +507,7 @@ export class KubeApi<T extends KubeObject> {
|
||||
});
|
||||
|
||||
const watchUrl = this.getWatchUrl(namespace);
|
||||
const responsePromise = this.request.getResponse(
|
||||
watchUrl,
|
||||
null,
|
||||
merge({ signal, timeout: 600_000 }, getRequestOptions?.())
|
||||
);
|
||||
const responsePromise = this.request.getResponse(watchUrl, null,{ signal, timeout: 600_000 });
|
||||
|
||||
logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user