Skip to content

Commit

Permalink
fix: route parameters are not filled
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed May 17, 2021
1 parent e45bf14 commit e69c7db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"dependencies": {
"@types/node": "^14.14.31",
"object-inspect": "^1.10.2",
"path-to-regexp": "^6.2.0",
"ramda": "^0.27.1",
"url": "^0.11.0"
},
Expand Down
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Middleware } from 'keq'
import { Middleware, KeqURL } from 'keq'
import * as inspect from 'object-inspect'
import * as url from 'url'
import * as R from 'ramda'
import { compile } from 'path-to-regexp'


interface Options {
Expand Down Expand Up @@ -31,6 +32,17 @@ function formatHeader(key: string, value: string): string {
return `${key}=${firstLine}`
}

function formatUrl(keqURL: KeqURL): string {
const urlobj = url.parse(url.format(keqURL))

if (urlobj.pathname) {
const toPath = compile(urlobj.pathname, { encode: encodeURIComponent })
urlobj.pathname = toPath(keqURL.params)
}

return url.format(urlobj)
}

export default function debug({
responseBody = false,
requestBody = false,
Expand All @@ -42,7 +54,7 @@ export default function debug({

return async(ctx, next) => {
let expectMessage = 'Expect Request\n'
expectMessage += `\t${ctx.request.method.toUpperCase()}: ${url.format(ctx.url)}\n`
expectMessage += `\t${ctx.request.method.toUpperCase()}: ${formatUrl(ctx.url)}\n`

expectMessage += '\tHeaders:\n'
ctx.headers.forEach((value, key) => {
Expand All @@ -59,7 +71,7 @@ export default function debug({
await next()

let realMessage = 'Real Request\n'
realMessage += `\t${ctx.request.method.toUpperCase()}: ${url.format(ctx.url)}\n`
realMessage += `\t${ctx.request.method.toUpperCase()}: ${formatUrl(ctx.url)}\n`
realMessage += '\tHeaders:\n'
ctx.headers.forEach((value, key) => {
const str = formatHeader(key, value)
Expand Down

0 comments on commit e69c7db

Please sign in to comment.