Comments (5)
We can't generate types for environment variables because they are not globally unique - they're scoped to the specific target they're attached to.
it's a bit challenging to try and collect all the environment variables from everywhere to produce a unique list of variables that may be available
a work around is to manually define them the same way sst-env.d.ts defines them
from ion.
That's fair, then again you can access a resource on the type level even if it's not scoped to the target you're currently in and it will throw at runtime. I guess the same can be true here, but I agree it only really works if you're using the same env vars in all your functions (which I currently am).
from ion.
this might not be what you really want but you could do new sst.Secret("Name", value)
and link that to all your functions if you want the typesafety
from ion.
The secrets approach works until two functions need the same env var but with different values (I have a sentry helper function which grabs the dsn from the env instead of being passed as an argument). I was using define
in the esbuild config as build-time vars, but I also needed to do the same when bundling the workers for testing so defining it in the sst config seemed best. Given my use-case, the following works well since all my workers will need the same env vars:
/// <reference path="./../../sst-env.d.ts" />
declare module "sst" {
export interface Resource {
SentryRelease: string;
SentryDsn: string;
AppName: string;
FunctionOrigin: string;
}
}
export type {};
from ion.
Just had a play around and have come up with a solutio to achieve better scoping of env vars in a monorepo setup. My setup is an infra
package with the following package.json exports:
"exports": {
".": "./src/index.d.ts",
"./env/cloudflare": "./src/env/cloudflare.d.ts",
"./env/sentry": "./src/env/sentry.d.ts",
},
My ./src/index.d.ts
is simply:
/// <reference path="./../../sst-env.d.ts" />
declare module "sst" {
export interface Resource {}
}
export type {};
Which just gives me the original generated types, then in src/env/sentry.d.ts
, I only include sentry specific env vars:
/// <reference path="./../../sst-env.d.ts" />
declare module "sst" {
export interface Resource {
SentryRelease: string;
SentryDsn: string;
}
}
export type {};
This way, in packages that have a target which will use these env vars, you only need to include the specific export in your tsconfig:
{
"compilerOptions": {
"types": [
"@repo/infra",
"@repo/infra/env/sentry"
]
},
}
This way, you can scope env vars only to where they will be defined. Keep in mind, this approach falls apart if you have multiple targets in the same package who do not share all env vars. I think I prefer this approach rather than requiring sst to generate them
from ion.
Related Issues (20)
- Cloudflare bucket resource types missing HOT 2
- npm: `npx sst upgrade` does not work HOT 4
- Allow getting all nodes of a Postgres construct when using .get()
- Updating SST package HOT 16
- Queue + Function - Batch Size Config HOT 1
- Custom lambda function name not reflected in log group name
- sst.vercel.dns fails to set apex domain record
- Docs: Add a monorepo how to doc
- AccessDenied S3 Bucket
- SST Console unable to show issues due to missing permissions
- Deploy of Nextjs app creates CloudFront Distribution, subsequent sst dev deletes it
- `sst dev` ignores architecture config for lambdas HOT 1
- Docs: IAM permissions
- Error: [Secret variable] is not linked
- Cloudfalre Astro
- document new sst dev
- No support for partial failures on Queue handlers
- update quickstarts with multiplexer
- Add support for concurrent dev instances for separate projects
- Remix polyfill breaks certain streams
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ion.