Skip to content

Commit

Permalink
chore: pass context directly
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Jan 24, 2025
1 parent 7f808fa commit f1ce19b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
14 changes: 7 additions & 7 deletions packages/tanstack-start/src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getPublicEnvVariables } from '../utils/env';

export const commonEnvs = () => {
const event = getEvent();
const publicEnvs = getPublicEnvVariables(event);
const publicEnvs = getPublicEnvVariables(event.context);

return {
// Public environment variables
Expand All @@ -22,16 +22,16 @@ export const commonEnvs = () => {
TELEMETRY_DEBUG: publicEnvs.telemetryDebug,

// Server-only environment variables
API_VERSION: getEnvVariable('CLERK_API_VERSION', event) || 'v1',
SECRET_KEY: getEnvVariable('CLERK_SECRET_KEY', event),
ENCRYPTION_KEY: getEnvVariable('CLERK_ENCRYPTION_KEY', event),
CLERK_JWT_KEY: getEnvVariable('CLERK_JWT_KEY', event),
API_URL: getEnvVariable('CLERK_API_URL', event) || apiUrlFromPublishableKey(publicEnvs.publishableKey),
API_VERSION: getEnvVariable('CLERK_API_VERSION', event.context) || 'v1',
SECRET_KEY: getEnvVariable('CLERK_SECRET_KEY', event.context),
ENCRYPTION_KEY: getEnvVariable('CLERK_ENCRYPTION_KEY', event.context),
CLERK_JWT_KEY: getEnvVariable('CLERK_JWT_KEY', event.context),
API_URL: getEnvVariable('CLERK_API_URL', event.context) || apiUrlFromPublishableKey(publicEnvs.publishableKey),

SDK_METADATA: {
name: PACKAGE_NAME,
version: PACKAGE_VERSION,
environment: getEnvVariable('NODE_ENV', event),
environment: getEnvVariable('NODE_ENV', event.context),
},
} as const;
};
6 changes: 3 additions & 3 deletions packages/tanstack-start/src/server/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export const loadOptions = (request: Request, overrides: LoaderOptions = {}) =>
const secretKey = overrides.secretKey || commonEnv.SECRET_KEY;
const publishableKey = overrides.publishableKey || commonEnv.PUBLISHABLE_KEY;
const jwtKey = overrides.jwtKey || commonEnv.CLERK_JWT_KEY;
const apiUrl = getEnvVariable('CLERK_API_URL', event) || apiUrlFromPublishableKey(publishableKey);
const apiUrl = getEnvVariable('CLERK_API_URL', event.context) || apiUrlFromPublishableKey(publishableKey);
const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || commonEnv.DOMAIN;
const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || commonEnv.IS_SATELLITE;
const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, commonEnv.PROXY_URL);
const signInUrl = overrides.signInUrl || commonEnv.SIGN_IN_URL;
const signUpUrl = overrides.signUpUrl || commonEnv.SIGN_UP_URL;
const afterSignInUrl = overrides.afterSignInUrl || getPublicEnvVariables(event).afterSignInUrl;
const afterSignUpUrl = overrides.afterSignUpUrl || getPublicEnvVariables(event).afterSignUpUrl;
const afterSignInUrl = overrides.afterSignInUrl || getPublicEnvVariables(event.context).afterSignInUrl;
const afterSignUpUrl = overrides.afterSignUpUrl || getPublicEnvVariables(event.context).afterSignUpUrl;

let proxyUrl;
if (!!relativeOrAbsoluteProxyUrl && isProxyUrlRelative(relativeOrAbsoluteProxyUrl)) {
Expand Down
26 changes: 18 additions & 8 deletions packages/tanstack-start/src/server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RequestState } from '@clerk/backend/internal';
import { debugRequestState } from '@clerk/backend/internal';
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
import { isTruthy } from '@clerk/shared/underscore';
import { getEvent } from 'vinxi/http';

import type { AdditionalStateOptions } from '../types';

Expand All @@ -22,6 +23,7 @@ export const wrapWithClerkState = (data: any) => {
*/
export function getResponseClerkState(requestState: RequestState, additionalStateOptions: AdditionalStateOptions = {}) {
const { reason, message, isSignedIn, ...rest } = requestState;
const event = getEvent();

const clerkInitialState = wrapWithClerkState({
__clerk_ssr_state: rest.toAuth(),
Expand All @@ -34,18 +36,26 @@ export function getResponseClerkState(requestState: RequestState, additionalStat
__afterSignInUrl: requestState.afterSignInUrl,
__afterSignUpUrl: requestState.afterSignUpUrl,
__clerk_debug: debugRequestState(requestState),
__clerkJSUrl: getEnvVariable('CLERK_JS'),
__clerkJSVersion: getEnvVariable('CLERK_JS_VERSION'),
__telemetryDisabled: isTruthy(getEnvVariable('CLERK_TELEMETRY_DISABLED')),
__telemetryDebug: isTruthy(getEnvVariable('CLERK_TELEMETRY_DEBUG')),
__clerkJSUrl: getEnvVariable('CLERK_JS', event.context),
__clerkJSVersion: getEnvVariable('CLERK_JS_VERSION', event.context),
__telemetryDisabled: isTruthy(getEnvVariable('CLERK_TELEMETRY_DISABLED', event.context)),
__telemetryDebug: isTruthy(getEnvVariable('CLERK_TELEMETRY_DEBUG', event.context)),
__signInForceRedirectUrl:
additionalStateOptions.signInForceRedirectUrl || getEnvVariable('CLERK_SIGN_IN_FORCE_REDIRECT_URL') || '',
additionalStateOptions.signInForceRedirectUrl ||
getEnvVariable('CLERK_SIGN_IN_FORCE_REDIRECT_URL', event.context) ||
'',
__signUpForceRedirectUrl:
additionalStateOptions.signUpForceRedirectUrl || getEnvVariable('CLERK_SIGN_UP_FORCE_REDIRECT_URL') || '',
additionalStateOptions.signUpForceRedirectUrl ||
getEnvVariable('CLERK_SIGN_UP_FORCE_REDIRECT_URL', event.context) ||
'',
__signInFallbackRedirectUrl:
additionalStateOptions.signInFallbackRedirectUrl || getEnvVariable('CLERK_SIGN_IN_FALLBACK_REDIRECT_URL') || '',
additionalStateOptions.signInFallbackRedirectUrl ||
getEnvVariable('CLERK_SIGN_IN_FALLBACK_REDIRECT_URL', event.context) ||
'',
__signUpFallbackRedirectUrl:
additionalStateOptions.signUpFallbackRedirectUrl || getEnvVariable('CLERK_SIGN_UP_FALLBACK_REDIRECT_URL') || '',
additionalStateOptions.signUpFallbackRedirectUrl ||
getEnvVariable('CLERK_SIGN_UP_FALLBACK_REDIRECT_URL', event.context) ||
'',
});

return {
Expand Down
6 changes: 3 additions & 3 deletions packages/tanstack-start/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
import { isTruthy } from '@clerk/shared/underscore';
import type { HTTPEvent } from 'vinxi/http';
import type { H3EventContext } from 'vinxi/http';

export const getPublicEnvVariables = (event?: HTTPEvent) => {
export const getPublicEnvVariables = (context?: H3EventContext) => {
const getValue = (name: string): string => {
return getEnvVariable(`VITE_${name}`, event) || getEnvVariable(name, event);
return getEnvVariable(`VITE_${name}`, context) || getEnvVariable(name, context);
};

return {
Expand Down

0 comments on commit f1ce19b

Please sign in to comment.