Skip to content

Commit

Permalink
feat: Adding TypeScript types
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed May 20, 2024
1 parent 78764c5 commit 1f8d4ff
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 5 deletions.
42 changes: 42 additions & 0 deletions common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Pattern, Methods } from 'trouter'

export interface IRouterConfig {
defaultRoute?: RequestHandler
errorHandler?: (err: Error) => Response | Promise<Response>
port?: number
}

export type StepFunction = (error?: unknown) => Response | Promise<Response>

type ZeroRequest = Request & {
params: Record<string, string>
query: Record<string, string>
ctx?: Record<string, any>
}

export type RequestHandler = (
req: ZeroRequest,
next: StepFunction
) => Response | Promise<Response>

export interface IRouter {
fetch: (req: Request) => Response | Promise<Response>

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
}
24 changes: 24 additions & 0 deletions demos/basic.ts
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IRouter, IRouterConfig } from './common'

export default function zero(config?: IRouterConfig): {
router: IRouter
}

export * from './common'
3 changes: 3 additions & 0 deletions lib/router/sequential.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { IRouter, IRouterConfig } from './../../index'

export default function createSequentialRouter(config?: IRouterConfig): IRouter
47 changes: 42 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"devDependencies": {
"0http-bun": "^1.0.3",
"bun-types": "^1.1.8",
"mitata": "^0.1.11"
},
"keywords": [
Expand Down

0 comments on commit 1f8d4ff

Please sign in to comment.