Skip to content

Commit 8597a2c

Browse files
committed
fix: add types for 'preRewrite'.
1 parent bbe97fb commit 8597a2c

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
@@ -53,6 +53,12 @@ type FastifyHttpProxy = FastifyPluginCallback<
5353
>
5454

5555
declare namespace fastifyHttpProxy {
56+
type preRewriteHookHandler = (
57+
url: string,
58+
params: unknown,
59+
prefix: string
60+
) => string
61+
5662
type QueryStringFunction = (
5763
search: string | undefined,
5864
reqUrl: string,
@@ -67,6 +73,7 @@ declare namespace fastifyHttpProxy {
6773
preHandler?: preHandlerHookHandler;
6874
beforeHandler?: preHandlerHookHandler;
6975
preValidation?: preValidationHookHandler;
76+
preRewrite?: preRewriteHookHandler;
7077
config?: Object;
7178
replyOptions?: FastifyReplyFromHooks;
7279
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
@@ -34,6 +34,12 @@ app.register(fastifyHttpProxy, {
3434
expectType<RawRequestDefaultExpression>(request.raw)
3535
expectType<RawReplyDefaultExpression>(reply.raw)
3636
},
37+
preRewrite: (url, params, prefix): string => {
38+
expectType<string>(url)
39+
expectType<unknown>(params)
40+
expectType<string>(prefix)
41+
return ''
42+
},
3743
base: 'whatever',
3844
cacheURLs: 10,
3945
undici: {

0 commit comments

Comments
 (0)