42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
|
|
// packages/contracts/src/events.ts
|
|||
|
|
// Outbox 事件契约(docs/03-技术实施方案.md §8.2):P1a 在业务事务内写 outbox,
|
|||
|
|
// P2 海外副本按 eventId 去重、按 entityVersion 单调递增消费。payload 由各事件
|
|||
|
|
// 类型自行约定,此处仅落 P2 展示一张照片所需的 photo.published 载荷(YAGNI)。
|
|||
|
|
export interface OutboxEvent<T = unknown> {
|
|||
|
|
eventId: string; // cuid,持久唯一
|
|||
|
|
type:
|
|||
|
|
| 'photo.published'
|
|||
|
|
| 'photo.hidden'
|
|||
|
|
| 'photo.deleted'
|
|||
|
|
| 'photo.likes.updated'
|
|||
|
|
| 'album.updated';
|
|||
|
|
albumId: string;
|
|||
|
|
photoId?: string;
|
|||
|
|
entityVersion: number; // 单调递增,P2 inbox 消费依据
|
|||
|
|
payload: T;
|
|||
|
|
occurredAt: string; // ISO UTC
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// photo.published 载荷:P2 海外副本展示一张照片所需的全部字段。
|
|||
|
|
export interface PhotoPublishedPayload {
|
|||
|
|
photoId: string;
|
|||
|
|
albumId: string;
|
|||
|
|
objectKey: string; // 原图对象键
|
|||
|
|
thumbKey: string; // 缩略图对象键
|
|||
|
|
browseKey: string; // 浏览图对象键
|
|||
|
|
width: number;
|
|||
|
|
height: number;
|
|||
|
|
publishedAt: string; // ISO UTC
|
|||
|
|
entityVersion: number;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 运行时校验用:全部合法事件类型字符串。satisfies 保证 数组 ⊆ 接口 union;
|
|||
|
|
// __tests__/events.test.ts 的 Equal 断言反向锁定 union ⊆ 数组,两侧永远 1:1。
|
|||
|
|
export const OUTBOX_EVENT_TYPES = [
|
|||
|
|
'photo.published',
|
|||
|
|
'photo.hidden',
|
|||
|
|
'photo.deleted',
|
|||
|
|
'photo.likes.updated',
|
|||
|
|
'album.updated',
|
|||
|
|
] as const satisfies readonly OutboxEvent['type'][];
|