feat: middlewares
feat: highlighter
This commit is contained in:
@ -34,7 +34,7 @@ export class Engine {
|
||||
}
|
||||
|
||||
async handle(input: HandlerInput): Promise<EngineOutput> {
|
||||
const url = new URL(input.getUrl());
|
||||
const url = new URL(input.url);
|
||||
const path = url.pathname + url.search + url.hash;
|
||||
for (const route of this.routes) {
|
||||
const match = route.route.match(path);
|
||||
|
@ -24,9 +24,7 @@ export function createElement(
|
||||
})
|
||||
.join(' ');
|
||||
|
||||
return inner.length === 0
|
||||
? `<${name} ${propsstr}/>`
|
||||
: `<${name} ${propsstr}>${content}</${name}>`;
|
||||
return `<${name} ${propsstr}>${content}</${name}>`;
|
||||
} else if (typeof name === 'function') {
|
||||
return name(props, content);
|
||||
} else {
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { Engine } from './engine';
|
||||
import { Middleware } from './middleware';
|
||||
|
||||
import {
|
||||
EngineParseError,
|
||||
@ -16,17 +17,22 @@ import {
|
||||
HandlerOutput,
|
||||
Route,
|
||||
handlerSchema,
|
||||
EngineOutput,
|
||||
MiddleFunction,
|
||||
} from './types/handler';
|
||||
|
||||
import * as JSX from './jsx';
|
||||
|
||||
export {
|
||||
Engine,
|
||||
Middleware,
|
||||
EngineParseError,
|
||||
NoHandlerFoundError,
|
||||
TxtDotError,
|
||||
EngineFunction,
|
||||
MiddleFunction,
|
||||
EngineMatch,
|
||||
EngineOutput,
|
||||
Engines,
|
||||
RouteValues,
|
||||
EnginesMatch,
|
||||
|
61
packages/sdk/src/middleware.ts
Normal file
61
packages/sdk/src/middleware.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import Route from 'route-parser';
|
||||
|
||||
import {
|
||||
HandlerInput,
|
||||
RouteValues,
|
||||
EngineOutput,
|
||||
MiddleFunction,
|
||||
} from './types/handler';
|
||||
|
||||
interface IMiddle<TParams extends RouteValues> {
|
||||
route: Route;
|
||||
handler: MiddleFunction<TParams>;
|
||||
}
|
||||
|
||||
export class Middleware {
|
||||
name: string;
|
||||
description: string;
|
||||
domains: string[];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
middles: IMiddle<any>[] = [];
|
||||
constructor(name: string, description: string, domains: string[] = []) {
|
||||
this.domains = domains;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
route<TParams extends RouteValues>(
|
||||
path: string,
|
||||
handler: MiddleFunction<TParams>
|
||||
) {
|
||||
this.middles.push({ route: new Route<TParams>(path), handler });
|
||||
}
|
||||
|
||||
use<TParams extends RouteValues>(handler: MiddleFunction<TParams>) {
|
||||
this.middles.push({ route: new Route<{ path: string }>('*path'), handler });
|
||||
}
|
||||
|
||||
async handle(input: HandlerInput, out: EngineOutput): Promise<EngineOutput> {
|
||||
const url = new URL(input.url);
|
||||
const path = url.pathname + url.search + url.hash;
|
||||
|
||||
let processed_out = out;
|
||||
|
||||
for (const middle of this.middles) {
|
||||
const match = middle.route.match(path);
|
||||
|
||||
if (match) {
|
||||
processed_out = await middle.handler(
|
||||
input,
|
||||
{
|
||||
q: match,
|
||||
reverse: (req) => middle.route.reverse(req),
|
||||
},
|
||||
out
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return processed_out;
|
||||
}
|
||||
}
|
@ -2,26 +2,30 @@ import { parseHTML } from 'linkedom';
|
||||
import { Engine } from '../engine';
|
||||
|
||||
export class HandlerInput {
|
||||
private data: string;
|
||||
private url: string;
|
||||
private window?: Window;
|
||||
private _data: string;
|
||||
private _url: string;
|
||||
private _window?: Window;
|
||||
|
||||
constructor(data: string, url: string) {
|
||||
this.data = data;
|
||||
this.url = url;
|
||||
this._data = data;
|
||||
this._url = url;
|
||||
}
|
||||
|
||||
getUrl(): string {
|
||||
return this.url;
|
||||
get url(): string {
|
||||
return this._url;
|
||||
}
|
||||
|
||||
get data(): string {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
get document(): Document {
|
||||
if (this.window) {
|
||||
return this.window.document;
|
||||
if (this._window) {
|
||||
return this._window.document;
|
||||
}
|
||||
|
||||
this.window = parseHTML(this.data);
|
||||
return this.window.document;
|
||||
this._window = parseHTML(this._data);
|
||||
return this._window.document;
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,6 +79,12 @@ export type EngineFunction<TParams extends RouteValues> = (
|
||||
ro: Route<TParams>
|
||||
) => Promise<EngineOutput>;
|
||||
|
||||
export type MiddleFunction<TParams extends RouteValues> = (
|
||||
input: HandlerInput,
|
||||
ro: Route<TParams>,
|
||||
out: EngineOutput
|
||||
) => Promise<EngineOutput>;
|
||||
|
||||
export type EnginesMatch<TParams extends RouteValues> = EngineMatch<TParams>[];
|
||||
|
||||
export interface Route<TParams extends RouteValues> {
|
||||
|
Reference in New Issue
Block a user