File tree Expand file tree Collapse file tree 4 files changed +21
-17
lines changed
Expand file tree Collapse file tree 4 files changed +21
-17
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,9 @@ export function loadEnv (
2626 fromWorkerEnv : boolean
2727) : env {
2828 const Env :env = {
29+ //if `fromWokerEnv` is true, then load from the `env` passed as argument to the function,
30+ // else if `fromWokerEnv` is false, load from process.env
31+ //(or set to '' if value from process.env is undefined) to avoid Error TS2322
2932 CURRENT_ENVIRONMENT : fromWorkerEnv ? env . CURRENT_ENVIRONMENT : process . env . CURRENT_ENVIRONMENT || '' ,
3033 DISCORD_APPLICATION_ID : fromWorkerEnv ? env . DISCORD_APPLICATION_ID : process . env . DISCORD_APPLICATION_ID || '' ,
3134 DISCORD_GUILD_ID : fromWorkerEnv ? env . DISCORD_GUILD_ID : process . env . DISCORD_GUILD_ID || '' ,
Original file line number Diff line number Diff line change @@ -4,19 +4,19 @@ import { loadEnv } from "./config";
44validateEnv ( ) ;
55
66/**
7- * Validate if all the required environment variables defined in the schema above are set
8- * and are in the correct format else throw an error
7+ * Validate if all the required environment variables are set to a non empty value
8+ * else throw an error
99 * ---
1010 */
1111export function validateEnv ( ) {
12- const envLoadedFromProcess : env = loadEnv ( { } , false ) ;
13- console . log ( envLoadedFromProcess ) ;
14- const missingEnvVars = Object . keys ( envLoadedFromProcess ) . filter ( ( key ) => envLoadedFromProcess [ key ] == '' ) ;
12+ //pass empty object as env and fromWorkerEnv = false, since this method is should get executed in github actions and not in worker
13+ const envLoadedFromProcess : env = loadEnv ( { } , false ) ;
14+ const missingEnvVars = Object . keys ( envLoadedFromProcess ) . filter ( ( key ) => envLoadedFromProcess [ key ] == '' ) ;
1515
16- // Logging missing environment variables and exit if any are missing
17- if ( missingEnvVars . length > 0 ) {
18- throw new Error ( `Missing environment variables: ${ missingEnvVars . join ( ', ' ) } ` ) ;
19- } else {
20- console . log ( 'All required environment variables are set.' ) ;
21- }
16+ // Logging missing environment variables and throw error if any are missing
17+ if ( missingEnvVars . length > 0 ) {
18+ throw new Error ( `Missing environment variables: ${ missingEnvVars . join ( ', ' ) } ` ) ;
19+ } else {
20+ console . log ( 'All required environment variables are set.' ) ;
21+ }
2222}
Original file line number Diff line number Diff line change 3636 "ts-jest" : " ^29.0.5" ,
3737 "ts-node" : " ^10.9.1" ,
3838 "typescript" : " ^4.9.4" ,
39- "wrangler" : " ^3.1.1" ,
40- "zod" : " ^3.23.8"
39+ "wrangler" : " ^3.1.1"
4140 },
4241 "dependencies" : {
4342 "@tsndr/cloudflare-worker-jwt" : " ^2.2.1" ,
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ import { generateInviteLink } from "./controllers/generateDiscordInvite";
2222import { sendProfileBlockedMessage } from "./controllers/profileHandler" ;
2323import { sendTaskUpdatesHandler } from "./controllers/taskUpdatesHandler" ;
2424
25- import config from "./../config/config" ;
26- import { loadEnv } from "./../config/config" ;
25+ import config , { loadEnv } from "./../config/config" ;
26+
2727const router = Router ( ) ;
2828
2929router . get ( "/" , async ( ) => {
@@ -107,10 +107,12 @@ export default {
107107 return new JSONResponse ( response . BAD_SIGNATURE , { status : 401 } ) ;
108108 }
109109 }
110- return router . handle ( request , loadEnv ( env , true ) , ctx ) ;
110+ const envLoadedFromWorker : env = loadEnv ( env , true ) ;
111+ return router . handle ( request , envLoadedFromWorker , ctx ) ;
111112 } ,
112113
113114 async scheduled ( req : Request , env : env , ctx : ExecutionContext ) {
114- ctx . waitUntil ( send ( loadEnv ( env , true ) ) ) ;
115+ const envLoadedFromWorker : env = loadEnv ( env , true ) ;
116+ ctx . waitUntil ( send ( envLoadedFromWorker ) ) ;
115117 } ,
116118} ;
You can’t perform that action at this time.
0 commit comments