-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from expatfile/@HofmannZ/feature/new-utilites
New utilities
- Loading branch information
Showing
10 changed files
with
254 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { allEnv } from './all-env'; | ||
|
||
let envBackup: NodeJS.ProcessEnv; | ||
|
||
beforeAll(() => { | ||
envBackup = process.env; | ||
process.env = {}; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = envBackup; | ||
}); | ||
|
||
describe('allEnv()', () => { | ||
afterEach(() => { | ||
delete process.env.FOO; | ||
delete process.env.NEXT_PUBLIC_BAR; | ||
delete process.env.NEXT_PUBLIC_BAZ; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
global.window = undefined as any; | ||
}); | ||
|
||
it('should return all values from the server', () => { | ||
process.env.FOO = 'foo'; | ||
process.env.NEXT_PUBLIC_BAR = 'bar'; | ||
process.env.NEXT_PUBLIC_BAZ = 'baz'; | ||
|
||
expect(allEnv()).toEqual({ | ||
FOO: 'foo', | ||
NEXT_PUBLIC_BAR: 'bar', | ||
NEXT_PUBLIC_BAZ: 'baz', | ||
}); | ||
}); | ||
|
||
it('should return all values from the browser', () => { | ||
Object.defineProperty(global, 'window', { | ||
value: { | ||
__ENV: { | ||
NEXT_PUBLIC_BAR: 'bar', | ||
NEXT_PUBLIC_BAZ: 'baz', | ||
}, | ||
}, | ||
writable: true, | ||
}); | ||
|
||
expect(allEnv()).toEqual({ | ||
NEXT_PUBLIC_BAR: 'bar', | ||
NEXT_PUBLIC_BAZ: 'baz', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { isBrowser } from './utils/is-browser'; | ||
|
||
/** | ||
* Reads all safe environment variables from the browser or all environment | ||
* variables from the server. | ||
*/ | ||
export function allEnv(): NodeJS.ProcessEnv { | ||
if (isBrowser()) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
return window.__ENV; | ||
} | ||
|
||
return process.env; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ declare global { | |
} | ||
} | ||
|
||
export { allEnv } from './all-env'; | ||
export { env } from './env'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { makeEnvPublic } from './make-env-public'; | ||
|
||
const warnSpy = jest.spyOn(console, 'warn'); | ||
|
||
beforeAll(() => { | ||
warnSpy.mockImplementation(); | ||
}); | ||
|
||
afterAll(() => { | ||
warnSpy.mockRestore(); | ||
}); | ||
|
||
describe('makeEnvPublic()', () => { | ||
afterEach(() => { | ||
delete process.env.FOO; | ||
delete process.env.BAR; | ||
delete process.env.BAZ; | ||
delete process.env.NEXT_PUBLIC_FOO; | ||
delete process.env.NEXT_PUBLIC_BAR; | ||
delete process.env.NEXT_PUBLIC_BAZ; | ||
}); | ||
|
||
it('should prefix an env var with NEXT_PUBLIC_', () => { | ||
process.env.FOO = 'foo'; | ||
|
||
makeEnvPublic('FOO'); | ||
|
||
expect(process.env.FOO).toEqual('foo'); | ||
expect(process.env.NEXT_PUBLIC_FOO).toEqual('foo'); | ||
}); | ||
|
||
it('should prefix multiple env vars with NEXT_PUBLIC_', () => { | ||
process.env.FOO = 'foo'; | ||
process.env.BAR = 'bar'; | ||
process.env.BAZ = 'baz'; | ||
|
||
makeEnvPublic(['FOO', 'BAR', 'BAZ']); | ||
|
||
expect(process.env.FOO).toEqual('foo'); | ||
expect(process.env.NEXT_PUBLIC_FOO).toEqual('foo'); | ||
expect(process.env.BAR).toEqual('bar'); | ||
expect(process.env.NEXT_PUBLIC_BAR).toEqual('bar'); | ||
expect(process.env.BAZ).toEqual('baz'); | ||
expect(process.env.NEXT_PUBLIC_BAZ).toEqual('baz'); | ||
}); | ||
|
||
it('should warn when the env var already starts with NEXT_PUBLIC_', () => { | ||
process.env.NEXT_PUBLIC_FOO = 'foo'; | ||
|
||
makeEnvPublic('NEXT_PUBLIC_FOO'); | ||
|
||
expect(warnSpy).toHaveBeenCalledWith( | ||
'> [next-runtime-env] The environment variable "NEXT_PUBLIC_FOO" is already public.' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function prefixKey(key: string) { | ||
// Check if the key already is already public. | ||
if (/^NEXT_PUBLIC_/i.test(key)) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`> [next-runtime-env] The environment variable "${key}" is already public.` | ||
); | ||
} | ||
|
||
const prefixedKey = `NEXT_PUBLIC_${key}`; | ||
|
||
process.env[prefixedKey] = process.env[key]; | ||
} | ||
|
||
/** | ||
* Make a private environment variable public, so that it can be accessed in the | ||
* browser. | ||
* | ||
* Usage: | ||
* ```ts | ||
* // Make a single variable public. | ||
* makeEnvPublic('FOO'); | ||
* | ||
* // Make multiple variables public. | ||
* makeEnvPublic(['FOO', 'BAR', 'BAZ']); | ||
* ``` | ||
*/ | ||
export function makeEnvPublic(key: string | string[]): void { | ||
if (typeof key === 'string') { | ||
prefixKey(key); | ||
} else { | ||
key.forEach(prefixKey); | ||
} | ||
} |