From 1f8d4ff25b644fb1b1ab32cc0e0c413cda1d5072 Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Mon, 20 May 2024 15:03:31 +0200 Subject: [PATCH] feat: Adding TypeScript types --- common.d.ts | 42 ++++++++++++++++++++++++++++++++++ demos/basic.ts | 24 +++++++++++++++++++ index.d.ts | 7 ++++++ lib/router/sequential.d.ts | 3 +++ package-lock.json | 47 ++++++++++++++++++++++++++++++++++---- package.json | 1 + 6 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 common.d.ts create mode 100644 demos/basic.ts create mode 100644 index.d.ts create mode 100644 lib/router/sequential.d.ts diff --git a/common.d.ts b/common.d.ts new file mode 100644 index 0000000..c08b4c4 --- /dev/null +++ b/common.d.ts @@ -0,0 +1,42 @@ +import { Pattern, Methods } from 'trouter' + +export interface IRouterConfig { + defaultRoute?: RequestHandler + errorHandler?: (err: Error) => Response | Promise + port?: number +} + +export type StepFunction = (error?: unknown) => Response | Promise + +type ZeroRequest = Request & { + params: Record + query: Record + ctx?: Record +} + +export type RequestHandler = ( + req: ZeroRequest, + next: StepFunction +) => Response | Promise + +export interface IRouter { + fetch: (req: Request) => Response | Promise + + use(...handlers: RequestHandler[]): this + use(router: IRouter): this + use(pattern: Pattern, ...handlers: RequestHandler[]): this + use(prefix: Pattern, router: IRouter): this + + on(method: Methods, pattern: Pattern, ...middlewares: RequestHandler[]): this + + all(pattern: Pattern, ...handlers: RequestHandler[]): this + get(pattern: Pattern, ...handlers: RequestHandler[]): this + head(pattern: Pattern, ...handlers: RequestHandler[]): this + patch(pattern: Pattern, ...handlers: RequestHandler[]): this + options(pattern: Pattern, ...handlers: RequestHandler[]): this + connect(pattern: Pattern, ...handlers: RequestHandler[]): this + delete(pattern: Pattern, ...handlers: RequestHandler[]): this + trace(pattern: Pattern, ...handlers: RequestHandler[]): this + post(pattern: Pattern, ...handlers: RequestHandler[]): this + put(pattern: Pattern, ...handlers: RequestHandler[]): this +} diff --git a/demos/basic.ts b/demos/basic.ts new file mode 100644 index 0000000..72e4a01 --- /dev/null +++ b/demos/basic.ts @@ -0,0 +1,24 @@ +import { StepFunction, ZeroRequest } from "../index" +import http from '../index' + +const { router } = http({}) +router.use((req: ZeroRequest, next: StepFunction) => { + req.ctx = { + engine: 'bun' + } + + return next() +}) +router.get('/:id', async (req: ZeroRequest) => { + return Response.json(req.params) +}) +router.post('/', async (req: ZeroRequest) => { + return new Response('POST') +}) +router.delete('/:id', async (req: ZeroRequest) => { + return Response.json(req.params, { + status: 200 + }) +}) + +export default router diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e1f4456 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,7 @@ +import { IRouter, IRouterConfig } from './common' + +export default function zero(config?: IRouterConfig): { + router: IRouter +} + +export * from './common' \ No newline at end of file diff --git a/lib/router/sequential.d.ts b/lib/router/sequential.d.ts new file mode 100644 index 0000000..e70060d --- /dev/null +++ b/lib/router/sequential.d.ts @@ -0,0 +1,3 @@ +import { IRouter, IRouterConfig } from './../../index' + +export default function createSequentialRouter(config?: IRouterConfig): IRouter diff --git a/package-lock.json b/package-lock.json index 9dcc3e1..5737055 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,30 +1,61 @@ { "name": "0http-bun", - "version": "1.0.2", + "version": "1.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "0http-bun", - "version": "1.0.2", + "version": "1.0.3", "license": "MIT", "dependencies": { "fast-querystring": "^1.1.2", "trouter": "^3.2.1" }, "devDependencies": { - "0http-bun": "^1.0.0", + "0http-bun": "^1.0.3", + "bun-types": "^1.1.8", "mitata": "^0.1.11" } }, + "node_modules/@types/node": { + "version": "20.12.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.12.tgz", + "integrity": "sha512-eWLDGF/FOSPtAvEqeRAQ4C8LSA7M1I7i0ky1I8U7kD1J5ITyW3AsRhQrKVoWf5pFKZ2kILsEGJhsI9r93PYnOw==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/@types/ws": { + "version": "8.5.10", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz", + "integrity": "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/0http-bun": { - "version": "1.0.0", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/0http-bun/-/0http-bun-1.0.3.tgz", + "integrity": "sha512-LrdT8iRo0VfelD1b6rs1d/G6TEo0XRdCR8usaglh46GcusxbkveZy7SlUHwhtAN6VvQ6Ht3eUrmO/Dea7/ub9Q==", "dev": true, - "license": "MIT", "dependencies": { + "fast-querystring": "^1.1.2", "trouter": "^3.2.1" } }, + "node_modules/bun-types": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/bun-types/-/bun-types-1.1.8.tgz", + "integrity": "sha512-dwhfuUKSGK8hm5Llcvb5+ejRh+4mIt8ibObJVKhZBsi0ScpXmt+AlaS1eDW6uRXCHj084Qt0kIqAJ08/7ZGC9Q==", + "dev": true, + "dependencies": { + "@types/node": "~20.12.8", + "@types/ws": "~8.5.10" + } + }, "node_modules/fast-decode-uri-component": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/fast-decode-uri-component/-/fast-decode-uri-component-1.0.1.tgz", @@ -59,6 +90,12 @@ "engines": { "node": ">=6" } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true } } } diff --git a/package.json b/package.json index ca5f96b..54ae2cb 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ ], "devDependencies": { "0http-bun": "^1.0.3", + "bun-types": "^1.1.8", "mitata": "^0.1.11" }, "keywords": [