Skip to content

Commit

Permalink
fix: missing query params
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed Mar 1, 2022
1 parent c0e4c82 commit 387a8c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -63,10 +63,11 @@
},
"dependencies": {
"@types/node": "^14.14.31",
"url": "^0.11.0"
"@types/whatwg-url": "^8.2.1",
"whatwg-url": "^11.0.0"
},
"peerDependencies": {
"keq": "^1.0.1"
"keq": "^1.7.0"
},
"directories": {
"lib": "lib",
Expand Down
40 changes: 12 additions & 28 deletions src/index.ts
Expand Up @@ -3,7 +3,7 @@
*/

import { Middleware } from 'keq'
import * as url from 'url'
import { URL } from 'whatwg-url'


export type KeqProxyReplacer = (substring: string, ...args: any[]) => string
Expand All @@ -19,49 +19,33 @@ type Host = Readonly<string>

const proxy: KeqProxy = function(from, to) {
return async(ctx, next) => {
if (ctx.url.host === from) {
ctx.url = {
...url.parse(url.format({ ...ctx.url, host: to }), true),
params: ctx.url.params,
}
ctx.url.host = to
}
if (ctx.url.host === from) ctx.url.host = to
await next()
}
}


proxy.replace = function(regexp, replaceValue) {
return async(ctx, next) => {
const href = url.format(ctx.url)
ctx.url = {
...url.parse(href.replace(regexp, replaceValue as any), true),
params: ctx.url.params,
}
const href = ctx.url.href
ctx.url.href = href.replace(regexp, replaceValue as any)

await next()
}
}

proxy.module = function(moduleName , uri) {
return async(ctx, next) => {
if (typeof ctx.options.module === 'string' && ctx.options.module === moduleName) {
if (!ctx.module) throw new Error('Please set the module middleware first.')
const pathname = ctx.module.pathname
if (ctx.options.module?.name === moduleName) {
const pathname = ctx.options.module.pathname.replace(/^\/+/, '')
const url = new URL(uri)

ctx.url = {
...url.parse(`${uri.replace(/\/+$/, '')}/${pathname.replace(/^\/+/, '')}`, true),
query: ctx.url.query,
params: ctx.url.params,
}
} else if (ctx.options.module?.name === moduleName) {
const pathname = ctx.options.module.pathname
ctx.url.protocol = url.protocol
ctx.url.host = url.host
ctx.url.username = url.username
ctx.url.password = url.password

ctx.url = {
...url.parse(`${uri.replace(/\/+$/, '')}/${pathname.replace(/^\/+/, '')}`, true),
query: ctx.url.query,
params: ctx.url.params,
}
ctx.url.pathname = `${url.pathname.replace(/^\/+/, '')}/${pathname}`
}

await next()
Expand Down
11 changes: 4 additions & 7 deletions tests/index.ts
@@ -1,19 +1,16 @@
import anyTest, { TestInterface } from 'ava'
import proxy from '../src'
import * as url from 'url'
import { Context } from 'keq'
import * as sinon from 'sinon'
import { KeqURL } from 'keq/lib/src/keq-url'


const test = anyTest as TestInterface<{ ctx: Context }>


test.beforeEach(t => {
t.context.ctx = {
url: {
...url.parse('http://example.com/api/api_path', true),
params: {}
}
url: new KeqURL('http://example.com/api/api_path?query=1'),
} as Context
})

Expand All @@ -23,7 +20,7 @@ test('Proxy Host', async t => {
await proxy('example.com', 'expect.com')(t.context.ctx, next)

t.true(next.calledOnce)
t.is(t.context.ctx.url.href, 'http://expect.com/api/api_path')
t.is(t.context.ctx.url.href, 'http://expect.com/api/api_path?query=1')
})

test('Proxy Regexp Replace', async t => {
Expand All @@ -33,5 +30,5 @@ test('Proxy Regexp Replace', async t => {


t.true(next.calledOnce)
t.is(t.context.ctx.url.href, 'https://expect.com/api_path')
t.is(t.context.ctx.url.href, 'https://expect.com/api_path?query=1')
})

0 comments on commit 387a8c5

Please sign in to comment.