Skip to content

fix(gateway): allow custom proxyFactory to use non-default proxyTypes #96

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

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
8 changes: 6 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ module.exports.handler = serverless(service)
timeout: 0,
// Optional "target" value that overrides the routes "target" config value. Feature intended for testing purposes.
targetOverride: "https://yourdev.api-gateway.com",
// Optional "Proxy Factory" implementation, allows the integration of custom proxying strategies.
// Default value: require('fast-proxy-lite/lib/proxy-factory')
// Optional "Proxy Factory" implementation, allows integration of custom proxying strategies.
// Behavior:
// - If it returns any value (e.g. a custom proxy), that value will be used directly.
// - If it returns `undefined` (or does not return anything), the default factory from `fast-gateway` will be used as a fallback.
// - If it returns `null`, no proxy will be used and the default factory will be skipped entirely.
// Default: the built-in proxy factory from `fast-gateway`
proxyFactory: ({ proxyType, opts, route }) => {...}

// HTTP proxy
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const defaultProxyHandler = (req, res, url, proxy, proxyOpts) =>
const DEFAULT_METHODS = require('restana/libs/methods').filter(
(method) => method !== 'all'
)
const NOOP = (req, res) => {}
const send = require('@polka/send-type')
const PROXY_TYPES = ['http', 'lambda']
const registerWebSocketRoutes = require('./lib/ws-proxy')

const gateway = (opts) => {
const proxyFactory = opts.proxyFactory || defaultProxyFactory
const proxyFactory = opts.proxyFactory ? (...args) => ((r) => r === undefined ? defaultProxyFactory(...args) : r)(opts.proxyFactory(...args)) : defaultProxyFactory

opts = Object.assign(
{
Expand Down Expand Up @@ -67,16 +68,16 @@ const gateway = (opts) => {

// retrieve proxy type
const { proxyType = 'http' } = route
if (!PROXY_TYPES.includes(proxyType)) {
const isDefaultProxyType = PROXY_TYPES.includes(proxyType)
if (!opts.proxyFactory && !isDefaultProxyType) {
throw new Error(
'Unsupported proxy type, expecting one of ' + PROXY_TYPES.toString()
)
}

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

// populating required NOOPS
route.hooks = route.hooks || {}
Expand Down