@jdcpuwiz/zoo-kit 0.0.0-stage → 1.0.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 +21 -0
- package/README.md +238 -2
- package/dist/categories.d.ts +31 -0
- package/dist/categories.js +36 -0
- package/dist/env.d.ts +62 -0
- package/dist/env.js +91 -0
- package/dist/generated/categories.d.ts +85 -0
- package/dist/generated/categories.js +7 -0
- package/dist/generated/contract.d.ts +7 -0
- package/dist/generated/contract.js +9 -0
- package/dist/generated/notifier.d.ts +230 -0
- package/dist/generated/notifier.js +4 -0
- package/dist/generated/people.d.ts +261 -0
- package/dist/generated/people.js +4 -0
- package/dist/health.d.ts +46 -0
- package/dist/health.js +112 -0
- package/dist/http.d.ts +51 -0
- package/dist/http.js +102 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +19 -0
- package/dist/keys.d.ts +51 -0
- package/dist/keys.js +97 -0
- package/dist/notify.d.ts +75 -0
- package/dist/notify.js +119 -0
- package/dist/people.d.ts +61 -0
- package/dist/people.js +165 -0
- package/dist/whoami.d.ts +86 -0
- package/dist/whoami.js +187 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JdCpuWiz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,239 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @jdcpuwiz/zoo-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared server-side plumbing for the homelab's own apps: **notify**, **whoami**,
|
|
4
|
+
**people**, the **health route** and **app-to-app keys**.
|
|
5
|
+
|
|
6
|
+
The behaviour lives in the shared services (zoo-notifier, the people
|
|
7
|
+
directory, authentik); this package is a **thin, pinned client** of their
|
|
8
|
+
versioned v1 contract (`contracts/v1` in the zoo-kit repo). Types are
|
|
9
|
+
generated from those OpenAPI files, never restated by hand.
|
|
10
|
+
|
|
11
|
+
- **Server-only.** Node ≥ 20, ESM + `.d.ts`. Next.js route handlers,
|
|
12
|
+
Hono / Express / Fastify and Bun all import it. It imports `node:crypto`,
|
|
13
|
+
so a browser bundle fails at build time; it does *not* import Next's
|
|
14
|
+
`server-only` marker (that would break every non-Next runtime).
|
|
15
|
+
- **No hosts, IPs or keys inside.** Everything comes from env. Missing config
|
|
16
|
+
is a logged warning and a no-op (notify) or a refusal (whoami, app keys) —
|
|
17
|
+
never a guessed address.
|
|
18
|
+
- **Failover-ready.** Every service is a LIST of base URLs, tried in list
|
|
19
|
+
order (primary first, never rotated). The next URL is tried ONLY on a
|
|
20
|
+
network error, a timeout or a 5xx; a 4xx returns at once.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install --save-exact @jdcpuwiz/zoo-kit@1.0.0
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Pin the exact version (no caret) — rollback is one revert of that line. v1.x
|
|
29
|
+
only ever adds optional things.
|
|
30
|
+
|
|
31
|
+
## Env (names only — values live in ZooVault / the app's `.env`)
|
|
32
|
+
|
|
33
|
+
| Name | Module | Meaning |
|
|
34
|
+
|---|---|---|
|
|
35
|
+
| `ZOO_NOTIFIER_URLS` | notify | comma-separated hub base URLs, primary first |
|
|
36
|
+
| `ZOO_NOTIFIER_URL` | notify | legacy single URL — read only when `ZOO_NOTIFIER_URLS` is unset |
|
|
37
|
+
| `ZOO_NOTIFIER_KEY` | notify | this app's per-source key on the hub |
|
|
38
|
+
| `ZOO_PEOPLE_URLS` | people, whoami | comma-separated people-directory base URLs |
|
|
39
|
+
| `ZOO_PEOPLE_TOKEN` | people, whoami | this app's consumer token on the directory |
|
|
40
|
+
| `ZOO_PEOPLE_CONSUMER` | people, whoami | the consumer name the directory declared for this app (`X-Dispenser-Consumer`) |
|
|
41
|
+
| `ZOO_KIT_PUBLIC_HOSTS` | whoami | comma-separated public host(s) the authentik edge serves this app on |
|
|
42
|
+
| `PUBLIC_HOST` | whoami | legacy single host — read only when `ZOO_KIT_PUBLIC_HOSTS` is unset |
|
|
43
|
+
| `ZOO_KIT_GATE_SECRET` | whoami | the `X-Zoo-Gate` value Traefik stamps behind authentik |
|
|
44
|
+
| `AUTH_GATE_SECRET` | whoami | legacy name of the same — read only when `ZOO_KIT_GATE_SECRET` is unset |
|
|
45
|
+
| `DEV_USER` | whoami | dev-box identity, honoured only with `authentik: { allowDevUser: true }` and never when `NODE_ENV=production` |
|
|
46
|
+
| `ZOO_KIT_APP_KEY_<CALLER>` | requireApp, whoami | the key caller `<CALLER>` presents to this app |
|
|
47
|
+
| `ZOO_KIT_APP_URLS_<APP>` | callApp | base URLs of `<APP>` |
|
|
48
|
+
| `ZOO_KIT_APP_KEY_FOR_<APP>` | callApp | the key this app presents to `<APP>` |
|
|
49
|
+
| `APP_VERSION` | health | `version` fallback |
|
|
50
|
+
| `GIT_SHA` | health | `sha` (unset / `unknown` = `null`) |
|
|
51
|
+
|
|
52
|
+
`<CALLER>` / `<APP>` is the app name uppercased with dashes as underscores:
|
|
53
|
+
`zoo-vet` → `ZOO_KIT_APP_KEY_ZOO_VET`. `ENV`, `appKeyEnv()`, `appUrlsEnv()`
|
|
54
|
+
and `appKeyForEnv()` export the names.
|
|
55
|
+
|
|
56
|
+
## notify
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { notify, dedupeKey } from "@jdcpuwiz/zoo-kit";
|
|
60
|
+
|
|
61
|
+
const r = await notify({
|
|
62
|
+
title: "Phone went dark",
|
|
63
|
+
body: "No fix for 30 min",
|
|
64
|
+
category: "location", // NotifyCategory — a free string is a type error
|
|
65
|
+
priority: "time-sensitive",
|
|
66
|
+
target: ["shad"],
|
|
67
|
+
dedupeKey: dedupeKey("dark", personId),
|
|
68
|
+
});
|
|
69
|
+
if (!r.ok) console.log("push not sent:", r.error); // never throws
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
- `category` is typed from `categories.v1.json` (+ aliases such as
|
|
73
|
+
`infrastructure`). A category the hub holds only as a row goes through
|
|
74
|
+
`customCategory("printing")`; one read from runtime config goes through
|
|
75
|
+
`toCategory(setting)` (returns `null` for a non-slug).
|
|
76
|
+
- `dedupeKey(kind, ...parts)` → `"kind:part:part"`, table-prefixed so bare ids
|
|
77
|
+
never collide (claude-peek B206), escaped, hashed past 200 chars.
|
|
78
|
+
- With no `dedupeKey` the kit adds a one-off `kit-once:<uuid>` so a retry
|
|
79
|
+
after a timeout cannot push twice.
|
|
80
|
+
- Result: `{ ok: true, id, queued, deduped, status, url }` or
|
|
81
|
+
`{ ok: false, error, status, skipped?, attempts }`. `{ dryRun: true }` builds
|
|
82
|
+
the request and sends nothing.
|
|
83
|
+
- **No `notify.batch`.** v1 has no batch route, so a client batch would be N
|
|
84
|
+
calls with invented partial-failure semantics; `Promise.all(items.map(notify))`
|
|
85
|
+
is exactly as good and says what it does.
|
|
86
|
+
|
|
87
|
+
## whoami
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { whoami, requirePerson, requireApp, ZooKitAuthError } from "@jdcpuwiz/zoo-kit";
|
|
91
|
+
|
|
92
|
+
const who = await whoami(req, {
|
|
93
|
+
authentik: true, // the edge header, host-gated + X-Zoo-Gate
|
|
94
|
+
apps: ["zoo-queen"], // app keys
|
|
95
|
+
resolveDeviceKey: async (key) => findDevice(key), // your Device table → { personSlug, device } | null
|
|
96
|
+
session: async () => ({ slug: (await getServerSession())?.user?.slug }), // NextAuth etc.
|
|
97
|
+
});
|
|
98
|
+
// { kind: "person", slug, personId, via: "authentik" | "session" | "device", person, username?, device? }
|
|
99
|
+
// { kind: "app", app, via: "key" }
|
|
100
|
+
// { kind: "anonymous", reason }
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The authentik header is trusted ONLY when the request names a configured
|
|
104
|
+
public host in `Host` or the first `X-Forwarded-Host` (both — B517), AND, when
|
|
105
|
+
a gate secret is configured, `X-Zoo-Gate` matches (timing-safe). The username
|
|
106
|
+
is lowercased and resolved through `people.byAuthentikUsername` (USERNAME,
|
|
107
|
+
never email); an archived person is refused. No public host configured = the
|
|
108
|
+
source refuses everything.
|
|
109
|
+
|
|
110
|
+
`requirePerson(req, opts)` / `requireApp(req, allowed)` return the identity or
|
|
111
|
+
throw `ZooKitAuthError` with a ready `.response` (`{"error":"unauthorized"}`
|
|
112
|
+
401 / `{"error":"forbidden"}` 403 — no detail leaked; `.reason` is for your logs).
|
|
113
|
+
|
|
114
|
+
### Next route handler
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
// app/api/people/route.ts
|
|
118
|
+
import { requireApp, ZooKitAuthError } from "@jdcpuwiz/zoo-kit";
|
|
119
|
+
|
|
120
|
+
export async function GET(req: Request) {
|
|
121
|
+
try {
|
|
122
|
+
const caller = requireApp(req, ["zoo-queen", "zoo-health"]);
|
|
123
|
+
return Response.json({ caller: caller.app, people: await listPeople() });
|
|
124
|
+
} catch (e) {
|
|
125
|
+
if (e instanceof ZooKitAuthError) return e.response;
|
|
126
|
+
throw e;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Hono
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { Hono } from "hono";
|
|
135
|
+
import { requirePerson, ZooKitAuthError } from "@jdcpuwiz/zoo-kit";
|
|
136
|
+
|
|
137
|
+
const app = new Hono();
|
|
138
|
+
app.onError((e, c) => (e instanceof ZooKitAuthError ? e.response : c.text("error", 500)));
|
|
139
|
+
app.get("/api/me", async (c) => {
|
|
140
|
+
const me = await requirePerson(c.req.raw, { authentik: true });
|
|
141
|
+
return c.json({ slug: me.slug });
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## people
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { people } from "@jdcpuwiz/zoo-kit";
|
|
149
|
+
|
|
150
|
+
const { people: roster, status } = await people.list(); // status: fresh | stale | unavailable | unconfigured
|
|
151
|
+
const val = await people.get("val"); // Person | null (from the cached list)
|
|
152
|
+
const me = await people.byAuthentikUsername("Shad"); // Person | null
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Person carries the contract's roster keys only (anything else in an answer is
|
|
156
|
+
dropped). Archived people are in the list with `enabled: false`.
|
|
157
|
+
|
|
158
|
+
## Health route
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
// app/api/health/route.ts AND app/api/healthz/route.ts — the same two lines
|
|
162
|
+
import { healthHandler, people } from "@jdcpuwiz/zoo-kit";
|
|
163
|
+
import { version } from "../../../package.json";
|
|
164
|
+
export const dynamic = "force-dynamic";
|
|
165
|
+
export const GET = healthHandler({
|
|
166
|
+
app: "zoo-where",
|
|
167
|
+
version,
|
|
168
|
+
checks: { db: () => prisma.$queryRaw`SELECT 1`, people: people.healthCheck },
|
|
169
|
+
requiredEnv: ["DATABASE_URL", "ZOO_NOTIFIER_KEY", "ZOO_PEOPLE_TOKEN"],
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
// Hono
|
|
175
|
+
const health = healthHandler({ app: "traefik-manager" });
|
|
176
|
+
app.get("/api/health", (c) => health(c.req.raw));
|
|
177
|
+
app.get("/api/healthz", (c) => health(c.req.raw));
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Body: `{ ok, app, version, sha, kit, uptimeS, checks }`. `kit` is this
|
|
181
|
+
package's version; `version` is the option, else `APP_VERSION`, else the
|
|
182
|
+
working directory's `package.json`; `sha` is the option, else `GIT_SHA`, else
|
|
183
|
+
`null`. Each check gets a timeout (default 3 s). The route answers 200 even
|
|
184
|
+
when `ok` is false (zoo-vet reads `ok`); opt into `failStatus: 503`.
|
|
185
|
+
|
|
186
|
+
## App keys
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
requireApp(req, ["zoo-vet", "zoo-siribridge"]); // inbound — Bearer only, every key compared
|
|
190
|
+
const res = await callApp("zoo-where", "/api/people"); // outbound — ZOO_KIT_APP_URLS_ZOO_WHERE + ZOO_KIT_APP_KEY_FOR_ZOO_WHERE
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`callApp` fails over GET/HEAD/PUT/DELETE/OPTIONS; a POST/PATCH goes to the
|
|
194
|
+
primary only unless `{ retryUnsafe: true }` (a timed-out POST may have landed).
|
|
195
|
+
It throws `ZooKitConfigError` (naming the env vars) when unconfigured.
|
|
196
|
+
|
|
197
|
+
## Config check
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
checkConfig(["DATABASE_URL", "ZOO_NOTIFIER_KEY"]); // { ok: false, missing: ["ZOO_NOTIFIER_KEY"] } — names only
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Fail-soft table
|
|
204
|
+
|
|
205
|
+
| Module | Service down / misconfigured | Never |
|
|
206
|
+
|---|---|---|
|
|
207
|
+
| notify | bounded retry across the list (2 passes, 5 s/attempt, 15 s total), then `{ ok: false }` + one log line | throws, guesses a host |
|
|
208
|
+
| people | list cached 5 min, served **stale** on error; empty only if never loaded (`status` says which) | throws |
|
|
209
|
+
| whoami | a username lookup is served from its last answer ≤ 15 min, then `anonymous`; resolvers that throw = `anonymous` | guesses who you are |
|
|
210
|
+
| health | always answers; a failing / hung check marks `ok: false` | throws, hangs |
|
|
211
|
+
| app keys | no configured key = every caller refused | opens up |
|
|
212
|
+
|
|
213
|
+
## Adopting in an app
|
|
214
|
+
|
|
215
|
+
1. `npm install --save-exact @jdcpuwiz/zoo-kit@1.0.0`.
|
|
216
|
+
2. **notify:** delete `lib/notify-client.ts`; `import { notify } from "@jdcpuwiz/zoo-kit"`.
|
|
217
|
+
Callers that did `if (await notify(...))` now read `.ok`. Category strings
|
|
218
|
+
from settings go through `toCategory()`. Env names are unchanged.
|
|
219
|
+
3. **health:** replace the hand-written route with `healthHandler(...)` on both
|
|
220
|
+
`/api/health` and `/api/healthz`; pass `GIT_SHA` into the container.
|
|
221
|
+
4. **people:** set `ZOO_PEOPLE_URLS` / `ZOO_PEOPLE_TOKEN` / `ZOO_PEOPLE_CONSUMER`
|
|
222
|
+
(the directory must have the consumer declared), then replace the roster
|
|
223
|
+
fetch with `people.list()`.
|
|
224
|
+
5. **whoami / app keys:** replace `lib/identity.ts` / `lib/auth.ts` key checks
|
|
225
|
+
with `whoami` / `requireApp`; set `ZOO_KIT_PUBLIC_HOSTS`,
|
|
226
|
+
`ZOO_KIT_GATE_SECRET`, `ZOO_KIT_APP_KEY_<CALLER>`.
|
|
227
|
+
6. Add every new env name to compose's environment allowlist — a name only in
|
|
228
|
+
`.env` never reaches the container.
|
|
229
|
+
|
|
230
|
+
## Develop
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
npm ci
|
|
234
|
+
npm run gen # regenerate types after a contracts/v1 change (gen:check runs in test + build)
|
|
235
|
+
npm run typecheck
|
|
236
|
+
npm test
|
|
237
|
+
npm run build
|
|
238
|
+
npm pack --dry-run # dist/, README.md, LICENSE, package.json only
|
|
239
|
+
```
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type CategoryAlias, type NotifyCategory } from "./generated/categories.js";
|
|
2
|
+
export { CATEGORIES, CATEGORY_ALIASES, CATEGORY_SLUGS } from "./generated/categories.js";
|
|
3
|
+
export type { CategoryAlias, NotifyCategory } from "./generated/categories.js";
|
|
4
|
+
declare const customBrand: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* A category the hub holds as a Category ROW but which is not published in
|
|
7
|
+
* categories.v1.json (created on the hub's /categories page). Only reachable
|
|
8
|
+
* through {@link customCategory} / {@link toCategory}, so a typo in code is a
|
|
9
|
+
* type error rather than a 400 at 3am.
|
|
10
|
+
*/
|
|
11
|
+
export type CustomCategory = string & {
|
|
12
|
+
readonly [customBrand]: true;
|
|
13
|
+
};
|
|
14
|
+
/** Anything `notify({ category })` accepts. */
|
|
15
|
+
export type CategoryInput = NotifyCategory | CategoryAlias | CustomCategory;
|
|
16
|
+
export declare function isCanonicalCategory(s: string): s is NotifyCategory;
|
|
17
|
+
export declare function isCategoryAlias(s: string): s is CategoryAlias;
|
|
18
|
+
/** Alias -> its canonical slug; a canonical slug -> itself; anything else unchanged. */
|
|
19
|
+
export declare function canonicalCategory(s: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Mark a hub-only category as usable. Throws TypeError when the string cannot
|
|
22
|
+
* be a slug at all — this is a programming error at the call site, not a
|
|
23
|
+
* delivery failure.
|
|
24
|
+
*/
|
|
25
|
+
export declare function customCategory(slug: string): CustomCategory;
|
|
26
|
+
/**
|
|
27
|
+
* For a category that comes from runtime config (a settings row, an env var):
|
|
28
|
+
* returns it typed, or null when it cannot be a slug. Whether the HUB holds a
|
|
29
|
+
* non-canonical one is only knowable by asking it — notify() reports its 400.
|
|
30
|
+
*/
|
|
31
|
+
export declare function toCategory(s: string | null | undefined): CategoryInput | null;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CATEGORY_ALIASES, CATEGORY_SLUGS } from "./generated/categories.js";
|
|
2
|
+
import { SLUG_PATTERN } from "./generated/contract.js";
|
|
3
|
+
export { CATEGORIES, CATEGORY_ALIASES, CATEGORY_SLUGS } from "./generated/categories.js";
|
|
4
|
+
const SLUG_RE = new RegExp(SLUG_PATTERN);
|
|
5
|
+
const CANON = new Set(CATEGORY_SLUGS);
|
|
6
|
+
export function isCanonicalCategory(s) {
|
|
7
|
+
return CANON.has(s);
|
|
8
|
+
}
|
|
9
|
+
export function isCategoryAlias(s) {
|
|
10
|
+
return Object.prototype.hasOwnProperty.call(CATEGORY_ALIASES, s);
|
|
11
|
+
}
|
|
12
|
+
/** Alias -> its canonical slug; a canonical slug -> itself; anything else unchanged. */
|
|
13
|
+
export function canonicalCategory(s) {
|
|
14
|
+
return isCategoryAlias(s) ? CATEGORY_ALIASES[s] : s;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Mark a hub-only category as usable. Throws TypeError when the string cannot
|
|
18
|
+
* be a slug at all — this is a programming error at the call site, not a
|
|
19
|
+
* delivery failure.
|
|
20
|
+
*/
|
|
21
|
+
export function customCategory(slug) {
|
|
22
|
+
if (!SLUG_RE.test(slug))
|
|
23
|
+
throw new TypeError(`not a category slug: ${JSON.stringify(slug)}`);
|
|
24
|
+
return slug;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* For a category that comes from runtime config (a settings row, an env var):
|
|
28
|
+
* returns it typed, or null when it cannot be a slug. Whether the HUB holds a
|
|
29
|
+
* non-canonical one is only knowable by asking it — notify() reports its 400.
|
|
30
|
+
*/
|
|
31
|
+
export function toCategory(s) {
|
|
32
|
+
const v = (s ?? "").trim();
|
|
33
|
+
if (!v || !SLUG_RE.test(v))
|
|
34
|
+
return null;
|
|
35
|
+
return v;
|
|
36
|
+
}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every environment variable the kit reads, by NAME. Values live in ZooVault
|
|
3
|
+
* and each app's .env — never in this package (it is public on npm), so there
|
|
4
|
+
* is no default host, IP or key anywhere in the kit.
|
|
5
|
+
*
|
|
6
|
+
* Read at CALL time, never at import: an app (or a test) that sets an env var
|
|
7
|
+
* after importing the kit must be honoured.
|
|
8
|
+
*/
|
|
9
|
+
/** Static names. Per-app names are built by {@link appKeyEnv} and friends. */
|
|
10
|
+
export declare const ENV: {
|
|
11
|
+
/** Comma-separated hub base URLs, primary first. */
|
|
12
|
+
readonly NOTIFIER_URLS: "ZOO_NOTIFIER_URLS";
|
|
13
|
+
/** Legacy single hub URL — read only when ZOO_NOTIFIER_URLS is unset. */
|
|
14
|
+
readonly NOTIFIER_URL: "ZOO_NOTIFIER_URL";
|
|
15
|
+
/** This app's per-source key on the hub. */
|
|
16
|
+
readonly NOTIFIER_KEY: "ZOO_NOTIFIER_KEY";
|
|
17
|
+
/** Comma-separated people-directory base URLs, primary first. */
|
|
18
|
+
readonly PEOPLE_URLS: "ZOO_PEOPLE_URLS";
|
|
19
|
+
/** This app's consumer token on the people directory. */
|
|
20
|
+
readonly PEOPLE_TOKEN: "ZOO_PEOPLE_TOKEN";
|
|
21
|
+
/** The consumer name the directory has declared for this app. */
|
|
22
|
+
readonly PEOPLE_CONSUMER: "ZOO_PEOPLE_CONSUMER";
|
|
23
|
+
/** Comma-separated public host(s) the authentik edge serves this app on. */
|
|
24
|
+
readonly PUBLIC_HOSTS: "ZOO_KIT_PUBLIC_HOSTS";
|
|
25
|
+
/** Legacy single public host — read only when ZOO_KIT_PUBLIC_HOSTS is unset. */
|
|
26
|
+
readonly PUBLIC_HOST: "PUBLIC_HOST";
|
|
27
|
+
/** The X-Zoo-Gate secret Traefik stamps behind authentik. */
|
|
28
|
+
readonly GATE_SECRET: "ZOO_KIT_GATE_SECRET";
|
|
29
|
+
/** Legacy name of the same secret — read only when ZOO_KIT_GATE_SECRET is unset. */
|
|
30
|
+
readonly AUTH_GATE_SECRET: "AUTH_GATE_SECRET";
|
|
31
|
+
/** Dev-box identity short-circuit; honoured only with `allowDevUser`. */
|
|
32
|
+
readonly DEV_USER: "DEV_USER";
|
|
33
|
+
/** App version fallback for the health handler. */
|
|
34
|
+
readonly APP_VERSION: "APP_VERSION";
|
|
35
|
+
/** Build commit for the health handler. */
|
|
36
|
+
readonly GIT_SHA: "GIT_SHA";
|
|
37
|
+
};
|
|
38
|
+
/** `zoo-vet` -> `ZOO_VET`. */
|
|
39
|
+
export declare function envSuffix(app: string): string;
|
|
40
|
+
/** Inbound: the key caller `app` presents to US. */
|
|
41
|
+
export declare const appKeyEnv: (app: string) => string;
|
|
42
|
+
/** Outbound: base URLs of `app`. */
|
|
43
|
+
export declare const appUrlsEnv: (app: string) => string;
|
|
44
|
+
/** Outbound: the key WE present to `app`. */
|
|
45
|
+
export declare const appKeyForEnv: (app: string) => string;
|
|
46
|
+
export declare function str(name: string): string;
|
|
47
|
+
/** A comma-separated list, trimmed, blanks dropped, trailing slashes removed. */
|
|
48
|
+
export declare function list(name: string): string[];
|
|
49
|
+
/** The first non-empty list among `names`, in order. */
|
|
50
|
+
export declare function firstList(...names: string[]): string[];
|
|
51
|
+
export declare function firstStr(...names: string[]): string;
|
|
52
|
+
/** One warning per process per key — a misconfigured app must not spam its log. */
|
|
53
|
+
export declare function warnOnce(key: string, msg: string): void;
|
|
54
|
+
/** Test hook. */
|
|
55
|
+
export declare function _resetWarnings(): void;
|
|
56
|
+
export interface ConfigReport {
|
|
57
|
+
ok: boolean;
|
|
58
|
+
/** Env var NAMES that are unset or blank. Never values. */
|
|
59
|
+
missing: string[];
|
|
60
|
+
}
|
|
61
|
+
/** Which of `required` are unset or blank — names only, for `checks.config`. */
|
|
62
|
+
export declare function checkConfig(required: string[]): ConfigReport;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every environment variable the kit reads, by NAME. Values live in ZooVault
|
|
3
|
+
* and each app's .env — never in this package (it is public on npm), so there
|
|
4
|
+
* is no default host, IP or key anywhere in the kit.
|
|
5
|
+
*
|
|
6
|
+
* Read at CALL time, never at import: an app (or a test) that sets an env var
|
|
7
|
+
* after importing the kit must be honoured.
|
|
8
|
+
*/
|
|
9
|
+
/** Static names. Per-app names are built by {@link appKeyEnv} and friends. */
|
|
10
|
+
export const ENV = {
|
|
11
|
+
/** Comma-separated hub base URLs, primary first. */
|
|
12
|
+
NOTIFIER_URLS: "ZOO_NOTIFIER_URLS",
|
|
13
|
+
/** Legacy single hub URL — read only when ZOO_NOTIFIER_URLS is unset. */
|
|
14
|
+
NOTIFIER_URL: "ZOO_NOTIFIER_URL",
|
|
15
|
+
/** This app's per-source key on the hub. */
|
|
16
|
+
NOTIFIER_KEY: "ZOO_NOTIFIER_KEY",
|
|
17
|
+
/** Comma-separated people-directory base URLs, primary first. */
|
|
18
|
+
PEOPLE_URLS: "ZOO_PEOPLE_URLS",
|
|
19
|
+
/** This app's consumer token on the people directory. */
|
|
20
|
+
PEOPLE_TOKEN: "ZOO_PEOPLE_TOKEN",
|
|
21
|
+
/** The consumer name the directory has declared for this app. */
|
|
22
|
+
PEOPLE_CONSUMER: "ZOO_PEOPLE_CONSUMER",
|
|
23
|
+
/** Comma-separated public host(s) the authentik edge serves this app on. */
|
|
24
|
+
PUBLIC_HOSTS: "ZOO_KIT_PUBLIC_HOSTS",
|
|
25
|
+
/** Legacy single public host — read only when ZOO_KIT_PUBLIC_HOSTS is unset. */
|
|
26
|
+
PUBLIC_HOST: "PUBLIC_HOST",
|
|
27
|
+
/** The X-Zoo-Gate secret Traefik stamps behind authentik. */
|
|
28
|
+
GATE_SECRET: "ZOO_KIT_GATE_SECRET",
|
|
29
|
+
/** Legacy name of the same secret — read only when ZOO_KIT_GATE_SECRET is unset. */
|
|
30
|
+
AUTH_GATE_SECRET: "AUTH_GATE_SECRET",
|
|
31
|
+
/** Dev-box identity short-circuit; honoured only with `allowDevUser`. */
|
|
32
|
+
DEV_USER: "DEV_USER",
|
|
33
|
+
/** App version fallback for the health handler. */
|
|
34
|
+
APP_VERSION: "APP_VERSION",
|
|
35
|
+
/** Build commit for the health handler. */
|
|
36
|
+
GIT_SHA: "GIT_SHA",
|
|
37
|
+
};
|
|
38
|
+
/** `zoo-vet` -> `ZOO_VET`. */
|
|
39
|
+
export function envSuffix(app) {
|
|
40
|
+
return app.trim().toUpperCase().replace(/[^A-Z0-9]+/g, "_");
|
|
41
|
+
}
|
|
42
|
+
/** Inbound: the key caller `app` presents to US. */
|
|
43
|
+
export const appKeyEnv = (app) => `ZOO_KIT_APP_KEY_${envSuffix(app)}`;
|
|
44
|
+
/** Outbound: base URLs of `app`. */
|
|
45
|
+
export const appUrlsEnv = (app) => `ZOO_KIT_APP_URLS_${envSuffix(app)}`;
|
|
46
|
+
/** Outbound: the key WE present to `app`. */
|
|
47
|
+
export const appKeyForEnv = (app) => `ZOO_KIT_APP_KEY_FOR_${envSuffix(app)}`;
|
|
48
|
+
export function str(name) {
|
|
49
|
+
return (process.env[name] ?? "").trim();
|
|
50
|
+
}
|
|
51
|
+
/** A comma-separated list, trimmed, blanks dropped, trailing slashes removed. */
|
|
52
|
+
export function list(name) {
|
|
53
|
+
return str(name)
|
|
54
|
+
.split(",")
|
|
55
|
+
.map((s) => s.trim().replace(/\/+$/, ""))
|
|
56
|
+
.filter(Boolean);
|
|
57
|
+
}
|
|
58
|
+
/** The first non-empty list among `names`, in order. */
|
|
59
|
+
export function firstList(...names) {
|
|
60
|
+
for (const n of names) {
|
|
61
|
+
const l = list(n);
|
|
62
|
+
if (l.length)
|
|
63
|
+
return l;
|
|
64
|
+
}
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
export function firstStr(...names) {
|
|
68
|
+
for (const n of names) {
|
|
69
|
+
const v = str(n);
|
|
70
|
+
if (v)
|
|
71
|
+
return v;
|
|
72
|
+
}
|
|
73
|
+
return "";
|
|
74
|
+
}
|
|
75
|
+
const warned = new Set();
|
|
76
|
+
/** One warning per process per key — a misconfigured app must not spam its log. */
|
|
77
|
+
export function warnOnce(key, msg) {
|
|
78
|
+
if (warned.has(key))
|
|
79
|
+
return;
|
|
80
|
+
warned.add(key);
|
|
81
|
+
console.warn(`[zoo-kit] ${msg}`);
|
|
82
|
+
}
|
|
83
|
+
/** Test hook. */
|
|
84
|
+
export function _resetWarnings() {
|
|
85
|
+
warned.clear();
|
|
86
|
+
}
|
|
87
|
+
/** Which of `required` are unset or blank — names only, for `checks.config`. */
|
|
88
|
+
export function checkConfig(required) {
|
|
89
|
+
const missing = required.filter((n) => !str(n));
|
|
90
|
+
return { ok: missing.length === 0, missing };
|
|
91
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Canonical category slugs (contracts/v1/categories.v1.json). */
|
|
2
|
+
export declare const CATEGORY_SLUGS: readonly ["backups", "bat-signal", "calendar", "cameras", "chores", "deploys", "financial", "general", "home", "infra", "location", "mail", "questions", "security", "siri", "summons", "test", "todos", "zoo-news"];
|
|
3
|
+
export type NotifyCategory = (typeof CATEGORY_SLUGS)[number];
|
|
4
|
+
/** alias -> canonical slug. The hub accepts an alias as sent. */
|
|
5
|
+
export declare const CATEGORY_ALIASES: {
|
|
6
|
+
readonly infrastructure: "infra";
|
|
7
|
+
};
|
|
8
|
+
export type CategoryAlias = keyof typeof CATEGORY_ALIASES;
|
|
9
|
+
export declare const CATEGORIES: readonly [{
|
|
10
|
+
readonly slug: "backups";
|
|
11
|
+
readonly name: "Backups";
|
|
12
|
+
readonly aliases: readonly [];
|
|
13
|
+
}, {
|
|
14
|
+
readonly slug: "bat-signal";
|
|
15
|
+
readonly name: "Bat Signal";
|
|
16
|
+
readonly aliases: readonly [];
|
|
17
|
+
}, {
|
|
18
|
+
readonly slug: "calendar";
|
|
19
|
+
readonly name: "Calendar";
|
|
20
|
+
readonly aliases: readonly [];
|
|
21
|
+
}, {
|
|
22
|
+
readonly slug: "cameras";
|
|
23
|
+
readonly name: "Cameras";
|
|
24
|
+
readonly aliases: readonly [];
|
|
25
|
+
}, {
|
|
26
|
+
readonly slug: "chores";
|
|
27
|
+
readonly name: "Chores";
|
|
28
|
+
readonly aliases: readonly [];
|
|
29
|
+
}, {
|
|
30
|
+
readonly slug: "deploys";
|
|
31
|
+
readonly name: "Deploys";
|
|
32
|
+
readonly aliases: readonly [];
|
|
33
|
+
}, {
|
|
34
|
+
readonly slug: "financial";
|
|
35
|
+
readonly name: "Financial";
|
|
36
|
+
readonly aliases: readonly [];
|
|
37
|
+
}, {
|
|
38
|
+
readonly slug: "general";
|
|
39
|
+
readonly name: "General";
|
|
40
|
+
readonly aliases: readonly [];
|
|
41
|
+
}, {
|
|
42
|
+
readonly slug: "home";
|
|
43
|
+
readonly name: "Home";
|
|
44
|
+
readonly aliases: readonly [];
|
|
45
|
+
}, {
|
|
46
|
+
readonly slug: "infra";
|
|
47
|
+
readonly name: "Infra";
|
|
48
|
+
readonly aliases: readonly ["infrastructure"];
|
|
49
|
+
}, {
|
|
50
|
+
readonly slug: "location";
|
|
51
|
+
readonly name: "Location";
|
|
52
|
+
readonly aliases: readonly [];
|
|
53
|
+
}, {
|
|
54
|
+
readonly slug: "mail";
|
|
55
|
+
readonly name: "Mail";
|
|
56
|
+
readonly aliases: readonly [];
|
|
57
|
+
}, {
|
|
58
|
+
readonly slug: "questions";
|
|
59
|
+
readonly name: "Questions";
|
|
60
|
+
readonly aliases: readonly [];
|
|
61
|
+
}, {
|
|
62
|
+
readonly slug: "security";
|
|
63
|
+
readonly name: "Security";
|
|
64
|
+
readonly aliases: readonly [];
|
|
65
|
+
}, {
|
|
66
|
+
readonly slug: "siri";
|
|
67
|
+
readonly name: "Siri";
|
|
68
|
+
readonly aliases: readonly [];
|
|
69
|
+
}, {
|
|
70
|
+
readonly slug: "summons";
|
|
71
|
+
readonly name: "Summons";
|
|
72
|
+
readonly aliases: readonly [];
|
|
73
|
+
}, {
|
|
74
|
+
readonly slug: "test";
|
|
75
|
+
readonly name: "Test";
|
|
76
|
+
readonly aliases: readonly [];
|
|
77
|
+
}, {
|
|
78
|
+
readonly slug: "todos";
|
|
79
|
+
readonly name: "Todos";
|
|
80
|
+
readonly aliases: readonly [];
|
|
81
|
+
}, {
|
|
82
|
+
readonly slug: "zoo-news";
|
|
83
|
+
readonly name: "Zoo News";
|
|
84
|
+
readonly aliases: readonly [];
|
|
85
|
+
}];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// GENERATED by packages/zoo-kit/scripts/generate.mjs from contracts/v1/categories.v1.json.
|
|
2
|
+
// Do not edit — change the contract and run `npm run gen`.
|
|
3
|
+
/** Canonical category slugs (contracts/v1/categories.v1.json). */
|
|
4
|
+
export const CATEGORY_SLUGS = ["backups", "bat-signal", "calendar", "cameras", "chores", "deploys", "financial", "general", "home", "infra", "location", "mail", "questions", "security", "siri", "summons", "test", "todos", "zoo-news"];
|
|
5
|
+
/** alias -> canonical slug. The hub accepts an alias as sent. */
|
|
6
|
+
export const CATEGORY_ALIASES = { "infrastructure": "infra" };
|
|
7
|
+
export const CATEGORIES = [{ "slug": "backups", "name": "Backups", "aliases": [] }, { "slug": "bat-signal", "name": "Bat Signal", "aliases": [] }, { "slug": "calendar", "name": "Calendar", "aliases": [] }, { "slug": "cameras", "name": "Cameras", "aliases": [] }, { "slug": "chores", "name": "Chores", "aliases": [] }, { "slug": "deploys", "name": "Deploys", "aliases": [] }, { "slug": "financial", "name": "Financial", "aliases": [] }, { "slug": "general", "name": "General", "aliases": [] }, { "slug": "home", "name": "Home", "aliases": [] }, { "slug": "infra", "name": "Infra", "aliases": ["infrastructure"] }, { "slug": "location", "name": "Location", "aliases": [] }, { "slug": "mail", "name": "Mail", "aliases": [] }, { "slug": "questions", "name": "Questions", "aliases": [] }, { "slug": "security", "name": "Security", "aliases": [] }, { "slug": "siri", "name": "Siri", "aliases": [] }, { "slug": "summons", "name": "Summons", "aliases": [] }, { "slug": "test", "name": "Test", "aliases": [] }, { "slug": "todos", "name": "Todos", "aliases": [] }, { "slug": "zoo-news", "name": "Zoo News", "aliases": [] }];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** The kit's own version — reported as `kit` by the health handler. */
|
|
2
|
+
export declare const KIT_VERSION = "1.0.0";
|
|
3
|
+
/** The exact keys a v1 Person may carry; anything else is dropped. */
|
|
4
|
+
export declare const PERSON_KEYS: readonly ["authentikUsername", "dob", "enabled", "haEntityId", "headingColor", "id", "kind", "name", "photoUrl", "role", "slug", "sortOrder", "traccarDeviceId"];
|
|
5
|
+
export declare const NOTIFY_PRIORITIES: readonly ["passive", "active", "time-sensitive", "critical"];
|
|
6
|
+
/** The Slug pattern a category must match. */
|
|
7
|
+
export declare const SLUG_PATTERN = "^[a-z0-9][a-z0-9-]{0,39}$";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// GENERATED by packages/zoo-kit/scripts/generate.mjs from contracts/v1/*.v1.yaml + packages/zoo-kit/package.json.
|
|
2
|
+
// Do not edit — change the contract and run `npm run gen`.
|
|
3
|
+
/** The kit's own version — reported as `kit` by the health handler. */
|
|
4
|
+
export const KIT_VERSION = "1.0.0";
|
|
5
|
+
/** The exact keys a v1 Person may carry; anything else is dropped. */
|
|
6
|
+
export const PERSON_KEYS = ["authentikUsername", "dob", "enabled", "haEntityId", "headingColor", "id", "kind", "name", "photoUrl", "role", "slug", "sortOrder", "traccarDeviceId"];
|
|
7
|
+
export const NOTIFY_PRIORITIES = ["passive", "active", "time-sensitive", "critical"];
|
|
8
|
+
/** The Slug pattern a category must match. */
|
|
9
|
+
export const SLUG_PATTERN = "^[a-z0-9][a-z0-9-]{0,39}$";
|