Skip to content

Commit c74a187

Browse files
fix(gateway): allow custom proxyFactory to use non-default proxyTypes (#96)
* fix(gateway): allow custom proxyFactory to use non-default proxyTypes - Skip proxyType validation when a custom proxyFactory is provided - Fallback to defaultProxyFactory only if custom one returns undefined - Default missing proxy hooks to NOOP functions * fix(gateway): improve proxyFactory logic to handle undefined responses * chore(format): run code formatter * docs: enhance README with detailed proxyFactory behavior explanation
1 parent 2f695e5 commit c74a187

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

docs/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ module.exports.handler = serverless(service)
131131
timeout: 0,
132132
// Optional "target" value that overrides the routes "target" config value. Feature intended for testing purposes.
133133
targetOverride: "https://yourdev.api-gateway.com",
134-
// Optional "Proxy Factory" implementation, allows the integration of custom proxying strategies.
135-
// Default value: require('fast-proxy-lite/lib/proxy-factory')
134+
// Optional "Proxy Factory" implementation, allows integration of custom proxying strategies.
135+
// Behavior:
136+
// - If it returns any value (e.g. a custom proxy), that value will be used directly.
137+
// - If it returns `undefined` (or does not return anything), the default factory from `fast-gateway` will be used as a fallback.
138+
// - If it returns `null`, no proxy will be used and the default factory will be skipped entirely.
139+
// Default: the built-in proxy factory from `fast-gateway`
136140
proxyFactory: ({ proxyType, opts, route }) => {...}
137141

138142
// HTTP proxy

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ const defaultProxyHandler = (req, res, url, proxy, proxyOpts) =>
99
const DEFAULT_METHODS = require('restana/libs/methods').filter(
1010
(method) => method !== 'all'
1111
)
12+
const NOOP = (req, res) => {}
1213
const send = require('@polka/send-type')
1314
const PROXY_TYPES = ['http', 'lambda']
1415
const registerWebSocketRoutes = require('./lib/ws-proxy')
1516

1617
const gateway = (opts) => {
17-
const proxyFactory = opts.proxyFactory || defaultProxyFactory
18+
const proxyFactory = opts.proxyFactory ? (...args) => ((r) => r === undefined ? defaultProxyFactory(...args) : r)(opts.proxyFactory(...args)) : defaultProxyFactory
1819

1920
opts = Object.assign(
2021
{
@@ -67,16 +68,16 @@ const gateway = (opts) => {
6768

6869
// retrieve proxy type
6970
const { proxyType = 'http' } = route
70-
if (!PROXY_TYPES.includes(proxyType)) {
71+
const isDefaultProxyType = PROXY_TYPES.includes(proxyType)
72+
if (!opts.proxyFactory && !isDefaultProxyType) {
7173
throw new Error(
7274
'Unsupported proxy type, expecting one of ' + PROXY_TYPES.toString()
7375
)
7476
}
7577

7678
// retrieve default hooks for proxy
77-
const { onRequestNoOp, onResponse } = require('./lib/default-hooks')[
78-
proxyType
79-
]
79+
const hooksForDefaultType = isDefaultProxyType ? require('./lib/default-hooks')[proxyType] : {}
80+
const { onRequestNoOp = NOOP, onResponse = NOOP } = hooksForDefaultType
8081

8182
// populating required NOOPS
8283
route.hooks = route.hooks || {}

0 commit comments

Comments
 (0)