Skip to content

Commit 4012ae1

Browse files
Adding TS types (#32)
* Adding TS types Co-authored-by: RyanWright <[email protected]>
1 parent 905e4cc commit 4012ae1

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

common.d.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { type Server as HTTPServer, IncomingMessage, ServerResponse } from "http";
2+
import { type Server as HTTPSServer } from "https";
3+
import { Pattern, Methods } from "trouter";
4+
5+
import {
6+
Http2SecureServer,
7+
Http2ServerRequest,
8+
Http2ServerResponse
9+
} from 'http2'
10+
11+
export enum Protocol {
12+
HTTP = 'http',
13+
HTTPS = 'https',
14+
HTTP2 = 'http2'
15+
}
16+
17+
export type Server<P extends Protocol> = P extends Protocol.HTTP2 ? Http2SecureServer : P extends Protocol.HTTPS ? HTTPSServer : HTTPServer
18+
19+
export type StepFunction = (error?: unknown) => void
20+
21+
export type Request<P extends Protocol> = P extends Protocol.HTTP2
22+
? Http2ServerRequest
23+
: IncomingMessage
24+
25+
export type Response<P extends Protocol> = P extends Protocol.HTTP2
26+
? Http2ServerResponse : ServerResponse
27+
28+
export type RequestHandler<P extends Protocol> = (
29+
req: Request<P>,
30+
res: Response<P>,
31+
next: (error?: unknown) => void
32+
) => void | Promise<unknown>
33+
34+
export interface IRouter<P extends Protocol> {
35+
lookup: RequestHandler<P>
36+
37+
use(...handlers: RequestHandler<P>[]): this;
38+
use(router: IRouter<P>): this;
39+
use(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
40+
use(prefix: Pattern, router: IRouter<P>): this;
41+
42+
all(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
43+
get(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
44+
head(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
45+
patch(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
46+
options(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
47+
connect(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
48+
delete(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
49+
trace(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
50+
post(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
51+
put(pattern: Pattern, ...handlers: RequestHandler<P>[]): this;
52+
53+
on(method: Methods, pattern: Pattern, ...middlewares: RequestHandler<P>[]): this;
54+
}
55+

demos/basic.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import zero from '../index'
2+
import { createServer } from 'node:http'
3+
import createRouter from '../lib/router/sequential'
4+
import { Protocol } from '../common'
5+
6+
const { router, server } = zero<Protocol.HTTP>({
7+
router: createRouter<Protocol.HTTP>(),
8+
server: createServer()
9+
})
10+
11+
router.use((req, res, next) => {
12+
return next()
13+
})
14+
15+
router.get('/hi', (req, res) => {
16+
res.end(`Hello World from TS!`)
17+
})
18+
19+
server.listen(3000)

index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Protocol, IRouter, Server } from './common'
2+
3+
interface IBuildServerAndRouterConfig<P extends Protocol, S extends Server<P>, R extends IRouter<P>> {
4+
router?: R;
5+
server?: S;
6+
prioRequestsProcessing?: boolean;
7+
}
8+
9+
export default function zero<P extends Protocol>(config?: IBuildServerAndRouterConfig<P, Server<P>, IRouter<P>>): {
10+
server: Server<P>,
11+
router: IRouter<P>
12+
};
13+

lib/router/sequential.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Protocol, IRouter, RequestHandler } from './../../common'
2+
3+
export default function createSequentialRouter<P extends Protocol>(config?: object): IRouter<P>

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"homepage": "https://github.com/BackendStack21/0http#readme",
3030
"devDependencies": {
31+
"@types/node": "^18.14.4",
3132
"body-parser": "^1.20.1",
3233
"chai": "^4.3.7",
3334
"cross-env": "^7.0.3",
@@ -46,5 +47,6 @@
4647
"lru-cache": "^7.14.1",
4748
"regexparam": "^2.0.1",
4849
"trouter": "^3.2.0"
49-
}
50-
}
50+
},
51+
"types": "./index.d.ts"
52+
}

0 commit comments

Comments
 (0)