@neondatabase/env 0.15.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +21 -29
  2. package/dist/cli.js +971 -6
  3. package/dist/cli.js.map +1 -1
  4. package/dist/{lib/env.js → env.js} +70 -219
  5. package/dist/env.js.map +1 -0
  6. package/dist/index.d.ts +528 -2
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +197 -1
  9. package/dist/index.js.map +1 -0
  10. package/package.json +11 -14
  11. package/dist/_shared/auth_selection.js +0 -76
  12. package/dist/_shared/auth_selection.js.map +0 -1
  13. package/dist/_shared/credentials.js +0 -131
  14. package/dist/_shared/credentials.js.map +0 -1
  15. package/dist/_shared/paths.js +0 -131
  16. package/dist/_shared/paths.js.map +0 -1
  17. package/dist/_shared/profiles.d.ts +0 -7
  18. package/dist/_shared/profiles.d.ts.map +0 -1
  19. package/dist/_shared/profiles.js +0 -124
  20. package/dist/_shared/profiles.js.map +0 -1
  21. package/dist/config/dist/lib/define-config.d.ts +0 -20
  22. package/dist/config/dist/lib/define-config.d.ts.map +0 -1
  23. package/dist/config/dist/lib/neon-api.d.ts +0 -375
  24. package/dist/config/dist/lib/neon-api.d.ts.map +0 -1
  25. package/dist/config/dist/lib/types.d.ts +0 -544
  26. package/dist/config/dist/lib/types.d.ts.map +0 -1
  27. package/dist/config/dist/v1.d.ts +0 -5
  28. package/dist/lib/cli/commands.d.ts +0 -68
  29. package/dist/lib/cli/commands.d.ts.map +0 -1
  30. package/dist/lib/cli/commands.js +0 -233
  31. package/dist/lib/cli/commands.js.map +0 -1
  32. package/dist/lib/cli/resolve-api-key.d.ts +0 -29
  33. package/dist/lib/cli/resolve-api-key.d.ts.map +0 -1
  34. package/dist/lib/cli/resolve-api-key.js +0 -74
  35. package/dist/lib/cli/resolve-api-key.js.map +0 -1
  36. package/dist/lib/cli/resolve-context.d.ts +0 -34
  37. package/dist/lib/cli/resolve-context.d.ts.map +0 -1
  38. package/dist/lib/cli/resolve-context.js +0 -88
  39. package/dist/lib/cli/resolve-context.js.map +0 -1
  40. package/dist/lib/env.d.ts +0 -509
  41. package/dist/lib/env.d.ts.map +0 -1
  42. package/dist/lib/env.js.map +0 -1
  43. package/dist/lib/reuse-secrets.d.ts +0 -95
  44. package/dist/lib/reuse-secrets.d.ts.map +0 -1
  45. package/dist/lib/reuse-secrets.js +0 -181
  46. package/dist/lib/reuse-secrets.js.map +0 -1
  47. package/dist/runtime.d.ts +0 -2
  48. package/dist/runtime.js +0 -2
package/dist/lib/env.d.ts DELETED
@@ -1,509 +0,0 @@
1
- import { Config, CredentialScope, ResolvedPreviewConfig } from "../config/dist/lib/types.js";
2
- import { NeonApi, NeonBranchSnapshot } from "../config/dist/lib/neon-api.js";
3
- import { resolveConfig } from "../config/dist/lib/define-config.js";
4
- import "../config/dist/v1.js";
5
-
6
- //#region src/lib/env.d.ts
7
- declare const NEON_ENV_VAR_KEYS: {
8
- /**
9
- * Branch identity. `NEON_BRANCH` carries the branch **name** and is injected into the
10
- * Neon Functions runtime on every branch (including the default) by default. `env pull` /
11
- * `neon dev` / `neon-env run` emit it too so local dev mirrors the deployed runtime.
12
- */
13
- readonly branch: {
14
- readonly name: "NEON_BRANCH";
15
- };
16
- readonly postgres: {
17
- readonly databaseUrl: "DATABASE_URL";
18
- readonly databaseUrlUnpooled: "DATABASE_URL_UNPOOLED";
19
- };
20
- readonly auth: {
21
- readonly baseUrl: "NEON_AUTH_BASE_URL";
22
- readonly jwksUrl: "NEON_AUTH_JWKS_URL";
23
- };
24
- readonly dataApi: {
25
- readonly url: "NEON_DATA_API_URL";
26
- };
27
- /**
28
- * Object storage (Preview). The S3 SDKs read `AWS_*` from their standard config chain, so
29
- * a branch credential + `neon dev` / `env pull` makes object storage work from env alone.
30
- * `region` is injected under the SDK-standard `AWS_REGION`.
31
- */
32
- readonly storage: {
33
- readonly accessKeyId: "AWS_ACCESS_KEY_ID";
34
- readonly secretAccessKey: "AWS_SECRET_ACCESS_KEY";
35
- readonly endpoint: "AWS_ENDPOINT_URL_S3";
36
- readonly region: "AWS_REGION";
37
- };
38
- /**
39
- * AI Gateway (Preview). Exposed under the Neon-branded env vars the deployed Functions
40
- * runtime injects: `apiKey` is the minted credential's bearer (`NEON_AI_GATEWAY_TOKEN`)
41
- * and `baseUrl` is the bare branch gateway host (`NEON_AI_GATEWAY_BASE_URL`,
42
- * `scheme://host`, no path). Clients like `@neon/ai-sdk-provider` read these and append the
43
- * dialect route (`/v1`, `/openai/v1`, `/anthropic/v1`) themselves (https://github.com/vercel/ai/pull/15997).
44
- */
45
- readonly aiGateway: {
46
- readonly apiKey: "NEON_AI_GATEWAY_TOKEN";
47
- readonly baseUrl: "NEON_AI_GATEWAY_BASE_URL";
48
- };
49
- };
50
- /**
51
- * Branch identity for the resolved branch. Always present on a `fetchEnv` result (the branch
52
- * name is always known); on a `parseEnv` result it's present only when `NEON_BRANCH` was
53
- * injected into `process.env` (the Functions runtime injects it by default, as do `neon dev` /
54
- * `neon-env run` / `env pull`). `name` is the branch **name** (e.g. `main`, `preview/foo`).
55
- */
56
- interface NeonBranchEnv {
57
- name: string;
58
- }
59
- /** Per-namespace inner shapes. Exposed so consumers can name the parts independently. */
60
- interface NeonPostgresEnv {
61
- /**
62
- * Pooled connection string (via Neon's PgBouncer pooler). The right default for
63
- * serverless drivers (`@neondatabase/serverless`, edge runtimes, Postgres.js, …).
64
- */
65
- databaseUrl: string;
66
- /**
67
- * Direct (unpooled) connection string. Use this when you need session-level
68
- * features (`LISTEN`/`NOTIFY`, prepared statements across calls, transactions
69
- * spanning round-trips) that PgBouncer's transaction-mode pooling drops.
70
- */
71
- databaseUrlUnpooled: string;
72
- }
73
- /**
74
- * Bits of a Neon Auth integration for the resolved branch. Only present on `NeonEnv`
75
- * when the branch policy enables `auth`.
76
- *
77
- * Neon Auth exposes the `baseUrl` (which doubles as the publishable client identifier) and
78
- * the `jwksUrl` used to verify tokens it issues. `fetchEnv` reads both from the live
79
- * integration; `parseEnv` reads them from `process.env` (`NEON_AUTH_BASE_URL` /
80
- * `NEON_AUTH_JWKS_URL`).
81
- */
82
- interface NeonAuthEnv {
83
- baseUrl: string;
84
- /** JWKS URL for verifying tokens issued by Neon Auth (`NEON_AUTH_JWKS_URL`). */
85
- jwksUrl: string;
86
- }
87
- /** Bits of a Neon Data API integration. Only present when the branch policy enables it. */
88
- interface NeonDataApiEnv {
89
- url: string;
90
- }
91
- /**
92
- * S3-compatible object-storage access for the branch (Preview). Present on `NeonEnv` only
93
- * when the policy declares `preview.buckets`. Combines a minted branch credential's access
94
- * keys (`accessKeyId` = the credential's full token id, e.g. `nak_live_…`, which is what the
95
- * storage gateway authenticates against; `secretAccessKey` = its
96
- * `s3_secret_access_key`) with the branch's non-secret connection details
97
- * (`endpoint`/`region`, from `GET .../storage`). Projects to the AWS SDK's
98
- * standard config env (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_ENDPOINT_URL_S3`,
99
- * `AWS_REGION`) so the S3 client works from env alone. Neon's storage gateway always
100
- * requires path-style addressing, so set `forcePathStyle: true` on your S3 client.
101
- */
102
- interface NeonStorageEnv {
103
- accessKeyId: string;
104
- secretAccessKey: string;
105
- /** S3-compatible endpoint URL for the branch. */
106
- endpoint: string;
107
- /** AWS region string (e.g. `us-east-2`). Injected as `AWS_REGION`. */
108
- region: string;
109
- }
110
- /**
111
- * AI Gateway access for the branch (Preview). Present on `NeonEnv` only when the policy
112
- * enables `preview.aiGateway`. `apiKey` is the minted credential's bearer (`api_token`);
113
- * `baseUrl` is the bare branch-scoped gateway host
114
- * (`https://<branchId>-api.ai.<region>.…`, no path). Projects to the Neon-branded env
115
- * (`NEON_AI_GATEWAY_TOKEN`, `NEON_AI_GATEWAY_BASE_URL`); clients like `@neon/ai-sdk-provider`
116
- * append the dialect route (`/v1`, `/openai/v1`, `/anthropic/v1`) themselves.
117
- */
118
- interface NeonAiGatewayEnv {
119
- apiKey: string;
120
- baseUrl: string;
121
- }
122
- /**
123
- * Empty record alias used as the "false" branch of the conditional namespace adds below.
124
- * `Record<never, never>` is the no-op for intersection — the cleaner alternative to `{}`,
125
- * which biome rejects (it means "any non-null", not "empty object").
126
- */
127
- type NoNamespace = Record<never, never>;
128
- /**
129
- * Resolve a **static** service toggle (the value of `config.auth` / `config.dataApi`) to a
130
- * type-level boolean. The whole-thing wrapping (`[T] extends […]`) turns off distribution
131
- * so a union/`undefined` is checked as one unit:
132
- *
133
- * - `false` / `{ enabled: false }` / `undefined` → `false`
134
- * - `true` / `{ enabled: true }` / any other object (`{}`, `{ enabled?: boolean }`) → `true`
135
- * (a present toggle defaults to enabled)
136
- * - the bare `boolean | ServiceToggle | undefined` (the default `Config` param, no literal
137
- * info) → `false`, so an untyped policy yields just `{ postgres }`.
138
- */
139
- type ServiceOn<T> = [T] extends [false] ? false : [T] extends [{
140
- enabled: false;
141
- }] ? false : [T] extends [undefined] ? false : [T] extends [true] ? true : [T] extends [{
142
- enabled: true;
143
- }] ? true : [T] extends [object] ? true : false;
144
- /** True when `T` has at least one known key; `false` for `{}` / `never`. */
145
- type HasKeys<T> = [keyof T] extends [never] ? false : true;
146
- /**
147
- * Whether the policy's **static** `preview` block declares at least one object-storage bucket
148
- * (`preview.buckets`). Drives whether {@link NeonEnv} carries the `storage` namespace.
149
- *
150
- * The leading `[never]` guard is load-bearing: when a policy has no `preview` at all,
151
- * `NonNullable<C["preview"]>` is `never`, and without the guard the `extends { … }` probe
152
- * below would vacuously match (everything extends `never`-derived shapes) and `HasKeys<never>`
153
- * would resolve `true`, wrongly adding the namespace. The guard short-circuits to `false`.
154
- */
155
- type HasBuckets<C extends Config> = [NonNullable<C["preview"]>] extends [never] ? false : NonNullable<C["preview"]> extends {
156
- buckets: infer B;
157
- } ? HasKeys<NonNullable<B>> : false;
158
- /**
159
- * Whether the policy's **static** `preview` block enables the AI Gateway
160
- * (`preview.aiGateway`). Drives whether {@link NeonEnv} carries the `aiGateway` namespace.
161
- *
162
- * The leading `[never]` guard is load-bearing for the same reason as {@link HasBuckets}: when
163
- * a policy has no `preview`, `NonNullable<C["preview"]>` is `never`, and a naked `never` in the
164
- * `extends` below would *distribute* (collapsing the result — and the whole `NeonEnv`
165
- * intersection — to `never`). The tuple-wrapped guard short-circuits that to `false`.
166
- */
167
- type AiGatewayOn<C extends Config> = [NonNullable<C["preview"]>] extends [never] ? false : NonNullable<C["preview"]> extends {
168
- aiGateway: infer A;
169
- } ? ServiceOn<NonNullable<A>> : false;
170
- /**
171
- * Static, namespaced shape of `fetchEnv` / `parseEnv`'s return value. Generic over the
172
- * {@link Config} so the type system knows which optional namespaces are present.
173
- *
174
- * Because the secret-bearing toggles now live in the **static** top-level `config.auth` /
175
- * `config.dataApi` (not inside a per-branch closure), the namespace presence is a direct
176
- * read of those fields — no union-across-branches, no default-config escape hatch:
177
- *
178
- * - `postgres` is always present.
179
- * - `auth` is added iff `config.auth` is statically enabled.
180
- * - `dataApi` is added iff `config.dataApi` is statically enabled.
181
- * - `storage` is added iff `config.preview.buckets` declares at least one bucket.
182
- * - `aiGateway` is added iff `config.preview.aiGateway` is statically enabled.
183
- */
184
- type NeonEnv<C extends Config = Config> = {
185
- postgres: NeonPostgresEnv;
186
- /**
187
- * Branch identity (`NEON_BRANCH`). Optional because `parseEnv` only surfaces it when the
188
- * var was injected; `fetchEnv` always populates it.
189
- */
190
- branch?: NeonBranchEnv;
191
- } & (ServiceOn<NonNullable<C["auth"]>> extends true ? {
192
- auth: NeonAuthEnv;
193
- } : NoNamespace) & (ServiceOn<NonNullable<C["dataApi"]>> extends true ? {
194
- dataApi: NeonDataApiEnv;
195
- } : NoNamespace) & (HasBuckets<C> extends true ? {
196
- storage: NeonStorageEnv;
197
- } : NoNamespace) & (AiGatewayOn<C> extends true ? {
198
- aiGateway: NeonAiGatewayEnv;
199
- } : NoNamespace);
200
- /** The static `preview.functions` record of a config, or an empty record when absent. */
201
- type PreviewFunctionsOf<C extends Config> = NonNullable<C["preview"]> extends {
202
- functions: infer F;
203
- } ? F : Record<never, never>;
204
- /** The declared function slugs of a config (record keys), as a string union. */
205
- type FunctionSlugOf<C extends Config> = Extract<keyof PreviewFunctionsOf<C>, string>;
206
- /**
207
- * Human-readable hint surfaced as the **expected type** of `parseEnv`'s `scope` argument when
208
- * the policy declares no functions at all. Without it the argument's expected type is the bare
209
- * `never` {@link FunctionSlugOf} yields, and TypeScript reports the opaque `Type '"x"' is not
210
- * assignable to type 'never'`; the literal turns that into a sentence naming the fix (and the
211
- * editor offers it as the single completion, so the empty completion list is explained rather
212
- * than just empty). Mirrors `NeonAuthRequiredHint` in `@neon/config`.
213
- */
214
- type NoFunctionScopeHint = "this policy declares no `preview.functions`, so there is no function scope to read. Declare the function in `neon.ts` first, or omit the scope to read the branch env";
215
- /**
216
- * The expected type of `parseEnv`'s function-slug `scope` argument: the caller's inferred slug
217
- * `S` normally, and the {@link NoFunctionScopeHint} message when the policy declares no
218
- * functions. Keeping `S` (rather than `FunctionSlugOf<C>`) in the enabled branch is what makes
219
- * the returned `function` namespace exact — it stays the one function's env keys instead of
220
- * widening to every declared function's.
221
- */
222
- type FunctionScopeField<C extends Config, S extends string> = [FunctionSlugOf<C>] extends [never] ? NoFunctionScopeHint : S;
223
- /** The declared env-var keys of one function `S`, as a string union. */
224
- type FunctionEnvKeysOf<C extends Config, S extends string> = S extends keyof PreviewFunctionsOf<C> ? NonNullable<PreviewFunctionsOf<C>[S]> extends {
225
- env: infer E;
226
- } ? Extract<keyof E, string> : never : never;
227
- /**
228
- * The extra `function` namespace added to `parseEnv`'s result when called with a function
229
- * slug scope: the declared env-var keys for that function, each resolved to a `string`.
230
- */
231
- type NeonFunctionEnv<C extends Config, S extends string> = {
232
- function: Record<FunctionEnvKeysOf<C, S>, string>;
233
- };
234
- /**
235
- * OS-level env-var keys grouped by the {@link NeonEnv} namespace they populate. Only the
236
- * **input** vars `parseEnv` validates are listed — the output-only aliases in
237
- * {@link NEON_ENV_VAR_KEYS} (`NEON_AI_GATEWAY_TOKEN`, …) are intentionally absent, so they
238
- * are not selectable in a `parseEnv(config, keys)` filter. Keep in sync with
239
- * {@link EnvKeyToProp}.
240
- */
241
- interface EnvKeysByNamespace {
242
- postgres: "DATABASE_URL" | "DATABASE_URL_UNPOOLED";
243
- branch: "NEON_BRANCH";
244
- auth: "NEON_AUTH_BASE_URL" | "NEON_AUTH_JWKS_URL";
245
- dataApi: "NEON_DATA_API_URL";
246
- storage: "AWS_ACCESS_KEY_ID" | "AWS_SECRET_ACCESS_KEY" | "AWS_ENDPOINT_URL_S3" | "AWS_REGION";
247
- aiGateway: "NEON_AI_GATEWAY_TOKEN" | "NEON_AI_GATEWAY_BASE_URL";
248
- }
249
- /** The {@link NeonEnv} namespace interface backing each namespace key. */
250
- interface NamespaceEnv {
251
- postgres: NeonPostgresEnv;
252
- branch: NeonBranchEnv;
253
- auth: NeonAuthEnv;
254
- dataApi: NeonDataApiEnv;
255
- storage: NeonStorageEnv;
256
- aiGateway: NeonAiGatewayEnv;
257
- }
258
- /** OS-level env-var key → the camelCase property it sets on its namespace object. */
259
- interface EnvKeyToProp {
260
- DATABASE_URL: "databaseUrl";
261
- DATABASE_URL_UNPOOLED: "databaseUrlUnpooled";
262
- NEON_BRANCH: "name";
263
- NEON_AUTH_BASE_URL: "baseUrl";
264
- NEON_AUTH_JWKS_URL: "jwksUrl";
265
- NEON_DATA_API_URL: "url";
266
- AWS_ACCESS_KEY_ID: "accessKeyId";
267
- AWS_SECRET_ACCESS_KEY: "secretAccessKey";
268
- AWS_ENDPOINT_URL_S3: "endpoint";
269
- AWS_REGION: "region";
270
- NEON_AI_GATEWAY_TOKEN: "apiKey";
271
- NEON_AI_GATEWAY_BASE_URL: "baseUrl";
272
- }
273
- /**
274
- * The OS-level env-var keys selectable for a given policy: the union of input vars across
275
- * exactly the namespaces {@link NeonEnv}<C> carries. Drives the typesafe autocomplete of the
276
- * `keys` filter — selecting a var from a namespace the policy does not enable is a type error
277
- * (e.g. `NEON_AUTH_BASE_URL` is only offered once the policy turns on `auth`).
278
- */
279
- type SelectableEnvKey<C extends Config> = EnvKeysByNamespace[keyof NeonEnv<C> & keyof EnvKeysByNamespace];
280
- /**
281
- * The result shape of a **filtered** `parseEnv(config, keys)` call: the namespaced
282
- * {@link NeonEnv} restricted to exactly the selected OS-level keys `K`. Namespaces with no
283
- * selected key are dropped, and within a kept namespace only the selected properties survive
284
- * — selecting just `["DATABASE_URL"]` yields `{ postgres: { databaseUrl: string } }`, with no
285
- * `databaseUrlUnpooled`.
286
- *
287
- * The policy gating lives on the `parseEnv` overload (which binds `K` to
288
- * {@link SelectableEnvKey}); this type only needs the selection, so it takes a bare
289
- * `K extends string` and filters with `Extract`. The outer mapped type's `as` clause drops
290
- * any namespace whose intersection with the selection is empty (`[…] extends [never]`,
291
- * tuple-wrapped to switch off distribution); the inner one re-keys each selected OS var to its
292
- * camelCase property and looks the value type up on the canonical namespace interface, so it
293
- * stays correct if a field ever stops being a plain `string`.
294
- */
295
- type FilteredNeonEnv<K extends string> = { [N in keyof EnvKeysByNamespace as [Extract<K, EnvKeysByNamespace[N]>] extends [never] ? never : N]: { [P in Extract<K, EnvKeysByNamespace[N]> as EnvKeyToProp[P & keyof EnvKeyToProp]]: NamespaceEnv[N][EnvKeyToProp[P & keyof EnvKeyToProp] & keyof NamespaceEnv[N]] } };
296
- interface FetchEnvOptions {
297
- /**
298
- * Neon project id. **Required** — the management API addresses branches through their
299
- * project. Resolve it in your CLI (e.g. neonctl) and pass it in.
300
- */
301
- projectId: string;
302
- /**
303
- * Neon branch — its **name** (e.g. `main`) or its id (`br-…`). **Required** (or pass the
304
- * legacy {@link FetchEnvOptions.branchId}). Resolved against the project's branches by
305
- * id first, then by name, so either form works.
306
- */
307
- branch?: string;
308
- /**
309
- * @deprecated Legacy id-only field. Prefer {@link FetchEnvOptions.branch}, which accepts
310
- * a branch name or id. Still honored for backward compatibility; ignored when `branch`
311
- * is set.
312
- */
313
- branchId?: string;
314
- /**
315
- * Neon API key. Resolved via the standard chain (option → `NEON_API_KEY` →
316
- * `~/.config/neonctl/credentials.json`) when omitted. Ignored when a custom `api`
317
- * is supplied.
318
- */
319
- apiKey?: string;
320
- /**
321
- * Neon **management** API base URL (not the Auth base URL). Falls back to
322
- * `NEON_API_HOST`, then production. Ignored when a custom `api` is supplied.
323
- */
324
- apiHost?: string;
325
- /**
326
- * Inject a custom NeonApi adapter. Primarily used by tests; production callers can rely
327
- * on the default real adapter built from `apiKey`.
328
- */
329
- api?: NeonApi;
330
- /**
331
- * Role name to fetch credentials for. When omitted, the connection role is auto-picked:
332
- * the only role on the branch, else Neon's default owner (`neondb_owner`), else the
333
- * single role left after dropping the managed Auth/Data API roles
334
- * (`authenticator`/`anonymous`/`authenticated`). Throws {@link PlatformError} with
335
- * `PLATFORM_AMBIGUOUS_BRANCH_AUTH` only when more than one app role remains.
336
- */
337
- roleName?: string;
338
- /**
339
- * Database name. When omitted, it is auto-picked: Neon's default `neondb` if present,
340
- * else the only database on the branch. Throws {@link PlatformError} with
341
- * `PLATFORM_AMBIGUOUS_BRANCH_AUTH` when the branch has several databases and none is
342
- * `neondb` (pass `databaseName` to disambiguate), and `PLATFORM_BRANCH_NOT_FOUND` when
343
- * the branch has no databases or the requested `databaseName` does not exist.
344
- */
345
- databaseName?: string;
346
- }
347
- /**
348
- * Resolve the project + branch this process should target, then fetch live Neon
349
- * connection strings for that branch over the network. Async — calls the Neon API.
350
- *
351
- * Use this from build scripts and the `neon-env run` command, where top-level await is
352
- * fine. For application code that needs a synchronous bootstrap (most frameworks: Drizzle
353
- * config, Next.js, Vite, etc.), inject env vars via `neon-env run -- <cmd>` and use
354
- * {@link parseEnv} instead — same {@link NeonEnv} shape, but a sync call against
355
- * `process.env`.
356
- *
357
- * Filesystem- and env-agnostic: pass `projectId` and the target `branch` (name or id)
358
- * explicitly (resolve them in your CLI, e.g. neonctl).
359
- *
360
- * ```ts
361
- * import config from "../neon";
362
- * import { fetchEnv } from "@neon/env";
363
- *
364
- * const env = await fetchEnv(config, { projectId: "patient-art-12345", branch: "main" });
365
- * const db = drizzle(neon(env.postgres.databaseUrl), { schema });
366
- * ```
367
- *
368
- * Pass `keys` to fetch only some of them — see the overload below.
369
- *
370
- * The package does **not** read `process.env`, mutate it, or touch the filesystem. Everything
371
- * it returns comes from the Neon API, so a value the API cannot produce (a one-time secret
372
- * issued to a previous call) is minted afresh rather than recovered. Callers that hold
373
- * persisted secrets and want to keep them use {@link fetchEnvReusingSecrets}, which decides
374
- * what is still valid and narrows this call's `keys` accordingly.
375
- */
376
- declare function fetchEnv<const C extends Config, const K extends SelectableEnvKey<C>>(config: C, options: FetchEnvOptions & {
377
- /**
378
- * Fetch only these OS-level env vars, instead of everything the policy enables. The
379
- * keys autocomplete from the policy ({@link SelectableEnvKey}), and the result is
380
- * narrowed to match ({@link FilteredNeonEnv}).
381
- *
382
- * The point is not just a smaller result: **work is skipped too.** Leave out
383
- * `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` / `NEON_AI_GATEWAY_TOKEN` and no branch
384
- * credential is minted at all, so a caller that already holds valid secrets can refresh
385
- * everything else without issuing a new one. The non-secret vars of the same features
386
- * (`AWS_ENDPOINT_URL_S3`, `AWS_REGION`, `NEON_AI_GATEWAY_BASE_URL`) are not
387
- * credential-backed and stay available on their own.
388
- *
389
- * The selection **intersects** with the policy rather than overriding it: naming a var
390
- * the branch policy does not enable is not an error, it simply yields nothing.
391
- */
392
- keys: readonly K[];
393
- }): Promise<FilteredNeonEnv<K>>;
394
- declare function fetchEnv<const C extends Config>(config: C, options: FetchEnvOptions): Promise<NeonEnv<C>>;
395
- /**
396
- * The {@link fetchEnv} body, with the key selection as a plain argument and no generic
397
- * narrowing. Exists for callers that compute the selection at runtime — notably
398
- * {@link fetchEnvReusingSecrets}, which decides which keys it still needs by checking the
399
- * branch — since the public overload's `keys` is bound to a literal union those callers cannot
400
- * produce without asserting.
401
- *
402
- * `keys === null` selects everything the policy enables.
403
- */
404
- declare function fetchEnvKeys(config: Config, options: FetchEnvOptions, keys: readonly string[] | null): Promise<ResolvedNeonEnv>;
405
- /**
406
- * Resolve the target branch and evaluate the policy against it — the first thing any
407
- * branch-scoped operation needs. Shared by {@link fetchEnv} and {@link fetchEnvReusingSecrets}
408
- * so the two agree on which branch they're talking about and what it has enabled.
409
- */
410
- declare function resolveBranchPolicy(config: Config, options: Pick<FetchEnvOptions, "projectId" | "branch" | "branchId">, api: NeonApi): Promise<{
411
- branch: NeonBranchSnapshot;
412
- desired: ReturnType<typeof resolveConfig>;
413
- }>;
414
- /**
415
- * Scopes the branch credential should carry for a resolved branch policy. Only object storage
416
- * and the AI Gateway *require* a credential; functions never force one (they have no credential
417
- * of their own), but `functions:invoke` is added to the scope set when a credential is already
418
- * being minted for storage / the AI Gateway, so the one credential can invoke the branch's
419
- * functions too. Returns `[]` only when nothing credential-bearing is enabled.
420
- */
421
- declare function previewCredentialScopes(preview: ResolvedPreviewConfig | undefined): CredentialScope[];
422
- /** The `name` this tool stamps on every credential it mints, so it can recognize its own. */
423
- declare function credentialName(branchName: string): string;
424
- /** The env-var keys a branch credential's secrets surface under, in emit order. */
425
- declare function credentialEnvKeys(flags: {
426
- storage: boolean;
427
- aiGateway: boolean;
428
- }): string[];
429
- /**
430
- * Every OS-level env var a resolved branch policy produces, in emit order. Lets a caller
431
- * subtract the ones it already holds and pass the rest as {@link fetchEnv}'s `keys`, without
432
- * re-deriving which vars a policy implies.
433
- */
434
- declare function policyEnvKeys(desired: ReturnType<typeof resolveConfig>): string[];
435
- declare function createApiFromOptions(options: FetchEnvOptions): NeonApi;
436
- /**
437
- * Synchronous, network-free counterpart to {@link fetchEnv}. Reads `process.env`, validates
438
- * the required Neon env vars with zod, and returns the same {@link NeonEnv} shape — so the
439
- * rest of your app touches `env.postgres.databaseUrl` instead of stringly-typed
440
- * `process.env.DATABASE_URL` lookups.
441
- *
442
- * Designed for the **"env-vars-already-injected"** path:
443
- * - You wrapped your dev command with `neon-env run -- <cmd>` or `neon dev`.
444
- * - Your platform (Vercel, Fly, Railway, …) injected the vars via its own integration.
445
- * - You are **inside a deployed Neon Function**, whose env was uploaded at `config apply`.
446
- *
447
- * Unlike the old API, `parseEnv` does **not** take a branch name: the secret set is now
448
- * static (top-level `config.auth` / `config.dataApi`), so it reads those directly without
449
- * evaluating the per-branch closure.
450
- *
451
- * The second argument is a **scope** or a **key filter**:
452
- * - omitted — *external* scope (app bootstrap, build scripts, your dev machine). Returns the
453
- * full `{ postgres, auth?, dataApi?, … }` the policy enables.
454
- * - a **function slug** (a key of `config.preview.functions`) — *function* scope: you are
455
- * running inside that function. Returns the same branch secrets **plus** a typed
456
- * `function` namespace with the function's declared env-var keys. The slug autocompletes
457
- * from the policy ({@link FunctionSlugOf}) and an undeclared one is a type error.
458
- * - an **array of OS-level env-var keys** (e.g. `["DATABASE_URL", "NEON_AUTH_BASE_URL"]`) —
459
- * *filtered* mode: only those vars are required and returned, as a narrowed namespaced
460
- * shape. The keys autocomplete from the policy ({@link SelectableEnvKey}), so you can only
461
- * pick vars the policy actually enables. Use this when a process needs just a subset (a
462
- * Next.js app that reads `DATABASE_URL` but not `DATABASE_URL_UNPOOLED`, say) and you don't
463
- * want `parseEnv` to throw over vars you never use.
464
- *
465
- * Throws `PlatformError(EnvNotInjected)` listing every missing/invalid var when the env
466
- * isn't fully populated, with a fix hint pointing back at `neon dev` / `neon-env run`.
467
- *
468
- * ```ts
469
- * import config from "../neon";
470
- * import { parseEnv } from "@neon/env";
471
- *
472
- * // External (app / build):
473
- * const env = parseEnv(config);
474
- * const db = drizzle(neon(env.postgres.databaseUrl), { schema });
475
- *
476
- * // Inside the "hello" function:
477
- * const env = parseEnv(config, "hello");
478
- * env.function.resendApiKey; // typed from hello's declared env keys
479
- *
480
- * // Filtered: only enforce + return the pooled URL.
481
- * const { postgres } = parseEnv(config, ["DATABASE_URL"]);
482
- * postgres.databaseUrl; // string — `databaseUrlUnpooled` is absent
483
- * ```
484
- */
485
- declare function parseEnv<const C extends Config>(config: C): NeonEnv<C>;
486
- declare function parseEnv<const C extends Config, const S extends FunctionSlugOf<C>>(config: C, scope: FunctionScopeField<C, S>): NeonEnv<C> & NeonFunctionEnv<C, S>;
487
- declare function parseEnv<const C extends Config, const K extends SelectableEnvKey<C>>(config: C, keys: readonly K[]): FilteredNeonEnv<K>;
488
- /**
489
- * Project a fully-resolved {@link NeonEnv} into the OS-level `{ KEY: value }` pairs used
490
- * for cross-process transport. Named after the web-platform `.entries()` convention
491
- * (`URLSearchParams` / `Headers` / `FormData`); returns a `Record` rather than an
492
- * iterator of tuples since that's the shape env injection needs (wrap with
493
- * `Object.entries(...)` if you want literal `[key, value]` pairs). Used by `neon-env run`
494
- * to inject the vars into a subprocess's `process.env`.
495
- *
496
- * Walks the value at runtime so it works for any `NeonEnv<C>` regardless of which
497
- * conditional namespaces are present.
498
- */
499
- declare function toEntries(env: ResolvedNeonEnv): Record<string, string>;
500
- /**
501
- * Any resolved env {@link toEntries} can project: a full {@link NeonEnv}, or the narrowed
502
- * result of a `keys`-filtered {@link fetchEnv} / {@link parseEnv} call. Every namespace and
503
- * property is optional so a filtered result — which legitimately carries only what was asked
504
- * for — projects to exactly the vars it holds instead of failing to type-check.
505
- */
506
- type ResolvedNeonEnv = { [N in keyof NamespaceEnv]?: Partial<NamespaceEnv[N]> };
507
- //#endregion
508
- export { FetchEnvOptions, FilteredNeonEnv, FunctionSlugOf, NEON_ENV_VAR_KEYS, NeonAiGatewayEnv, NeonAuthEnv, NeonBranchEnv, NeonDataApiEnv, NeonEnv, NeonFunctionEnv, NeonPostgresEnv, NeonStorageEnv, NoFunctionScopeHint, ResolvedNeonEnv, SelectableEnvKey, createApiFromOptions, credentialEnvKeys, credentialName, fetchEnv, fetchEnvKeys, parseEnv, policyEnvKeys, previewCredentialScopes, resolveBranchPolicy, toEntries };
509
- //# sourceMappingURL=env.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"env.d.ts","names":[],"sources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