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;
|
apiBase: string;
|
||||||
serverAddress: string;
|
serverAddress: string;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
|
getRequestOptions?: () => 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 = {
|
||||||
@ -68,18 +69,21 @@ export class JsonApi<D = JsonApiData, P extends JsonApiParams = JsonApiParams> {
|
|||||||
this.config = Object.assign({}, JsonApi.configDefault, config);
|
this.config = Object.assign({}, JsonApi.configDefault, config);
|
||||||
this.reqInit = merge({}, JsonApi.reqInitDefault, reqInit);
|
this.reqInit = merge({}, JsonApi.reqInitDefault, reqInit);
|
||||||
this.parseResponse = this.parseResponse.bind(this);
|
this.parseResponse = this.parseResponse.bind(this);
|
||||||
|
this.getRequestOptions = config.getRequestOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
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> {
|
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, init);
|
const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init);
|
||||||
const { query } = params || {} as P;
|
const { query } = params || {} as P;
|
||||||
|
|
||||||
if (!reqInit.method) {
|
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 = {}) {
|
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, init);
|
const reqInit: RequestInit = merge({}, this.reqInit, this.getRequestOptions?.(), init);
|
||||||
const { data, query } = params || {} as P;
|
const { data, query } = params || {} as P;
|
||||||
|
|
||||||
if (data && !reqInit.body) {
|
if (data && !reqInit.body) {
|
||||||
|
|||||||
@ -109,6 +109,7 @@ export interface IRemoteKubeApiConfig {
|
|||||||
}
|
}
|
||||||
user: {
|
user: {
|
||||||
token?: string;
|
token?: string;
|
||||||
|
getToken?: () => string;
|
||||||
clientCertificateData?: string;
|
clientCertificateData?: string;
|
||||||
clientKeyData?: 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> {
|
export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiConfig, kubeClass: KubeObjectConstructor<T>): KubeApi<T> {
|
||||||
const reqInit: RequestInit = {};
|
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 = {
|
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,
|
serverAddress: config.cluster.server,
|
||||||
apiBase: "",
|
apiBase: "",
|
||||||
debug: isDevelopment,
|
debug: isDevelopment,
|
||||||
|
...(config.user.getToken ? {
|
||||||
|
getRequestOptions: () => ({
|
||||||
|
headers: {
|
||||||
|
"Authorization": `Bearer ${config.user.getToken()}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} : {})
|
||||||
}, reqInit);
|
}, reqInit);
|
||||||
|
|
||||||
return new KubeApi({
|
return new KubeApi({
|
||||||
@ -195,9 +207,6 @@ export type KubeApiWatchOptions = {
|
|||||||
abortController?: AbortController
|
abortController?: AbortController
|
||||||
watchId?: string;
|
watchId?: string;
|
||||||
retry?: boolean;
|
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> {
|
export class KubeApi<T extends KubeObject> {
|
||||||
@ -489,12 +498,7 @@ export class KubeApi<T extends KubeObject> {
|
|||||||
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
|
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
|
||||||
let errorReceived = false;
|
let errorReceived = false;
|
||||||
let timedRetry: NodeJS.Timeout;
|
let timedRetry: NodeJS.Timeout;
|
||||||
const {
|
const { abortController: { abort, signal } = new AbortController(), namespace, callback = noop, retry } = opts;
|
||||||
abortController: { abort, signal } = new AbortController(),
|
|
||||||
namespace,
|
|
||||||
callback = noop,
|
|
||||||
retry,
|
|
||||||
getRequestOptions } = opts;
|
|
||||||
const { watchId = `${this.kind.toLowerCase()}-${this.watchId++}` } = opts;
|
const { watchId = `${this.kind.toLowerCase()}-${this.watchId++}` } = opts;
|
||||||
|
|
||||||
signal.addEventListener("abort", () => {
|
signal.addEventListener("abort", () => {
|
||||||
@ -503,11 +507,7 @@ export class KubeApi<T extends KubeObject> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const watchUrl = this.getWatchUrl(namespace);
|
const watchUrl = this.getWatchUrl(namespace);
|
||||||
const responsePromise = this.request.getResponse(
|
const responsePromise = this.request.getResponse(watchUrl, null,{ signal, timeout: 600_000 });
|
||||||
watchUrl,
|
|
||||||
null,
|
|
||||||
merge({ signal, timeout: 600_000 }, getRequestOptions?.())
|
|
||||||
);
|
|
||||||
|
|
||||||
logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);
|
logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user