Skip to content

Commit 23c6f30

Browse files
committed
feat(makeEnvPublic): ✨ allow passing multiple env vars and don't throw
1 parent 2e766ec commit 23c6f30

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,15 @@ export const getStaticProps: GetStaticProps = async (context) => {
166166
};
167167
```
168168

169-
#### `makeEnvPublic(key: string): void`
169+
#### `makeEnvPublic(key: string | string[]): void`
170170

171-
Makes the environment variable with the given key public. This is useful if you
171+
Makes an environment variable with a given key public. This is useful if you
172172
want to use an environment variable in the browser, but it was was not declared
173173
with a `NEXT_PUBLIC_` prefix.
174174

175+
For ease of use you can also make multiple env vars public at once by passing an
176+
array of keys.
177+
175178
##### Example
176179

177180
```js
@@ -183,6 +186,9 @@ const { makeEnvPublic } = require('next-runtime-env/build/make-env-public');
183186
// will make it public and available as `NEXT_PUBLIC_FOO`.
184187
makeEnvPublic('FOO');
185188

189+
// Or you can make multiple env vars public at once.
190+
makeEnvPublic(['BAR', 'BAZ']);
191+
186192
// This will generate the `__ENV.js` file and include `NEXT_PUBLIC_FOO`.
187193
configureRuntimeEnv();
188194
```

src/make-env-public.spec.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
11
import { makeEnvPublic } from './make-env-public';
22

3+
const warnSpy = jest.spyOn(console, 'warn');
4+
5+
beforeAll(() => {
6+
warnSpy.mockImplementation();
7+
});
8+
9+
afterAll(() => {
10+
warnSpy.mockRestore();
11+
});
12+
313
describe('makeEnvPublic()', () => {
414
afterEach(() => {
515
delete process.env.FOO;
16+
delete process.env.BAR;
17+
delete process.env.BAZ;
618
delete process.env.NEXT_PUBLIC_FOO;
19+
delete process.env.NEXT_PUBLIC_BAR;
20+
delete process.env.NEXT_PUBLIC_BAZ;
721
});
822

923
it('should prefix an env var with NEXT_PUBLIC_', () => {
1024
process.env.FOO = 'foo';
1125

1226
makeEnvPublic('FOO');
1327

14-
expect(process.env.FOO).toBeDefined();
15-
expect(process.env.NEXT_PUBLIC_FOO).toBeDefined();
28+
expect(process.env.FOO).toEqual('foo');
29+
expect(process.env.NEXT_PUBLIC_FOO).toEqual('foo');
1630
});
1731

18-
it('should copy the value to the new NEXT_PUBLIC_ env var', () => {
32+
it('should prefix multiple env vars with NEXT_PUBLIC_', () => {
1933
process.env.FOO = 'foo';
34+
process.env.BAR = 'bar';
35+
process.env.BAZ = 'baz';
2036

21-
makeEnvPublic('FOO');
37+
makeEnvPublic(['FOO', 'BAR', 'BAZ']);
2238

2339
expect(process.env.FOO).toEqual('foo');
2440
expect(process.env.NEXT_PUBLIC_FOO).toEqual('foo');
41+
expect(process.env.BAR).toEqual('bar');
42+
expect(process.env.NEXT_PUBLIC_BAR).toEqual('bar');
43+
expect(process.env.BAZ).toEqual('baz');
44+
expect(process.env.NEXT_PUBLIC_BAZ).toEqual('baz');
2545
});
2646

27-
it('should throw when the env var already starts with NEXT_PUBLIC_', () => {
47+
it('should warn when the env var already starts with NEXT_PUBLIC_', () => {
2848
process.env.NEXT_PUBLIC_FOO = 'foo';
2949

30-
expect(() => {
31-
makeEnvPublic('NEXT_PUBLIC_FOO');
32-
}).toThrowError(
33-
'The environment variable "NEXT_PUBLIC_FOO" is already public.'
50+
makeEnvPublic('NEXT_PUBLIC_FOO');
51+
52+
expect(warnSpy).toHaveBeenCalledWith(
53+
'> [next-runtime-env] The environment variable "NEXT_PUBLIC_FOO" is already public.'
3454
);
3555
});
3656
});

src/make-env-public.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1-
/**
2-
* Make a private environment variable public, so that it can be accessed in the
3-
* browser.
4-
*/
5-
export function makeEnvPublic(key: string): void {
1+
function prefixKey(key: string) {
62
// Check if the key already is already public.
73
if (/^NEXT_PUBLIC_/i.test(key)) {
8-
throw new Error(`The environment variable "${key}" is already public.`);
4+
// eslint-disable-next-line no-console
5+
console.warn(
6+
`> [next-runtime-env] The environment variable "${key}" is already public.`
7+
);
98
}
109

1110
const prefixedKey = `NEXT_PUBLIC_${key}`;
1211

1312
process.env[prefixedKey] = process.env[key];
1413
}
14+
15+
/**
16+
* Make a private environment variable public, so that it can be accessed in the
17+
* browser.
18+
*
19+
* Usage:
20+
* ```ts
21+
* // Make a single variable public.
22+
* makeEnvPublic('FOO');
23+
*
24+
* // Make multiple variables public.
25+
* makeEnvPublic(['FOO', 'BAR', 'BAZ']);
26+
* ```
27+
*/
28+
export function makeEnvPublic(key: string | string[]): void {
29+
if (typeof key === 'string') {
30+
prefixKey(key);
31+
} else {
32+
key.forEach(prefixKey);
33+
}
34+
}

0 commit comments

Comments
 (0)