refactor: engine output

This commit is contained in:
Artemy
2024-05-13 13:30:47 +03:00
parent 494d1c8134
commit bdf625bb1f
13 changed files with 97 additions and 66 deletions

View File

@ -2,9 +2,9 @@ import Route from 'route-parser';
import {
HandlerInput,
IHandlerOutput,
EngineFunction,
RouteValues,
EngineOutput,
} from './types/handler';
import { NoHandlerFoundError } from './types/errors';
@ -33,7 +33,7 @@ export class Engine {
this.routes.push({ route: new Route<TParams>(path), handler });
}
async handle(input: HandlerInput): Promise<IHandlerOutput> {
async handle(input: HandlerInput): Promise<EngineOutput> {
const url = new URL(input.getUrl());
const path = url.pathname + url.search + url.hash;
for (const route of this.routes) {

View File

@ -13,7 +13,7 @@ import {
RouteValues,
EnginesMatch,
HandlerInput,
IHandlerOutput,
HandlerOutput,
Route,
handlerSchema,
} from './types/handler';
@ -29,7 +29,7 @@ export {
RouteValues,
EnginesMatch,
HandlerInput,
IHandlerOutput,
HandlerOutput,
Route,
handlerSchema,
};

View File

@ -4,7 +4,7 @@ import { Engine } from '../engine';
export class HandlerInput {
private data: string;
private url: string;
private dom?: Window;
private window?: Window;
constructor(data: string, url: string) {
this.data = data;
@ -15,23 +15,30 @@ export class HandlerInput {
return this.url;
}
parseDom(): Window {
if (this.dom) {
return this.dom;
get document(): Document {
if (this.window) {
return this.window.document;
}
this.dom = parseHTML(this.data);
return this.dom;
this.window = parseHTML(this.data);
return this.window.document;
}
}
export interface IHandlerOutput {
export interface HandlerOutput {
content: string;
textContent: string;
title?: string;
lang?: string;
}
export interface EngineOutput {
document: Document;
textContent?: string;
title?: string;
lang?: string;
}
export const handlerSchema = {
type: 'object',
properties: {
@ -66,7 +73,7 @@ export interface RouteValues {
export type EngineFunction<TParams extends RouteValues> = (
input: HandlerInput,
ro: Route<TParams>
) => Promise<IHandlerOutput>;
) => Promise<EngineOutput>;
export type EnginesMatch<TParams extends RouteValues> = EngineMatch<TParams>[];