Contracts: event, event feed, church, identity, sign-in return, Hub protocol v2

- crosscode.event/1 and crosscode.event-feed/1 (feed, event.changed
  webhook), with session and registration rules; parseEventFeed skips
  and reports bad events instead of failing the whole feed.
- crosscode.church/1: branding, campuses, service times, contact, links.
- crosscode.identity/1: sign-in claims and the user.updated /
  user.deleted / user.export back-channel calls and replies.
- Sign-in return helpers (isSafeReturnPath, encodeState/decodeState,
  isRegisteredRedirectUri) so every product returns people to the page
  they came from in the same safe way.
- Hub protocol v2: product on register/heartbeat, stats per product,
  install public key, linked products, customerId/product in licences.
- @crosscode/contracts/signing (Node only): the Hub's HMAC scheme.
- Samples for every contract in samples/, checked by the tests (120).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-04 09:47:40 +02:00
co-authored by Claude Opus 5.5
parent 19f79dfb17
commit ea1575f716
37 changed files with 1889 additions and 23 deletions
+99
View File
@@ -0,0 +1,99 @@
import { z } from "zod";
import { dateTimeSchema, emailSchema, hexColourSchema, idSchema, phoneSchema, slugSchema, timeOfDaySchema, timezoneSchema, urlSchema } from "./common";
import { schemaName } from "./schema-name";
/**
* `crosscode.church/1` — the church profile, set once and shared by every product of a church
* (ROADMAP-v3 §2): name, logo, colours, campuses, service times and contact details. The Website,
* App and Accounts' sign-in page are branded from it.
*
* Public information only: the church's own contact details, never a person's.
*/
export const CHURCH_SCHEMA = schemaName("church", 1);
export const WEEKDAYS = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] as const;
export const SOCIAL_NETWORKS = ["facebook", "instagram", "youtube", "tiktok", "x", "spotify", "whatsapp-channel", "other"] as const;
export const addressSchema = z.object({
line1: z.string().trim().min(1).max(200),
line2: z.string().trim().max(200).nullable(),
suburb: z.string().trim().max(100).nullable(),
city: z.string().trim().min(1).max(100),
province: z.string().trim().max(100).nullable(),
postalCode: z.string().trim().max(20).nullable(),
/** ISO 3166-1 alpha-2, e.g. `ZA`. */
country: z.string().regex(/^[A-Z]{2}$/, "Expected an ISO 3166-1 alpha-2 code"),
/** For a map pin. */
geo: z.object({ lat: z.number().min(-90).max(90), lng: z.number().min(-180).max(180) }).nullable(),
});
export type Address = z.infer<typeof addressSchema>;
export const campusSchema = z.object({
slug: slugSchema,
name: z.string().trim().min(1).max(200),
address: addressSchema.nullable(),
});
export const serviceTimeSchema = z.object({
day: z.enum(WEEKDAYS),
/** Local time in the church's timezone. */
time: timeOfDaySchema,
/** E.g. "Morning service", "Afrikaans service". */
label: z.string().trim().max(100).nullable(),
/** The campus's slug, or `null` when the church has no campuses. */
campus: slugSchema.nullable(),
/** E.g. "Kids church during the service". */
note: z.string().trim().max(300).nullable(),
});
export const churchSchema = z
.object({
schema: z.literal(CHURCH_SCHEMA),
/** The Hub customer id: the same for every product of the church. */
customerId: idSchema,
updatedAt: dateTimeSchema,
name: z.string().trim().min(1).max(200),
/** For tight spaces (app icon label, sign-in page title). */
shortName: z.string().trim().max(30).nullable(),
timezone: timezoneSchema,
/** BCP 47, e.g. `en-ZA`. */
locale: z.string().regex(/^[a-z]{2,3}(-[A-Z]{2})?$/, "Expected a BCP 47 locale like en-ZA"),
branding: z.object({
logoUrl: urlSchema.nullable(),
/** Square, at least 512×512: app icon and favicon. */
iconUrl: urlSchema.nullable(),
colours: z.object({
primary: hexColourSchema,
accent: hexColourSchema.nullable(),
}),
}),
/** Empty when campuses are off. */
campuses: z.array(campusSchema).max(50),
serviceTimes: z.array(serviceTimeSchema).max(100),
contact: z.object({
email: emailSchema.nullable(),
phone: phoneSchema.nullable(),
whatsapp: phoneSchema.nullable(),
address: addressSchema.nullable(),
}),
social: z.array(z.object({ network: z.enum(SOCIAL_NETWORKS), url: urlSchema })).max(20),
/** The church's products, so each can link to the others. */
links: z.object({
website: urlSchema.nullable(),
events: urlSchema.nullable(),
app: urlSchema.nullable(),
accounts: urlSchema,
}),
})
.superRefine((church, ctx) => {
const slugs = new Set(church.campuses.map((c) => c.slug));
if (slugs.size !== church.campuses.length) ctx.addIssue({ code: "custom", path: ["campuses"], message: "Campus slugs must be unique" });
church.serviceTimes.forEach((s, i) => {
if (s.campus !== null && !slugs.has(s.campus)) ctx.addIssue({ code: "custom", path: ["serviceTimes", i, "campus"], message: `Unknown campus "${s.campus}"` });
});
});
export type Church = z.infer<typeof churchSchema>;
export type Campus = z.infer<typeof campusSchema>;
export type ServiceTime = z.infer<typeof serviceTimeSchema>;