1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

chore: fix jest failure

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2023-05-29 09:55:03 +03:00
parent 0685470009
commit 0cb4b8a112
3 changed files with 43 additions and 21 deletions

View File

@ -1,3 +1,3 @@
const config = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact; const config = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;
module.exports = { ...config, coverageThreshold: undefined }; module.exports = { ...config, coverageThreshold: undefined };

View File

@ -0,0 +1,20 @@
import { route } from "./route";
describe("route", () => {
it("returns a bind handler", () => {
const handler = route({
path: "/test",
method: "get",
});
const response = handler((req) => {
return {
response: "test",
};
});
expect(response.path).toBe("/test");
expect(response.method).toBe("get");
expect(response.handler).toEqual(expect.any(Function));
});
});

View File

@ -9,22 +9,21 @@ import type { URLSearchParams } from "url";
import type Joi from "joi"; import type Joi from "joi";
export interface LensApiResultContentType { export interface LensApiResultContentType {
resultMapper: (result: LensApiResult<unknown>) => ({ resultMapper: (result: LensApiResult<unknown>) => {
statusCode: number; statusCode: number;
content: unknown; content: unknown;
headers: Record<string, string>; headers: Record<string, string>;
}); };
} }
export type InferParam< export type InferParam<
T extends string, T extends string,
PathParams extends Record<string, string>, PathParams extends Record<string, string>
> = > = T extends `{${infer P}?}`
T extends `{${infer P}?}` ? PathParams & Partial<Record<P, string>>
? PathParams & Partial<Record<P, string>> : T extends `{${infer P}}`
: T extends `{${infer P}}` ? PathParams & Record<P, string>
? PathParams & Record<P, string> : PathParams;
: PathParams;
export type InferParamFromPath<P extends string> = export type InferParamFromPath<P extends string> =
P extends `${string}/{${infer B}*}${infer Tail}` P extends `${string}/{${infer B}*}${infer Tail}`
@ -32,8 +31,8 @@ export type InferParamFromPath<P extends string> =
? Record<B, string> ? Record<B, string>
: never : never
: P extends `${infer A}/${infer B}` : P extends `${infer A}/${infer B}`
? InferParam<A, InferParamFromPath<B>> ? InferParam<A, InferParamFromPath<B>>
: InferParam<P, {}>; : InferParam<P, {}>;
export interface LensApiRequest<Path extends string> { export interface LensApiRequest<Path extends string> {
path: Path; path: Path;
@ -55,12 +54,12 @@ export interface LensApiResult<Response> {
proxy?: httpProxy; proxy?: httpProxy;
} }
export type RouteResponse<Response> = export type RouteResponse<Response> = LensApiResult<Response> | void;
| LensApiResult<Response>
| void;
export interface RouteHandler<TResponse, Path extends string>{ export interface RouteHandler<TResponse, Path extends string> {
(request: LensApiRequest<Path>): RouteResponse<TResponse> | Promise<RouteResponse<TResponse>>; (request: LensApiRequest<Path>):
| RouteResponse<TResponse>
| Promise<RouteResponse<TResponse>>;
} }
export interface BaseRoutePaths<Path extends string> { export interface BaseRoutePaths<Path extends string> {
@ -72,20 +71,23 @@ export interface PayloadValidator<Payload> {
validate(payload: unknown): Joi.ValidationResult<Payload>; validate(payload: unknown): Joi.ValidationResult<Payload>;
} }
export interface ValidatorBaseRoutePaths<Path extends string, Payload> extends BaseRoutePaths<Path> { export interface ValidatorBaseRoutePaths<Path extends string, Payload>
extends BaseRoutePaths<Path> {
payloadValidator: PayloadValidator<Payload>; payloadValidator: PayloadValidator<Payload>;
} }
export interface Route<TResponse, Path extends string> extends BaseRoutePaths<Path> { export interface Route<TResponse, Path extends string>
extends BaseRoutePaths<Path> {
handler: RouteHandler<TResponse, Path>; handler: RouteHandler<TResponse, Path>;
} }
export interface BindHandler<Path extends string> { export interface BindHandler<Path extends string> {
<TResponse>(handler: RouteHandler<TResponse, Path>): Route<TResponse, Path>; <TResponse>(handler: RouteHandler<TResponse, Path>): Route<TResponse, Path>;
} }
export function route<Path extends string>(parts: BaseRoutePaths<Path>): BindHandler<Path> { export function route<Path extends string>(
parts: BaseRoutePaths<Path>
): BindHandler<Path> {
return (handler) => ({ return (handler) => ({
...parts, ...parts,
handler, handler,