Skip to content

Commit

Permalink
fixes for lint
Browse files Browse the repository at this point in the history
  • Loading branch information
krutoo committed Jun 28, 2024
1 parent 95a941f commit e1ca35d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
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": ["deno.json", "deno.lock", "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

0 comments on commit e1ca35d

Please sign in to comment.