|
1 | 1 | /** |
2 | | - * Centralized Environment Configuration for Cloudflare Workers |
| 2 | + * Centralized Environment Configuration |
3 | 3 | * |
4 | 4 | * This module provides type-safe access to environment variables/secrets. |
5 | 5 | * |
6 | | - * ## How it works: |
7 | | - * |
8 | | - * Both local development and production use the same access pattern: |
9 | | - * `import { env } from 'cloudflare:workers'` |
10 | | - * |
11 | | - * - **Production**: Secrets are set via `wrangler secret put` in CI/CD |
12 | | - * - **Local Dev**: Secrets are loaded from `.dev.vars` by Miniflare |
13 | | - * |
14 | | - * ## Required files: |
15 | | - * |
16 | | - * - `.dev.vars` - Local development secrets (DO NOT COMMIT) |
17 | | - * ``` |
18 | | - * DISCOGS_CONSUMER_KEY=your_key |
19 | | - * DISCOGS_CONSUMER_SECRET=your_secret |
20 | | - * ``` |
21 | | - * |
22 | | - * - `wrangler.toml` - Non-sensitive env vars in [vars] section |
23 | | - * |
24 | | - * @see https://developers.cloudflare.com/workers/configuration/secrets/ |
25 | | - * @see https://developers.cloudflare.com/workers/local-development/environment-variables/ |
| 6 | + * Adapted for Netlify/Node.js environment. |
26 | 7 | */ |
27 | 8 |
|
28 | | -import { env as cloudflareEnv } from 'cloudflare:workers'; |
29 | | - |
30 | 9 | /** |
31 | | - * Type definition for Cloudflare environment bindings. |
32 | | - * Add new secrets/vars here as they are added to the project. |
33 | | - * |
34 | | - * To generate types automatically, run: `nr cf-typegen` |
| 10 | + * Type definition for Environment variables. |
35 | 11 | */ |
36 | 12 | export interface CloudflareEnv { |
37 | 13 | // Discogs OAuth credentials |
38 | 14 | DISCOGS_CONSUMER_KEY?: string; |
39 | 15 | DISCOGS_CONSUMER_SECRET?: string; |
40 | 16 |
|
41 | | - // Environment identifier (set in wrangler.toml [env.*.vars]) |
| 17 | + // Environment identifier |
42 | 18 | ENVIRONMENT?: 'development' | 'staging' | 'production'; |
43 | 19 | } |
44 | 20 |
|
45 | 21 | /** |
46 | | - * Get the typed Cloudflare environment object. |
47 | | - * |
48 | | - * @returns The environment bindings from Cloudflare Workers runtime |
| 22 | + * Get the environment object. |
| 23 | + * In Node.js/Netlify, this comes from process.env |
49 | 24 | */ |
50 | 25 | export function getCloudflareEnv(): CloudflareEnv { |
51 | | - return cloudflareEnv as CloudflareEnv; |
| 26 | + return (typeof process !== 'undefined' ? process.env : {}) as unknown as CloudflareEnv; |
52 | 27 | } |
53 | 28 |
|
54 | 29 | /** |
@@ -93,5 +68,5 @@ export function hasDiscogsCredentials(): boolean { |
93 | 68 | * @returns 'development', 'staging', or 'production' |
94 | 69 | */ |
95 | 70 | export function getEnvironment(): CloudflareEnv['ENVIRONMENT'] { |
96 | | - return getEnvVar('ENVIRONMENT') as CloudflareEnv['ENVIRONMENT']; |
| 71 | + return (getEnvVar('ENVIRONMENT') as CloudflareEnv['ENVIRONMENT']) || 'development'; |
97 | 72 | } |
0 commit comments