Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SDK #48

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"dependencies": {
"@croct/plug": "^0.15.0",
"@croct/plug-react": "^0.7.2",
"@croct/sdk": "^0.16.2",
"@croct/plug": "^0.16.0",
"@croct/plug-react": "^0.8.0",
"@croct/sdk": "^0.17.2",
"cookie": "^0.6.0",
"uuid": "^10.0.0"
},
Expand All @@ -57,7 +57,7 @@
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"@types/set-cookie-parser": "^2.4.7",
"@types/uuid": "^9.0.5",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"babel-loader": "^9.1.0",
Expand Down
36 changes: 36 additions & 0 deletions src/CroctProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ 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;
delete process.env.NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT;
delete process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE;
});

afterEach(() => {
Expand Down Expand Up @@ -67,11 +69,15 @@ describe('<CroctProvider />', () => {
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';
process.env.NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT = '3000';
process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE = 'es';

const config = {
appId: '11111111-1111-1111-1111-111111111111',
debug: false,
baseEndpointUrl: 'https://override.com',
defaultFetchTimeout: 2000,
defaultPreferredLocale: 'en',
cookie: {
clientId: {
name: 'custom-client-id',
Expand All @@ -96,6 +102,8 @@ describe('<CroctProvider />', () => {
debug: config.debug,
cookie: config.cookie,
baseEndpointUrl: config.baseEndpointUrl,
defaultFetchTimeout: config.defaultFetchTimeout,
defaultPreferredLocale: config.defaultPreferredLocale,
},
expect.anything(),
);
Expand Down Expand Up @@ -141,4 +149,32 @@ describe('<CroctProvider />', () => {
expect.anything(),
);
});

it('should detect the default fetch timeout from the environment', () => {
process.env.NEXT_PUBLIC_CROCT_APP_ID = '00000000-0000-0000-0000-000000000000';
process.env.NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT = '3000';

render(<CroctProvider />);

expect(UnderlyingProvider).toHaveBeenCalledWith<[ResolvedProviderProps, any]>(
expect.objectContaining({
defaultFetchTimeout: 3000,
}),
expect.anything(),
);
});

it('should detect the default preferred locale from the environment', () => {
process.env.NEXT_PUBLIC_CROCT_APP_ID = '00000000-0000-0000-0000-000000000000';
process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE = 'es';

render(<CroctProvider />);

expect(UnderlyingProvider).toHaveBeenCalledWith<[ResolvedProviderProps, any]>(
expect.objectContaining({
defaultPreferredLocale: process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE,
}),
expect.anything(),
);
});
});
4 changes: 4 additions & 0 deletions src/CroctProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {FunctionComponent} from 'react';
import {getClientIdCookieOptions, getPreviewCookieOptions, getUserTokenCookieOptions} from '@/config/cookie';
import {getAppId} from '@/config/appId';
import {getEnvEntry, getEnvEntryFlag} from '@/config/env';
import {getDefaultFetchTimeout} from '@/config/timeout';

type OmittedProps = 'appId' | 'disableCidMirroring' | 'cidAssignerEndpointUrl';

Expand All @@ -16,12 +17,15 @@ export type CroctProviderProps = Omit<ReactCroctProviderProps, OmittedProps>

export const CroctProvider: FunctionComponent<CroctProviderProps> = props => {
const {appId = getAppId(), ...rest} = props;
const defaultTimeout = getDefaultFetchTimeout();

return (
<ReactCroctProvider
appId={appId}
{...getEnvEntryFlag('debug', process.env.NEXT_PUBLIC_CROCT_DEBUG)}
{...getEnvEntry('baseEndpointUrl', process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL)}
{...getEnvEntry('defaultPreferredLocale', process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE)}
{...(defaultTimeout !== undefined ? {defaultFetchTimeout: defaultTimeout} : {})}
cookie={{
clientId: getClientIdCookieOptions(),
userToken: getUserTokenCookieOptions(),
Expand Down
21 changes: 20 additions & 1 deletion src/config/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ jest.mock(
describe('getRequestContext', () => {
const appId = '00000000-0000-0000-0000-000000000000';

beforeEach(() => {
jest.clearAllMocks();
delete process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE;
});

it('should throw an error when the client ID is missing', () => {
expect(() => getRequestContext(new Headers(), createCookieJar())).toThrow(
'Croct\'s Client ID is missing. Did you forget to configure Croct\'s middleware? '
Expand Down Expand Up @@ -66,7 +71,7 @@ describe('getRequestContext', () => {
headers.set(Header.CLIENT_IP, request.clientIp);
headers.set(Header.PREVIEW_TOKEN, request.previewToken);
headers.set(Header.USER_TOKEN, token.toString());
headers.set(Header.LOCALE, request.preferredLocale);
headers.set(Header.PREFERRED_LOCALE, request.preferredLocale);

expect(getRequestContext(headers, createCookieJar())).toEqual(request);
});
Expand Down Expand Up @@ -147,6 +152,18 @@ describe('getRequestContext', () => {

expect(context.userToken).toEqual(newToken.toString());
});

it('should return the preferred locale from the environment', () => {
process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE = 'en';

const headers = new Headers();

headers.set(Header.CLIENT_ID, '00000000-0000-0000-0000-000000000000');

const context = getRequestContext(headers, createCookieJar());

expect(context.preferredLocale).toEqual('en');
});
});

describe('resolveRequestContext', () => {
Expand All @@ -166,6 +183,7 @@ describe('resolveRequestContext', () => {
referrer: 'http://referrer.com',
clientIp: '192.0.0.1',
previewToken: 'ct.preview_token',
preferredLocale: 'en',
} satisfies RequestContext;

headers.set(Header.CLIENT_ID, request.clientId);
Expand All @@ -174,6 +192,7 @@ describe('resolveRequestContext', () => {
headers.set(Header.REFERRER, request.referrer);
headers.set(Header.CLIENT_IP, request.clientIp);
headers.set(Header.PREVIEW_TOKEN, request.previewToken);
headers.set(Header.PREFERRED_LOCALE, request.preferredLocale);

jest.mocked(getHeaders).mockReturnValue(headers);
jest.mocked(getCookies).mockReturnValue(cookies);
Expand Down
5 changes: 4 additions & 1 deletion src/config/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Token} from '@croct/sdk/token';
import {Header} from '@/config/http';
import {getUserTokenCookieOptions} from '@/config/cookie';
import {CookieReader, getCookies, getHeaders, HeaderReader, RouteContext} from '@/headers';
import {getEnvValue} from '@/config/env';

export type RequestContext = {
clientId: string,
Expand Down Expand Up @@ -32,7 +33,9 @@ export function getRequestContext(headers: HeaderReader, cookies: CookieReader):
clientId: clientId,
};

const locale = headers.get(Header.LOCALE);
const locale = headers.get(Header.PREFERRED_LOCALE)
?? getEnvValue(process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE)
?? null;

if (locale !== null) {
context.preferredLocale = locale;
Expand Down
2 changes: 1 addition & 1 deletion src/config/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export enum Header {
USER_TOKEN = 'x-user-token',
PREVIEW_TOKEN = 'x-preview-token',
REQUEST_URI = 'x-request-uri',
LOCALE = 'x-locale',
PREFERRED_LOCALE = 'x-preferred-locale',
}
4 changes: 2 additions & 2 deletions src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('middleware', () => {

expect(nextMiddleware).toHaveBeenCalledWith(request, fetchEvent);

expect(request.headers.get(Header.LOCALE)).toBe(locale);
expect(request.headers.get(Header.PREFERRED_LOCALE)).toBe(locale);
});

it('should not forward the locale through the request headers if it is empty', async () => {
Expand All @@ -282,7 +282,7 @@ describe('middleware', () => {

expect(nextMiddleware).toHaveBeenCalledWith(request, fetchEvent);

expect(request.headers.get(Header.LOCALE)).toBeNull();
expect(request.headers.get(Header.PREFERRED_LOCALE)).toBeNull();
});

it('should forward the URL through the request headers', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function withCroct(...args: CroctMiddlewareParams): NextMiddleware {
headers.set(Header.CLIENT_ID, clientId);

if (locale !== '') {
headers.set(Header.LOCALE, locale);
headers.set(Header.PREFERRED_LOCALE, locale);
}

if (request.ip !== undefined) {
Expand Down
Loading