Skip to content

Commit

Permalink
Rename KyukoRequestHandler to KyukoRouteHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
rikilele committed Jul 11, 2021
1 parent 42071aa commit dc45844
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { Kyuko } from "./src/Kyuko.ts";
export type {
KyukoErrorHandler,
KyukoMiddleware,
KyukoRequestHandler,
KyukoRouteHandler,
} from "./src/Kyuko.ts";
export type { KyukoRequest } from "./src/KyukoRequest.ts";
export type { KyukoResponse } from "./src/KyukoResponse.ts";
44 changes: 20 additions & 24 deletions src/Kyuko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { RoutePathHandler } from "./RoutePathHandler.ts";
* A function that is invoked in response to fetch requests.
* Runs after all middleware functions have been called.
*/
export type KyukoRequestHandler = (
export type KyukoRouteHandler = (
| ((req: KyukoRequest, res: KyukoResponse) => void)
| ((req: KyukoRequest, res: KyukoResponse) => Promise<void>)
);

/**
* A function that is invoked before the request handler is called.
* Hands over execution to the next middleware / request handler on return.
* A function that is invoked before the route handler is called.
* Hands over execution to the next middleware / route handler on return.
*/
export type KyukoMiddleware = (
| ((req: KyukoRequest, res: KyukoResponse) => void)
Expand Down Expand Up @@ -75,8 +75,8 @@ export class Kyuko {
#routes;
#middleware: KyukoMiddleware[];
#errorHandlers: KyukoErrorHandler[];
#defaultHandler: KyukoRequestHandler;
#customHandlers: Map<string, Map<string, KyukoRequestHandler>>;
#defaultHandler: KyukoRouteHandler;
#customHandlers: Map<string, Map<string, KyukoRouteHandler>>;

/**
* Initializes a new Kyuko app.
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Kyuko {
* });
* ```
*/
get(routePath: string, handler: KyukoRequestHandler) {
get(routePath: string, handler: KyukoRouteHandler) {
this.#routes.addRoutePath(routePath);
this.#customHandlers.get("GET")?.set(routePath, handler);
}
Expand All @@ -117,7 +117,7 @@ export class Kyuko {
* Registers a `handler` that is invoked when
* POST requests are made to url paths that match the `routePath`.
*/
post(routePath: string, handler: KyukoRequestHandler) {
post(routePath: string, handler: KyukoRouteHandler) {
this.#routes.addRoutePath(routePath);
this.#customHandlers.get("POST")?.set(routePath, handler);
}
Expand All @@ -138,7 +138,7 @@ export class Kyuko {
* });
* ```
*/
put(routePath: string, handler: KyukoRequestHandler) {
put(routePath: string, handler: KyukoRouteHandler) {
this.#routes.addRoutePath(routePath);
this.#customHandlers.get("PUT")?.set(routePath, handler);
}
Expand All @@ -147,7 +147,7 @@ export class Kyuko {
* Registers a `handler` that is invoked when
* DELETE requests are made to url paths that match the `routePath`.
*/
delete(routePath: string, handler: KyukoRequestHandler) {
delete(routePath: string, handler: KyukoRouteHandler) {
this.#routes.addRoutePath(routePath);
this.#customHandlers.get("DELETE")?.set(routePath, handler);
}
Expand All @@ -156,7 +156,7 @@ export class Kyuko {
* Registers a `handler` that is invoked when
* PATCH requests are made to url paths that match the `routePath`.
*/
patch(routePath: string, handler: KyukoRequestHandler) {
patch(routePath: string, handler: KyukoRouteHandler) {
this.#routes.addRoutePath(routePath);
this.#customHandlers.get("PATCH")?.set(routePath, handler);
}
Expand All @@ -175,7 +175,7 @@ export class Kyuko {
* });
* ```
*/
head(routePath: string, handler: KyukoRequestHandler) {
head(routePath: string, handler: KyukoRouteHandler) {
this.#routes.addRoutePath(routePath);
this.#customHandlers.get("HEAD")?.set(routePath, handler);
}
Expand All @@ -184,7 +184,7 @@ export class Kyuko {
* Registers a `handler` that is invoked when
* any type of requests are made to url paths that match the `routePath`.
*/
all(routePath: string, handler: KyukoRequestHandler) {
all(routePath: string, handler: KyukoRouteHandler) {
this.#routes.addRoutePath(routePath);
this.#customHandlers.get("GET")?.set(routePath, handler);
this.#customHandlers.get("POST")?.set(routePath, handler);
Expand All @@ -196,7 +196,7 @@ export class Kyuko {
* Registers a default `handler` that is invoked when
* a request isn't caught by any other custom handlers.
*/
default(handler: KyukoRequestHandler) {
default(handler: KyukoRouteHandler) {
this.#defaultHandler = handler;
}

Expand Down Expand Up @@ -235,14 +235,14 @@ export class Kyuko {

private handleRequest(req: KyukoRequest, res: KyukoResponse) {
const { pathname, searchParams } = new URL(req.url);
let handler: KyukoRequestHandler = this.#defaultHandler;
let handler: KyukoRouteHandler = this.#defaultHandler;

// Handle routing
const routePath = this.#routes.findMatch(pathname);
if (routePath !== undefined) {
const customHandlers = this.#customHandlers.get(req.method);
if (customHandlers?.has(routePath)) {
handler = customHandlers.get(routePath) as KyukoRequestHandler;
handler = customHandlers.get(routePath) as KyukoRouteHandler;
}

// Fill req.params
Expand All @@ -260,22 +260,20 @@ export class Kyuko {
private async invokeHandlers(
req: KyukoRequest,
res: KyukoResponse,
handler: KyukoRequestHandler,
handler: KyukoRouteHandler,
) {
// Run middleware and request handler
// Run middleware and route handler
try {
for (const middleware of this.#middleware) {
await middleware(req, res);
}

handler(req, res);

// Catch error from middleware OR request handler
// Catch error from middleware OR route handler
} catch (err1) {
console.error(
brightRed(
"Error thrown when calling a KyukoMiddleware / KyukoRequestHandler:",
),
brightRed("Error thrown from a KyukoMiddleware / KyukoRouteHandler:"),
);
console.error(err1);

Expand All @@ -287,9 +285,7 @@ export class Kyuko {

// Catch error from error handler
} catch (err2) {
console.error(
brightRed("Error thrown when calling a KyukoErrorHandler:"),
);
console.error(brightRed("Error thrown from a KyukoErrorHandler:"));
console.error(err2);
}

Expand Down

0 comments on commit dc45844

Please sign in to comment.