Skip to content

Commit a459b85

Browse files
authored
Merge pull request #1 from expatfile/development
build: 🔖 release stable version
2 parents 6706bb6 + 8f793af commit a459b85

File tree

8 files changed

+173
-11
lines changed

8 files changed

+173
-11
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
strategy:
1919
matrix:
20-
node-version: [18]
20+
node-version: [16, 18]
2121

2222
steps:
2323
- name: Checkout
@@ -39,7 +39,7 @@ jobs:
3939

4040
strategy:
4141
matrix:
42-
node-version: [18]
42+
node-version: [16, 18]
4343

4444
steps:
4545
- name: Checkout
@@ -61,7 +61,7 @@ jobs:
6161

6262
strategy:
6363
matrix:
64-
node-version: [18]
64+
node-version: [16, 18]
6565

6666
steps:
6767
- name: Checkout

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jest.config.js
1212
prettier.config.js
1313
tsconfig.json
1414
/.github
15+
/.vscode
1516

1617
# source
1718
/src

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Expatfile.tax LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# next-runtime-env - Runtime Environment Configuration
22

3+
[![codecov](https://codecov.io/gh/expatfile/next-runtime-env/branch/main/graph/badge.svg?token=mbGgsweFuP)](https://codecov.io/gh/expatfile/next-runtime-env)
4+
35
Populates your environment at **run-time** rather than **build-time**.
46

57
- Isomorphic - Server, browser and middleware compatible.
@@ -13,12 +15,14 @@ Runtime environment variables are used in common best-practice patterns for buil
1315
1. Add the following lines to your `next.config.js`:
1416

1517
```js
16-
const { configureRuntimeEnv } = require('next-runtime-env');
18+
const {
19+
configureRuntimeEnv,
20+
} = require('next-runtime-env/build/configure-runtime-env');
1721

1822
configureRuntimeEnv();
1923
```
2024

21-
This will generates a `__ENV.js` file that contains white-listed environment variables that have a `NEXT_PUBLIC_` prefix.
25+
This will generates a `__ENV.js` file that contains allow-listed environment variables that have a `NEXT_PUBLIC_` prefix.
2226

2327
2. Add the following to the head section fo your `pages/_document.js`:
2428

@@ -27,11 +31,15 @@ This will generates a `__ENV.js` file that contains white-listed environment var
2731
<script src="/__ENV.js" />
2832
```
2933

30-
Done!
34+
Done!
3135

3236
### Usage 🧑‍💻
3337

34-
In the browser your variables will be available at `window.__ENV.NEXT_PUBLIC_FOO` and on the server `process.env.NEXT_PUBLIC_FOO`. We have included a helper function to make retrieving a value easier:
38+
In the browser your variables will be available at `window.__ENV.NEXT_PUBLIC_FOO` and on the server `process.env.NEXT_PUBLIC_FOO`.
39+
40+
#### Helper 😉
41+
42+
We have included a helper function to make retrieving a value easier:
3543

3644
```bash
3745
# .env

src/helpers/get-public-env.spec.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { getPublicEnv } from './get-public-env';
2+
3+
describe('getPublicEnv', () => {
4+
afterEach(() => {
5+
delete process.env.FOO;
6+
delete process.env.BAR;
7+
delete process.env.BAR;
8+
delete process.env.NEXT_PUBLIC_FOO;
9+
delete process.env.NEXT_PUBLIC_BAR;
10+
delete process.env.NEXT_PUBLIC_BAZ;
11+
});
12+
13+
it('should return a allow-listed value', () => {
14+
process.env.NEXT_PUBLIC_FOO = 'foo';
15+
16+
expect(getPublicEnv()).toEqual({
17+
NEXT_PUBLIC_FOO: 'foo',
18+
});
19+
});
20+
21+
it('should return multiple allow-listed values', () => {
22+
process.env.NEXT_PUBLIC_FOO = 'foo';
23+
process.env.NEXT_PUBLIC_BAR = 'bar';
24+
process.env.NEXT_PUBLIC_BAZ = 'baz';
25+
26+
expect(getPublicEnv()).toEqual({
27+
NEXT_PUBLIC_FOO: 'foo',
28+
NEXT_PUBLIC_BAR: 'bar',
29+
NEXT_PUBLIC_BAZ: 'baz',
30+
});
31+
});
32+
33+
it('should not return a non allow-listed value', () => {
34+
process.env.FOO = 'foo';
35+
36+
expect(getPublicEnv()).toEqual({});
37+
});
38+
39+
it('should not return multiple non allow-listed values', () => {
40+
process.env.FOO = 'foo';
41+
process.env.BAR = 'bar';
42+
process.env.BAZ = 'baz';
43+
44+
expect(getPublicEnv()).toEqual({});
45+
});
46+
47+
it('should not return a mixed list of allow-listed and non allow-listed values', () => {
48+
process.env.NEXT_PUBLIC_FOO = 'foo';
49+
process.env.BAR = 'bar';
50+
process.env.NEXT_PUBLIC_BAZ = 'baz';
51+
52+
expect(getPublicEnv()).toEqual({
53+
NEXT_PUBLIC_FOO: 'foo',
54+
NEXT_PUBLIC_BAZ: 'baz',
55+
});
56+
});
57+
});

src/helpers/write-browser-env.spec.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import fs from 'fs';
2+
3+
import { writeBrowserEnv } from './write-browser-env';
4+
5+
const infoSpy = jest.spyOn(console, 'info');
6+
7+
const base = fs.realpathSync(process.cwd());
8+
const path = `${base}/public`;
9+
const file = `${path}/__ENV.js`;
10+
11+
beforeAll(() => {
12+
infoSpy.mockImplementation();
13+
14+
fs.mkdirSync(path);
15+
});
16+
17+
afterAll(() => {
18+
infoSpy.mockRestore();
19+
20+
fs.rmdirSync(path);
21+
});
22+
23+
describe('writeBrowserEnv', () => {
24+
afterEach(() => {
25+
fs.rmSync(file);
26+
});
27+
28+
it('should write an empty env', () => {
29+
writeBrowserEnv({});
30+
31+
expect(infoSpy).toHaveBeenCalledWith(
32+
'next-runtime-env: Writing browser runtime env',
33+
file
34+
);
35+
36+
const content = fs.readFileSync(file).toString();
37+
38+
expect(content).toEqual('window.__ENV = {};');
39+
});
40+
41+
it('should write and env with a value', () => {
42+
writeBrowserEnv({
43+
NEXT_PUBLIC_FOO: 'foo',
44+
});
45+
46+
expect(infoSpy).toHaveBeenCalledWith(
47+
'next-runtime-env: Writing browser runtime env',
48+
file
49+
);
50+
51+
const content = fs.readFileSync(file).toString();
52+
53+
expect(content).toEqual('window.__ENV = {"NEXT_PUBLIC_FOO":"foo"};');
54+
});
55+
56+
it('should write and env with multiple values', () => {
57+
writeBrowserEnv({
58+
NEXT_PUBLIC_FOO: 'foo',
59+
NEXT_PUBLIC_BAR: 'bar',
60+
NEXT_PUBLIC_BAZ: 'baz',
61+
});
62+
63+
expect(infoSpy).toHaveBeenCalledWith(
64+
'next-runtime-env: Writing browser runtime env',
65+
file
66+
);
67+
68+
const content = fs.readFileSync(file).toString();
69+
70+
expect(content).toEqual(
71+
'window.__ENV = {"NEXT_PUBLIC_FOO":"foo","NEXT_PUBLIC_BAR":"bar","NEXT_PUBLIC_BAZ":"baz"};'
72+
);
73+
});
74+
});

src/helpers/write-browser-env.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'node:fs';
1+
import fs from 'fs';
22

33
/**
44
* Writes the environment variables to the public __ENV.js file and make them
@@ -11,7 +11,7 @@ export function writeBrowserEnv(env: NodeJS.ProcessEnv) {
1111
// eslint-disable-next-line no-console
1212
console.info('next-runtime-env: Writing browser runtime env', path);
1313

14-
const populate = `window.__ENV = ${JSON.stringify(env)};`;
14+
const content = `window.__ENV = ${JSON.stringify(env)};`;
1515

16-
fs.writeFileSync(path, populate);
16+
fs.writeFileSync(path, content);
1717
}

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
/* istanbul ignore file */
2+
13
// This allows TypeScript to detect our global value.
24
declare global {
35
interface Window {
46
__ENV: NodeJS.ProcessEnv;
57
}
68
}
79

8-
export { configureRuntimeEnv } from './configure-runtime-env';
910
export { env } from './env';

0 commit comments

Comments
 (0)