mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
chore: extract route from core
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
a22229ba60
commit
0685470009
40 files changed
+241
-136
No files matched your search
@@ -0,0 +1,4 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
export * from "./src/route";
|
||||
export { routeInjectionToken } from "./src/route-injection-token";
|
||||
@@ -0,0 +1,3 @@
|
||||
const config = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;
|
||||
|
||||
module.exports = { ...config, coverageThreshold: undefined };
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@k8slens/route",
|
||||
"version": "1.0.0",
|
||||
"type": "commonjs",
|
||||
"description": "Route for Lens",
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"private": false,
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"author": {
|
||||
"name": "OpenLens Authors",
|
||||
"email": "info@k8slens.dev"
|
||||
},
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/lensapp/lens",
|
||||
"scripts": {
|
||||
"clean": "rimraf dist/",
|
||||
"build": "lens-webpack-build",
|
||||
"test:unit": "jest --coverage --runInBand"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@k8slens/test-utils": "^1.0.0-alpha.3",
|
||||
"@k8slens/utilities": "^1.0.0-alpha.1",
|
||||
"@ogre-tools/fp": "^16.1.0",
|
||||
"@ogre-tools/injectable": "^16.1.0",
|
||||
"http-proxy": "^1.18.1",
|
||||
"joi": "^17.9.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@k8slens/webpack": "^6.5.0-alpha.9"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type { Route } from "./route";
|
||||
|
||||
export const routeInjectionToken = getInjectionToken<Route<unknown, string>>({
|
||||
id: "route-injection-token",
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type http from "http";
|
||||
import type httpProxy from "http-proxy";
|
||||
import type { URLSearchParams } from "url";
|
||||
import type Joi from "joi";
|
||||
|
||||
export interface LensApiResultContentType {
|
||||
resultMapper: (result: LensApiResult<unknown>) => ({
|
||||
statusCode: number;
|
||||
content: unknown;
|
||||
headers: Record<string, string>;
|
||||
});
|
||||
}
|
||||
|
||||
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>;
|
||||
query: URLSearchParams;
|
||||
raw: {
|
||||
req: http.IncomingMessage;
|
||||
res: http.ServerResponse;
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "@k8slens/typescript/config/base.json"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
module.exports = require("@k8slens/webpack").configForNode;
|
||||
Reference in new issue
Block a user