Coder Social home page Coder Social logo

Comments (5)

thdxr avatar thdxr commented on July 26, 2024

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.

Ernxst avatar Ernxst commented on July 26, 2024

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.

thdxr avatar thdxr commented on July 26, 2024

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.

Ernxst avatar Ernxst commented on July 26, 2024

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.

Ernxst avatar Ernxst commented on July 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.