-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from sejori/params
feat: /added/:params/to/routes
- Loading branch information
Showing
6 changed files
with
96 additions
and
29 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,30 +1,46 @@ | ||
import { Server as stdServer } from "https://deno.land/[email protected]/http/server.ts" | ||
import { Router } from "./utils/Router.ts" | ||
import { Router, _Route } from "./utils/Router.ts" | ||
import { Cascade, PromiseMiddleware } from "./utils/Cascade.ts" | ||
import { Middleware, Route } from "./types.ts" | ||
import { Middleware } from "./types.ts" | ||
|
||
export class RequestContext { | ||
server: Server | ||
request: Request | ||
url: URL | ||
_route: _Route | undefined | ||
state: Record<string, unknown> | ||
params: Record<string, unknown> = {} | ||
|
||
constructor(server: Server, request: Request, state?: Record<string, unknown>) { | ||
this.server = server | ||
this.request = request | ||
this.state = state | ||
? state | ||
: {} | ||
constructor( | ||
public server: Server, | ||
public request: Request, | ||
state?: Record<string, unknown> | ||
) { | ||
this.url = new URL(request.url) | ||
this.state = state ? state : {} | ||
} | ||
|
||
get route () { | ||
return this._route | ||
} | ||
|
||
set route (r: _Route | undefined) { | ||
this._route = r; | ||
if (r?.params) { | ||
const pathBits = this.url.pathname.split("/") | ||
for (const param in r.params) { | ||
this.params[param] = pathBits[r.params[param]] | ||
} | ||
} | ||
} | ||
} | ||
|
||
export class Server extends Router { | ||
stdServer: stdServer | undefined | ||
port = 7777 | ||
hostname = "0.0.0.0" | ||
hostname = "127.0.0.1" | ||
middleware: PromiseMiddleware[] = [] | ||
routers: Router[] = [] | ||
|
||
public get allRoutes(): Route[] { | ||
public get allRoutes(): _Route[] { | ||
return [ this, ...this.routers].map(router => router.routes).flat() | ||
} | ||
|
||
|
@@ -93,14 +109,13 @@ export class Server extends Router { | |
*/ | ||
async requestHandler(request: Request): Promise<Response> { | ||
const ctx: RequestContext = new RequestContext(this, request) | ||
const requestURL = new URL(ctx.request.url) | ||
|
||
const route = this.allRoutes.find(route => | ||
route.path === requestURL.pathname && | ||
ctx.route = this.allRoutes.find(route => | ||
route.regexPath.test(ctx.url.pathname) && | ||
route.method === request.method | ||
) | ||
return await new Cascade(ctx, route).start() | ||
|
||
return await new Cascade(ctx).start() | ||
} | ||
|
||
/** | ||
|
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
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,7 @@ | ||
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts" | ||
import { Server, RequestContext } from "../../lib/Server.ts" | ||
import { Cascade } from "../../lib/utils/Cascade.ts" | ||
import { _Route } from "../../lib/utils/Router.ts" | ||
import { | ||
testMiddleware1, | ||
testMiddleware2, | ||
|
@@ -11,8 +12,7 @@ import { | |
Deno.test("UTIL: Cascade", async (t) => { | ||
const testServer = new Server() | ||
const testContext = new RequestContext(testServer, new Request("http://localhost")) | ||
|
||
const cascade = new Cascade(testContext, { | ||
testContext._route = new _Route({ | ||
path: "/", | ||
middleware: [ | ||
testMiddleware1, | ||
|
@@ -22,6 +22,8 @@ Deno.test("UTIL: Cascade", async (t) => { | |
], | ||
handler: testHandler | ||
}) | ||
|
||
const cascade = new Cascade(testContext) | ||
|
||
const result = await cascade.start() | ||
|
||
|
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