@prisma/cli 3.0.0-beta.0 → 3.0.0-beta.10
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/README.md +18 -3
- package/dist/adapters/git.js +8 -3
- package/dist/adapters/local-state.js +12 -4
- package/dist/adapters/mock-api.js +81 -2
- package/dist/adapters/token-storage.js +63 -22
- package/dist/cli.js +17 -2
- package/dist/cli2.js +7 -3
- package/dist/commands/app/index.js +26 -13
- package/dist/commands/branch/index.js +2 -27
- package/dist/commands/database/index.js +159 -0
- package/dist/commands/env.js +8 -4
- package/dist/commands/project/index.js +28 -2
- package/dist/controllers/app-env-api.js +54 -0
- package/dist/controllers/app-env-file.js +181 -0
- package/dist/controllers/app-env.js +284 -132
- package/dist/controllers/app.js +493 -344
- package/dist/controllers/auth.js +8 -8
- package/dist/controllers/branch.js +78 -48
- package/dist/controllers/database.js +318 -0
- package/dist/controllers/project.js +302 -75
- package/dist/lib/app/branch-database-deploy.js +373 -0
- package/dist/lib/app/branch-database.js +316 -0
- package/dist/lib/app/bun-project.js +12 -5
- package/dist/lib/app/deploy-output.js +10 -1
- package/dist/lib/app/env-config.js +1 -1
- package/dist/lib/app/env-file.js +82 -0
- package/dist/lib/app/env-vars.js +28 -2
- package/dist/lib/app/local-dev.js +34 -18
- package/dist/lib/app/preview-branch-database.js +102 -0
- package/dist/lib/app/preview-build-settings.js +385 -0
- package/dist/lib/app/preview-build.js +272 -81
- package/dist/lib/app/preview-provider.js +163 -54
- package/dist/lib/app/production-deploy-gate.js +161 -0
- package/dist/lib/auth/auth-ops.js +69 -19
- package/dist/lib/auth/guard.js +3 -2
- package/dist/lib/auth/login.js +109 -14
- package/dist/lib/database/provider.js +167 -0
- package/dist/lib/diagnostics.js +15 -0
- package/dist/lib/git/local-branch.js +41 -0
- package/dist/lib/git/local-status.js +57 -0
- package/dist/lib/project/interactive-setup.js +56 -0
- package/dist/lib/project/local-pin.js +182 -33
- package/dist/lib/project/resolution.js +287 -105
- package/dist/lib/project/setup.js +132 -0
- package/dist/presenters/app-env.js +149 -14
- package/dist/presenters/app.js +170 -20
- package/dist/presenters/auth.js +19 -6
- package/dist/presenters/branch.js +37 -102
- package/dist/presenters/database.js +274 -0
- package/dist/presenters/project.js +100 -47
- package/dist/presenters/verbose-context.js +64 -0
- package/dist/shell/command-arguments.js +6 -0
- package/dist/shell/command-meta.js +139 -16
- package/dist/shell/command-runner.js +38 -8
- package/dist/shell/diagnostics-output.js +63 -0
- package/dist/shell/errors.js +14 -1
- package/dist/shell/output.js +13 -2
- package/dist/shell/runtime.js +3 -3
- package/dist/shell/ui.js +23 -1
- package/dist/shell/update-check.js +247 -0
- package/dist/use-cases/auth.js +15 -4
- package/dist/use-cases/branch.js +20 -68
- package/dist/use-cases/create-cli-gateways.js +2 -17
- package/dist/use-cases/project.js +2 -1
- package/package.json +12 -10
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { access, readFile } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
//#region src/lib/app/bun-project.ts
|
|
4
|
-
async function readBunPackageJson(appPath) {
|
|
4
|
+
async function readBunPackageJson(appPath, signal) {
|
|
5
5
|
const packageJsonPath = path.join(appPath, "package.json");
|
|
6
6
|
let content;
|
|
7
|
+
signal?.throwIfAborted();
|
|
7
8
|
try {
|
|
8
|
-
content = await readFile(packageJsonPath,
|
|
9
|
+
content = await readFile(packageJsonPath, {
|
|
10
|
+
encoding: "utf8",
|
|
11
|
+
signal
|
|
12
|
+
});
|
|
9
13
|
} catch (error) {
|
|
10
14
|
if (error.code === "ENOENT") return null;
|
|
11
15
|
throw new Error(`Failed to read ${packageJsonPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -20,17 +24,20 @@ function readBunPackageEntrypoint(packageJson) {
|
|
|
20
24
|
if (typeof packageJson?.main === "string") return packageJson.main;
|
|
21
25
|
if (typeof packageJson?.module === "string") return packageJson.module;
|
|
22
26
|
}
|
|
23
|
-
async function resolveBunEntrypoint(appPath, explicitEntrypoint) {
|
|
24
|
-
const packageJson = await readBunPackageJson(appPath);
|
|
27
|
+
async function resolveBunEntrypoint(appPath, explicitEntrypoint, signal) {
|
|
28
|
+
const packageJson = await readBunPackageJson(appPath, signal);
|
|
25
29
|
const candidate = explicitEntrypoint ?? readBunPackageEntrypoint(packageJson);
|
|
26
30
|
if (!candidate) throw new Error("Entrypoint is required. Pass --entry or define package.json main or module.");
|
|
27
31
|
if (path.isAbsolute(candidate)) throw new Error("Entrypoint must be a relative path.");
|
|
28
32
|
const normalized = path.normalize(candidate);
|
|
29
33
|
if (normalized.startsWith("..") || path.isAbsolute(normalized) || normalized.includes(`${path.sep}..${path.sep}`)) throw new Error("Entrypoint must not escape the app directory.");
|
|
30
34
|
const entrypointPath = path.join(appPath, normalized);
|
|
35
|
+
signal?.throwIfAborted();
|
|
31
36
|
try {
|
|
32
37
|
await access(entrypointPath);
|
|
33
|
-
|
|
38
|
+
signal?.throwIfAborted();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (signal?.aborted) throw error;
|
|
34
41
|
throw new Error(`Entrypoint file does not exist: ${entrypointPath}`);
|
|
35
42
|
}
|
|
36
43
|
return normalized.split(path.sep).join("/");
|
|
@@ -2,6 +2,7 @@ import { padDisplay } from "../../shell/ui.js";
|
|
|
2
2
|
//#region src/lib/app/deploy-output.ts
|
|
3
3
|
const DEPLOY_OUTPUT_MIN_LABEL_WIDTH = 9;
|
|
4
4
|
const DEPLOY_OUTPUT_MIN_VALUE_WIDTH = 9;
|
|
5
|
+
const DEPLOY_SETTINGS_MIN_KEY_WIDTH = 10;
|
|
5
6
|
function renderDeployOutputRows(ui, rows) {
|
|
6
7
|
if (rows.length === 0) return [];
|
|
7
8
|
const labelWidth = Math.max(DEPLOY_OUTPUT_MIN_LABEL_WIDTH, ...rows.map((row) => row.label.length));
|
|
@@ -11,5 +12,13 @@ function renderDeployOutputRows(ui, rows) {
|
|
|
11
12
|
return ` ${padDisplay(row.label, labelWidth)} ${padDisplay(ui.strong(row.value), valueWidth)}${row.origin ? ` ${ui.dim(`· ${row.origin}`)}` : ""}`.trimEnd();
|
|
12
13
|
});
|
|
13
14
|
}
|
|
15
|
+
function renderDeploySettingsPreview(ui, rows) {
|
|
16
|
+
if (rows.length === 0) return [];
|
|
17
|
+
const keyWidth = Math.max(DEPLOY_SETTINGS_MIN_KEY_WIDTH, ...rows.map((row) => `${row.key}:`.length));
|
|
18
|
+
const rail = ui.dim("│");
|
|
19
|
+
return rows.map((row) => {
|
|
20
|
+
return `${rail} ${ui.accent(padDisplay(`${row.key}:`, keyWidth))} ${ui.strong(row.value)}`;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
14
23
|
//#endregion
|
|
15
|
-
export { renderDeployOutputRows };
|
|
24
|
+
export { renderDeployOutputRows, renderDeploySettingsPreview };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { usageError } from "../../shell/errors.js";
|
|
2
|
+
import { validateKey } from "./env-config.js";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { parse } from "dotenv";
|
|
6
|
+
//#region src/lib/app/env-file.ts
|
|
7
|
+
const ASSIGNMENT_KEY_PATTERN = /^\s*(?:export\s+)?([^#=\s]+)\s*=/;
|
|
8
|
+
async function readEnvFileAssignments(cwd, filePath, command) {
|
|
9
|
+
const resolvedPath = path.resolve(cwd, filePath);
|
|
10
|
+
let contents;
|
|
11
|
+
try {
|
|
12
|
+
contents = await readFile(resolvedPath, "utf8");
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw usageError(`Failed to read env file "${filePath}"`, error instanceof Error ? error.message : "The file could not be read.", "Pass a readable dotenv file path.", [command === "deploy" ? "prisma-cli app deploy --env .env" : `prisma-cli project env ${command} --file .env --role preview`], "app");
|
|
15
|
+
}
|
|
16
|
+
return parseEnvFileContents(contents, filePath, command);
|
|
17
|
+
}
|
|
18
|
+
function parseEnvFileContents(contents, filePath, command) {
|
|
19
|
+
const parsedKeys = extractParsedKeys(contents);
|
|
20
|
+
if (parsedKeys.length === 0) throw usageError(`No environment variables found in "${filePath}"`, "The file does not contain any KEY=VALUE assignments.", "Pass a dotenv file with at least one non-empty variable.", [], "app");
|
|
21
|
+
const seen = /* @__PURE__ */ new Map();
|
|
22
|
+
for (const entry of parsedKeys) {
|
|
23
|
+
validateEnvFileKey(entry.key, entry.line, filePath, command);
|
|
24
|
+
const firstLine = seen.get(entry.key);
|
|
25
|
+
if (firstLine !== void 0) throw usageError(`Duplicate environment variable "${entry.key}" in "${filePath}"`, `Lines ${firstLine} and ${entry.line} both define ${entry.key}.`, "Keep one assignment for each key before importing the file.", [], "app");
|
|
26
|
+
seen.set(entry.key, entry.line);
|
|
27
|
+
}
|
|
28
|
+
const parsedValues = parse(contents);
|
|
29
|
+
return parsedKeys.map(({ key }) => {
|
|
30
|
+
const value = parsedValues[key];
|
|
31
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
32
|
+
const line = seen.get(key);
|
|
33
|
+
throw usageError(`Environment variable "${key}" in "${filePath}" has an empty value`, line === void 0 ? `${key} has an empty value.` : `Line ${line} defines ${key} with an empty value.`, "Pass a non-empty value, or omit the key from the file.", [], "app");
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
key,
|
|
37
|
+
value
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function extractParsedKeys(contents) {
|
|
42
|
+
const keys = [];
|
|
43
|
+
let multilineQuote = null;
|
|
44
|
+
const lines = contents.split(/\n/);
|
|
45
|
+
for (const [index, line] of lines.entries()) {
|
|
46
|
+
const lineNumber = index + 1;
|
|
47
|
+
if (multilineQuote !== null) {
|
|
48
|
+
if (hasClosingQuote(line, multilineQuote, 0)) multilineQuote = null;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const match = ASSIGNMENT_KEY_PATTERN.exec(line);
|
|
52
|
+
if (!match) continue;
|
|
53
|
+
const key = match[1];
|
|
54
|
+
keys.push({
|
|
55
|
+
key,
|
|
56
|
+
line: lineNumber
|
|
57
|
+
});
|
|
58
|
+
const valueStart = line.slice(match[0].length).trimStart();
|
|
59
|
+
const openingQuote = valueStart[0];
|
|
60
|
+
if ((openingQuote === "\"" || openingQuote === "'" || openingQuote === "`") && !hasClosingQuote(valueStart, openingQuote, 1)) multilineQuote = openingQuote;
|
|
61
|
+
}
|
|
62
|
+
return keys;
|
|
63
|
+
}
|
|
64
|
+
function validateEnvFileKey(key, line, filePath, command) {
|
|
65
|
+
try {
|
|
66
|
+
validateKey(key, command === "deploy" ? "add" : command);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
const reason = error instanceof Error && error.message.length > 0 ? error.message : "Invalid environment variable key.";
|
|
69
|
+
throw usageError(`Invalid environment variable "${key}" in "${filePath}"`, `Line ${line}: ${reason}`, "Use a valid env-var key and retry the import.", [], "app");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function hasClosingQuote(value, quote, startIndex) {
|
|
73
|
+
for (let index = startIndex; index < value.length; index += 1) if (value[index] === quote && !isEscaped(value, index)) return true;
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
function isEscaped(value, index) {
|
|
77
|
+
let backslashes = 0;
|
|
78
|
+
for (let cursor = index - 1; cursor >= 0 && value[cursor] === "\\"; cursor -= 1) backslashes += 1;
|
|
79
|
+
return backslashes % 2 === 1;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
export { readEnvFileAssignments };
|
package/dist/lib/app/env-vars.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { usageError } from "../../shell/errors.js";
|
|
2
|
+
import { validateKey } from "./env-config.js";
|
|
3
|
+
import { readEnvFileAssignments } from "./env-file.js";
|
|
2
4
|
//#region src/lib/app/env-vars.ts
|
|
3
5
|
function parseEnvAssignments(assignments, options) {
|
|
4
6
|
const values = assignments ?? [];
|
|
@@ -10,15 +12,39 @@ function parseEnvAssignments(assignments, options) {
|
|
|
10
12
|
if (separatorIndex === -1) throw usageError("Environment variable assignment must use NAME=VALUE", "A provided --env flag is missing the = separator.", `Pass repeated --env NAME=VALUE flags, for example prisma-cli app ${options.commandName} --env DATABASE_URL=postgresql://example.`, [`prisma-cli app ${options.commandName} --env DATABASE_URL=postgresql://example`], "app");
|
|
11
13
|
const name = assignment.slice(0, separatorIndex);
|
|
12
14
|
if (name.length === 0) throw usageError("Environment variable name is required", "A provided --env flag has an empty variable name.", `Pass repeated --env NAME=VALUE flags, for example prisma-cli app ${options.commandName} --env DATABASE_URL=postgresql://example.`, [`prisma-cli app ${options.commandName} --env DATABASE_URL=postgresql://example`], "app");
|
|
15
|
+
validateEnvAssignmentName(name, options.commandName);
|
|
13
16
|
if (seen.has(name)) throw usageError(`Environment variable "${name}" was provided more than once`, "Each environment variable name may be set only once per command invocation.", `Remove the duplicate "${name}" assignment and rerun prisma-cli app ${options.commandName}.`, [`prisma-cli app ${options.commandName} --env ${name}=value`], "app");
|
|
17
|
+
const value = assignment.slice(separatorIndex + 1);
|
|
18
|
+
if (value.length === 0) throw usageError(`Environment variable "${name}" has an empty value`, `A provided --env flag defines ${name} with no value.`, "Pass a non-empty value, or omit the key from the deploy command.", [`prisma-cli app ${options.commandName} --env ${name}=value`], "app");
|
|
14
19
|
seen.add(name);
|
|
15
|
-
parsed[name] =
|
|
20
|
+
parsed[name] = value;
|
|
16
21
|
}
|
|
17
22
|
return parsed;
|
|
18
23
|
}
|
|
24
|
+
async function parseEnvInputs(cwd, inputs, options) {
|
|
25
|
+
const values = inputs ?? [];
|
|
26
|
+
const expandedAssignments = [];
|
|
27
|
+
for (const value of values) {
|
|
28
|
+
if (value.includes("=")) {
|
|
29
|
+
expandedAssignments.push(value);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const fileAssignments = await readEnvFileAssignments(cwd, value, options.commandName);
|
|
33
|
+
expandedAssignments.push(...fileAssignments.map((assignment) => `${assignment.key}=${assignment.value}`));
|
|
34
|
+
}
|
|
35
|
+
return parseEnvAssignments(expandedAssignments, options);
|
|
36
|
+
}
|
|
37
|
+
function validateEnvAssignmentName(name, commandName) {
|
|
38
|
+
try {
|
|
39
|
+
validateKey(name, "add");
|
|
40
|
+
} catch (error) {
|
|
41
|
+
const reason = error instanceof Error && error.message.length > 0 ? error.message : "Invalid environment variable name.";
|
|
42
|
+
throw usageError(`Invalid environment variable "${name}"`, reason, "Use a valid env-var name and retry the deploy.", [`prisma-cli app ${commandName} --env DATABASE_URL=postgresql://example`], "app");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
19
45
|
function envVarNames(envVars) {
|
|
20
46
|
if (!envVars) return [];
|
|
21
47
|
return Object.entries(envVars).filter(([, value]) => value !== null).map(([name]) => name).sort((left, right) => left.localeCompare(right));
|
|
22
48
|
}
|
|
23
49
|
//#endregion
|
|
24
|
-
export { envVarNames,
|
|
50
|
+
export { envVarNames, parseEnvInputs };
|
|
@@ -10,14 +10,14 @@ const NEXT_CONFIG_FILENAMES = [
|
|
|
10
10
|
"next.config.mts"
|
|
11
11
|
];
|
|
12
12
|
const DEFAULT_LOCAL_DEV_PORT = 3e3;
|
|
13
|
-
async function resolveLocalBuildType(appPath, buildType) {
|
|
13
|
+
async function resolveLocalBuildType(appPath, buildType, signal) {
|
|
14
14
|
if (buildType === "bun" || buildType === "nextjs") return buildType;
|
|
15
15
|
if (buildType !== "auto") return null;
|
|
16
|
-
return detectLocalBuildType(appPath);
|
|
16
|
+
return detectLocalBuildType(appPath, signal);
|
|
17
17
|
}
|
|
18
|
-
async function detectLocalBuildType(appPath) {
|
|
19
|
-
if (await isNextProject(appPath)) return "nextjs";
|
|
20
|
-
if (await isBunProject(appPath)) return "bun";
|
|
18
|
+
async function detectLocalBuildType(appPath, signal) {
|
|
19
|
+
if (await isNextProject(appPath, signal)) return "nextjs";
|
|
20
|
+
if (await isBunProject(appPath, signal)) return "bun";
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
23
|
async function runLocalApp(options) {
|
|
@@ -58,7 +58,8 @@ async function runLocalApp(options) {
|
|
|
58
58
|
env: {
|
|
59
59
|
...options.env,
|
|
60
60
|
PORT: String(options.port)
|
|
61
|
-
}
|
|
61
|
+
},
|
|
62
|
+
signal: options.signal
|
|
62
63
|
}, spawnImpl, "Could not find the Next.js CLI. Install it with `npm install next` or ensure npx/bunx is available.");
|
|
63
64
|
return {
|
|
64
65
|
framework: "nextjs",
|
|
@@ -69,7 +70,7 @@ async function runLocalApp(options) {
|
|
|
69
70
|
signal: command.signal
|
|
70
71
|
};
|
|
71
72
|
}
|
|
72
|
-
const entrypoint = await resolveBunEntrypoint(options.appPath, options.entrypoint);
|
|
73
|
+
const entrypoint = await resolveBunEntrypoint(options.appPath, options.entrypoint, options.signal);
|
|
73
74
|
const command = await runWithFallback([{
|
|
74
75
|
command: "bun",
|
|
75
76
|
args: ["--watch", entrypoint],
|
|
@@ -79,7 +80,8 @@ async function runLocalApp(options) {
|
|
|
79
80
|
env: {
|
|
80
81
|
...options.env,
|
|
81
82
|
PORT: String(options.port)
|
|
82
|
-
}
|
|
83
|
+
},
|
|
84
|
+
signal: options.signal
|
|
83
85
|
}, spawnImpl, "Bun is required to run this app locally. Install it from https://bun.sh.");
|
|
84
86
|
return {
|
|
85
87
|
framework: "bun",
|
|
@@ -90,23 +92,37 @@ async function runLocalApp(options) {
|
|
|
90
92
|
signal: command.signal
|
|
91
93
|
};
|
|
92
94
|
}
|
|
93
|
-
async function isNextProject(appPath) {
|
|
94
|
-
for (const fileName of NEXT_CONFIG_FILENAMES)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
95
|
+
async function isNextProject(appPath, signal) {
|
|
96
|
+
for (const fileName of NEXT_CONFIG_FILENAMES) {
|
|
97
|
+
signal?.throwIfAborted();
|
|
98
|
+
try {
|
|
99
|
+
await access(path.join(appPath, fileName));
|
|
100
|
+
signal?.throwIfAborted();
|
|
101
|
+
return true;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (signal?.aborted) throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return hasDependency(await readBunPackageJson(appPath, signal), "next");
|
|
99
107
|
}
|
|
100
|
-
async function isBunProject(appPath) {
|
|
108
|
+
async function isBunProject(appPath, signal) {
|
|
109
|
+
signal?.throwIfAborted();
|
|
101
110
|
try {
|
|
102
111
|
await access(path.join(appPath, "bun.lock"));
|
|
112
|
+
signal?.throwIfAborted();
|
|
103
113
|
return true;
|
|
104
|
-
} catch {
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (signal?.aborted) throw error;
|
|
116
|
+
}
|
|
117
|
+
signal?.throwIfAborted();
|
|
105
118
|
try {
|
|
106
119
|
await access(path.join(appPath, "bun.lockb"));
|
|
120
|
+
signal?.throwIfAborted();
|
|
107
121
|
return true;
|
|
108
|
-
} catch {
|
|
109
|
-
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (signal?.aborted) throw error;
|
|
124
|
+
}
|
|
125
|
+
const packageJson = await readBunPackageJson(appPath, signal);
|
|
110
126
|
if (!packageJson) return false;
|
|
111
127
|
const hasEntrypoint = typeof readBunPackageEntrypoint(packageJson) === "string";
|
|
112
128
|
const hasBunDependency = hasDependency(packageJson, "@types/bun") || hasDependency(packageJson, "bun");
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
//#region src/lib/app/preview-branch-database.ts
|
|
2
|
+
async function createBranchDatabase(client, options) {
|
|
3
|
+
const result = await client.POST("/v1/databases", {
|
|
4
|
+
body: {
|
|
5
|
+
projectId: options.projectId,
|
|
6
|
+
branchId: options.branchId,
|
|
7
|
+
name: options.branchName,
|
|
8
|
+
source: { type: "empty" }
|
|
9
|
+
},
|
|
10
|
+
signal: options.signal
|
|
11
|
+
});
|
|
12
|
+
if (result.error || !result.data) throw apiCallError(`Failed to create database for branch "${options.branchName}"`, result.response, result.error);
|
|
13
|
+
return normalizeBranchDatabaseRecord(result.data.data);
|
|
14
|
+
}
|
|
15
|
+
async function listEnvironmentVariables(client, options) {
|
|
16
|
+
const variables = [];
|
|
17
|
+
let cursor;
|
|
18
|
+
while (true) {
|
|
19
|
+
const result = await client.GET("/v1/environment-variables", {
|
|
20
|
+
params: { query: {
|
|
21
|
+
projectId: options.projectId,
|
|
22
|
+
class: options.className,
|
|
23
|
+
key: options.key,
|
|
24
|
+
branchId: options.branchId,
|
|
25
|
+
cursor
|
|
26
|
+
} },
|
|
27
|
+
signal: options.signal
|
|
28
|
+
});
|
|
29
|
+
if (result.error || !result.data) throw apiCallError("Failed to list environment variables", result.response, result.error);
|
|
30
|
+
variables.push(...result.data.data);
|
|
31
|
+
if (!result.data.pagination.hasMore || !result.data.pagination.nextCursor) break;
|
|
32
|
+
cursor = result.data.pagination.nextCursor;
|
|
33
|
+
}
|
|
34
|
+
return variables.map((variable) => normalizeEnvironmentVariable(variable));
|
|
35
|
+
}
|
|
36
|
+
async function createEnvironmentVariable(client, options) {
|
|
37
|
+
const result = await client.POST("/v1/environment-variables", {
|
|
38
|
+
body: {
|
|
39
|
+
projectId: options.projectId,
|
|
40
|
+
class: options.className,
|
|
41
|
+
key: options.key,
|
|
42
|
+
value: options.value,
|
|
43
|
+
...options.branchId ? { branchId: options.branchId } : {}
|
|
44
|
+
},
|
|
45
|
+
signal: options.signal
|
|
46
|
+
});
|
|
47
|
+
if (result.error || !result.data) throw apiCallError(`Failed to add ${options.key}`, result.response, result.error);
|
|
48
|
+
return normalizeEnvironmentVariable(result.data.data);
|
|
49
|
+
}
|
|
50
|
+
async function deleteBranchDatabase(client, options) {
|
|
51
|
+
const result = await client.DELETE("/v1/databases/{databaseId}", {
|
|
52
|
+
params: { path: { databaseId: options.databaseId } },
|
|
53
|
+
signal: options.signal
|
|
54
|
+
});
|
|
55
|
+
if (result.error) throw apiCallError("Failed to delete branch database", result.response, result.error);
|
|
56
|
+
}
|
|
57
|
+
async function updateEnvironmentVariable(client, options) {
|
|
58
|
+
const result = await client.PATCH("/v1/environment-variables/{envVarId}", {
|
|
59
|
+
params: { path: { envVarId: options.envVarId } },
|
|
60
|
+
body: { value: options.value },
|
|
61
|
+
signal: options.signal
|
|
62
|
+
});
|
|
63
|
+
if (result.error || !result.data) throw apiCallError("Failed to update environment variable", result.response, result.error);
|
|
64
|
+
return normalizeEnvironmentVariable(result.data.data);
|
|
65
|
+
}
|
|
66
|
+
async function deleteEnvironmentVariable(client, options) {
|
|
67
|
+
const result = await client.DELETE("/v1/environment-variables/{envVarId}", {
|
|
68
|
+
params: { path: { envVarId: options.envVarId } },
|
|
69
|
+
signal: options.signal
|
|
70
|
+
});
|
|
71
|
+
if (result.error) throw apiCallError("Failed to delete environment variable", result.response, result.error);
|
|
72
|
+
}
|
|
73
|
+
function normalizeEnvironmentVariable(variable) {
|
|
74
|
+
return {
|
|
75
|
+
id: variable.id,
|
|
76
|
+
key: variable.key,
|
|
77
|
+
branchId: variable.branchId,
|
|
78
|
+
className: variable.class,
|
|
79
|
+
isManagedBySystem: variable.isManagedBySystem
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function normalizeBranchDatabaseRecord(database) {
|
|
83
|
+
const connection = database.connections?.[0];
|
|
84
|
+
const databaseUrl = connection?.endpoints?.pooled?.connectionString;
|
|
85
|
+
const directUrl = connection?.endpoints?.direct?.connectionString ?? null;
|
|
86
|
+
if (!databaseUrl) throw new Error("Created database did not return a pooled connection string.");
|
|
87
|
+
return {
|
|
88
|
+
id: database.id,
|
|
89
|
+
name: database.name,
|
|
90
|
+
branchId: database.branchId,
|
|
91
|
+
databaseUrl,
|
|
92
|
+
directUrl
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function apiCallError(summary, response, error) {
|
|
96
|
+
if (response.status === 404) return /* @__PURE__ */ new Error("Resource Not Found");
|
|
97
|
+
const message = error.error?.message ?? `Management API returned HTTP ${response.status}.`;
|
|
98
|
+
const hint = error.error?.hint ? ` ${error.error.hint}` : "";
|
|
99
|
+
return /* @__PURE__ */ new Error(`${summary}: ${message}${hint}`);
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
export { createBranchDatabase, createEnvironmentVariable, deleteBranchDatabase, deleteEnvironmentVariable, listEnvironmentVariables, updateEnvironmentVariable };
|