Skip to content

Commit

Permalink
Allow specifying base URL per environment (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos authored Sep 5, 2024
1 parent 6b41239 commit b8cf51b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/CroctProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('<CroctProvider />', () => {

delete process.env.NEXT_PUBLIC_CROCT_APP_ID;
delete process.env.NEXT_PUBLIC_CROCT_DEBUG;
delete process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL;
});

afterEach(() => {
Expand Down Expand Up @@ -65,10 +66,12 @@ describe('<CroctProvider />', () => {
it('should allow overriding the environment configuration', () => {
process.env.NEXT_PUBLIC_CROCT_APP_ID = '00000000-0000-0000-0000-000000000000';
process.env.NEXT_PUBLIC_CROCT_DEBUG = 'true';
process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL = 'https://example.com';

const config = {
appId: '11111111-1111-1111-1111-111111111111',
debug: false,
baseEndpointUrl: 'https://override.com',
cookie: {
clientId: {
name: 'custom-client-id',
Expand All @@ -92,6 +95,7 @@ describe('<CroctProvider />', () => {
appId: config.appId,
debug: config.debug,
cookie: config.cookie,
baseEndpointUrl: config.baseEndpointUrl,
},
expect.anything(),
);
Expand Down Expand Up @@ -123,4 +127,18 @@ describe('<CroctProvider />', () => {
expect.anything(),
);
});

it('should detect the base endpoint URL from the environment', () => {
process.env.NEXT_PUBLIC_CROCT_APP_ID = '00000000-0000-0000-0000-000000000000';
process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL = 'https://example.com';

render(<CroctProvider />);

expect(UnderlyingProvider).toHaveBeenCalledWith<[ResolvedProviderProps, any]>(
expect.objectContaining({
baseEndpointUrl: process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL,
}),
expect.anything(),
);
});
});
9 changes: 8 additions & 1 deletion src/CroctProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export type CroctProviderProps = Omit<ReactCroctProviderProps, OmittedProps>

export const CroctProvider: FunctionComponent<CroctProviderProps> = props => {
const {appId = getAppId(), ...rest} = props;
const debugMode = process.env.NEXT_PUBLIC_CROCT_DEBUG === 'true';
const endpointUrl = normalizeEnvVar(process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL);

return (
<ReactCroctProvider
debug={process.env.NEXT_PUBLIC_CROCT_DEBUG === 'true'}
debug={debugMode}
appId={appId}
{...(endpointUrl !== undefined ? {baseEndpointUrl: endpointUrl} : {})}
cookie={{
clientId: getClientIdCookieOptions(),
userToken: getUserTokenCookieOptions(),
Expand All @@ -29,3 +32,7 @@ export const CroctProvider: FunctionComponent<CroctProviderProps> = props => {
/>
);
};

function normalizeEnvVar(value: string | undefined): string|undefined {
return value === undefined || value === '' ? undefined : value;
}

0 comments on commit b8cf51b

Please sign in to comment.