Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for deno publish #8

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"deno.enable": true,
"deno.lint": true,
"editor.formatOnSave": true,
"[typescript]": { "editor.defaultFormatter": "denoland.vscode-deno" }
}
15 changes: 14 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@
"./response": "./src/response.ts",
"./server": "./src/server.ts"
},
"fmt": {
"lineWidth": 100,
"indentWidth": 2,
"semiColons": true,
"singleQuote": true,
"exclude": ["npm/**/*", "example/**/*"]
},
"lint": {
"exclude": ["npm/**/*"]
},
"publish": {
"include": ["LICENSE", "README.md", "src/**/*.ts"],
"include": [
"deno.json",
"deno.lock",
"LICENSE",
"README.md",
"src/**/*.ts"
],
"exclude": ["src/**/*.test.ts"]
}
}
37 changes: 29 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,29 @@ interface Route {

type RoutePattern = string | ((url: URL, request: Request) => boolean);

export function router(...routes: Route[]): Handler {
return request => {
interface RouteFactory {
(pattern: RoutePattern, handler: Handler): Route;
}

interface RouterAPI {
(...routes: Route[]): Handler;
builder: () => HandlerBuilder;
}

interface RouteAPI extends RouteFactory {
all: RouteFactory;
get: RouteFactory;
post: RouteFactory;
put: RouteFactory;
delete: RouteFactory;
head: RouteFactory;
options: RouteFactory;
connect: RouteFactory;
patch: RouteFactory;
}

export const router: RouterAPI = (...routes: Route[]): Handler => {
return (request) => {
const url = new URL(request.url);

for (const route of routes) {
Expand All @@ -19,11 +40,11 @@ export function router(...routes: Route[]): Handler {

return new Response('Not found', { status: 404 });
};
}
};

router.builder = builder;

export function route(pattern: RoutePattern, handler: Handler): Route {
export const route: RouteAPI = (pattern: RoutePattern, handler: Handler): Route => {
if (typeof pattern === 'function') {
return {
matches: pattern,
Expand All @@ -32,12 +53,12 @@ export function route(pattern: RoutePattern, handler: Handler): Route {
}

return {
matches: url => url.pathname === pattern,
matches: (url) => url.pathname === pattern,
handler,
};
}
};

route.all = route; // for express compatibility
route.all = (...args) => route(...args); // for express compatibility
route.get = createRouteFactoryForMethod('get');
route.post = createRouteFactoryForMethod('post');
route.put = createRouteFactoryForMethod('put');
Expand All @@ -47,7 +68,7 @@ route.options = createRouteFactoryForMethod('options');
route.connect = createRouteFactoryForMethod('connect');
route.patch = createRouteFactoryForMethod('patch');

function createRouteFactoryForMethod(method: string) {
function createRouteFactoryForMethod(method: string): RouteFactory {
const isSuitableMethod = (request: Request) => request.method.toLowerCase() === method;

return (pattern: RoutePattern, handler: Handler): Route => {
Expand Down
Loading