mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* wip: restructure to monorepo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor create-release-pr to a package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * github workflow fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix typo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * add webpack-env types to core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix github workflows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor/fix integration tests Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * lint fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * yarn run dev Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * eslint settings for vscode Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * move templates to right package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * open-lens build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * integration test fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix nx task dependencies Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * use bash shell for unit tests in test workflow Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit for windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix build:app on windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove ELECTRON_BUILDER_EXTRA_ARGS Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * sync src/ from master Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove Makefile from core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
148 lines
4.3 KiB
TypeScript
148 lines
4.3 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
import type { Cluster } from "../../common/cluster/cluster";
|
|
import type http from "http";
|
|
import type httpProxy from "http-proxy";
|
|
import type { LensApiResultContentType } from "./router-content-types";
|
|
import type { URLSearchParams } from "url";
|
|
import type Joi from "joi";
|
|
|
|
export type InferParam<
|
|
T extends string,
|
|
PathParams extends Record<string, string>,
|
|
> =
|
|
T extends `{${infer P}?}`
|
|
? PathParams & Partial<Record<P, string>>
|
|
: T extends `{${infer P}}`
|
|
? PathParams & Record<P, string>
|
|
: PathParams;
|
|
|
|
export type InferParamFromPath<P extends string> =
|
|
P extends `${string}/{${infer B}*}${infer Tail}`
|
|
? Tail extends ""
|
|
? Record<B, string>
|
|
: never
|
|
: P extends `${infer A}/${infer B}`
|
|
? InferParam<A, InferParamFromPath<B>>
|
|
: InferParam<P, {}>;
|
|
|
|
export interface LensApiRequest<Path extends string> {
|
|
path: Path;
|
|
payload: unknown;
|
|
params: InferParamFromPath<Path>;
|
|
cluster: Cluster | undefined;
|
|
query: URLSearchParams;
|
|
raw: {
|
|
req: http.IncomingMessage;
|
|
res: http.ServerResponse;
|
|
};
|
|
}
|
|
|
|
export interface ClusterLensApiRequest<Path extends string> extends LensApiRequest<Path> {
|
|
cluster: Cluster;
|
|
}
|
|
|
|
export interface LensApiResult<Response> {
|
|
statusCode?: number;
|
|
response?: Response;
|
|
error?: any;
|
|
contentType?: LensApiResultContentType;
|
|
headers?: Partial<Record<string, string>>;
|
|
proxy?: httpProxy;
|
|
}
|
|
|
|
export type RouteResponse<Response> =
|
|
| LensApiResult<Response>
|
|
| void;
|
|
|
|
export interface RouteHandler<TResponse, Path extends string>{
|
|
(request: LensApiRequest<Path>): RouteResponse<TResponse> | Promise<RouteResponse<TResponse>>;
|
|
}
|
|
|
|
export interface BaseRoutePaths<Path extends string> {
|
|
path: Path;
|
|
method: "get" | "post" | "put" | "patch" | "delete";
|
|
}
|
|
|
|
export interface PayloadValidator<Payload> {
|
|
validate(payload: unknown): Joi.ValidationResult<Payload>;
|
|
}
|
|
|
|
export interface ValidatorBaseRoutePaths<Path extends string, Payload> extends BaseRoutePaths<Path> {
|
|
payloadValidator: PayloadValidator<Payload>;
|
|
}
|
|
|
|
export interface Route<TResponse, Path extends string> extends BaseRoutePaths<Path> {
|
|
handler: RouteHandler<TResponse, Path>;
|
|
}
|
|
|
|
export interface BindHandler<Path extends string> {
|
|
<TResponse>(handler: RouteHandler<TResponse, Path>): Route<TResponse, Path>;
|
|
}
|
|
|
|
export function route<Path extends string>(parts: BaseRoutePaths<Path>): BindHandler<Path> {
|
|
return (handler) => ({
|
|
...parts,
|
|
handler,
|
|
});
|
|
}
|
|
|
|
export interface ClusterRouteHandler<Response, Path extends string>{
|
|
(request: ClusterLensApiRequest<Path>): RouteResponse<Response> | Promise<RouteResponse<Response>>;
|
|
}
|
|
|
|
export interface BindClusterHandler<Path extends string> {
|
|
<TResponse>(handler: ClusterRouteHandler<TResponse, Path>): Route<TResponse, Path>;
|
|
}
|
|
|
|
export function clusterRoute<Path extends string>(parts: BaseRoutePaths<Path>): BindClusterHandler<Path> {
|
|
return (handler) => ({
|
|
...parts,
|
|
handler: ({ cluster, ...rest }) => {
|
|
if (!cluster) {
|
|
return {
|
|
error: "Cluster missing",
|
|
statusCode: 400,
|
|
};
|
|
}
|
|
|
|
return handler({ cluster, ...rest });
|
|
},
|
|
});
|
|
}
|
|
|
|
export interface ValidatedClusterLensApiRequest<Path extends string, Payload> extends ClusterLensApiRequest<Path> {
|
|
payload: Payload;
|
|
}
|
|
|
|
export interface ValidatedClusterRouteHandler<Payload, Response, Path extends string> {
|
|
(request: ValidatedClusterLensApiRequest<Path, Payload>): RouteResponse<Response> | Promise<RouteResponse<Response>>;
|
|
}
|
|
|
|
export interface BindValidatedClusterHandler<Path extends string, Payload> {
|
|
<Response>(handler: ValidatedClusterRouteHandler<Payload, Response, Path>): Route<Response, Path>;
|
|
}
|
|
|
|
export function payloadValidatedClusterRoute<Path extends string, Payload>({ payloadValidator, ...parts }: ValidatorBaseRoutePaths<Path, Payload>): BindValidatedClusterHandler<Path, Payload> {
|
|
const boundClusterRoute = clusterRoute(parts);
|
|
|
|
return (handler) => boundClusterRoute(({ payload, ...rest }) => {
|
|
const validationResult = payloadValidator.validate(payload);
|
|
|
|
if (validationResult.error) {
|
|
return {
|
|
error: validationResult.error,
|
|
statusCode: 400,
|
|
};
|
|
}
|
|
|
|
return handler({
|
|
payload: validationResult.value,
|
|
...rest,
|
|
});
|
|
});
|
|
}
|