Cursor rule
.cursor/rules/env-vars.mdcHow to add env vas or environmental variables to the app.
Cursor rules
Quality
63/100
Scores the file, not the repository.Length
640 words
0 headings · 7 code blocksRepository
0
— · pushed 396 days agoLast changed
3 days ago
First indexed 3 days ago.123456**Environment Variables (Astro Way)**: Astro provides type-safe environment variables. Follow these steps:78 **1. Define in astro.config.ts:**9```typescript10 // astro.config.ts11 env: {12 schema: {13 MY_ENV_VAR: envField.string({14 context: 'server', // 'server' or 'client'15 access: 'secret', // 'secret' or 'public'16 optional: false, // true if the variable is optional17 }),18 PUBLIC_API_URL: envField.string({19 context: 'client',20 access: 'public',21 optional: false,22 }),23 },24 },25```2627 **2. Import and use in your code:**28```typescript29 // Server-side variables (access: 'secret', context: 'server')30 import { DATABASE_URL, MAILGUN_WEBHOOK_SIGNING_KEY } from 'astro:env/server'3132 // Client-side variables (access: 'public', context: 'client')33 import { LIVEKIT_API_KEY } from 'astro:env/client'34```3536 **Key guidelines:**37 - **Server variables** (`context: 'server'`): Only accessible in server-side code, import from `astro:env/server`38 - **Client variables** (`context: 'client'`): Accessible in both client and server code, import from `astro:env/client`39 - **Secret variables** (`access: 'secret'`): Should never be exposed to the client40 - **Public variables** (`access: 'public'`): Can be safely exposed to the client41 - All environment variables are type-safe and validated at build time42 - Ignore linter errors on recently created env vars. Astro needs to rerun its dev server to update its internal types references first.4344 **Example usage:**45```typescript46 // apps/web/src/server/db.ts47 import { setupDb } from '@app/db'48 import { DATABASE_URL } from 'astro:env/server'4950 export const db = setupDb({51 connectionString: DATABASE_URL,52 })53```545556**Passing Environment Variables to tRPC**: To make environment variables available in tRPC procedures, follow this pattern:5758 **1. Define the Env interface in context.ts:**59```typescript60 // packages/api/src/context.ts61 export interface Env {62 appEndpoint: string63 gcsProjectId: string64 ...65 }6667 export interface Context {68 db: Kysely<DB>69 env: Env70 session: Session | null71 user: SelectableUser | null72 }73```7475 **2. Pass environment variables in the tRPC handler:**76```typescript77 // apps/web/src/pages/api/trpc/[trpc].ts78 import { LIVEKIT_API_KEY } from 'astro:env/client'79 import {80 GCS_BUCKET,81 GCS_CLIENT_EMAIL,82 GCS_PRIVATE_KEY,83 GCS_PROJECT_ID,84 GEMINI_API_KEY,85 LIVEKIT_API_SECRET,86 LIVEKIT_URL,87 PERPLEXITY_API_KEY,88 } from 'astro:env/server'8990 async function createContext({ req }: CreateContextOptions): Promise<Context> {91 const env: Env = {92 appEndpoint: getAppEndpoint(req),93 gcsProjectId: GCS_PROJECT_ID,94 ...95 }9697 return { db, session: session ?? null, env, user }98 }99```100101 **3. Access environment variables in tRPC procedures:**102```typescript103 // In any tRPC procedure104 export const myProcedure = protectedProcedure105 .input(z.object({ /* ... */ }))106 .mutation(async ({ ctx, input }) => {107 // Access env vars through ctx.env108 const gcsProjectId = ctx.env.gcsProjectId109 // ... use the environment variables110 })111```112113 **Key guidelines for tRPC env vars:**114 - Always define new env vars in the `Env` interface in `context.ts`115 - Import env vars from Astro's env system in the tRPC handler116 - Map them to the `env` object in `createContext`117 - Access them via `ctx.env` in any tRPC procedure118 - This ensures type safety and centralized env var management119120**Build and CI Configuration**: When adding new environment variables, you must also update build and CI configuration files:121122 **1. Update turbo.json:**123 Add new environment variables to the `env` array under the `build` task so Turborepo can properly cache builds:124```json125 // turbo.json126 {127 "tasks": {128 "build": {129 "outputs": ["dist/**"],130 "dependsOn": ["^build"],131 "env": [132 "DATABASE_URL",133 "AUTH_SECRET",134 "MY_NEW_ENV_VAR", // Add your new env var here135 // ... other env vars136 ]137 }138 }139 }140```141142 **2. Update GitHub Actions (if needed):**143 If your environment variable needs to be available during CI/CD builds or tests, ensure it's properly configured in your GitHub Actions workflows. This may involve:144 - Adding the env var to repository secrets (for sensitive values)145 - Setting the env var in workflow files (for public values)146 - Updating setup actions if they need access to the env var147148 **Key guidelines for build/CI config:**149 - **Always** add new env vars to `turbo.json` if they affect build output150 - Only add sensitive env vars to GitHub repository secrets, never commit them to workflow files151 - Test your builds locally with the new env vars before pushing152 - Consider whether the env var is needed at build time vs runtime153 - Document any new env vars in your project's README or deployment docs154
Also in poglesbyg/htsf-consultant
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| poglesbyg/htsf-consultant.cursor/rules/always.mdc · 0 | Cursor rules | test | 35/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/code-conventions.mdc · 0 | Cursor rules | setuplint-formatstyledo-not+1 | 80/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/crispr-algorithms.mdc · 0 | Cursor rules | deployment | 44/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/data-models.mdc · 0 | Cursor rules | typessecuritydatabase | 44/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/database.mdc · 0 | Cursor rules | typesdatabasedo-not | 61/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/react.mdc · 0 | Cursor rules | style | 34/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/shadcn-components.mdc · 0 | Cursor rules | ui | 63/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/smart-polling.mdc · 0 | Cursor rules | no sections | 44/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/trpc-architecture.mdc · 0 | Cursor rules | archtypes | 56/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/trpc.mdc · 0 | Cursor rules | no sections | 40/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursor/rules/typescript.mdc · 0 | Cursor rules | styletypesdo-not | 55/100 | 3 days ago | |
| poglesbyg/htsf-consultant.cursorrules · 0 | .cursorrules | archdeploymentagent-behaviour | 48/100 | 3 days ago |
Diff against .cursor/rules/always.mdc Diff against .cursor/rules/code-conventions.mdc Diff against .cursor/rules/crispr-algorithms.mdc Diff against .cursor/rules/data-models.mdc Diff against .cursor/rules/database.mdc Diff against .cursor/rules/react.mdc Diff against .cursor/rules/shadcn-components.mdc Diff against .cursor/rules/smart-polling.mdc Diff against .cursor/rules/trpc-architecture.mdc Diff against .cursor/rules/trpc.mdc Diff against .cursor/rules/typescript.mdc Diff against .cursorrules
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 45 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 3 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 3 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 45 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 3 days ago |
