Skip to content

Commit

Permalink
Merge pull request #100 from thomasKn/env
Browse files Browse the repository at this point in the history
Add check env function
  • Loading branch information
thomasKn committed Feb 25, 2024
2 parents 8da63d8 + 8bbecc6 commit d1e6b34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PUBLIC_STORE_DOMAIN="headless-journey.myshopify.com"
PUBLIC_STOREFRONT_API_TOKEN="2cb9e48134803a2f8331ed6bd6304ebb"
PRIVATE_STOREFRONT_API_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
PUBLIC_STOREFRONT_ID="13526"
PUBLIC_STOREFRONT_API_VERSION="2023-10"
PUBLIC_STOREFRONT_API_VERSION="2024-01"
# Sanity
SANITY_STUDIO_PROJECT_ID="r29ig4oy"
SANITY_STUDIO_DATASET="production"
Expand Down
24 changes: 24 additions & 0 deletions app/lib/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export function envVariables(contextEnv: Env) {
env = process.env;
}

checkEnv(env as Env);

return {
NODE_ENV: env.NODE_ENV,
PRIVATE_STOREFRONT_API_TOKEN: env.PRIVATE_STOREFRONT_API_TOKEN,
Expand All @@ -25,3 +27,25 @@ export function envVariables(contextEnv: Env) {
SANITY_STUDIO_USE_STEGA: env.SANITY_STUDIO_USE_STEGA,
};
}

function checkEnv(env: Env) {
const requiredVariables: (keyof Env)[] = [
'PUBLIC_STORE_DOMAIN',
'PUBLIC_STOREFRONT_API_TOKEN',
'PUBLIC_STOREFRONT_API_VERSION',
'PRIVATE_STOREFRONT_API_TOKEN',
'SESSION_SECRET',
'SANITY_STUDIO_API_VERSION',
'SANITY_STUDIO_PROJECT_ID',
'SANITY_STUDIO_DATASET',
'SANITY_STUDIO_URL',
] as const;

for (const requiredEnv of requiredVariables) {
if (!env[requiredEnv]) {
throw new Error(
`Missing environment variable => ${requiredEnv} is not set`,
);
}
}
}
21 changes: 12 additions & 9 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ export default {
executionContext: ExecutionContext,
): Promise<Response> {
try {
/*
* Open a cache instance in the worker and a custom session instance.
*/
if (!env.SESSION_SECRET) {
throw new Error('SESSION_SECRET environment variable is not set');
}

const envVars = envVariables(env);
const isDev = envVars.NODE_ENV === 'development';
const origin = new URL(request.url).origin;
const locale = getLocaleFromRequest(request);
const waitUntil = executionContext.waitUntil.bind(executionContext);

/*
* Open a cache instance in the worker and a custom session instance.
*/
const [cache, session, sanitySession] = await Promise.all([
caches.open('hydrogen'),
HydrogenSession.init(request, [env.SESSION_SECRET]),
Expand Down Expand Up @@ -125,9 +121,16 @@ export default {

return response;
} catch (error) {
const errorString = error instanceof Error ? error.toString() : '';
let message = 'An unexpected error occurred';

if (errorString.includes('Missing environment variable')) {
message = 'Missing environment variable';
}

// eslint-disable-next-line no-console
console.error(error);
return new Response('An unexpected error occurred', {status: 500});
return new Response(message, {status: 500});
}
},
};

0 comments on commit d1e6b34

Please sign in to comment.