Skip to content

Commit 3501054

Browse files
authored
Merge pull request #11 from menthol/master
feat: allow overrides config by env variable
2 parents ecac139 + 8c356bc commit 3501054

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ export default defineNuxtConfig({
8282
// GET /api/todos -> https://jsonplaceholder.typicode.com/todos [304]
8383
// GET /api/launches -> https://api.spacexdata.com/v5/launches/latest [304]
8484
```
85+
### Runtime config
86+
87+
Runtime configuration is valid if you put the configuration inside "runtimeConfig" and set your environment variables
88+
89+
```env
90+
NUXT_PROXY_OPTIONS_TARGET=https://reqres.in/api
91+
```
92+
```ts
93+
export default defineNuxtConfig({
94+
modules: ['nuxt-proxy'],
95+
runtimeConfig: {
96+
proxy: {
97+
options: { target: 'https://jsonplaceholder.typicode.com', ...{ /* config */} }
98+
}
99+
}
100+
})
101+
// GET /api/users -> https://reqres.in/api/users [304]
102+
```
103+
104+
Or for multiple targets
105+
106+
```ts
107+
export default defineNuxtConfig({
108+
modules: ['nuxt-proxy'],
109+
runtimeConfig: {
110+
proxy: {
111+
options: [
112+
{ target: 'https://jsonplaceholder.typicode.com', ...{ /* config */} },
113+
{ target: 'https://api.spacexdata.com/v5', ...{ /* config */} },
114+
]
115+
}
116+
},
117+
})
118+
119+
// GET /api/todos -> https://jsonplaceholder.typicode.com/todos [304]
120+
// GET /api/launches -> https://fake.wobsoriano.space/launches/latest [304]
121+
```
122+
```env
123+
NUXT_PROXY_OPTIONS=[{}, {"target": "https://fake.wobsoriano.space"}]
124+
```
85125

86126
## License
87127

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"dependencies": {
5050
"@nuxt/kit": "3.0.0-rc.12",
5151
"dedent": "^0.7.0",
52+
"defu": "^6.0.0",
5253
"h3": "^0.8.5",
5354
"http-proxy-middleware": "^3.0.0-beta.0",
5455
"ohash": "^0.1.5",

pnpm-lock.yaml

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

src/module.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import { join } from 'pathe'
55
import dedent from 'dedent'
66
import { hash, objectHash } from 'ohash'
77

8-
function createProxyMiddleware(buildDir: string, filename: string, options: Options) {
8+
function createProxyMiddleware(buildDir: string, filename: string, options: Options, index?: number) {
99
addTemplate({
1010
filename,
1111
write: true,
1212
getContents: () => dedent`
1313
import { createProxyMiddleware } from 'nuxt-proxy/middleware'
14+
import { defu } from 'defu'
1415
import { useRuntimeConfig } from '#imports'
1516
16-
export default createProxyMiddleware(${JSON.stringify(options)})
17+
const buildtimeOptions = ${JSON.stringify(options)}
18+
const runtimeOptions = [].concat(useRuntimeConfig().proxy?.options)[${JSON.stringify(index)} ?? 0]
19+
20+
export default createProxyMiddleware(defu(runtimeOptions, buildtimeOptions))
1721
`,
1822
})
1923

@@ -43,9 +47,9 @@ export default defineNuxtModule<ModuleOptions>({
4347
const finalConfig = (nuxt.options.runtimeConfig.proxy = nuxt.options.runtimeConfig.proxy || { options: options.options }) as ModuleOptions
4448

4549
if (Array.isArray(finalConfig.options)) {
46-
finalConfig.options.forEach((options) => {
50+
finalConfig.options.forEach((options, index) => {
4751
const filename = `proxy/handler_${hash(objectHash(options))}.ts`
48-
createProxyMiddleware(nuxt.options.buildDir, filename, options)
52+
createProxyMiddleware(nuxt.options.buildDir, filename, options, index)
4953
})
5054
}
5155
else {

0 commit comments

Comments
 (0)