Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit cb31fd6

Browse files
author
v1rtl
committed
Fix type issues, add async example
1 parent 99ff621 commit cb31fd6

File tree

15 files changed

+76
-46
lines changed

15 files changed

+76
-46
lines changed

app.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// deno-lint-ignore-file
2-
import { NextFunction, Router, Handler, Middleware, UseMethodParams } from 'https://esm.sh/@tinyhttp/router'
3-
export type { NextFunction, Router, Handler, Middleware, UseMethodParams }
2+
import { Router, serve, Server } from './deps.ts'
3+
import { NextFunction, Handler, Middleware, UseMethodParams } from './types.ts'
44
import { onErrorHandler, ErrorHandler } from './onError.ts'
55
// import { setImmediate } from 'https://deno.land/std@0.88.0/node/timers.ts'
66
import rg from 'https://esm.sh/regexparam'
7-
import { Request, getRouteFromApp } from './request.ts'
8-
import { Response } from './response.ts'
7+
import type { Request } from './request.ts'
8+
import type { Response } from './response.ts'
99
import { getURLParams, getPathname } from './utils/parseUrl.ts'
1010
import { extendMiddleware } from './extend.ts'
11-
import { serve, Server } from 'https://deno.land/std/http/server.ts'
1211
import * as path from 'https://deno.land/std/path/mod.ts'
1312

1413
const lead = (x: string) => (x.charCodeAt(0) === 47 ? x : '/' + x)
@@ -81,6 +80,9 @@ export type TemplateEngineOptions<O = any> = Partial<{
8180
_locals: Record<string, any>
8281
}>
8382

83+
export const getRouteFromApp = ({ middleware }: App, h: Handler) =>
84+
middleware.find(({ handler }) => typeof handler === 'function' && handler.name === h.name)
85+
8486
/**
8587
* `App` class - the starting point of tinyhttp app.
8688
*

deps.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@ export { Accepts } from 'https://deno.land/x/accepts@2.1.0/mod.ts'
99
export { encodeUrl } from 'https://deno.land/x/encodeurl@1.0.0/mod.ts'
1010
export { charset, contentType, lookup } from 'https://deno.land/x/media_types@v2.6.1/mod.ts'
1111

12-
export type { ServerRequest as Req, Response as Res } from 'https://deno.land/std@0.88.0/http/server.ts'
12+
import type { ServerRequest as Req, Response as ServerResponse } from 'https://deno.land/std@0.88.0/http/server.ts'
13+
14+
interface Res extends ServerResponse {
15+
headers: Headers
16+
}
17+
18+
export type { Req, Res }
19+
20+
export { serve, Server } from 'https://deno.land/std@0.88.0/http/server.ts'
21+
22+
export { Router } from 'https://esm.sh/@tinyhttp/router'

egg.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
"entry": "./app.ts",
55
"description": "0-legacy, tiny & fast web framework as a replacement of Express",
66
"homepage": "https://github.com/talentlessguy/tinyhttp-deno",
7-
"version": "0.0.14",
8-
"ignore": [
9-
"./examples/**/*.ts"
10-
],
11-
"files": [
12-
"./**/*.ts",
13-
"README.md"
14-
],
7+
"version": "0.0.15",
8+
"ignore": ["./examples/**/*.ts"],
9+
"files": ["./**/*.ts", "README.md"],
1510
"checkFormat": false,
1611
"checkTests": false,
1712
"checkInstallation": false,

examples/async/server.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { App } from '../../mod.ts'
2+
3+
const app = new App()
4+
5+
app.get('/', async (_, res) => {
6+
const decoder = new TextDecoder('utf-8')
7+
const file = await Deno.readFile(`test.txt`)
8+
9+
res.send(decoder.decode(file))
10+
})
11+
12+
app.listen(3000, () => console.log('Started on http://localhost:3000'))

examples/async/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello async

examples/basic/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App } from '../../app.ts'
1+
import { App } from '../../mod.ts'
22

33
const app = new App()
44

examples/mongodb/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import * as dotenv from 'https://deno.land/x/tiny_env@1.0.0/mod.ts'
66
dotenv.load()
77

88
type Req = Request & {
9-
bodyResult: any
9+
bodyResult: Record<string, unknown>
1010
}
1111

12-
const app = new App<any, Req>()
12+
const app = new App<unknown, Req>()
1313
const port = parseInt(Deno.env.get('PORT') || '') || 3000
1414

1515
// connect to mongodb

extend.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { App, renderTemplate } from './app.ts'
33
import { Request } from './request.ts'
44
import {
55
getRequestHeader,
6-
/* getFreshOrStale, */
6+
getFreshOrStale,
77
getAccepts,
88
getAcceptsCharsets,
99
getAcceptsEncodings,
@@ -13,8 +13,9 @@ import {
1313
getIP,
1414
getIPs,
1515
getProtocol,
16-
getSubdomains
17-
/* reqIs */
16+
getSubdomains,
17+
getRangeFromHeader,
18+
reqIs
1819
} from './extensions/req/mod.ts'
1920
import {
2021
send,
@@ -61,19 +62,19 @@ export const extendMiddleware = <
6162

6263
req.get = getRequestHeader(req)
6364

64-
/* if (settings?.freshnessTesting) {
65+
if (settings?.freshnessTesting) {
6566
req.fresh = getFreshOrStale<Req, Res>(req, res)
6667
req.stale = !req.fresh
67-
} */
68+
}
6869

6970
req.accepts = getAccepts<Req>(req)
7071
req.acceptsCharsets = getAcceptsCharsets<Req>(req)
7172
req.acceptsEncodings = getAcceptsEncodings<Req>(req)
7273
req.acceptsLanguages = getAcceptsLanguages<Req>(req)
7374

74-
// req.range = getRangeFromHeader(req)
75+
req.range = getRangeFromHeader(req)
7576
req.xhr = checkIfXMLHttpRequest(req)
76-
// req.is = reqIs(req)
77+
req.is = reqIs(req)
7778

7879
if (settings?.networkExtensions) {
7980
req.protocol = getProtocol<Req>(req)

extensions/res/headers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const setHeader = <Response extends Res = Res>(res: Response) => (
2323
}
2424
}
2525

26-
res.headers?.set(field, value as string)
26+
res.headers.set(field, value as string)
2727
} else {
2828
for (const key in field) {
2929
setHeader(res)(key, field[key] as string)
@@ -46,7 +46,7 @@ export const setLocationHeader = <Request extends Req = Req, Response extends Re
4646
if (url === 'back') loc = (getRequestHeader(req)('Referrer') as string) || '/'
4747

4848
// set location
49-
res.headers?.set('Location', encodeUrl(loc))
49+
res.headers.set('Location', encodeUrl(loc))
5050
return res
5151
}
5252

@@ -55,7 +55,7 @@ export const setLinksHeader = <Response extends Res = Res>(res: Response) => (li
5555
}): Response => {
5656
let link = res.headers?.get('Link') || ''
5757
if (link) link += ', '
58-
res.headers?.set(
58+
res.headers.set(
5959
'Link',
6060
link +
6161
Object.keys(links)

extensions/res/json.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Req, Res } from '../../deps.ts'
1+
import type { Req, Res } from '../../deps.ts'
22

33
export const json = <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => <
44
T = unknown
@@ -9,8 +9,8 @@ export const json = <Request extends Req = Req, Response extends Res = Res>(req:
99
if (typeof body === 'object' && body != null) req.respond({ body: JSON.stringify(body, null, 2) })
1010
else if (typeof body === 'string') req.respond({ body })
1111
else if (body == null) {
12-
res.headers?.delete('Content-Length')
13-
res.headers?.delete('Transfer-Encoding')
12+
res.headers.delete('Content-Length')
13+
res.headers.delete('Transfer-Encoding')
1414
req.respond({})
1515
}
1616

0 commit comments

Comments
 (0)