Skip to content

Commit 66bd0d1

Browse files
author
v1rtl
committed
refactor: move URL extensions to @tinyhttp/url
1 parent 2edf653 commit 66bd0d1

34 files changed

Lines changed: 239 additions & 100 deletions

examples/basic/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ const app = new App()
55

66
app
77
.use(logger())
8-
.get('/', (_, res) => void res.send('<h1>Hello World</h1>'))
8+
.get(
9+
'/',
10+
(_, res) =>
11+
void res.format({
12+
html: () => res.send('<h1>Hello World</h1>'),
13+
text: () => res.send('Hello World'),
14+
})
15+
)
916
.get('/page/:page/', (req, res) => {
1017
res.status(200).send(`
1118
<h1>Some cool page</h1>
@@ -16,4 +23,4 @@ app
1623
`)
1724
})
1825

19-
.listen(3000)
26+
.listen(3000, () => console.log(`Listening on http://localhost:3000`))

examples/eta/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
2-
const { App } = require('@tinyhttp/app')
3-
const eta = require('eta')
1+
import { App } from '@tinyhttp/app'
2+
import { renderFile as eta } from 'eta'
43

54
const app = new App()
65

7-
app.engine('eta', eta.renderFile)
6+
app.engine('eta', eta)
87

98
app.use((_, res) => void res.render('index.eta', { name: 'Eta' }))
109

examples/eta/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "eta",
33
"private": true,
4-
"type": "commonjs",
4+
"type": "module",
55
"dependencies": {
66
"@tinyhttp/app": "workspace:*",
7-
"eta": "^1.10.0"
7+
"eta": "^1.10.1"
88
}
99
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"eslint": "^7.9.0",
2222
"eslint-config-prettier": "^6.11.0",
2323
"eslint-plugin-prettier": "^3.1.4",
24-
"eta": "^1.10.0",
24+
"eta": "^1.10.1",
2525
"husky": "4.3.0",
2626
"jest": "^26.4.2",
2727
"prettier": "^2.1.2",

packages/app/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# @tinyhttp/app
22

3+
## 0.3.10
4+
5+
### Patch Changes
6+
7+
- Move URL extensions to @tinyhttp/url
8+
- Updated dependencies [undefined]
9+
- @tinyhttp/req@0.1.6
10+
- @tinyhttp/res@0.1.9
11+
312
## 0.3.9
413

514
### Patch Changes

packages/app/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tinyhttp/app",
3-
"version": "0.3.9",
3+
"version": "0.3.10",
44
"description": "tinyhttp core with App, Request, Response and Router",
55
"type": "module",
66
"homepage": "https://github.com/talentlessguy/tinyhttp#readme",
@@ -43,11 +43,10 @@
4343
"license": "MIT",
4444
"dependencies": {
4545
"@tinyhttp/cookie": "workspace:*",
46-
"@tinyhttp/req": "workspace:^0.1.5",
46+
"@tinyhttp/req": "workspace:^0.1.6",
4747
"@tinyhttp/res": "workspace:*",
4848
"@tinyhttp/router": "workspace:*",
49-
"proxy-addr": "^2.0.6",
50-
"regexparam": "^1.3.0"
49+
"proxy-addr": "^2.0.6"
5150
},
5251
"devDependencies": {
5352
"@types/proxy-addr": "^2.0.0"

packages/app/src/app.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { createServer } from 'http'
22
import path from 'path'
3-
import rg from 'regexparam'
4-
import { getURLParams, getRouteFromApp } from './request'
3+
import { getRouteFromApp, getURLParams } from './request'
54
import type { Request } from './request'
65
import type { Response } from './response'
76
import type { ErrorHandler } from './onError'
87
import { onErrorHandler } from './onError'
98
import { isAsync } from './utils/async'
109
import { Middleware, Handler, NextFunction, Router } from '@tinyhttp/router'
1110
import { extendMiddleware } from './extend'
11+
import { matchParams } from '@tinyhttp/req'
1212

1313
export const applyHandler = (h: Handler) => async (req: Request, res: Response, next?: NextFunction) => {
1414
if (isAsync(h)) {
@@ -176,7 +176,7 @@ export class App<RenderOptions = any, Req extends Request = Request, Res extends
176176
const queryParamStart = req.url.lastIndexOf('?')
177177
const reqUrlWithoutParams = req.url.slice(0, queryParamStart === -1 ? req.url.length : queryParamStart)
178178

179-
if (rg(path).pattern.test(reqUrlWithoutParams)) {
179+
if (matchParams(path, reqUrlWithoutParams)) {
180180
req.params = getURLParams(req.url, path)
181181
req.route = getRouteFromApp(this, handler as Handler<Req, Res>)
182182

packages/app/src/extend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Request } from './request'
22
import type { NextFunction } from '@tinyhttp/router'
33
import type { Response } from './response'
44

5-
import { getAccepts, getFreshOrStale, getRangeFromHeader, getRequestHeader, checkIfXMLHttpRequest } from '@tinyhttp/req'
6-
import { getQueryParams, getProtocol, getHostname, getIP, getIPs } from './request'
5+
import { getAccepts, getFreshOrStale, getRangeFromHeader, getRequestHeader, checkIfXMLHttpRequest, getQueryParams } from '@tinyhttp/req'
6+
import { getProtocol, getHostname, getIP, getIPs } from './request'
77
import {
88
send,
99
json,

packages/app/src/request.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
import { IncomingMessage } from 'http'
22
import { ParsedUrlQuery } from 'querystring'
3-
import rg from 'regexparam'
4-
import { parse } from 'url'
3+
54
import { Ranges } from 'range-parser'
65
import proxyAddr from 'proxy-addr'
76
import { App } from './app'
87
import type { Middleware, Handler } from '@tinyhttp/router'
98
import type { Response } from './response'
10-
import { compileTrust, rgExec } from './utils/request'
11-
12-
export const getQueryParams = (url = '/'): ParsedUrlQuery => {
13-
return parse(url, true).query
14-
}
9+
import { compileTrust } from './utils/request'
1510

16-
export type URLParams = {
17-
[key: string]: string
18-
}
11+
import type { URLParams } from '@tinyhttp/req'
1912

20-
export const getURLParams = (reqUrl = '/', url = '/'): URLParams => {
21-
return rgExec(reqUrl, rg(url))
22-
}
13+
export { getURLParams } from '@tinyhttp/req'
2314

2415
export const getRouteFromApp = (app: App, handler: Handler<Request, Response>) => {
2516
return app.middleware.find((h) => h.handler.name === handler.name)
@@ -84,6 +75,8 @@ export type Connection = IncomingMessage['socket'] & {
8475

8576
export type Protocol = 'http' | 'https' | string
8677

78+
export type { URLParams }
79+
8780
export interface Request extends IncomingMessage {
8881
query: ParsedUrlQuery
8982
params: URLParams

packages/app/src/utils/request.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,3 @@ export const compileTrust = (val: any) => {
2727

2828
return proxyAddr.compile(val || [])
2929
}
30-
31-
export const rgExec = (
32-
path: string,
33-
result: {
34-
pattern: RegExp
35-
keys: string[]
36-
}
37-
) => {
38-
let i = 0
39-
const out = {}
40-
const matches = result.pattern.exec(path)
41-
while (i < result.keys.length) {
42-
out[result.keys[i]] = matches?.[++i] || null
43-
}
44-
return out
45-
}

0 commit comments

Comments
 (0)