diff --git a/examples/spa/server.js b/examples/spa/server.ts similarity index 87% rename from examples/spa/server.js rename to examples/spa/server.ts index a87972c..e9a7871 100644 --- a/examples/spa/server.js +++ b/examples/spa/server.ts @@ -3,7 +3,6 @@ import { join, extname } from 'ant:path'; import { createRouter, addRoute, findRoute } from '../rou3'; const router = createRouter(); - const validPaths = new Set(); const invalidPaths = new Set(); @@ -25,6 +24,10 @@ const mimeTypes = new Map([ ['.woff2', 'font/woff2'] ]); +type Context = HttpContext & { params: Record }; +type Handler = (ctx: Context) => void | Promise; +declare function addRoute(router: unknown, method: string, path: string, handler: Handler): void; + addRoute(router, 'GET', '/api/version', async c => c.res.json({ version: Ant.version })); addRoute(router, 'GET', '**', c => { @@ -54,7 +57,7 @@ addRoute(router, 'GET', '**', c => { } }); -async function handleRequest(c) { +async function handleRequest(c: Context) { console.log('request:', c.req.method, c.req.uri); const result = findRoute(router, c.req.method, c.req.uri); diff --git a/src/types/ant.d.ts b/src/types/ant.d.ts index 0b420ea..a989ee9 100644 --- a/src/types/ant.d.ts +++ b/src/types/ant.d.ts @@ -51,22 +51,24 @@ interface AntRaw { } interface HttpContext { - request: { + req: { method: string; - url: string; - path: string; + uri: string; query: string; body: string; - headers: Record; + header(name: string): string | undefined; }; - response: { - status(code: number): HttpContext['response']; - header(name: string, value: string): HttpContext['response']; - send(body: string): void; - json(data: unknown): void; + res: { + header(name: string, value: string): void; + status(code: number): void; + body(body: string, status?: number, contentType?: string): void; + html(body: string, status?: number): void; + json(data: unknown, status?: number): void; + notFound(): void; redirect(url: string, status?: number): void; }; - store: Record; + set(key: string, value: unknown): void; + get(key: string): unknown; } interface AntStatic { @@ -81,12 +83,12 @@ interface AntStatic { alloc(): AntAllocResult; stats(): AntStatsResult; - signal(signum: number, handler: (signum: number) => void): void; sleep(seconds: number): void; msleep(milliseconds: number): void; usleep(microseconds: number): void; - serve(port: number, handler?: (ctx: HttpContext) => void | Promise): number; + signal(signum: number, handler: (signum: number) => void): void; + serve(port: number, handler?: (ctx: T) => void | Promise): number; } declare const Ant: AntStatic; diff --git a/src/types/import.d.ts b/src/types/import.d.ts new file mode 100644 index 0000000..5ef8a59 --- /dev/null +++ b/src/types/import.d.ts @@ -0,0 +1,9 @@ +interface ImportMeta { + url: string; + filename: boolean; + dirname: string; + main: boolean; + resolve(specifier: string): string; + + readonly env: { [key: string]: string }; +} diff --git a/src/types/snapshot.d.ts b/src/types/snapshot.d.ts deleted file mode 100644 index e2be415..0000000 --- a/src/types/snapshot.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -interface ImportMeta { - readonly env: { [key: string]: string }; -} diff --git a/tsconfig.json b/tsconfig.json index 082eb3b..4a3e92d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "noEmit": true, "strict": true, - "noUnusedLocals": false, + "noImplicitAny": true, "target": "ES2024", "module": "ESNext",