Skip to content

Commit 2011441

Browse files
committed
feat: allow multiple targets
1 parent 6a3e571 commit 2011441

File tree

5 files changed

+66
-40
lines changed

5 files changed

+66
-40
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
shamefully-hoist=true
2+
ignore-workspace-root-check=true

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"defu": "^6.0.0",
5151
"h3": "^0.7.9",
5252
"http-proxy-middleware": "^3.0.0-beta.0",
53+
"ohash": "^0.1.0",
5354
"pathe": "^0.3.0"
5455
},
5556
"devDependencies": {

playground/nuxt.config.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,29 @@ import { defineNuxtConfig } from 'nuxt'
44
export default defineNuxtConfig({
55
modules: ['nuxt-proxy'],
66
proxy: {
7-
target: 'https://jsonplaceholder.typicode.com',
8-
changeOrigin: true,
9-
pathRewrite: {
10-
'^/api/todos': '/todos',
11-
'^/api/users': '/users',
12-
},
13-
pathFilter: [
14-
'/api/todos',
15-
'/api/users',
7+
options: [
8+
{
9+
target: 'https://jsonplaceholder.typicode.com',
10+
changeOrigin: true,
11+
pathRewrite: {
12+
'^/api/todos': '/todos',
13+
'^/api/users': '/users',
14+
},
15+
pathFilter: [
16+
'/api/todos',
17+
'/api/users',
18+
],
19+
},
20+
{
21+
target: 'https://api.spacexdata.com/v5',
22+
changeOrigin: true,
23+
pathRewrite: {
24+
'^/api/launches': '/launches/latest',
25+
},
26+
pathFilter: [
27+
'/api/launches',
28+
],
29+
},
1630
],
1731
},
1832
})

pnpm-lock.yaml

Lines changed: 3 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,54 @@ import type { Options } from 'http-proxy-middleware'
44
import { join } from 'pathe'
55
import { defu } from 'defu'
66
import dedent from 'dedent'
7+
import { hash, objectHash } from 'ohash'
78

8-
export default defineNuxtModule<Options>({
9+
function createProxyMiddleware(buildDir: string, filename: string, options: Options) {
10+
addTemplate({
11+
filename,
12+
write: true,
13+
getContents: () => dedent`
14+
import { createProxyMiddleware } from 'nuxt-proxy/middleware'
15+
16+
export default createProxyMiddleware(${JSON.stringify(options)})
17+
`,
18+
})
19+
20+
addServerHandler({
21+
handler: join(buildDir, filename),
22+
middleware: true,
23+
})
24+
}
25+
26+
export interface ModuleOptions {
27+
options: Options[] | Options
28+
}
29+
30+
export default defineNuxtModule<ModuleOptions>({
931
meta: {
1032
name: 'nuxt-proxy',
1133
configKey: 'proxy',
1234
},
13-
defaults: {},
35+
defaults: {
36+
options: [],
37+
},
1438
setup(options, nuxt) {
1539
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
1640
nuxt.options.build.transpile.push(runtimeDir, '#build/proxy-handler')
1741

1842
// Final resolved configuration
19-
const finalConfig = nuxt.options.runtimeConfig.proxy = defu(nuxt.options.runtimeConfig.proxy, options)
20-
21-
addTemplate({
22-
filename: 'proxy-handler.ts',
23-
write: true,
24-
getContents: () => dedent`
25-
import { createProxyMiddleware } from 'nuxt-proxy/middleware'
26-
27-
export default createProxyMiddleware(${JSON.stringify(finalConfig)})
28-
`,
29-
})
30-
31-
addServerHandler({
32-
handler: join(nuxt.options.buildDir, 'proxy-handler.ts'),
33-
middleware: true,
34-
})
43+
const finalConfig = (nuxt.options.runtimeConfig.proxy = defu(nuxt.options.runtimeConfig.proxy, options)) as ModuleOptions
44+
45+
if (Array.isArray(finalConfig.options)) {
46+
finalConfig.options.forEach((options) => {
47+
const filename = `proxy/${hash(objectHash(options))}.ts`
48+
createProxyMiddleware(nuxt.options.buildDir, filename, options)
49+
})
50+
}
51+
else {
52+
const filename = 'proxy_handler.ts'
53+
createProxyMiddleware(nuxt.options.buildDir, filename, finalConfig.options)
54+
}
3555
},
3656
})
3757

0 commit comments

Comments
 (0)