-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
32 lines (28 loc) · 971 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { FABRuntime } from '@fab/core'
type Args = {
enabled?: string | boolean
port?: number
}
export default ({ Router, Cache }: FABRuntime, args: Args = {}) => {
const { enabled, port } = args
if (!port)
return console.log(
`[@fab/plugin-dev-proxy] Required argument 'port' missing. Skipping.`
)
if (!enabled)
return console.log(
`[@fab/plugin-dev-proxy] ${
typeof enabled === 'undefined'
? `Required argument 'enabled' missing.`
: `'enabled' argument for dev-proxy is falsy (${enabled}).`
} Skipping.`
)
Router.onAll(async ({ request, settings }) => {
// We know 'enabled` is truthy, but if it's a string check the settings
if (typeof enabled === 'string' && !settings[enabled]) return undefined
const proxied_url = new URL(request.url)
proxied_url.host = `localhost:${args.port}`
const proxied = new Request(proxied_url.href, request)
return fetch(proxied)
})
}