Skip to content

Commit

Permalink
Safely access environment variables (#2737)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer authored Oct 24, 2024
1 parent 9969c45 commit c2c819c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-scissors-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sumup-oss/circuit-ui": patch
---

Fixed safely accessing environment variables in environments where the `process` variable is undefined.
3 changes: 2 additions & 1 deletion packages/circuit-ui/components/Display/Display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { forwardRef, type HTMLAttributes } from 'react';
import { clsx } from '../../styles/clsx.js';
import { CircuitError } from '../../util/errors.js';
import { deprecate } from '../../util/logger.js';
import { getEnvVariable } from '../../util/env.js';

import classes from './Display.module.css';

Expand Down Expand Up @@ -75,7 +76,7 @@ export const Display = forwardRef<HTMLHeadingElement, DisplayProps>(
if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV !== 'test' &&
!process?.env?.UNSAFE_DISABLE_ELEMENT_ERRORS &&
!getEnvVariable('UNSAFE_DISABLE_ELEMENT_ERRORS') &&
!as
) {
throw new CircuitError('Display', 'The `as` prop is required.');
Expand Down
3 changes: 2 additions & 1 deletion packages/circuit-ui/components/Headline/Headline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { forwardRef, type HTMLAttributes } from 'react';
import { clsx } from '../../styles/clsx.js';
import { CircuitError } from '../../util/errors.js';
import { deprecate } from '../../util/logger.js';
import { getEnvVariable } from '../../util/env.js';

import classes from './Headline.module.css';

Expand Down Expand Up @@ -68,7 +69,7 @@ export const Headline = forwardRef<HTMLHeadingElement, HeadlineProps>(
if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV !== 'test' &&
!process?.env?.UNSAFE_DISABLE_ELEMENT_ERRORS &&
!getEnvVariable('UNSAFE_DISABLE_ELEMENT_ERRORS') &&
!as
) {
throw new CircuitError('Headline', 'The `as` prop is required.');
Expand Down
52 changes: 52 additions & 0 deletions packages/circuit-ui/util/env.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2024, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { beforeEach, describe, expect, it } from 'vitest';

import { getEnvVariable } from './env.js';

describe('env', () => {
describe('getEnvVariable', () => {
const originalProcess = process;

beforeEach(() => {
process = originalProcess;
process.env = {};
});

it('should return the environment variable`', () => {
process.env.FOO = 'foo';
const actual = getEnvVariable('FOO');
expect(actual).toBe('foo');
});

it('should return undefined if the environment variable is not defined', () => {
const actual = getEnvVariable('FOO');
expect(actual).toBeUndefined();
});

it('should return undefined if `process` is not defined', () => {
// @ts-expect-error We're testing for this error
process = undefined;
const actual = getEnvVariable('FOO');
expect(actual).toBeUndefined();
});

it('should throw an error when used for `NODE_ENV`', () => {
const actual = () => getEnvVariable('NODE_ENV');
expect(actual).toThrowError();
});
});
});
29 changes: 29 additions & 0 deletions packages/circuit-ui/util/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2024, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/