@neondatabase/config 0.0.0 → 0.2.0
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.
- package/LICENSE.md +178 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +8 -0
- package/dist/lib/auth.d.ts +63 -0
- package/dist/lib/auth.d.ts.map +1 -0
- package/dist/lib/auth.js +93 -0
- package/dist/lib/auth.js.map +1 -0
- package/dist/lib/define-config.d.ts +43 -0
- package/dist/lib/define-config.d.ts.map +1 -0
- package/dist/lib/define-config.js +112 -0
- package/dist/lib/define-config.js.map +1 -0
- package/dist/lib/diff.d.ts +109 -0
- package/dist/lib/diff.d.ts.map +1 -0
- package/dist/lib/diff.js +205 -0
- package/dist/lib/diff.js.map +1 -0
- package/dist/lib/duration.d.ts +46 -0
- package/dist/lib/duration.d.ts.map +1 -0
- package/dist/lib/duration.js +96 -0
- package/dist/lib/duration.js.map +1 -0
- package/dist/lib/errors.d.ts +129 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +168 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/loader.d.ts +44 -0
- package/dist/lib/loader.d.ts.map +1 -0
- package/dist/lib/loader.js +119 -0
- package/dist/lib/loader.js.map +1 -0
- package/dist/lib/neon-api-real.d.ts +45 -0
- package/dist/lib/neon-api-real.d.ts.map +1 -0
- package/dist/lib/neon-api-real.js +582 -0
- package/dist/lib/neon-api-real.js.map +1 -0
- package/dist/lib/neon-api.d.ts +262 -0
- package/dist/lib/neon-api.d.ts.map +1 -0
- package/dist/lib/neon-api.js +1 -0
- package/dist/lib/patterns.d.ts +43 -0
- package/dist/lib/patterns.d.ts.map +1 -0
- package/dist/lib/patterns.js +76 -0
- package/dist/lib/patterns.js.map +1 -0
- package/dist/lib/schema.d.ts +130 -0
- package/dist/lib/schema.d.ts.map +1 -0
- package/dist/lib/schema.js +218 -0
- package/dist/lib/schema.js.map +1 -0
- package/dist/lib/types.d.ts +289 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +1 -0
- package/dist/lib/wrap-neon-error.d.ts +30 -0
- package/dist/lib/wrap-neon-error.d.ts.map +1 -0
- package/dist/lib/wrap-neon-error.js +139 -0
- package/dist/lib/wrap-neon-error.js.map +1 -0
- package/dist/v1.d.ts +153 -0
- package/dist/v1.d.ts.map +1 -0
- package/dist/v1.js +69 -0
- package/dist/v1.js.map +1 -0
- package/package.json +67 -17
- package/.env.example +0 -5
- package/e2e/errors.e2e.test.ts +0 -52
- package/e2e/helpers.ts +0 -205
- package/e2e/load-env.ts +0 -29
- package/e2e/setup.ts +0 -24
- package/src/index.ts +0 -5
- package/src/lib/auth.test.ts +0 -166
- package/src/lib/auth.ts +0 -124
- package/src/lib/define-config.test.ts +0 -161
- package/src/lib/define-config.ts +0 -152
- package/src/lib/diff.test.ts +0 -142
- package/src/lib/diff.ts +0 -391
- package/src/lib/duration.test.ts +0 -105
- package/src/lib/duration.ts +0 -147
- package/src/lib/errors.test.ts +0 -26
- package/src/lib/errors.ts +0 -220
- package/src/lib/fake-neon-api.ts +0 -782
- package/src/lib/loader.test.ts +0 -35
- package/src/lib/loader.ts +0 -215
- package/src/lib/neon-api-real.test.ts +0 -72
- package/src/lib/neon-api-real.ts +0 -1123
- package/src/lib/neon-api.ts +0 -356
- package/src/lib/patterns.test.ts +0 -80
- package/src/lib/patterns.ts +0 -98
- package/src/lib/schema.test.ts +0 -88
- package/src/lib/schema.ts +0 -252
- package/src/lib/test-utils.ts +0 -83
- package/src/lib/types.ts +0 -268
- package/src/lib/wrap-neon-error.test.ts +0 -145
- package/src/lib/wrap-neon-error.ts +0 -204
- package/src/v1.test.ts +0 -33
- package/src/v1.ts +0 -148
- package/tsconfig.json +0 -4
- package/tsdown.config.ts +0 -19
- package/vitest.config.ts +0 -19
- package/vitest.e2e.config.ts +0 -29
package/LICENSE.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# Apache License 2.0
|
|
2
|
+
|
|
3
|
+
Apache License
|
|
4
|
+
Version 2.0, January 2004
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
13
|
+
|
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
15
|
+
the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
18
|
+
other entities that control, are controlled by, or are under common
|
|
19
|
+
control with that entity. For the purposes of this definition,
|
|
20
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
21
|
+
direction or management of such entity, whether by contract or
|
|
22
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
23
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
24
|
+
|
|
25
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
26
|
+
exercising permissions granted by this License.
|
|
27
|
+
|
|
28
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
29
|
+
including but not limited to software source code, documentation
|
|
30
|
+
source, and configuration files.
|
|
31
|
+
|
|
32
|
+
"Object" form shall mean any form resulting from mechanical
|
|
33
|
+
transformation or translation of a Source form, including but
|
|
34
|
+
not limited to compiled object code, generated documentation,
|
|
35
|
+
and conversions to other media types.
|
|
36
|
+
|
|
37
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
38
|
+
Object form, made available under the License, as indicated by a
|
|
39
|
+
copyright notice that is included in or attached to the work
|
|
40
|
+
(an example is provided in the Appendix below).
|
|
41
|
+
|
|
42
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
43
|
+
form, that is based on (or derived from) the Work and for which the
|
|
44
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
45
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
46
|
+
of this License, Derivative Works shall not include works that remain
|
|
47
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
48
|
+
the Work and Derivative Works thereof.
|
|
49
|
+
|
|
50
|
+
"Contribution" shall mean any work of authorship, including
|
|
51
|
+
the original version of the Work and any modifications or additions
|
|
52
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
53
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
54
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
55
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
56
|
+
means any form of electronic, verbal, or written communication sent
|
|
57
|
+
to the Licensor or its representatives, including but not limited to
|
|
58
|
+
communication on electronic mailing lists, source code control systems,
|
|
59
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
60
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
61
|
+
excluding communication that is conspicuously marked or otherwise
|
|
62
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
63
|
+
|
|
64
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
65
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
66
|
+
subsequently incorporated within the Work.
|
|
67
|
+
|
|
68
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
69
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
70
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
71
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
72
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
73
|
+
Work and such Derivative Works in Source or Object form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
(except as stated in this section) patent license to make, have made,
|
|
79
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
80
|
+
where such license applies only to those patent claims licensable
|
|
81
|
+
by such Contributor that are necessarily infringed by their
|
|
82
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
83
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
84
|
+
institute patent litigation against any entity (including a
|
|
85
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
86
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
87
|
+
or contributory patent infringement, then any patent licenses
|
|
88
|
+
granted to You under this License for that Work shall terminate
|
|
89
|
+
as of the date such litigation is filed.
|
|
90
|
+
|
|
91
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
92
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
93
|
+
modifications, and in Source or Object form, provided that You
|
|
94
|
+
meet the following conditions:
|
|
95
|
+
|
|
96
|
+
(a) You must give any other recipients of the Work or
|
|
97
|
+
Derivative Works a copy of this License; and
|
|
98
|
+
|
|
99
|
+
(b) You must cause any modified files to carry prominent notices
|
|
100
|
+
stating that You changed the files; and
|
|
101
|
+
|
|
102
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
103
|
+
that You distribute, all copyright, patent, trademark, and
|
|
104
|
+
attribution notices from the Source form of the Work,
|
|
105
|
+
excluding those notices that do not pertain to any part of
|
|
106
|
+
the Derivative Works; and
|
|
107
|
+
|
|
108
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
109
|
+
distribution, then any Derivative Works that You distribute must
|
|
110
|
+
include a readable copy of the attribution notices contained
|
|
111
|
+
within such NOTICE file, excluding those notices that do not
|
|
112
|
+
pertain to any part of the Derivative Works, in at least one
|
|
113
|
+
of the following places: within a NOTICE text file distributed
|
|
114
|
+
as part of the Derivative Works; within the Source form or
|
|
115
|
+
documentation, if provided along with the Derivative Works; or,
|
|
116
|
+
within a display generated by the Derivative Works, if and
|
|
117
|
+
wherever such third-party notices normally appear. The contents
|
|
118
|
+
of the NOTICE file are for informational purposes only and
|
|
119
|
+
do not modify the License. You may add Your own attribution
|
|
120
|
+
notices within Derivative Works that You distribute, alongside
|
|
121
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
122
|
+
that such additional attribution notices cannot be construed
|
|
123
|
+
as modifying the License.
|
|
124
|
+
|
|
125
|
+
You may add Your own copyright statement to Your modifications and
|
|
126
|
+
may provide additional or different license terms and conditions
|
|
127
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
128
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
129
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
130
|
+
the conditions stated in this License.
|
|
131
|
+
|
|
132
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
133
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
134
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
135
|
+
this License, without any additional terms or conditions.
|
|
136
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
137
|
+
the terms of any separate license agreement you may have executed
|
|
138
|
+
with Licensor regarding such Contributions.
|
|
139
|
+
|
|
140
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
141
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
142
|
+
except as required for reasonable and customary use in describing the
|
|
143
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
144
|
+
|
|
145
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
146
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
147
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
148
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
149
|
+
implied, including, without limitation, any warranties or conditions
|
|
150
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
151
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
152
|
+
appropriateness of using or redistributing the Work and assume any
|
|
153
|
+
risks associated with Your exercise of permissions under this License.
|
|
154
|
+
|
|
155
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
156
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
157
|
+
unless required by applicable law (such as deliberate and grossly
|
|
158
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
159
|
+
liable to You for damages, including any direct, indirect, special,
|
|
160
|
+
incidental, or consequential damages of any character arising as a
|
|
161
|
+
result of this License or out of the use or inability to use the
|
|
162
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
163
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
164
|
+
other commercial damages or losses), even if such Contributor
|
|
165
|
+
has been advised of the possibility of such damages.
|
|
166
|
+
|
|
167
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
168
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
169
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
170
|
+
or other liability obligations and/or rights consistent with this
|
|
171
|
+
License. However, in accepting such obligations, You may act only
|
|
172
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
173
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
174
|
+
defend, and hold each Contributor harmless for any liability
|
|
175
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
176
|
+
of your accepting any such warranty or additional liability.
|
|
177
|
+
|
|
178
|
+
END OF TERMS AND CONDITIONS
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AppliedChange, BranchConfig, BranchTarget, BucketAccessLevel, BucketConfig, ComputeSettings, Config, ConflictReport, FunctionConfig, FunctionDevConfig, FunctionMemoryMib, FunctionRuntime, PostgresConfig, PreviewConfig, PushResult, ResolvedBranchConfig, ResolvedBucketConfig, ResolvedFunctionConfig, ResolvedPreviewConfig, ServiceToggle } from "./lib/types.js";
|
|
2
|
+
import { ConfigLoadError, ConfigValidationError, ErrorCode, MissingContextError, PlatformError, PushAbortedError, PushConflictError } from "./lib/errors.js";
|
|
3
|
+
import { CreateBranchInput, CreateBucketInput, CreateProjectInput, DeployFunctionInput, GetConnectionUriInput, NeonApi, NeonAuthSnapshot, NeonBranchSnapshot, NeonBucketSnapshot, NeonDataApiSnapshot, NeonDatabaseSnapshot, NeonEndpointSnapshot, NeonFunctionDeploymentSnapshot, NeonFunctionSnapshot, NeonProjectSnapshot, NeonRoleSnapshot, UpdateBranchInput } from "./lib/neon-api.js";
|
|
4
|
+
import { createNeonApiFromOptions, resolveApiKey } from "./lib/auth.js";
|
|
5
|
+
import { defineConfig, resolveConfig } from "./lib/define-config.js";
|
|
6
|
+
import { DiffOptions, DiffResult, PlanStep, RemotePreviewState, RemoteServiceState, RemoteState, diffConfig } from "./lib/diff.js";
|
|
7
|
+
import { LoadConfigOptions, loadConfigFromFile } from "./lib/loader.js";
|
|
8
|
+
import { createRealNeonApi } from "./lib/neon-api-real.js";
|
|
9
|
+
import { errors, schemas } from "./v1.js";
|
|
10
|
+
export { AppliedChange, BranchConfig, BranchTarget, BucketAccessLevel, BucketConfig, ComputeSettings, Config, ConfigLoadError, ConfigValidationError, ConflictReport, CreateBranchInput, CreateBucketInput, CreateProjectInput, DeployFunctionInput, DiffOptions, DiffResult, ErrorCode, FunctionConfig, FunctionDevConfig, FunctionMemoryMib, FunctionRuntime, GetConnectionUriInput, LoadConfigOptions, MissingContextError, NeonApi, NeonAuthSnapshot, NeonBranchSnapshot, NeonBucketSnapshot, NeonDataApiSnapshot, NeonDatabaseSnapshot, NeonEndpointSnapshot, NeonFunctionDeploymentSnapshot, NeonFunctionSnapshot, NeonProjectSnapshot, NeonRoleSnapshot, PlanStep, PlatformError, PostgresConfig, PreviewConfig, PushAbortedError, PushConflictError, PushResult, RemotePreviewState, RemoteServiceState, RemoteState, ResolvedBranchConfig, ResolvedBucketConfig, ResolvedFunctionConfig, ResolvedPreviewConfig, ServiceToggle, UpdateBranchInput, createNeonApiFromOptions, createRealNeonApi, defineConfig, diffConfig, errors, loadConfigFromFile, resolveApiKey, resolveConfig, schemas };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ConfigLoadError, ConfigValidationError, ErrorCode, MissingContextError, PlatformError, PushAbortedError, PushConflictError } from "./lib/errors.js";
|
|
2
|
+
import { createRealNeonApi } from "./lib/neon-api-real.js";
|
|
3
|
+
import { createNeonApiFromOptions, resolveApiKey } from "./lib/auth.js";
|
|
4
|
+
import { defineConfig, resolveConfig } from "./lib/define-config.js";
|
|
5
|
+
import { diffConfig } from "./lib/diff.js";
|
|
6
|
+
import { loadConfigFromFile } from "./lib/loader.js";
|
|
7
|
+
import { errors, schemas } from "./v1.js";
|
|
8
|
+
export { ConfigLoadError, ConfigValidationError, ErrorCode, MissingContextError, PlatformError, PushAbortedError, PushConflictError, createNeonApiFromOptions, createRealNeonApi, defineConfig, diffConfig, errors, loadConfigFromFile, resolveApiKey, resolveConfig, schemas };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { NeonApi } from "./neon-api.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/auth.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal shape of `~/.config/neonctl/credentials.json` we read. `neonctl` writes more
|
|
7
|
+
* fields (refresh_token, expires_at, …) but only `access_token` is what we need — it's a
|
|
8
|
+
* Bearer token the Neon API accepts on the same endpoints `napi_*` API keys do.
|
|
9
|
+
*/
|
|
10
|
+
interface NeonctlCredentials {
|
|
11
|
+
access_token: string;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Locate and read the OAuth credentials neonctl writes after `neon auth`.
|
|
16
|
+
*
|
|
17
|
+
* Resolution:
|
|
18
|
+
* 1. `options.configDir` (explicit override — mirrors neonctl's `--config-dir` flag).
|
|
19
|
+
* 2. `NEONCTL_CONFIG_DIR` environment variable.
|
|
20
|
+
* 3. `<home>/.config/neonctl/credentials.json` (the neonctl default; `home` reads
|
|
21
|
+
* `HOME`, falling back to `USERPROFILE` for Windows parity).
|
|
22
|
+
*
|
|
23
|
+
* Returns `null` (never throws) when the file is missing, unreadable, malformed, or has
|
|
24
|
+
* no `access_token` — so callers can use this as a quiet fallback in a resolution chain
|
|
25
|
+
* without try/catch noise.
|
|
26
|
+
*/
|
|
27
|
+
declare function readNeonctlCredentials(options?: {
|
|
28
|
+
configDir?: string;
|
|
29
|
+
}): NeonctlCredentials | null;
|
|
30
|
+
/**
|
|
31
|
+
* Resolution chain for the Bearer token sent to the Neon API. Each entry wins over the
|
|
32
|
+
* next:
|
|
33
|
+
*
|
|
34
|
+
* 1. `options.apiKey` (explicit).
|
|
35
|
+
* 2. `NEON_API_KEY` environment variable.
|
|
36
|
+
* 3. `access_token` from `~/.config/neonctl/credentials.json` (or `NEONCTL_CONFIG_DIR`).
|
|
37
|
+
*
|
|
38
|
+
* Returns `null` when no source provides one. Callers wrap the null case in a
|
|
39
|
+
* `PLATFORM_MISSING_API_KEY` error with a message tailored to the operation.
|
|
40
|
+
*/
|
|
41
|
+
declare function resolveApiKey(options?: {
|
|
42
|
+
apiKey?: string;
|
|
43
|
+
configDir?: string;
|
|
44
|
+
}): {
|
|
45
|
+
token: string;
|
|
46
|
+
source: "option" | "env" | "neonctl";
|
|
47
|
+
} | null;
|
|
48
|
+
/**
|
|
49
|
+
* Resolve the Neon API key via the standard chain (option → `NEON_API_KEY` env →
|
|
50
|
+
* `~/.config/neonctl/credentials.json`) and construct a real {@link NeonApi} adapter from
|
|
51
|
+
* it, or throw a uniform `PLATFORM_MISSING_API_KEY` error if no key can be found.
|
|
52
|
+
*
|
|
53
|
+
* Used by `pullConfig`, `pushConfig`, `fetchEnv`, and `branch` to build their default
|
|
54
|
+
* `NeonApi` when the caller doesn't inject one. `operation` is the calling function's
|
|
55
|
+
* name (e.g. `"pushConfig"`, `"branch"`) — it's prepended to the error message so users
|
|
56
|
+
* can tell which call surfaced the missing key.
|
|
57
|
+
*/
|
|
58
|
+
declare function createNeonApiFromOptions(operation: string, options?: {
|
|
59
|
+
apiKey?: string;
|
|
60
|
+
}): NeonApi;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { NeonctlCredentials, createNeonApiFromOptions, readNeonctlCredentials, resolveApiKey };
|
|
63
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","names":[],"sources":["../../src/lib/auth.ts"],"mappings":";;;;;;AAWA;AAkBA;AA8CA;AA6BgB,UA7FC,kBAAA,CA6FuB;;;;;;;;;;;;;;;;;iBA3ExB,sBAAA;;IAEb;;;;;;;;;;;;iBA4Ca,aAAA;;;;;;;;;;;;;;;;;iBA6BA,wBAAA;;IAKb"}
|
package/dist/lib/auth.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ErrorCode, PlatformError } from "./errors.js";
|
|
2
|
+
import { createRealNeonApi } from "./neon-api-real.js";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
//#region src/lib/auth.ts
|
|
6
|
+
/**
|
|
7
|
+
* Locate and read the OAuth credentials neonctl writes after `neon auth`.
|
|
8
|
+
*
|
|
9
|
+
* Resolution:
|
|
10
|
+
* 1. `options.configDir` (explicit override — mirrors neonctl's `--config-dir` flag).
|
|
11
|
+
* 2. `NEONCTL_CONFIG_DIR` environment variable.
|
|
12
|
+
* 3. `<home>/.config/neonctl/credentials.json` (the neonctl default; `home` reads
|
|
13
|
+
* `HOME`, falling back to `USERPROFILE` for Windows parity).
|
|
14
|
+
*
|
|
15
|
+
* Returns `null` (never throws) when the file is missing, unreadable, malformed, or has
|
|
16
|
+
* no `access_token` — so callers can use this as a quiet fallback in a resolution chain
|
|
17
|
+
* without try/catch noise.
|
|
18
|
+
*/
|
|
19
|
+
function readNeonctlCredentials(options = {}) {
|
|
20
|
+
const home = process.env.HOME ?? process.env.USERPROFILE;
|
|
21
|
+
const configDir = options.configDir ?? process.env.NEONCTL_CONFIG_DIR ?? (home ? resolve(home, ".config", "neonctl") : void 0);
|
|
22
|
+
if (!configDir) return null;
|
|
23
|
+
const credentialsPath = resolve(configDir, "credentials.json");
|
|
24
|
+
if (!existsSync(credentialsPath)) return null;
|
|
25
|
+
let raw;
|
|
26
|
+
try {
|
|
27
|
+
raw = readFileSync(credentialsPath, "utf-8");
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
let parsed;
|
|
32
|
+
try {
|
|
33
|
+
parsed = JSON.parse(raw);
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
38
|
+
const obj = parsed;
|
|
39
|
+
if (typeof obj.access_token !== "string" || obj.access_token === "") return null;
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Resolution chain for the Bearer token sent to the Neon API. Each entry wins over the
|
|
44
|
+
* next:
|
|
45
|
+
*
|
|
46
|
+
* 1. `options.apiKey` (explicit).
|
|
47
|
+
* 2. `NEON_API_KEY` environment variable.
|
|
48
|
+
* 3. `access_token` from `~/.config/neonctl/credentials.json` (or `NEONCTL_CONFIG_DIR`).
|
|
49
|
+
*
|
|
50
|
+
* Returns `null` when no source provides one. Callers wrap the null case in a
|
|
51
|
+
* `PLATFORM_MISSING_API_KEY` error with a message tailored to the operation.
|
|
52
|
+
*/
|
|
53
|
+
function resolveApiKey(options = {}) {
|
|
54
|
+
if (options.apiKey && options.apiKey.trim() !== "") return {
|
|
55
|
+
token: options.apiKey.trim(),
|
|
56
|
+
source: "option"
|
|
57
|
+
};
|
|
58
|
+
const envKey = process.env.NEON_API_KEY;
|
|
59
|
+
if (typeof envKey === "string" && envKey.trim() !== "") return {
|
|
60
|
+
token: envKey.trim(),
|
|
61
|
+
source: "env"
|
|
62
|
+
};
|
|
63
|
+
const creds = readNeonctlCredentials(options.configDir ? { configDir: options.configDir } : {});
|
|
64
|
+
if (creds) return {
|
|
65
|
+
token: creds.access_token,
|
|
66
|
+
source: "neonctl"
|
|
67
|
+
};
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Resolve the Neon API key via the standard chain (option → `NEON_API_KEY` env →
|
|
72
|
+
* `~/.config/neonctl/credentials.json`) and construct a real {@link NeonApi} adapter from
|
|
73
|
+
* it, or throw a uniform `PLATFORM_MISSING_API_KEY` error if no key can be found.
|
|
74
|
+
*
|
|
75
|
+
* Used by `pullConfig`, `pushConfig`, `fetchEnv`, and `branch` to build their default
|
|
76
|
+
* `NeonApi` when the caller doesn't inject one. `operation` is the calling function's
|
|
77
|
+
* name (e.g. `"pushConfig"`, `"branch"`) — it's prepended to the error message so users
|
|
78
|
+
* can tell which call surfaced the missing key.
|
|
79
|
+
*/
|
|
80
|
+
function createNeonApiFromOptions(operation, options = {}) {
|
|
81
|
+
const resolved = resolveApiKey(options.apiKey ? { apiKey: options.apiKey } : {});
|
|
82
|
+
if (resolved) return createRealNeonApi({ apiKey: resolved.token });
|
|
83
|
+
throw new PlatformError(ErrorCode.MissingApiKey, [
|
|
84
|
+
`${operation} has no Neon API key to work with.`,
|
|
85
|
+
"Tried (in order): `apiKey` option, NEON_API_KEY env, and `~/.config/neonctl/credentials.json`.",
|
|
86
|
+
"Either pass `apiKey` directly, set NEON_API_KEY, run `npx neonctl auth` to populate the credentials file, or pass a custom `api` adapter (e.g. an in-memory fake for tests).",
|
|
87
|
+
"Generate a key at https://console.neon.tech/app/settings/api-keys."
|
|
88
|
+
].join(" "));
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
export { createNeonApiFromOptions, readNeonctlCredentials, resolveApiKey };
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","names":[],"sources":["../../src/lib/auth.ts"],"sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\nimport { ErrorCode, PlatformError } from \"./errors.js\";\nimport type { NeonApi } from \"./neon-api.js\";\nimport { createRealNeonApi } from \"./neon-api-real.js\";\n\n/**\n * Minimal shape of `~/.config/neonctl/credentials.json` we read. `neonctl` writes more\n * fields (refresh_token, expires_at, …) but only `access_token` is what we need — it's a\n * Bearer token the Neon API accepts on the same endpoints `napi_*` API keys do.\n */\nexport interface NeonctlCredentials {\n\taccess_token: string;\n\t[key: string]: unknown;\n}\n\n/**\n * Locate and read the OAuth credentials neonctl writes after `neon auth`.\n *\n * Resolution:\n * 1. `options.configDir` (explicit override — mirrors neonctl's `--config-dir` flag).\n * 2. `NEONCTL_CONFIG_DIR` environment variable.\n * 3. `<home>/.config/neonctl/credentials.json` (the neonctl default; `home` reads\n * `HOME`, falling back to `USERPROFILE` for Windows parity).\n *\n * Returns `null` (never throws) when the file is missing, unreadable, malformed, or has\n * no `access_token` — so callers can use this as a quiet fallback in a resolution chain\n * without try/catch noise.\n */\nexport function readNeonctlCredentials(\n\toptions: { configDir?: string } = {},\n): NeonctlCredentials | null {\n\tconst home = process.env.HOME ?? process.env.USERPROFILE;\n\tconst configDir =\n\t\toptions.configDir ??\n\t\tprocess.env.NEONCTL_CONFIG_DIR ??\n\t\t(home ? resolve(home, \".config\", \"neonctl\") : undefined);\n\tif (!configDir) return null;\n\n\tconst credentialsPath = resolve(configDir, \"credentials.json\");\n\tif (!existsSync(credentialsPath)) return null;\n\n\tlet raw: string;\n\ttry {\n\t\traw = readFileSync(credentialsPath, \"utf-8\");\n\t} catch {\n\t\treturn null;\n\t}\n\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\treturn null;\n\t}\n\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\treturn null;\n\tconst obj = parsed as Record<string, unknown>;\n\tif (typeof obj.access_token !== \"string\" || obj.access_token === \"\")\n\t\treturn null;\n\treturn obj as NeonctlCredentials;\n}\n\n/**\n * Resolution chain for the Bearer token sent to the Neon API. Each entry wins over the\n * next:\n *\n * 1. `options.apiKey` (explicit).\n * 2. `NEON_API_KEY` environment variable.\n * 3. `access_token` from `~/.config/neonctl/credentials.json` (or `NEONCTL_CONFIG_DIR`).\n *\n * Returns `null` when no source provides one. Callers wrap the null case in a\n * `PLATFORM_MISSING_API_KEY` error with a message tailored to the operation.\n */\nexport function resolveApiKey(\n\toptions: { apiKey?: string; configDir?: string } = {},\n): { token: string; source: \"option\" | \"env\" | \"neonctl\" } | null {\n\tif (options.apiKey && options.apiKey.trim() !== \"\") {\n\t\treturn { token: options.apiKey.trim(), source: \"option\" };\n\t}\n\tconst envKey = process.env.NEON_API_KEY;\n\tif (typeof envKey === \"string\" && envKey.trim() !== \"\") {\n\t\treturn { token: envKey.trim(), source: \"env\" };\n\t}\n\tconst creds = readNeonctlCredentials(\n\t\toptions.configDir ? { configDir: options.configDir } : {},\n\t);\n\tif (creds) {\n\t\treturn { token: creds.access_token, source: \"neonctl\" };\n\t}\n\treturn null;\n}\n\n/**\n * Resolve the Neon API key via the standard chain (option → `NEON_API_KEY` env →\n * `~/.config/neonctl/credentials.json`) and construct a real {@link NeonApi} adapter from\n * it, or throw a uniform `PLATFORM_MISSING_API_KEY` error if no key can be found.\n *\n * Used by `pullConfig`, `pushConfig`, `fetchEnv`, and `branch` to build their default\n * `NeonApi` when the caller doesn't inject one. `operation` is the calling function's\n * name (e.g. `\"pushConfig\"`, `\"branch\"`) — it's prepended to the error message so users\n * can tell which call surfaced the missing key.\n */\nexport function createNeonApiFromOptions(\n\toperation: string,\n\toptions: {\n\t\tapiKey?: string;\n\t} = {},\n): NeonApi {\n\tconst resolved = resolveApiKey(\n\t\toptions.apiKey ? { apiKey: options.apiKey } : {},\n\t);\n\tif (resolved) return createRealNeonApi({ apiKey: resolved.token });\n\tthrow new PlatformError(\n\t\tErrorCode.MissingApiKey,\n\t\t[\n\t\t\t`${operation} has no Neon API key to work with.`,\n\t\t\t\"Tried (in order): `apiKey` option, NEON_API_KEY env, and `~/.config/neonctl/credentials.json`.\",\n\t\t\t\"Either pass `apiKey` directly, set NEON_API_KEY, run `npx neonctl auth` to populate the credentials file, or pass a custom `api` adapter (e.g. an in-memory fake for tests).\",\n\t\t\t\"Generate a key at https://console.neon.tech/app/settings/api-keys.\",\n\t\t].join(\" \"),\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA6BA,SAAgB,uBACf,UAAkC,CAAC,GACP;CAC5B,MAAM,OAAO,QAAQ,IAAI,QAAQ,QAAQ,IAAI;CAC7C,MAAM,YACL,QAAQ,aACR,QAAQ,IAAI,uBACX,OAAO,QAAQ,MAAM,WAAW,SAAS,IAAI,KAAA;CAC/C,IAAI,CAAC,WAAW,OAAO;CAEvB,MAAM,kBAAkB,QAAQ,WAAW,kBAAkB;CAC7D,IAAI,CAAC,WAAW,eAAe,GAAG,OAAO;CAEzC,IAAI;CACJ,IAAI;EACH,MAAM,aAAa,iBAAiB,OAAO;CAC5C,QAAQ;EACP,OAAO;CACR;CAEA,IAAI;CACJ,IAAI;EACH,SAAS,KAAK,MAAM,GAAG;CACxB,QAAQ;EACP,OAAO;CACR;CAEA,IAAI,WAAW,QAAQ,OAAO,WAAW,YAAY,MAAM,QAAQ,MAAM,GACxE,OAAO;CACR,MAAM,MAAM;CACZ,IAAI,OAAO,IAAI,iBAAiB,YAAY,IAAI,iBAAiB,IAChE,OAAO;CACR,OAAO;AACR;;;;;;;;;;;;AAaA,SAAgB,cACf,UAAmD,CAAC,GACa;CACjE,IAAI,QAAQ,UAAU,QAAQ,OAAO,KAAK,MAAM,IAC/C,OAAO;EAAE,OAAO,QAAQ,OAAO,KAAK;EAAG,QAAQ;CAAS;CAEzD,MAAM,SAAS,QAAQ,IAAI;CAC3B,IAAI,OAAO,WAAW,YAAY,OAAO,KAAK,MAAM,IACnD,OAAO;EAAE,OAAO,OAAO,KAAK;EAAG,QAAQ;CAAM;CAE9C,MAAM,QAAQ,uBACb,QAAQ,YAAY,EAAE,WAAW,QAAQ,UAAU,IAAI,CAAC,CACzD;CACA,IAAI,OACH,OAAO;EAAE,OAAO,MAAM;EAAc,QAAQ;CAAU;CAEvD,OAAO;AACR;;;;;;;;;;;AAYA,SAAgB,yBACf,WACA,UAEI,CAAC,GACK;CACV,MAAM,WAAW,cAChB,QAAQ,SAAS,EAAE,QAAQ,QAAQ,OAAO,IAAI,CAAC,CAChD;CACA,IAAI,UAAU,OAAO,kBAAkB,EAAE,QAAQ,SAAS,MAAM,CAAC;CACjE,MAAM,IAAI,cACT,UAAU,eACV;EACC,GAAG,UAAU;EACb;EACA;EACA;CACD,EAAE,KAAK,GAAG,CACX;AACD"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BranchTarget, Config, ResolvedBranchConfig } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/define-config.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validate and freeze a Neon Platform branch policy.
|
|
7
|
+
*
|
|
8
|
+
* Used at the top of `neon.ts`:
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { defineConfig } from "@neondatabase/config/v1";
|
|
11
|
+
*
|
|
12
|
+
* export default defineConfig((branch) => {
|
|
13
|
+
* if (branch.name === "main") {
|
|
14
|
+
* return { protected: true, auth: {} };
|
|
15
|
+
* }
|
|
16
|
+
* return { parent: "main", ttl: "7d" };
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The `branch` parameter is a **read-only {@link BranchTarget} descriptor** of the branch
|
|
21
|
+
* this policy invocation is deciding for — not a live branch handle. You don't mutate it
|
|
22
|
+
* (`branch.protected = true` does nothing); you switch on its facts (`branch.name`,
|
|
23
|
+
* `branch.isDefault`, `branch.exists`, …) and **return** the desired {@link BranchConfig}.
|
|
24
|
+
* The same callback runs in two modes: against an existing branch (fields populated from
|
|
25
|
+
* Neon) and during pre-create evaluation (`exists: false`, `id` undefined).
|
|
26
|
+
*
|
|
27
|
+
* Pure function — no I/O, no side effects. The returned policy validates its output every
|
|
28
|
+
* time it is evaluated so errors point at the concrete branch target that triggered them.
|
|
29
|
+
*/
|
|
30
|
+
declare function defineConfig<const C extends Config>(input: C): C;
|
|
31
|
+
/**
|
|
32
|
+
* Evaluate a branch policy for a specific branch target and return a normalized config.
|
|
33
|
+
*/
|
|
34
|
+
declare function resolveConfig(config: Config, branch: BranchTarget): ResolvedBranchConfig;
|
|
35
|
+
/**
|
|
36
|
+
* Normalize a region identifier to Neon's `<cloud>-<region>` format. When the user writes
|
|
37
|
+
* `us-east-1` we assume `aws-us-east-1`. Pure helper used by both the validator and the
|
|
38
|
+
* NeonApi adapter.
|
|
39
|
+
*/
|
|
40
|
+
declare function normalizeRegion(region: string): string;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { defineConfig, normalizeRegion, resolveConfig };
|
|
43
|
+
//# sourceMappingURL=define-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define-config.d.ts","names":[],"sources":["../../src/lib/define-config.ts"],"mappings":";;;;;;AA4CA;;;;;AAAiE;AAcjE;;;;;AAGuB;AA0FvB;;;;;;;;;;;iBA3GgB,6BAA6B,eAAe,IAAI;;;;iBAchD,aAAA,SACP,gBACA,eACN;;;;;;iBA0Fa,eAAA"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { ConfigValidationError } from "./errors.js";
|
|
2
|
+
import { parseDuration } from "./duration.js";
|
|
3
|
+
import { branchConfigSchema, formatZodIssues } from "./schema.js";
|
|
4
|
+
//#region src/lib/define-config.ts
|
|
5
|
+
/** Default deploy parameters applied to functions that omit them in `neon.ts`. */
|
|
6
|
+
const DEFAULT_FUNCTION_RUNTIME = "nodejs24";
|
|
7
|
+
const DEFAULT_FUNCTION_MEMORY_MIB = 512;
|
|
8
|
+
const REGION_PREFIX = /^(aws|azure|gcp)-/;
|
|
9
|
+
/**
|
|
10
|
+
* Validate and freeze a Neon Platform branch policy.
|
|
11
|
+
*
|
|
12
|
+
* Used at the top of `neon.ts`:
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { defineConfig } from "@neondatabase/config/v1";
|
|
15
|
+
*
|
|
16
|
+
* export default defineConfig((branch) => {
|
|
17
|
+
* if (branch.name === "main") {
|
|
18
|
+
* return { protected: true, auth: {} };
|
|
19
|
+
* }
|
|
20
|
+
* return { parent: "main", ttl: "7d" };
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* The `branch` parameter is a **read-only {@link BranchTarget} descriptor** of the branch
|
|
25
|
+
* this policy invocation is deciding for — not a live branch handle. You don't mutate it
|
|
26
|
+
* (`branch.protected = true` does nothing); you switch on its facts (`branch.name`,
|
|
27
|
+
* `branch.isDefault`, `branch.exists`, …) and **return** the desired {@link BranchConfig}.
|
|
28
|
+
* The same callback runs in two modes: against an existing branch (fields populated from
|
|
29
|
+
* Neon) and during pre-create evaluation (`exists: false`, `id` undefined).
|
|
30
|
+
*
|
|
31
|
+
* Pure function — no I/O, no side effects. The returned policy validates its output every
|
|
32
|
+
* time it is evaluated so errors point at the concrete branch target that triggered them.
|
|
33
|
+
*/
|
|
34
|
+
function defineConfig(input) {
|
|
35
|
+
if (typeof input !== "function") throw new ConfigValidationError(["defineConfig expects a function: `export default defineConfig((branch) => ({ ... }))`.", "Project-level config has moved to `neonctl link`; neon.ts now describes branch-level policy only."]);
|
|
36
|
+
return Object.freeze(input);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Evaluate a branch policy for a specific branch target and return a normalized config.
|
|
40
|
+
*/
|
|
41
|
+
function resolveConfig(config, branch) {
|
|
42
|
+
let raw;
|
|
43
|
+
try {
|
|
44
|
+
raw = config(Object.freeze({ ...branch }));
|
|
45
|
+
} catch (cause) {
|
|
46
|
+
throw new ConfigValidationError([`Config function threw while evaluating branch "${branch.name}".`, cause?.message ?? String(cause)]);
|
|
47
|
+
}
|
|
48
|
+
const parsed = branchConfigSchema.safeParse(raw);
|
|
49
|
+
if (!parsed.success) throw new ConfigValidationError(formatZodIssues(parsed.error));
|
|
50
|
+
const cfg = parsed.data;
|
|
51
|
+
const issues = [];
|
|
52
|
+
let ttlSeconds;
|
|
53
|
+
if (cfg.ttl !== void 0) {
|
|
54
|
+
const parsedTtl = parseDuration(cfg.ttl);
|
|
55
|
+
if ("error" in parsedTtl) issues.push(`ttl: ${parsedTtl.error}`);
|
|
56
|
+
else ttlSeconds = parsedTtl.seconds;
|
|
57
|
+
}
|
|
58
|
+
if (issues.length > 0) throw new ConfigValidationError(issues);
|
|
59
|
+
const resolved = {
|
|
60
|
+
authEnabled: isServiceEnabled(cfg.auth),
|
|
61
|
+
dataApiEnabled: isServiceEnabled(cfg.dataApi)
|
|
62
|
+
};
|
|
63
|
+
if (cfg.parent !== void 0) resolved.parent = cfg.parent;
|
|
64
|
+
if (ttlSeconds !== void 0) resolved.ttlSeconds = ttlSeconds;
|
|
65
|
+
if (cfg.protected !== void 0) resolved.protected = cfg.protected;
|
|
66
|
+
if (cfg.postgres) resolved.postgres = { ...cfg.postgres.computeSettings ? { computeSettings: { ...cfg.postgres.computeSettings } } : {} };
|
|
67
|
+
if (cfg.preview) resolved.preview = resolvePreviewConfig(cfg.preview);
|
|
68
|
+
return resolved;
|
|
69
|
+
}
|
|
70
|
+
function isServiceEnabled(service) {
|
|
71
|
+
return service !== void 0 && service.enabled !== false;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Normalize a {@link PreviewConfig} into a {@link ResolvedPreviewConfig}: apply per-function
|
|
75
|
+
* deploy defaults, default each bucket's access level to `private`, and collapse the
|
|
76
|
+
* `aiGateway` toggle to a boolean using the same present-and-not-`false` rule as
|
|
77
|
+
* `auth` / `dataApi`.
|
|
78
|
+
*/
|
|
79
|
+
function resolvePreviewConfig(preview) {
|
|
80
|
+
return {
|
|
81
|
+
functions: (preview.functions ?? []).map(resolveFunctionConfig),
|
|
82
|
+
buckets: (preview.buckets ?? []).map((bucket) => ({
|
|
83
|
+
name: bucket.name,
|
|
84
|
+
access: bucket.access ?? "private"
|
|
85
|
+
})),
|
|
86
|
+
aiGatewayEnabled: isServiceEnabled(preview.aiGateway)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function resolveFunctionConfig(fn) {
|
|
90
|
+
return {
|
|
91
|
+
slug: fn.slug,
|
|
92
|
+
name: fn.name,
|
|
93
|
+
source: fn.source,
|
|
94
|
+
env: { ...fn.env ?? {} },
|
|
95
|
+
runtime: fn.runtime ?? DEFAULT_FUNCTION_RUNTIME,
|
|
96
|
+
memoryMib: fn.memoryMib ?? DEFAULT_FUNCTION_MEMORY_MIB,
|
|
97
|
+
...fn.dev ? { dev: fn.dev } : {}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Normalize a region identifier to Neon's `<cloud>-<region>` format. When the user writes
|
|
102
|
+
* `us-east-1` we assume `aws-us-east-1`. Pure helper used by both the validator and the
|
|
103
|
+
* NeonApi adapter.
|
|
104
|
+
*/
|
|
105
|
+
function normalizeRegion(region) {
|
|
106
|
+
if (REGION_PREFIX.test(region)) return region;
|
|
107
|
+
return `aws-${region}`;
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
export { defineConfig, normalizeRegion, resolveConfig };
|
|
111
|
+
|
|
112
|
+
//# sourceMappingURL=define-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define-config.js","names":[],"sources":["../../src/lib/define-config.ts"],"sourcesContent":["import { parseDuration } from \"./duration.js\";\nimport { ConfigValidationError } from \"./errors.js\";\nimport { branchConfigSchema, formatZodIssues } from \"./schema.js\";\nimport type {\n\tBranchTarget,\n\tConfig,\n\tFunctionConfig,\n\tPreviewConfig,\n\tResolvedBranchConfig,\n\tResolvedFunctionConfig,\n\tResolvedPreviewConfig,\n} from \"./types.js\";\n\n/** Default deploy parameters applied to functions that omit them in `neon.ts`. */\nconst DEFAULT_FUNCTION_RUNTIME = \"nodejs24\" as const;\nconst DEFAULT_FUNCTION_MEMORY_MIB = 512 as const;\n\nconst REGION_PREFIX = /^(aws|azure|gcp)-/;\n\n/**\n * Validate and freeze a Neon Platform branch policy.\n *\n * Used at the top of `neon.ts`:\n * ```ts\n * import { defineConfig } from \"@neondatabase/config/v1\";\n *\n * export default defineConfig((branch) => {\n * if (branch.name === \"main\") {\n * return { protected: true, auth: {} };\n * }\n * return { parent: \"main\", ttl: \"7d\" };\n * });\n * ```\n *\n * The `branch` parameter is a **read-only {@link BranchTarget} descriptor** of the branch\n * this policy invocation is deciding for — not a live branch handle. You don't mutate it\n * (`branch.protected = true` does nothing); you switch on its facts (`branch.name`,\n * `branch.isDefault`, `branch.exists`, …) and **return** the desired {@link BranchConfig}.\n * The same callback runs in two modes: against an existing branch (fields populated from\n * Neon) and during pre-create evaluation (`exists: false`, `id` undefined).\n *\n * Pure function — no I/O, no side effects. The returned policy validates its output every\n * time it is evaluated so errors point at the concrete branch target that triggered them.\n */\nexport function defineConfig<const C extends Config>(input: C): C {\n\tif (typeof input !== \"function\") {\n\t\tthrow new ConfigValidationError([\n\t\t\t\"defineConfig expects a function: `export default defineConfig((branch) => ({ ... }))`.\",\n\t\t\t\"Project-level config has moved to `neonctl link`; neon.ts now describes branch-level policy only.\",\n\t\t]);\n\t}\n\n\treturn Object.freeze(input) as C;\n}\n\n/**\n * Evaluate a branch policy for a specific branch target and return a normalized config.\n */\nexport function resolveConfig(\n\tconfig: Config,\n\tbranch: BranchTarget,\n): ResolvedBranchConfig {\n\tlet raw: unknown;\n\ttry {\n\t\traw = config(Object.freeze({ ...branch }));\n\t} catch (cause) {\n\t\tthrow new ConfigValidationError([\n\t\t\t`Config function threw while evaluating branch \"${branch.name}\".`,\n\t\t\t(cause as Error)?.message ?? String(cause),\n\t\t]);\n\t}\n\n\tconst parsed = branchConfigSchema.safeParse(raw);\n\tif (!parsed.success) {\n\t\tthrow new ConfigValidationError(formatZodIssues(parsed.error));\n\t}\n\n\tconst cfg = parsed.data;\n\tconst issues: string[] = [];\n\tlet ttlSeconds: number | undefined;\n\tif (cfg.ttl !== undefined) {\n\t\tconst parsedTtl = parseDuration(cfg.ttl);\n\t\tif (\"error\" in parsedTtl) {\n\t\t\tissues.push(`ttl: ${parsedTtl.error}`);\n\t\t} else {\n\t\t\tttlSeconds = parsedTtl.seconds;\n\t\t}\n\t}\n\tif (issues.length > 0) {\n\t\tthrow new ConfigValidationError(issues);\n\t}\n\n\tconst resolved: ResolvedBranchConfig = {\n\t\tauthEnabled: isServiceEnabled(cfg.auth),\n\t\tdataApiEnabled: isServiceEnabled(cfg.dataApi),\n\t};\n\tif (cfg.parent !== undefined) resolved.parent = cfg.parent;\n\tif (ttlSeconds !== undefined) resolved.ttlSeconds = ttlSeconds;\n\tif (cfg.protected !== undefined) resolved.protected = cfg.protected;\n\tif (cfg.postgres) {\n\t\tresolved.postgres = {\n\t\t\t...(cfg.postgres.computeSettings\n\t\t\t\t? { computeSettings: { ...cfg.postgres.computeSettings } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\tif (cfg.preview) {\n\t\tresolved.preview = resolvePreviewConfig(cfg.preview);\n\t}\n\treturn resolved;\n}\n\nfunction isServiceEnabled(service: { enabled?: boolean } | undefined): boolean {\n\treturn service !== undefined && service.enabled !== false;\n}\n\n/**\n * Normalize a {@link PreviewConfig} into a {@link ResolvedPreviewConfig}: apply per-function\n * deploy defaults, default each bucket's access level to `private`, and collapse the\n * `aiGateway` toggle to a boolean using the same present-and-not-`false` rule as\n * `auth` / `dataApi`.\n */\nfunction resolvePreviewConfig(preview: PreviewConfig): ResolvedPreviewConfig {\n\treturn {\n\t\tfunctions: (preview.functions ?? []).map(resolveFunctionConfig),\n\t\tbuckets: (preview.buckets ?? []).map((bucket) => ({\n\t\t\tname: bucket.name,\n\t\t\taccess: bucket.access ?? \"private\",\n\t\t})),\n\t\taiGatewayEnabled: isServiceEnabled(preview.aiGateway),\n\t};\n}\n\nfunction resolveFunctionConfig(fn: FunctionConfig): ResolvedFunctionConfig {\n\treturn {\n\t\tslug: fn.slug,\n\t\tname: fn.name,\n\t\tsource: fn.source,\n\t\tenv: { ...(fn.env ?? {}) },\n\t\truntime: fn.runtime ?? DEFAULT_FUNCTION_RUNTIME,\n\t\tmemoryMib: fn.memoryMib ?? DEFAULT_FUNCTION_MEMORY_MIB,\n\t\t// Passed through untouched (no defaults); only `neon dev` reads it.\n\t\t...(fn.dev ? { dev: fn.dev } : {}),\n\t};\n}\n\n/**\n * Normalize a region identifier to Neon's `<cloud>-<region>` format. When the user writes\n * `us-east-1` we assume `aws-us-east-1`. Pure helper used by both the validator and the\n * NeonApi adapter.\n */\nexport function normalizeRegion(region: string): string {\n\tif (REGION_PREFIX.test(region)) return region;\n\treturn `aws-${region}`;\n}\n"],"mappings":";;;;;AAcA,MAAM,2BAA2B;AACjC,MAAM,8BAA8B;AAEpC,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BtB,SAAgB,aAAqC,OAAa;CACjE,IAAI,OAAO,UAAU,YACpB,MAAM,IAAI,sBAAsB,CAC/B,0FACA,mGACD,CAAC;CAGF,OAAO,OAAO,OAAO,KAAK;AAC3B;;;;AAKA,SAAgB,cACf,QACA,QACuB;CACvB,IAAI;CACJ,IAAI;EACH,MAAM,OAAO,OAAO,OAAO,EAAE,GAAG,OAAO,CAAC,CAAC;CAC1C,SAAS,OAAO;EACf,MAAM,IAAI,sBAAsB,CAC/B,kDAAkD,OAAO,KAAK,KAC7D,OAAiB,WAAW,OAAO,KAAK,CAC1C,CAAC;CACF;CAEA,MAAM,SAAS,mBAAmB,UAAU,GAAG;CAC/C,IAAI,CAAC,OAAO,SACX,MAAM,IAAI,sBAAsB,gBAAgB,OAAO,KAAK,CAAC;CAG9D,MAAM,MAAM,OAAO;CACnB,MAAM,SAAmB,CAAC;CAC1B,IAAI;CACJ,IAAI,IAAI,QAAQ,KAAA,GAAW;EAC1B,MAAM,YAAY,cAAc,IAAI,GAAG;EACvC,IAAI,WAAW,WACd,OAAO,KAAK,QAAQ,UAAU,OAAO;OAErC,aAAa,UAAU;CAEzB;CACA,IAAI,OAAO,SAAS,GACnB,MAAM,IAAI,sBAAsB,MAAM;CAGvC,MAAM,WAAiC;EACtC,aAAa,iBAAiB,IAAI,IAAI;EACtC,gBAAgB,iBAAiB,IAAI,OAAO;CAC7C;CACA,IAAI,IAAI,WAAW,KAAA,GAAW,SAAS,SAAS,IAAI;CACpD,IAAI,eAAe,KAAA,GAAW,SAAS,aAAa;CACpD,IAAI,IAAI,cAAc,KAAA,GAAW,SAAS,YAAY,IAAI;CAC1D,IAAI,IAAI,UACP,SAAS,WAAW,EACnB,GAAI,IAAI,SAAS,kBACd,EAAE,iBAAiB,EAAE,GAAG,IAAI,SAAS,gBAAgB,EAAE,IACvD,CAAC,EACL;CAED,IAAI,IAAI,SACP,SAAS,UAAU,qBAAqB,IAAI,OAAO;CAEpD,OAAO;AACR;AAEA,SAAS,iBAAiB,SAAqD;CAC9E,OAAO,YAAY,KAAA,KAAa,QAAQ,YAAY;AACrD;;;;;;;AAQA,SAAS,qBAAqB,SAA+C;CAC5E,OAAO;EACN,YAAY,QAAQ,aAAa,CAAC,GAAG,IAAI,qBAAqB;EAC9D,UAAU,QAAQ,WAAW,CAAC,GAAG,KAAK,YAAY;GACjD,MAAM,OAAO;GACb,QAAQ,OAAO,UAAU;EAC1B,EAAE;EACF,kBAAkB,iBAAiB,QAAQ,SAAS;CACrD;AACD;AAEA,SAAS,sBAAsB,IAA4C;CAC1E,OAAO;EACN,MAAM,GAAG;EACT,MAAM,GAAG;EACT,QAAQ,GAAG;EACX,KAAK,EAAE,GAAI,GAAG,OAAO,CAAC,EAAG;EACzB,SAAS,GAAG,WAAW;EACvB,WAAW,GAAG,aAAa;EAE3B,GAAI,GAAG,MAAM,EAAE,KAAK,GAAG,IAAI,IAAI,CAAC;CACjC;AACD;;;;;;AAOA,SAAgB,gBAAgB,QAAwB;CACvD,IAAI,cAAc,KAAK,MAAM,GAAG,OAAO;CACvC,OAAO,OAAO;AACf"}
|