create-stitchkit 0.7.0 → 0.8.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.
- package/CHANGELOG.md +15 -0
- package/UPGRADING.md +21 -0
- package/dist/cli.js +0 -2
- package/examples/repository/project.json +0 -1
- package/package.json +2 -2
- package/template/bun.lock +2 -7
- package/template/package.json +1 -1
- package/template/packages/backend/package.json +0 -1
- package/template/packages/backend/src/cli.ts +2 -2
- package/template/packages/backend/src/index.ts +3 -3
- package/template/packages/config/package.json +0 -1
- package/template/packages/config/src/app-identity.generated.ts +0 -1
- package/template/packages/config/src/declaration.ts +10 -0
- package/template/packages/db/package.json +0 -1
- package/template/packages/frontend/package.json +0 -1
- package/template/packages/shared/package.json +0 -1
- package/template/project.json +0 -1
- package/template/scripts/declaration.ts +4 -87
- package/template/scripts/env-variables.ts +83 -0
- package/templates/agent/package.json +1 -1
- package/templates/agent/project.json +0 -1
- package/templates/telegram-bot/project.json +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -12,6 +12,21 @@ step is overwritten by the next release.
|
|
|
12
12
|
|
|
13
13
|
## [Unreleased]
|
|
14
14
|
|
|
15
|
+
## [0.8.1] - 2026-11-25
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- Updated the starter catalog to `stitchkit@^0.129.0` and the agent template catalog to `stitchkit-tui@^0.5.2`.
|
|
20
|
+
|
|
21
|
+
## [0.8.0] - 2026-11-25
|
|
22
|
+
|
|
23
|
+
### ⚠️ Breaking changes
|
|
24
|
+
|
|
25
|
+
**Who must act:** every project scaffolded from an earlier `create-stitchkit` release that upgrades to `stitchkit@^0.128.0`, still carries `identity.version` in `project.json` or duplicate `"version"` fields in private workspace `packages/*/package.json`, or reads `appDeclaration.identity.version` in `packages/backend/src/{cli,index}.ts` ([migration](./UPGRADING.md#single-root-version)).
|
|
26
|
+
|
|
27
|
+
- Removed `identity.version` from all template `project.json` files (`template/`, `examples/repository/`, `templates/agent/`, `templates/telegram-bot/`), `packages/config/src/app-identity.generated.ts`, and `createApplicationIdentity()`, and removed duplicate `"version": "0.1.0"` fields from all private workspace subpackages (`template/packages/{backend,config,db,frontend,shared}/package.json`). The root `package.json` is now the single version source, exported as `appVersion` from `packages/config/src/declaration.ts` and passed to `createCli`, `createMcpHandler` (`serverInfo.version`), and `generateOpenApiDocument` (`info.version`). Before: `version: appDeclaration.identity.version`. After: `version: appVersion`. Existing projects adopt it by [`UPGRADING.md`](./UPGRADING.md#single-root-version).
|
|
28
|
+
- Updated the starter catalog to `stitchkit@^0.128.0` and the agent template catalog to `stitchkit-tui@^0.5.1`.
|
|
29
|
+
|
|
15
30
|
## [0.7.0] - 2026-11-25
|
|
16
31
|
|
|
17
32
|
### ⚠️ Breaking changes
|
package/UPGRADING.md
CHANGED
|
@@ -61,6 +61,27 @@ the first scaffolder release with a migration channel of its own.
|
|
|
61
61
|
|
|
62
62
|
---
|
|
63
63
|
|
|
64
|
+
## Released migration: 0.8.0
|
|
65
|
+
|
|
66
|
+
### single-root-version
|
|
67
|
+
|
|
68
|
+
**Required.** Adopt these edits together with upgrading `stitchkit` to `0.128.0`
|
|
69
|
+
(see [`docs/guide/upgrading.md`](../../docs/guide/upgrading.md#declaration-single-version) for the
|
|
70
|
+
framework change).
|
|
71
|
+
|
|
72
|
+
1. Remove `"version"` from `project.json` under `identity` and regenerate
|
|
73
|
+
`packages/config/src/app-identity.generated.ts` (`bun run gen:declaration`).
|
|
74
|
+
2. Remove duplicate `"version"` fields from private internal workspace manifests
|
|
75
|
+
(`packages/*/package.json` where `"private": true`), keeping `"version"` only in the root
|
|
76
|
+
`package.json` (unless a subpackage is released independently).
|
|
77
|
+
3. Export `appVersion` from `packages/config/src/declaration.ts` by reading `../../../package.json`
|
|
78
|
+
and pass `appVersion` instead of `appDeclaration.identity.version` in
|
|
79
|
+
`packages/backend/src/cli.ts` (`createCli`) and `packages/backend/src/index.ts`
|
|
80
|
+
(`createMcpHandler` `serverInfo.version` and `generateOpenApiDocument` `info.version`).
|
|
81
|
+
4. Update the root `package.json` catalog to `"stitchkit": "^0.128.0"` (and in agent-template
|
|
82
|
+
projects, `"stitchkit-tui": "^0.5.1"`), run `bun install`, and verify with `bun run check` and
|
|
83
|
+
`bun run test`.
|
|
84
|
+
|
|
64
85
|
## Released migration: 0.7.0
|
|
65
86
|
|
|
66
87
|
### starter uses stitchkit/testing and mandatory tool access
|
package/dist/cli.js
CHANGED
|
@@ -73,7 +73,6 @@ var SemanticVersionSchema = z2.string().regex(/^\d+\.\d+\.\d+$/, "Use a semantic
|
|
|
73
73
|
var ProjectIdentitySchema = z2.object({
|
|
74
74
|
slug: ProjectSlugSchema,
|
|
75
75
|
name: refuseMachineNames("A project name")(z2.string().trim().min(1).max(80)),
|
|
76
|
-
version: SemanticVersionSchema,
|
|
77
76
|
description: ProjectDescriptionSchema
|
|
78
77
|
}).strict();
|
|
79
78
|
var ProjectStarterSchema = z2.strictObject({
|
|
@@ -205,7 +204,6 @@ function createApplicationIdentity(destination, displayName) {
|
|
|
205
204
|
return ProjectIdentitySchema.parse({
|
|
206
205
|
slug,
|
|
207
206
|
name,
|
|
208
|
-
version: "0.1.0",
|
|
209
207
|
description: {
|
|
210
208
|
en: `${name} is a production application built with Stitchkit.`,
|
|
211
209
|
ru: `${name} \u2014 production-\u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \u043D\u0430 Stitchkit.`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-stitchkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Create a production-shaped Stitchkit application",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Max Listov <maxlistov@gmail.com>",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"@types/react": "^19.3.0",
|
|
89
89
|
"ai": "^7.0.179",
|
|
90
90
|
"react": "^19.3.1",
|
|
91
|
-
"stitchkit": "0.
|
|
91
|
+
"stitchkit": "0.129.0",
|
|
92
92
|
"typescript": "^7.0.2"
|
|
93
93
|
},
|
|
94
94
|
"engines": {
|
package/template/bun.lock
CHANGED
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
},
|
|
26
26
|
"packages/backend": {
|
|
27
27
|
"name": "@app/backend",
|
|
28
|
-
"version": "0.1.0",
|
|
29
28
|
"dependencies": {
|
|
30
29
|
"@app/config": "workspace:*",
|
|
31
30
|
"@app/db": "workspace:*",
|
|
@@ -45,7 +44,6 @@
|
|
|
45
44
|
},
|
|
46
45
|
"packages/config": {
|
|
47
46
|
"name": "@app/config",
|
|
48
|
-
"version": "0.1.0",
|
|
49
47
|
"dependencies": {
|
|
50
48
|
"@t3-oss/env-core": "^0.13.11",
|
|
51
49
|
"dotenv": "^18.0.10",
|
|
@@ -59,7 +57,6 @@
|
|
|
59
57
|
},
|
|
60
58
|
"packages/db": {
|
|
61
59
|
"name": "@app/db",
|
|
62
|
-
"version": "0.1.0",
|
|
63
60
|
"dependencies": {
|
|
64
61
|
"@app/config": "workspace:*",
|
|
65
62
|
"@prisma/adapter-pg": "^7.10.1",
|
|
@@ -74,7 +71,6 @@
|
|
|
74
71
|
},
|
|
75
72
|
"packages/frontend": {
|
|
76
73
|
"name": "@app/frontend",
|
|
77
|
-
"version": "0.1.0",
|
|
78
74
|
"dependencies": {
|
|
79
75
|
"@app/config": "workspace:*",
|
|
80
76
|
"@app/shared": "workspace:*",
|
|
@@ -127,7 +123,6 @@
|
|
|
127
123
|
},
|
|
128
124
|
"packages/shared": {
|
|
129
125
|
"name": "@app/shared",
|
|
130
|
-
"version": "0.1.0",
|
|
131
126
|
"dependencies": {
|
|
132
127
|
"stitchkit": "catalog:",
|
|
133
128
|
"zod": "^4.6.7",
|
|
@@ -145,7 +140,7 @@
|
|
|
145
140
|
"zod": "4.6.7",
|
|
146
141
|
},
|
|
147
142
|
"catalog": {
|
|
148
|
-
"stitchkit": "^0.
|
|
143
|
+
"stitchkit": "^0.129.0",
|
|
149
144
|
},
|
|
150
145
|
"packages": {
|
|
151
146
|
"@ai-sdk/gateway": ["@ai-sdk/gateway@4.0.146", "", { "dependencies": { "@ai-sdk/provider": "4.0.38", "@ai-sdk/provider-utils": "5.0.81", "@vercel/oidc": "3.2.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-rJ5NQSyjnpo6HSBEYc9Ff6k1EROUuB7KeAfHYVbVDsk68ynY0Eb8JeWflZKXlT6NhYgqyCiBDxE5blXWACXMKQ=="],
|
|
@@ -1126,7 +1121,7 @@
|
|
|
1126
1121
|
|
|
1127
1122
|
"std-env": ["std-env@3.10.0", "", {}, "sha512-5GS12FdOZNliM5mAOxFRg7Ir0pWz8MdpYm6AY6VPkGpbA7ZzmbzNcBJQ0GPvvyWgcY7QAhCgf9Uy89I03faLkg=="],
|
|
1128
1123
|
|
|
1129
|
-
"stitchkit": ["stitchkit@0.
|
|
1124
|
+
"stitchkit": ["stitchkit@0.129.0", "", { "dependencies": { "ky": "^2.2.0" }, "peerDependencies": { "@anthropic-ai/claude-agent-sdk": "^0.3.330", "@modelcontextprotocol/ext-apps": "^1.7.2 || ^2.0.0", "@modelcontextprotocol/server": "^2.0.0", "@openrouter/ai-sdk-provider": "^3.1.0", "@opentelemetry/api": "^1.9.0", "@socket.io/bun-engine": "^0.1.2", "@socket.io/component-emitter": "^3.1.2", "@tanstack/react-query": ">=5", "@types/bun": "^1.4.2", "ai": "^7.0.107", "google-auth-library": "^11.1.0", "grammy": "^1.46.0", "maxmind": "^5.0.7", "react": ">=18", "react-query-kit": "^3.3.3", "socket.io": "^4.8.3", "socket.io-client": "^4.8.3", "srvx": "^0.12.5 || ^1.0.5", "web-vitals": "^6.2.3", "zod": "^4.6.5" }, "optionalPeers": ["@anthropic-ai/claude-agent-sdk", "@modelcontextprotocol/ext-apps", "@modelcontextprotocol/server", "@openrouter/ai-sdk-provider", "@opentelemetry/api", "@socket.io/bun-engine", "@socket.io/component-emitter", "@tanstack/react-query", "@types/bun", "ai", "google-auth-library", "grammy", "maxmind", "react", "react-query-kit", "socket.io", "socket.io-client", "srvx", "web-vitals"], "bin": { "stitchkit": "dist/entrypoints/bin/upgrade-cli.js" } }, "sha512-/wnMbqOFdFya8FX1mnGAmrTOequqzfriuS5P6+MqlF3+MF5a8t4YygZB5TnTOUGpzzdNxSOeALITE4fTL4i1rQ=="],
|
|
1130
1125
|
|
|
1131
1126
|
"stringify-entities": ["stringify-entities@4.0.4", "", { "dependencies": { "character-entities-html4": "^2.0.0", "character-entities-legacy": "^3.0.0" } }, "sha512-IwfBptatlO+QCJUo19AqvrPNqlVMpW9YEL2LIVY+Rpv2qsjCGxaDLNRgeGsQWJhfItebuJhsGSLjaBbNSQ+ieg=="],
|
|
1132
1127
|
|
package/template/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
|
-
import { appDeclaration } from '@app/config/declaration';
|
|
3
|
+
import { appDeclaration, appVersion } from '@app/config/declaration';
|
|
4
4
|
import { createCli } from 'stitchkit/cli';
|
|
5
5
|
import { createSurface } from './surface';
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ const { services, socket } = await createSurface();
|
|
|
9
9
|
try {
|
|
10
10
|
await createCli({
|
|
11
11
|
name: appDeclaration.identity.slug,
|
|
12
|
-
version:
|
|
12
|
+
version: appVersion,
|
|
13
13
|
services,
|
|
14
14
|
access: { open: 'local-operator' },
|
|
15
15
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { env } from '@app/config';
|
|
2
|
-
import { apiRole, appDeclaration } from '@app/config/declaration';
|
|
2
|
+
import { apiRole, appDeclaration, appVersion } from '@app/config/declaration';
|
|
3
3
|
import { serviceSurfacesAreOpen } from '@app/config/variables';
|
|
4
4
|
import { wrapInRequestContext } from 'stitchkit/observability';
|
|
5
5
|
import {
|
|
@@ -20,7 +20,7 @@ async function main(): Promise<void> {
|
|
|
20
20
|
const mcp = createMcpHandler({
|
|
21
21
|
serverInfo: {
|
|
22
22
|
name: appDeclaration.identity.slug,
|
|
23
|
-
version:
|
|
23
|
+
version: appVersion,
|
|
24
24
|
},
|
|
25
25
|
auth: () => ({ scope: 'public' }),
|
|
26
26
|
// Every starter contract is public; replace with `{ gate: { beforeHandle } }`
|
|
@@ -31,7 +31,7 @@ async function main(): Promise<void> {
|
|
|
31
31
|
const openApi = generateOpenApiDocument({
|
|
32
32
|
info: {
|
|
33
33
|
title: `${appDeclaration.identity.name} API`,
|
|
34
|
-
version:
|
|
34
|
+
version: appVersion,
|
|
35
35
|
},
|
|
36
36
|
groups: [{ pathPrefix: '/api', services }],
|
|
37
37
|
});
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
export const appIdentity = {
|
|
13
13
|
"slug": "stitchkit-starter",
|
|
14
14
|
"name": "Stitchkit Starter",
|
|
15
|
-
"version": "0.1.0",
|
|
16
15
|
"description": {
|
|
17
16
|
"en": "Stitchkit Starter is a production application built with Stitchkit.",
|
|
18
17
|
"ru": "Stitchkit Starter — production-приложение на Stitchkit."
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { findProjectRole, parseProjectDeclaration } from 'stitchkit/declaration';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import rootManifest from '../../../package.json' with { type: 'json' };
|
|
2
4
|
import source from '../../../project.json' with { type: 'json' };
|
|
3
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Single version of the application, read from the root `package.json` so
|
|
8
|
+
* OpenAPI, MCP and the CLI never drift from the release version.
|
|
9
|
+
*/
|
|
10
|
+
export const appVersion = z
|
|
11
|
+
.looseObject({ version: z.string().min(1) })
|
|
12
|
+
.parse(rootManifest).version;
|
|
13
|
+
|
|
4
14
|
/**
|
|
5
15
|
* What this repository says about itself — the one machine-readable statement
|
|
6
16
|
* that is true with no machine in existence.
|
package/template/project.json
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { resolve } from 'node:path';
|
|
2
|
-
import type {
|
|
3
|
-
ProjectDeclaration,
|
|
4
|
-
ProjectEnvVariable,
|
|
5
|
-
ProjectRole,
|
|
6
|
-
} from 'stitchkit/declaration';
|
|
7
|
-
import { z } from 'zod';
|
|
2
|
+
import type { ProjectDeclaration, ProjectRole } from 'stitchkit/declaration';
|
|
8
3
|
import { appDeclaration } from '../packages/config/src/declaration';
|
|
9
4
|
import * as shutdownBudgets from '../packages/config/src/shutdown';
|
|
10
|
-
import {
|
|
5
|
+
import { renderEnvVariables } from './env-variables';
|
|
6
|
+
|
|
7
|
+
export { renderEnvVariables };
|
|
11
8
|
|
|
12
9
|
/**
|
|
13
10
|
* Everything derived from the project declaration.
|
|
@@ -60,86 +57,6 @@ export function terminationBudgetMs(role: ProjectRole): number {
|
|
|
60
57
|
return role.drainFloorMs + FORCE_BUDGET_MS + CLEANUP_BUDGET_MS;
|
|
61
58
|
}
|
|
62
59
|
|
|
63
|
-
/**
|
|
64
|
-
* JSON Schema types this projection can carry into the declaration.
|
|
65
|
-
*
|
|
66
|
-
* Mapping `number` to `integer` would misrepresent fractional values and make
|
|
67
|
-
* the declaration disagree with its Zod source. A type with no faithful shape
|
|
68
|
-
* is rejected: the
|
|
69
|
-
* declaration format gains the shape, or the project stops declaring that type.
|
|
70
|
-
*/
|
|
71
|
-
const SHAPE_BY_JSON_TYPE: Record<string, ProjectEnvVariable['shape']> = {
|
|
72
|
-
integer: 'integer',
|
|
73
|
-
boolean: 'boolean',
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const JsonSchemaSchema = z.object({
|
|
77
|
-
properties: z.record(
|
|
78
|
-
z.string(),
|
|
79
|
-
z.object({
|
|
80
|
-
type: z.string().optional(),
|
|
81
|
-
format: z.string().optional(),
|
|
82
|
-
enum: z.array(z.unknown()).optional(),
|
|
83
|
-
}),
|
|
84
|
-
),
|
|
85
|
-
required: z.array(z.string()).optional(),
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* The variables a deployment must supply, derived from the one Zod declaration.
|
|
90
|
-
*
|
|
91
|
-
* `required` follows the schema exactly: a variable with a default or an
|
|
92
|
-
* `.optional()` is not required, and nothing here restates that judgement. An
|
|
93
|
-
* enum carries its members, because "one of an unnamed set" tells a reader
|
|
94
|
-
* without a TypeScript runtime nothing — and that reader is the whole point.
|
|
95
|
-
*/
|
|
96
|
-
export function renderEnvVariables(
|
|
97
|
-
variables: Record<string, z.ZodType> = applicationVariables,
|
|
98
|
-
): ProjectEnvVariable[] {
|
|
99
|
-
const json = JsonSchemaSchema.parse(
|
|
100
|
-
z.toJSONSchema(z.object(variables), { io: 'input', unrepresentable: 'any' }),
|
|
101
|
-
);
|
|
102
|
-
const required = new Set(json.required ?? []);
|
|
103
|
-
return Object.entries(json.properties)
|
|
104
|
-
.map(([name, property]) => describeVariable(name, property, required.has(name)))
|
|
105
|
-
.sort((left, right) => left.name.localeCompare(right.name));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function describeVariable(
|
|
109
|
-
name: string,
|
|
110
|
-
property: { type?: string; format?: string; enum?: unknown[] },
|
|
111
|
-
required: boolean,
|
|
112
|
-
): ProjectEnvVariable {
|
|
113
|
-
if (property.enum) {
|
|
114
|
-
// Members are rejected rather than stringified. `String(member)` would turn
|
|
115
|
-
// numbers and booleans into text that does not match the value the Zod
|
|
116
|
-
// schema accepts, so a deployment reading the declaration would supply
|
|
117
|
-
// something the application then rejects — the declaration would be derived
|
|
118
|
-
// and still wrong.
|
|
119
|
-
const members = property.enum.filter((member) => typeof member === 'string');
|
|
120
|
-
if (members.length !== property.enum.length) {
|
|
121
|
-
throw new Error(
|
|
122
|
-
`Environment variable ${name} declares a non-string enum member. The declaration format carries enum members as strings; declare it as a string enum, or extend the format first.`,
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
return { name, shape: 'enum', required, members };
|
|
126
|
-
}
|
|
127
|
-
if (property.format === 'uri') return { name, shape: 'url', required };
|
|
128
|
-
if (property.type === undefined || property.type === 'string') {
|
|
129
|
-
return { name, shape: 'string', required };
|
|
130
|
-
}
|
|
131
|
-
const shape = SHAPE_BY_JSON_TYPE[property.type];
|
|
132
|
-
if (!shape) {
|
|
133
|
-
// Fail closed rather than describe the variable with the wrong shape: a
|
|
134
|
-
// reader acting on `string` when the value is something else is worse off
|
|
135
|
-
// than a reader told this project cannot describe it.
|
|
136
|
-
throw new Error(
|
|
137
|
-
`Cannot describe ${name}: no declaration shape for JSON Schema type "${property.type}".`,
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
return { name, shape, required };
|
|
141
|
-
}
|
|
142
|
-
|
|
143
60
|
/** The declaration as it must appear on disk: authored fields plus derived ones. */
|
|
144
61
|
export function renderProjectJson(): string {
|
|
145
62
|
const declaration: ProjectDeclaration = {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { ProjectEnvVariable } from 'stitchkit/declaration';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { applicationVariables } from '../packages/config/src/variables';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* JSON Schema types this projection can carry into the declaration.
|
|
7
|
+
*
|
|
8
|
+
* Mapping `number` to `integer` would misrepresent fractional values and make
|
|
9
|
+
* the declaration disagree with its Zod source. A type with no faithful shape
|
|
10
|
+
* is rejected: the declaration format gains the shape, or the project stops
|
|
11
|
+
* declaring that type.
|
|
12
|
+
*/
|
|
13
|
+
const SHAPE_BY_JSON_TYPE: Record<string, ProjectEnvVariable['shape']> = {
|
|
14
|
+
integer: 'integer',
|
|
15
|
+
boolean: 'boolean',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const JsonSchemaSchema = z.object({
|
|
19
|
+
properties: z.record(
|
|
20
|
+
z.string(),
|
|
21
|
+
z.object({
|
|
22
|
+
type: z.string().optional(),
|
|
23
|
+
format: z.string().optional(),
|
|
24
|
+
enum: z.array(z.unknown()).optional(),
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
required: z.array(z.string()).optional(),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The variables a deployment must supply, derived from the one Zod declaration.
|
|
32
|
+
*
|
|
33
|
+
* `required` follows the schema exactly: a variable with a default or an
|
|
34
|
+
* `.optional()` is not required, and nothing here restates that judgement. An
|
|
35
|
+
* enum carries its members, because "one of an unnamed set" tells a reader
|
|
36
|
+
* without a TypeScript runtime nothing — and that reader is the whole point.
|
|
37
|
+
*/
|
|
38
|
+
export function renderEnvVariables(
|
|
39
|
+
variables: Record<string, z.ZodType> = applicationVariables,
|
|
40
|
+
): ProjectEnvVariable[] {
|
|
41
|
+
const json = JsonSchemaSchema.parse(
|
|
42
|
+
z.toJSONSchema(z.object(variables), { io: 'input', unrepresentable: 'any' }),
|
|
43
|
+
);
|
|
44
|
+
const required = new Set(json.required ?? []);
|
|
45
|
+
return Object.entries(json.properties)
|
|
46
|
+
.map(([name, property]) => describeVariable(name, property, required.has(name)))
|
|
47
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function describeVariable(
|
|
51
|
+
name: string,
|
|
52
|
+
property: { type?: string; format?: string; enum?: unknown[] },
|
|
53
|
+
required: boolean,
|
|
54
|
+
): ProjectEnvVariable {
|
|
55
|
+
if (property.enum) {
|
|
56
|
+
// Members are rejected rather than stringified. `String(member)` would turn
|
|
57
|
+
// numbers and booleans into text that does not match the value the Zod
|
|
58
|
+
// schema accepts, so a deployment reading the declaration would supply
|
|
59
|
+
// something the application then rejects — the declaration would be derived
|
|
60
|
+
// and still wrong.
|
|
61
|
+
const members = property.enum.filter((member) => typeof member === 'string');
|
|
62
|
+
if (members.length !== property.enum.length) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Environment variable ${name} declares a non-string enum member. The declaration format carries enum members as strings; declare it as a string enum, or extend the format first.`,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return { name, shape: 'enum', required, members };
|
|
68
|
+
}
|
|
69
|
+
if (property.format === 'uri') return { name, shape: 'url', required };
|
|
70
|
+
if (property.type === undefined || property.type === 'string') {
|
|
71
|
+
return { name, shape: 'string', required };
|
|
72
|
+
}
|
|
73
|
+
const shape = SHAPE_BY_JSON_TYPE[property.type];
|
|
74
|
+
if (!shape) {
|
|
75
|
+
// Fail closed rather than describe the variable with the wrong shape: a
|
|
76
|
+
// reader acting on `string` when the value is something else is worse off
|
|
77
|
+
// than a reader told this project cannot describe it.
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Cannot describe ${name}: no declaration shape for JSON Schema type "${property.type}".`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
return { name, shape, required };
|
|
83
|
+
}
|