Adrián Mouly
10/31/2021, 1:54 AMenvironment: {
MY_VAR: 'my value'
}
And then in the function implementation..
var MY_VAR = process.env.MY_VAR;
I’m looking for a way to declare the MY_VAR
definition in a single place, that can avoid any possible renaming issue, or whatever.
Also would like to avoid developers to “read” environment variables from random places in the code.
I would like to know easily, which are the implemented “inputs” from the environment.thdxr
10/31/2021, 1:57 AMdeclare global {
namespace NodeJS {
interface ProcessEnv {
eventBusName: string
}
}
}
thdxr
10/31/2021, 1:57 AMAdrián Mouly
10/31/2021, 1:57 AMthdxr
10/31/2021, 1:57 AMConfig
module that is strongly typed that exports a constant object loaded from SSMthdxr
10/31/2021, 1:58 AMAdrián Mouly
10/31/2021, 1:58 AMAdrián Mouly
10/31/2021, 1:58 AMAdrián Mouly
10/31/2021, 1:58 AMAdrián Mouly
10/31/2021, 1:59 AMAdrián Mouly
10/31/2021, 1:59 AMAdrián Mouly
10/31/2021, 1:59 AMAdrián Mouly
10/31/2021, 2:00 AMthdxr
10/31/2021, 2:01 AMthdxr
10/31/2021, 2:01 AMthdxr
10/31/2021, 2:01 AMimport { SSM } from "aws-sdk"
import deasync from "deasync"
const ssm = new SSM()
export interface ConfigType {
STAGE: string
BUS_NAME: string
STRIPE_KEY: string
USER_POOL_ID: string
SLACK_WEBHOOK_URL: string
}
export const Config: ConfigType = {} as any
function refresh(cb: any) {
const time = Date.now()
const { SSM_PATH, APP_NAME } = process.env
if (!SSM_PATH) throw new Error("SSM_PATH is not defined")
if (!APP_NAME) throw new Error("APP_NAME is not defined")
function get(next?: string) {
ssm.getParametersByPath(
{
Path: SSM_PATH!,
Recursive: true,
NextToken: next,
},
(err, result) => {
if (err) throw err
const values = result.Parameters!.reduce((collect, item) => {
if (!item.Name!.includes("ship") && !item.Name!.includes(APP_NAME!))
return collect
const splits = item.Name!.split("/")
const key = splits.pop()!
collect[key] = item.Value!
return collect
}, {} as Record<string, string>)
Object.assign(Config, values)
if (result.NextToken) {
get(result.NextToken)
return
}
cb()
}
)
}
get(undefined)
}
deasync(refresh)()
thdxr
10/31/2021, 2:02 AM.d.ts
filethdxr
10/31/2021, 2:02 AMimport { ConfigType } from "@ironbay/ship"
declare module "@ironbay/ship" {
interface ConfigType {
MYSQL_HOST: string
MYSQL_USER: string
MYSQL_PASSWORD: string
MYSQL_SSL: string
APPSYNC_URL: string
APPSYNC_KEY: string
}
}
thdxr
10/31/2021, 2:02 AMdeasync
lol to freeze the node process until the call to SSM is doneAdrián Mouly
10/31/2021, 2:03 AM