mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add option for providing request options to watch requests (#4102)
* Add option for providing request options to watch requests. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Move getRequestOptions to JsonApi. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Style fix Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Make token to be a string or a function Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Fix isFunction import. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Make getRequestOptions async. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Remove commented code. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Only set token if it's truthy. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Review fixes. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Add this.getRequestOptions default Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
parent
d5719f29b8
commit
0dab7b8073
@ -52,6 +52,7 @@ export interface JsonApiConfig {
|
|||||||
apiBase: string;
|
apiBase: string;
|
||||||
serverAddress: string;
|
serverAddress: string;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
|
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 = {
|
||||||
@ -68,18 +69,26 @@ 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 ?? (() => Promise.resolve({}));
|
||||||
}
|
}
|
||||||
|
|
||||||
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: JsonApiConfig["getRequestOptions"];
|
||||||
|
|
||||||
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, init);
|
const reqInit: RequestInit = merge(
|
||||||
|
{},
|
||||||
|
this.reqInit,
|
||||||
|
await this.getRequestOptions(),
|
||||||
|
init
|
||||||
|
);
|
||||||
const { query } = params || {} as P;
|
const { query } = params || {} as P;
|
||||||
|
|
||||||
if (!reqInit.method) {
|
if (!reqInit.method) {
|
||||||
@ -113,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, init);
|
const reqInit: RequestInit = merge(
|
||||||
|
{},
|
||||||
|
this.reqInit,
|
||||||
|
await this.getRequestOptions(),
|
||||||
|
init
|
||||||
|
);
|
||||||
const { data, query } = params || {} as P;
|
const { data, query } = params || {} as P;
|
||||||
|
|
||||||
if (data && !reqInit.body) {
|
if (data && !reqInit.body) {
|
||||||
|
|||||||
@ -22,6 +22,7 @@
|
|||||||
// Base class for building all kubernetes apis
|
// Base class for building all kubernetes apis
|
||||||
|
|
||||||
import merge from "lodash/merge";
|
import merge from "lodash/merge";
|
||||||
|
import { isFunction } from "lodash";
|
||||||
import { stringify } from "querystring";
|
import { stringify } from "querystring";
|
||||||
import { apiKubePrefix, isDevelopment } from "../../common/vars";
|
import { apiKubePrefix, isDevelopment } from "../../common/vars";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
@ -108,7 +109,7 @@ export interface IRemoteKubeApiConfig {
|
|||||||
skipTLSVerify?: boolean;
|
skipTLSVerify?: boolean;
|
||||||
}
|
}
|
||||||
user: {
|
user: {
|
||||||
token?: string;
|
token?: string | (() => Promise<string>);
|
||||||
clientCertificateData?: string;
|
clientCertificateData?: string;
|
||||||
clientKeyData?: string;
|
clientKeyData?: string;
|
||||||
}
|
}
|
||||||
@ -135,12 +136,6 @@ 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) {
|
|
||||||
reqInit.headers = {
|
|
||||||
"Authorization": `Bearer ${config.user.token}`
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const agentOptions: AgentOptions = {};
|
const agentOptions: AgentOptions = {};
|
||||||
|
|
||||||
if (config.cluster.skipTLSVerify === true) {
|
if (config.cluster.skipTLSVerify === true) {
|
||||||
@ -163,10 +158,18 @@ export function forRemoteCluster<T extends KubeObject>(config: IRemoteKubeApiCon
|
|||||||
reqInit.agent = new Agent(agentOptions);
|
reqInit.agent = new Agent(agentOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const token = config.user.token;
|
||||||
const request = new KubeJsonApi({
|
const request = new KubeJsonApi({
|
||||||
serverAddress: config.cluster.server,
|
serverAddress: config.cluster.server,
|
||||||
apiBase: "",
|
apiBase: "",
|
||||||
debug: isDevelopment,
|
debug: isDevelopment,
|
||||||
|
...(token ? {
|
||||||
|
getRequestOptions: async () => ({
|
||||||
|
headers: {
|
||||||
|
"Authorization": `Bearer ${isFunction(token) ? await token() : token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} : {})
|
||||||
}, reqInit);
|
}, reqInit);
|
||||||
|
|
||||||
return new KubeApi({
|
return new KubeApi({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user