Skip to content

Commit a3796e5

Browse files
authored
fix: add types and updated docs for 'preRewrite'. (#442)
1 parent efcb312 commit a3796e5

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ A function that will be executed before rewriting the URL. It receives the URL,
203203

204204
The function cannot return a promise.
205205

206+
```javascript
207+
// `/api/abc` will be proxied to `http://api-upstream.com/api2/xyz`
208+
fastify.register(proxy, {
209+
upstream: `http://api-upstream.com`,
210+
prefix: '/api',
211+
rewritePrefix: '/api2/',
212+
preRewrite (url, params, prefix) {
213+
return url.replace('abc', 'xyz');
214+
}
215+
})
216+
```
217+
206218
### `websocket`
207219

208220
This module has _partial_ support for forwarding websockets by passing a

types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ type FastifyHttpProxy = FastifyPluginCallback<
7777
>
7878

7979
declare namespace fastifyHttpProxy {
80+
type ProxyPreRewriteHookHandler = (
81+
url: string,
82+
params: unknown,
83+
prefix: string
84+
) => string
85+
8086
type QueryStringFunction = (
8187
search: string | undefined,
8288
reqUrl: string,
@@ -91,6 +97,7 @@ declare namespace fastifyHttpProxy {
9197
preHandler?: ProxyPreHandlerHookHandler;
9298
beforeHandler?: ProxyPreHandlerHookHandler;
9399
preValidation?: ProxyPreValidationHookHandler;
100+
preRewrite?: ProxyPreRewriteHookHandler;
94101
config?: Object;
95102
replyOptions?: FastifyReplyFromHooks;
96103
wsClientOptions?: ClientOptions & { queryString?: { [key: string]: unknown } | QueryStringFunction; };

types/index.test-d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ app.register(fastifyHttpProxy, {
4141
expectType<unknown>(result.options)
4242
expectType<string>(result.url)
4343
},
44+
preRewrite: (url, params, prefix): string => {
45+
expectType<string>(url)
46+
expectType<unknown>(params)
47+
expectType<string>(prefix)
48+
return ''
49+
},
4450
base: 'whatever',
4551
cacheURLs: 10,
4652
undici: {

0 commit comments

Comments
 (0)