Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing a function to the queryString option #231

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,15 @@ Called to get upstream destination, before the request is being sent. Useful whe
Helpful for a gradual rollout of new services.
It must return the upstream destination.

#### `queryString`
#### `queryString` or `queryString(search, reqUrl)`

Replaces the original querystring of the request with what is specified.
This will be passed to
[`querystring.stringify`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options).

- `object`: accepts an object that will be passed to `querystring.stringify`
- `function`: function that will return a string with the query parameters e.g. `name=test&type=user`

#### `body`

Replaces the original request body with what is specified. Unless
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import {
SecureClientSessionOptions,
} from "http2";
import { Pool } from 'undici'
type QueryStringFunction = (search: string | undefined, reqUrl: string) => string;
export interface FastifyReplyFromHooks {
queryString?: { [key: string]: unknown };
queryString?: { [key: string]: unknown } | QueryStringFunction;
contentType?: string;
onResponse?: (
request: FastifyRequest<RequestGenericInterface, RawServerBase>,
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ module.exports = fp(function from (fastify, opts, next) {
}, '>=3')

function getQueryString (search, reqUrl, opts) {
if (typeof opts.queryString === 'function') {
return '?' + opts.queryString(search, reqUrl)
}

if (opts.queryString) {
return '?' + querystring.stringify(opts.queryString)
}
Expand Down
51 changes: 51 additions & 0 deletions test/full-querystring-rewrite-option-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'

const t = require('tap')
const Fastify = require('fastify')
const From = require('..')
const http = require('http')
const get = require('simple-get').concat
const querystring = require('querystring')

const instance = Fastify()

t.plan(10)
t.teardown(instance.close.bind(instance))

const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'GET')
t.equal(req.url, '/world?b=c')
res.statusCode = 205
res.setHeader('Content-Type', 'text/plain')
res.setHeader('x-my-header', 'hello!')
res.end('hello world')
})

instance.get('/hello', (request, reply) => {
reply.from(`http://localhost:${target.address().port}/world?a=b`, {
queryString () {
return querystring.stringify({ b: 'c' })
}
})
})

t.teardown(target.close.bind(target))

target.listen(0, (err) => {
t.error(err)

instance.register(From)

instance.listen(0, (err) => {
t.error(err)

get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.headers['x-my-header'], 'hello!')
t.equal(res.statusCode, 205)
t.equal(data.toString(), 'hello world')
})
})
})