Skip to content

Commit

Permalink
feat: add querystringStringifier to config
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 12, 2022
1 parent 045059e commit 63eaa1a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ proxy.register(require('@fastify/reply-from'), {
})
```

#### `querystringStringifier`

Set the default query-string stringifier
[`fast-querystring`](https://npmjs.com/package/fast-querystring) will be used by default
if you do not set a query-string stringifier.

```js
const querystring = require('querystring')

proxy.register(require('@fastify/reply-from'), {
base: 'https://localhost:3001',
querystringStringifier: (str) => querystring.stringify(str),
})
```

#### `http`

Set the `http` option to an Object to use
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const {
} = require('./lib/errors')

module.exports = fp(function from (fastify, opts, next) {
if (opts.querystringStringifier && typeof opts.querystringStringifier !== 'function') {
throw new Error(`querystringStringifier option should be a function, instead got '${typeof opts.querystringStringifier}'`)
}

const contentTypesToEncode = new Set([
'application/json',
...(opts.contentTypesToEncode || [])
Expand Down Expand Up @@ -201,6 +205,10 @@ function getQueryString (search, reqUrl, opts) {
}

if (opts.queryString) {
if (opts.querystringStringifier) {
return '?' + opts.querystringStringifier(opts.queryString)
}

return '?' + querystring.stringify(opts.queryString)
}

Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface FastifyReplyFromOptions {
retryMethods?: (HTTPMethods | 'TRACE')[];
maxRetriesOn503?: number;
disableRequestLogging?: boolean;
querystringStringifier?: (str: { [key: string]: unknown } ) => string
}

declare const fastifyReplyFrom: FastifyPluginCallback<FastifyReplyFromOptions>;
Expand Down

0 comments on commit 63eaa1a

Please sign in to comment.