Skip to content

Commit

Permalink
feat: get rid of deprecated url.parse in favor of a custom func
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Feb 21, 2021
1 parent 344a8ae commit 9a5573f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
40 changes: 40 additions & 0 deletions __tests__/core/app.test.ts
Expand Up @@ -523,6 +523,46 @@ describe('Route handlers', () => {

await fetch('/').expect(200, 'hello world')
})
/* it('router accepts array of wares', async () => {
const app = new App({
onError: (err, _, res) => {
console.log(err)
res.sendStatus(500)
}
})
app.get(
'/',
[
(req, _, n) => {
req.body = 'hello'
n()
}
],
[
(req, _, n) => {
req.body += ' '
n()
}
],
[
(req, _, n) => {
req.body += 'world'
n()
},
(req, res) => {
res.send(req.body)
}
]
)
const server = app.listen()
const fetch = makeFetch(server)
await fetch('/').expect(200, 'hello world')
}) */
it('router methods do not match loosely', async () => {
const app = new App()

Expand Down
14 changes: 14 additions & 0 deletions __tests__/modules/router.test.ts
Expand Up @@ -128,6 +128,20 @@ describe('Testing Router', () => {
})

describe('Testing HTTP methods', () => {
/* it('should accept arrays for first argument', () => {
const router = new Router()
router.get('/', [() => 1, () => 2])
expect(router.middleware.length).toBe(2)
})
it('should accept arrays for first and second argument', () => {
const router = new Router()
router.get('/', [() => 1, () => 2], [() => 3])
expect(router.middleware.length).toBe(3)
}) */
it('app.get should set GET as HTTP method', () => {
const router = new Router()

Expand Down
3 changes: 2 additions & 1 deletion build/deps.js
Expand Up @@ -7,5 +7,6 @@ export default (deps) => [
'fs',
'fs/promises',
'net',
'events'
'events',
'querystring'
]
6 changes: 3 additions & 3 deletions packages/app/src/app.ts
@@ -1,6 +1,5 @@
import { createServer, Server } from 'http'
import path from 'path'
import { parse } from 'url'
import { getURLParams } from './request'
import type { Request } from './request'
import type { Response } from './response'
Expand All @@ -9,6 +8,7 @@ import { onErrorHandler } from './onError'
import { Middleware, Handler, NextFunction, Router, UseMethodParams } from '@tinyhttp/router'
import { extendMiddleware } from './extend'
import rg from 'regexparam'
import { getPathname } from '@tinyhttp/req'

/**
* Add leading slash if not present (e.g. path -> /path, /path -> /path)
Expand Down Expand Up @@ -241,7 +241,7 @@ export class App<

req.originalUrl = req.url || req.originalUrl

const { pathname } = parse(req.originalUrl)
const pathname = getPathname(req.originalUrl)

const mw: Middleware[] = [
{
Expand All @@ -262,7 +262,7 @@ export class App<

req.url = lead(req.url.substring(path.length)) || '/'

req.path = parse(req.url).pathname
req.path = getPathname(req.url)

if (type === 'route') req.params = getURLParams(regex, pathname)

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/request.ts
Expand Up @@ -28,7 +28,7 @@ const trustRemoteAddress = ({ connection }: Pick<IncomingMessage, 'headers' | 'c
}

export const getRouteFromApp = ({ middleware }: App, h: Handler<Request, Response>) =>
middleware.find(({ handler }) => handler.name === h.name)
middleware.find(({ handler }) => typeof handler === 'function' && handler.name === h.name)

export const getProtocol = (req: Request): Protocol => {
const proto = req.connection.encrypted ? 'https' : 'http'
Expand Down
16 changes: 10 additions & 6 deletions packages/url/src/index.ts
@@ -1,5 +1,5 @@
import rg from 'regexparam'
import { parse } from 'url'
import * as qs from 'querystring'
import { ParsedUrlQuery } from 'querystring'

type Regex = {
Expand All @@ -12,15 +12,19 @@ export const getURLParams = ({ pattern, keys }: Regex, reqUrl = '/'): URLParams

const params = {}

if (matches) {
for (let i = 0; i < keys.length; i++) params[keys[i]] = matches[i + 1]
}
if (matches) for (let i = 0; i < keys.length; i++) params[keys[i]] = matches[i + 1]

return params
}

export const getQueryParams = (url = '/'): ParsedUrlQuery => parse(url, true).query

export type URLParams = {
[key: string]: string
}

export const getPathname = (u: string) => {
const end = u.indexOf('?')

return u.slice(0, end === -1 ? u.length - 1 : end)
}

export const getQueryParams = (url = '/'): ParsedUrlQuery => qs.parse(url.slice(url.indexOf('?') + 1))

0 comments on commit 9a5573f

Please sign in to comment.