@prenta/cli 1.3.3 → 1.4.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.
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Reads a deployed `/api/cms/health` response for `prenta verify`.
3
+ *
4
+ * Answering is not the same as working: a deploy without a rate-limit store
5
+ * answers `/health` with `status: "unhealthy"` while refusing every sign-in
6
+ * and save (#1261), so the verdict comes from the body, not only the status.
7
+ */
8
+
9
+ export type HealthVerdict = 'pass' | 'warn' | 'fail'
10
+
11
+ export interface HealthCheckResult {
12
+ verdict: HealthVerdict
13
+ /** One line per finding, the first being the summary. */
14
+ lines: string[]
15
+ }
16
+
17
+ interface HealthEnvCheck {
18
+ name?: string
19
+ status?: string
20
+ message?: string
21
+ }
22
+
23
+ interface HealthBody {
24
+ status?: string
25
+ rateLimitBackendReady?: boolean
26
+ trustedIpSource?: boolean
27
+ trustProxyHint?: string
28
+ databaseConnected?: boolean
29
+ env?: { checks?: HealthEnvCheck[] }
30
+ }
31
+
32
+ function readBody(json: unknown): HealthBody | null {
33
+ if (!json || typeof json !== 'object') return null
34
+ const data = (json as { data?: unknown }).data
35
+ if (!data || typeof data !== 'object') return null
36
+ return data as HealthBody
37
+ }
38
+
39
+ /** Why the deploy reports itself unhealthy or degraded, as far as the body says. */
40
+ function reasons(body: HealthBody): string[] {
41
+ const out: string[] = []
42
+ if (body.rateLimitBackendReady === false) {
43
+ out.push(
44
+ 'No rate-limit store: set UPSTASH_REDIS_REST_URL/TOKEN or add Upstash from the Vercel Marketplace (KV_REST_API_URL/TOKEN). Until then sign-in, setup and saves are refused with 503.',
45
+ )
46
+ }
47
+ if (body.trustedIpSource === false) {
48
+ out.push(body.trustProxyHint ?? 'No trusted client-IP source: set PRENTA_TRUST_PROXY=1.')
49
+ }
50
+ for (const check of body.env?.checks ?? []) {
51
+ if (check.status === 'error' || check.status === 'missing') {
52
+ out.push(`${check.name ?? 'env'}: ${check.message ?? check.status}`)
53
+ }
54
+ }
55
+ if (body.databaseConnected === false) out.push('The database is not reachable.')
56
+ return out
57
+ }
58
+
59
+ export async function checkHealthResponse(
60
+ response: Response,
61
+ url: string,
62
+ ): Promise<HealthCheckResult> {
63
+ // Deployment Protection or an auth gate in front of the site: the route is
64
+ // there, but its report cannot be read.
65
+ if (response.status === 401 || response.status === 302) {
66
+ return {
67
+ verdict: 'warn',
68
+ lines: [
69
+ `Health route at ${url} answered HTTP ${response.status}, so its status could not be read (Deployment Protection?).`,
70
+ ],
71
+ }
72
+ }
73
+
74
+ let body: HealthBody | null = null
75
+ try {
76
+ body = readBody(await response.json())
77
+ } catch {
78
+ body = null
79
+ }
80
+
81
+ if (!body?.status) {
82
+ return response.ok
83
+ ? {
84
+ verdict: 'warn',
85
+ lines: [
86
+ `Health route at ${url} answered HTTP ${response.status} without a health report.`,
87
+ ],
88
+ }
89
+ : { verdict: 'fail', lines: [`Health route returned HTTP ${response.status} at ${url}.`] }
90
+ }
91
+
92
+ if (body.status === 'unhealthy') {
93
+ return {
94
+ verdict: 'fail',
95
+ lines: [
96
+ `Health route at ${url} reports "unhealthy" (HTTP ${response.status}).`,
97
+ ...reasons(body),
98
+ ],
99
+ }
100
+ }
101
+ if (!response.ok) {
102
+ return {
103
+ verdict: 'fail',
104
+ lines: [`Health route returned HTTP ${response.status} at ${url} (status "${body.status}").`],
105
+ }
106
+ }
107
+ if (body.status === 'degraded') {
108
+ return {
109
+ verdict: 'warn',
110
+ lines: [`Health route at ${url} reports "degraded".`, ...reasons(body)],
111
+ }
112
+ }
113
+ return { verdict: 'pass', lines: [`Health route at ${url} reports "${body.status}".`] }
114
+ }
@@ -40,10 +40,22 @@ export interface EnvMatrix {
40
40
  /** Production and preview are the deploy-blocking targets; development is local-only. */
41
41
  const DEPLOY_TARGETS: VercelTarget[] = ['production', 'preview']
42
42
 
43
+ /**
44
+ * A requirement any one of several sets of variables satisfies, such as the
45
+ * rate-limit store, which reads either the Upstash console names or the names
46
+ * Vercel Marketplace Upstash provisions.
47
+ */
48
+ export interface RequiredOneOf {
49
+ /** Shown in `missingCritical` when no alternative is complete on a target. */
50
+ label: string
51
+ alternatives: readonly (readonly string[])[]
52
+ }
53
+
43
54
  export function buildEnvMatrix(
44
55
  envVars: VercelEnvVar[],
45
56
  requiredKeys: readonly string[],
46
57
  optionalKeys: readonly string[] = [],
58
+ requiredOneOf: readonly RequiredOneOf[] = [],
47
59
  ): EnvMatrix {
48
60
  const rows: EnvMatrixRow[] = []
49
61
  const seen = new Set<string>()
@@ -63,6 +75,16 @@ export function buildEnvMatrix(
63
75
  rows.push({ key, required: false, presence: presenceForKey(envVars, key) })
64
76
  }
65
77
 
78
+ for (const requirement of requiredOneOf) {
79
+ const missing = DEPLOY_TARGETS.filter(
80
+ (target) =>
81
+ !requirement.alternatives.some((keys) =>
82
+ keys.every((key) => presenceForKey(envVars, key)[target]),
83
+ ),
84
+ )
85
+ if (missing.length > 0) missingCritical.push({ key: requirement.label, targets: missing })
86
+ }
87
+
66
88
  return { rows, missingCritical }
67
89
  }
68
90