1- const recognisedMessageTypes = [ 'project-created' , 'project-updated' ] ;
2- type MessageType = ( typeof recognisedMessageTypes ) [ number ] ;
1+ import { z } from 'zod/v4' ;
32
4- export type UpsertMessage = {
5- type : MessageType ;
6- id : string ;
7- title : string ;
8- status : string ;
9- commissionId : string ;
10- commissionTitle : string ;
11- productionOffice : string ;
12- created : string ;
13- } ;
3+ const plutoMessageTypes = [ 'project-created' , 'project-updated' ] ;
4+
5+ const plutoMessageTypeSchema = z . enum ( plutoMessageTypes ) ;
6+
7+ export type MessageType = z . infer < typeof plutoMessageTypeSchema > ;
148
159export function isRecognisedMessageType ( type : unknown ) : type is MessageType {
16- return recognisedMessageTypes . includes ( type as MessageType ) ;
10+ return plutoMessageTypes . includes ( type as MessageType ) ;
1711}
1812
1913export function hasRecognisedMessageType ( data : unknown ) : boolean {
@@ -24,50 +18,37 @@ export function hasRecognisedMessageType(data: unknown): boolean {
2418 return isRecognisedMessageType ( type ) ;
2519}
2620
27- export function isUpsertMessage ( data : unknown ) : data is UpsertMessage {
28- if ( ! data || typeof data !== 'object' || data === null ) {
29- return false ;
30- }
31- const {
32- type,
33- id,
34- title,
35- status,
36- commissionId,
37- commissionTitle,
38- productionOffice,
39- created
40- } = data as {
41- [ key : string ] : unknown ;
42- } ;
43- return (
44- isRecognisedMessageType ( type ) &&
45- typeof id === 'string' &&
46- typeof title === 'string' &&
47- typeof status === 'string' &&
48- typeof commissionId === 'string' &&
49- typeof commissionTitle === 'string' &&
50- typeof productionOffice === 'string' &&
51- typeof created === 'string'
52- ) ;
21+ const plutoUpsertMessageSchema = z . looseObject ( {
22+ type : plutoMessageTypeSchema ,
23+ id : z . string ( ) ,
24+ title : z . string ( ) ,
25+ status : z . string ( ) ,
26+ commissionId : z . string ( ) ,
27+ commissionTitle : z . string ( ) ,
28+ productionOffice : z . string ( ) ,
29+ created : z . string ( )
30+ } ) ;
31+
32+ export type PlutoUpsertMessage = z . infer < typeof plutoUpsertMessageSchema > ;
33+
34+ export function isPlutoUpsertMessage (
35+ data : unknown
36+ ) : data is PlutoUpsertMessage {
37+ const parsed = plutoUpsertMessageSchema . safeParse ( data ) ;
38+ return parsed . success ;
5339}
5440
55- export type DeleteMessage = {
56- type : MessageType ;
57- commissionId : string ;
58- commissionTitle : '(DELETE)' ;
59- } ;
41+ const PlutoDeleteMessageSchema = z . looseObject ( {
42+ type : plutoMessageTypeSchema ,
43+ commissionId : z . string ( ) ,
44+ commissionTitle : z . literal ( '(DELETE)' )
45+ } ) ;
6046
61- export function isDeleteMessage ( data : unknown ) : data is DeleteMessage {
62- if ( ! data || typeof data !== 'object' || data === null ) {
63- return false ;
64- }
65- const { commissionId, commissionTitle, type } = data as {
66- [ key : string ] : unknown ;
67- } ;
68- return (
69- isRecognisedMessageType ( type ) &&
70- typeof commissionId === 'string' &&
71- commissionTitle === '(DELETE)'
72- ) ;
47+ export type PlutoDeleteMessage = z . infer < typeof PlutoDeleteMessageSchema > ;
48+
49+ export function isPlutoDeleteMessage (
50+ data : unknown
51+ ) : data is PlutoDeleteMessage {
52+ const parsed = PlutoDeleteMessageSchema . safeParse ( data ) ;
53+ return parsed . success ;
7354}
0 commit comments