-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! tmp: r2, origin middleware init
- Loading branch information
Showing
4 changed files
with
56 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { Context } from '../context'; | ||
|
||
export type MiddlewareNext = () => Promise<Response>; | ||
|
||
export interface Middleware { | ||
handle( | ||
request: Request, | ||
next: (request: Request) => Promise<Response> | ||
ctx: Context, | ||
next: MiddlewareNext | ||
): Promise<Response>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,62 @@ | ||
import { AutoRouter } from 'itty-router'; | ||
import { AutoRouter, IRequest } from 'itty-router'; | ||
import { Middleware } from '../middleware/middleware'; | ||
import { Context } from '../context'; | ||
|
||
export class Router { | ||
private itty = AutoRouter(); | ||
private notFoundMiddleware = new NotFoundMiddleware(); | ||
private itty = AutoRouter<IRequest, [Context]>(); | ||
|
||
handle(request: Request): Promise<Response> { | ||
return this.itty.fetch(request); | ||
} | ||
|
||
head(endpoint: string, middlewares: Middleware[]): void { | ||
const middlewareChain = buildMiddlewareChain(middlewares); | ||
|
||
this.itty.head(endpoint, async (req, ctx) => { | ||
return middlewareChain(req, ctx); | ||
}); | ||
} | ||
|
||
get(endpoint: string, middlewares: Middleware[]): void { | ||
const root = buildMiddlewareChain(middlewares, this.notFoundMiddleware); | ||
const middlewareChain = buildMiddlewareChain(middlewares); | ||
|
||
this.itty.get(endpoint, async req => { | ||
return root.middleware.handle(req, root.next); | ||
this.itty.get(endpoint, async (req, ctx) => { | ||
return middlewareChain(req, ctx); | ||
}); | ||
} | ||
} | ||
|
||
interface MiddlewareChain { | ||
middleware: Middleware; | ||
next: (req: Request) => Promise<Response>; | ||
} | ||
type MiddlewareChain = (request: Request, ctx: Context) => Promise<Response>; | ||
|
||
function buildMiddlewareChain( | ||
middlewares: Middleware[], | ||
fallbackMiddleware: Middleware | ||
): MiddlewareChain { | ||
// @ts-expect-error next is undefined | ||
let root: MiddlewareChain = { middleware: fallbackMiddleware }; | ||
function buildMiddlewareChain(middlewares: Middleware[]): MiddlewareChain { | ||
let root: MiddlewareChain = () => { | ||
throw new Error('reached the end of middleware chain'); | ||
}; | ||
|
||
for (let i = middlewares.length - 1; i >= 0; i--) { | ||
root = { | ||
middleware: middlewares[i], | ||
// @ts-expect-error next might be undefined | ||
next: root.middleware.handle, | ||
root = (request, ctx) => { | ||
const middleware = errorHandled(middlewares[i]); | ||
|
||
return middleware.handle(request, ctx, () => { | ||
return root(request, ctx); | ||
}); | ||
}; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
class NotFoundMiddleware implements Middleware { | ||
async handle(_: Request): Promise<Response> { | ||
return new Response('Not found', { status: 404 }); | ||
} | ||
function errorHandled(middleware: Middleware): Middleware { | ||
const wrapper: Middleware = { | ||
handle(request: Request, ctx: Context, next) { | ||
try { | ||
return middleware.handle(request, ctx, next); | ||
} catch (err) { | ||
// TODO send error to sentry | ||
return next(); | ||
} | ||
}, | ||
}; | ||
|
||
return wrapper; | ||
} |