Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX]:285 - Type proccess.env based off the available secrets in a no… #333

Merged
merged 19 commits into from
Oct 14, 2024

Conversation

BeRecursive22
Copy link
Contributor

…tebook

Dynamic Environment Type Declarations for Srcbooks

Approach

Initial Creation:

When creating a new srcbook, an empty environment type declaration file (env.d.ts) is generated under /src.
This file initially assumes all environment secrets are disabled.

Dynamic Updates:

Upon user actions (Add/Enable/Disable/Delete secrets), the system:
a. Retrieves the updated secrets available for the srcbook.
b. Dynamically generates a new env.d.ts file.
c. Writes the updated file to disk under the /src directory.

Type Safety:

All available environment secrets for a srcbook are typed in the ProcessEnv interface.
This enables TSServer to throw type errors when a user attempts to access non-existent environment secrets.

Limitations

This feature is not retroactively applicable to srcbooks created prior to this feature release.

Some assumptions:

  • Since env secrets are not created/updated frequently, I chose to write the file instead of reading and appending the types.
  • All the env secrets are disabled initially when a new srcbook is created

@BeRecursive22
Copy link
Contributor Author

BeRecursive22 commented Oct 6, 2024

fixes #285

@nichochar @benjreinhart I'd like to know your feedback on this approach and whether we can go ahead with this or not.

Type error on hover:

image

Error in console:
image

Suggestions:

image

if (!Object.entries(sessionSecrets).length) return;
const envTypeDeclarationFileContent = generateEnvTypesFile(sessionSecrets);
const srcbookDir = pathToSrcbook(sessionId);
const envDtsPath = Path.join(srcbookDir, 'src', 'env.d.ts');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit but i would put this in the repo root. I think that's where I've typically seen these global envs.

@@ -0,0 +1,16 @@
// vite.config.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file doesn't look like it should be checked in. Can we remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

@nichochar
Copy link
Contributor

Neat, thanks @BeRecursive22 !

@nichochar nichochar merged commit c3e857b into srcbookdev:main Oct 14, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants