From 3e91d526b72d32ed0733696ee0fd642016827a9a Mon Sep 17 00:00:00 2001 From: Carmine Porricelli Date: Tue, 16 Jul 2024 10:08:09 +0200 Subject: [PATCH] Small refactors --- src/agreement/index.ts | 28 +++++++++------------------- src/eservice/index.ts | 28 +++++++++------------------- src/events.ts | 24 ------------------------ src/purpose/index.ts | 28 +++++++++------------------- src/tenant/index.ts | 28 +++++++++------------------- 5 files changed, 36 insertions(+), 100 deletions(-) diff --git a/src/agreement/index.ts b/src/agreement/index.ts index be0b49b..a259fef 100644 --- a/src/agreement/index.ts +++ b/src/agreement/index.ts @@ -4,14 +4,14 @@ import { EventEnvelope, eventV1, eventV2 } from "../events.js"; import { AgreementEventV1, agreementEventToBinaryDataV1 } from "./eventsV1.js"; import { AgreementEventV2, agreementEventToBinaryDataV2 } from "./eventsV2.js"; -function agreementEventToBinaryData(event: AgreementEvent): Uint8Array { +export function agreementEventToBinaryData(event: AgreementEvent): Uint8Array { return match(event) .with({ event_version: 1 }, agreementEventToBinaryDataV1) .with({ event_version: 2 }, agreementEventToBinaryDataV2) .exhaustive(); } -const AgreementEvent = z +export const AgreementEvent = z .discriminatedUnion("event_version", [eventV1, eventV2]) .transform((obj, ctx) => { const res = match(obj) @@ -26,23 +26,13 @@ const AgreementEvent = z return res.data; }); -type AgreementEvent = z.infer; +export type AgreementEvent = z.infer; -const AgreementEventEnvelopeV1 = EventEnvelope(AgreementEventV1); -type AgreementEventEnvelopeV1 = z.infer; +export const AgreementEventEnvelopeV1 = EventEnvelope(AgreementEventV1); +export type AgreementEventEnvelopeV1 = z.infer; -const AgreementEventEnvelopeV2 = EventEnvelope(AgreementEventV2); -type AgreementEventEnvelopeV2 = z.infer; +export const AgreementEventEnvelopeV2 = EventEnvelope(AgreementEventV2); +export type AgreementEventEnvelopeV2 = z.infer; -const AgreementEventEnvelope = EventEnvelope(AgreementEvent); -type AgreementEventEnvelope = z.infer; - -export { - agreementEventToBinaryData, - AgreementEvent, - AgreementEventV1, - AgreementEventV2, - AgreementEventEnvelope, - AgreementEventEnvelopeV1, - AgreementEventEnvelopeV2, -}; +export const AgreementEventEnvelope = EventEnvelope(AgreementEvent); +export type AgreementEventEnvelope = z.infer; diff --git a/src/eservice/index.ts b/src/eservice/index.ts index fe19bf0..360c8ac 100644 --- a/src/eservice/index.ts +++ b/src/eservice/index.ts @@ -4,14 +4,14 @@ import { EventEnvelope, eventV1, eventV2 } from "../events.js"; import { EServiceEventV1, eServiceEventToBinaryDataV1 } from "./eventsV1.js"; import { EServiceEventV2, eServiceEventToBinaryDataV2 } from "./eventsV2.js"; -function eServiceEventToBinaryData(event: EServiceEvent): Uint8Array { +export function eServiceEventToBinaryData(event: EServiceEvent): Uint8Array { return match(event) .with({ event_version: 1 }, eServiceEventToBinaryDataV1) .with({ event_version: 2 }, eServiceEventToBinaryDataV2) .exhaustive(); } -const EServiceEvent = z +export const EServiceEvent = z .discriminatedUnion("event_version", [eventV1, eventV2]) .transform((obj, ctx) => { const res = match(obj) @@ -26,23 +26,13 @@ const EServiceEvent = z return res.data; }); -type EServiceEvent = z.infer; +export type EServiceEvent = z.infer; -const EServiceEventEnvelopeV1 = EventEnvelope(EServiceEventV1); -type EServiceEventEnvelopeV1 = z.infer; +export const EServiceEventEnvelopeV1 = EventEnvelope(EServiceEventV1); +export type EServiceEventEnvelopeV1 = z.infer; -const EServiceEventEnvelopeV2 = EventEnvelope(EServiceEventV2); -type EServiceEventEnvelopeV2 = z.infer; +export const EServiceEventEnvelopeV2 = EventEnvelope(EServiceEventV2); +export type EServiceEventEnvelopeV2 = z.infer; -const EServiceEventEnvelope = EventEnvelope(EServiceEvent); -type EServiceEventEnvelope = z.infer; - -export { - eServiceEventToBinaryData, - EServiceEvent, - EServiceEventV1, - EServiceEventV2, - EServiceEventEnvelope, - EServiceEventEnvelopeV1, - EServiceEventEnvelopeV2, -}; +export const EServiceEventEnvelope = EventEnvelope(EServiceEvent); +export type EServiceEventEnvelope = z.infer; diff --git a/src/events.ts b/src/events.ts index 66f9137..8dc6c0c 100644 --- a/src/events.ts +++ b/src/events.ts @@ -18,30 +18,6 @@ export type EventEnvelope = z.infer< ReturnType>> >; -export const DebeziumCreatePayload = ( - event: TEventZodType -) => - z.object({ - op: z.enum(["c", "r"]), - after: EventEnvelope(event), - }); -export type DebeziumCreatePayload = z.infer< - ReturnType>> ->; - -export const Message = ( - event: TEventZodType -) => - z.object({ - value: z.preprocess( - (v) => (v != null ? JSON.parse(v.toString()) : null), - DebeziumCreatePayload(EventEnvelope(event)) - ), - }); -export type Message = z.infer< - ReturnType>> ->; - export const eventV1 = z .object({ event_version: z.literal(1), diff --git a/src/purpose/index.ts b/src/purpose/index.ts index f9587a5..04ed710 100644 --- a/src/purpose/index.ts +++ b/src/purpose/index.ts @@ -4,14 +4,14 @@ import { EventEnvelope, eventV1, eventV2 } from "../events.js"; import { PurposeEventV1, purposeEventToBinaryDataV1 } from "./eventsV1.js"; import { PurposeEventV2, purposeEventToBinaryDataV2 } from "./eventsV2.js"; -function purposeEventToBinaryData(event: PurposeEvent): Uint8Array { +export function purposeEventToBinaryData(event: PurposeEvent): Uint8Array { return match(event) .with({ event_version: 1 }, purposeEventToBinaryDataV1) .with({ event_version: 2 }, purposeEventToBinaryDataV2) .exhaustive(); } -const PurposeEvent = z +export const PurposeEvent = z .discriminatedUnion("event_version", [eventV1, eventV2]) .transform((obj, ctx) => { const res = match(obj) @@ -25,23 +25,13 @@ const PurposeEvent = z } return res.data; }); -type PurposeEvent = z.infer; +export type PurposeEvent = z.infer; -const PurposeEventEnvelopeV1 = EventEnvelope(PurposeEventV1); -type PurposeEventEnvelopeV1 = z.infer; +export const PurposeEventEnvelopeV1 = EventEnvelope(PurposeEventV1); +export type PurposeEventEnvelopeV1 = z.infer; -const PurposeEventEnvelopeV2 = EventEnvelope(PurposeEventV2); -type PurposeEventEnvelopeV2 = z.infer; +export const PurposeEventEnvelopeV2 = EventEnvelope(PurposeEventV2); +export type PurposeEventEnvelopeV2 = z.infer; -const PurposeEventEnvelope = EventEnvelope(PurposeEvent); -type PurposeEventEnvelope = z.infer; - -export { - purposeEventToBinaryData, - PurposeEvent, - PurposeEventV1, - PurposeEventV2, - PurposeEventEnvelope, - PurposeEventEnvelopeV1, - PurposeEventEnvelopeV2, -}; +export const PurposeEventEnvelope = EventEnvelope(PurposeEvent); +export type PurposeEventEnvelope = z.infer; diff --git a/src/tenant/index.ts b/src/tenant/index.ts index f49c4f1..aae79e8 100644 --- a/src/tenant/index.ts +++ b/src/tenant/index.ts @@ -4,14 +4,14 @@ import { EventEnvelope, eventV1, eventV2 } from "../events.js"; import { TenantEventV1, tenantEventToBinaryDataV1 } from "./eventsV1.js"; import { TenantEventV2, tenantEventToBinaryDataV2 } from "./eventsV2.js"; -function tenantEventToBinaryData(event: TenantEvent): Uint8Array { +export function tenantEventToBinaryData(event: TenantEvent): Uint8Array { return match(event) .with({ event_version: 1 }, tenantEventToBinaryDataV1) .with({ event_version: 2 }, tenantEventToBinaryDataV2) .exhaustive(); } -const TenantEvent = z +export const TenantEvent = z .discriminatedUnion("event_version", [eventV1, eventV2]) .transform((obj, ctx) => { const res = match(obj) @@ -26,23 +26,13 @@ const TenantEvent = z return res.data; }); -type TenantEvent = z.infer; +export type TenantEvent = z.infer; -const TenantEventEnvelopeV1 = EventEnvelope(TenantEventV1); -type TenantEventEnvelopeV1 = z.infer; +export const TenantEventEnvelopeV1 = EventEnvelope(TenantEventV1); +export type TenantEventEnvelopeV1 = z.infer; -const TenantEventEnvelopeV2 = EventEnvelope(TenantEventV2); -type TenantEventEnvelopeV2 = z.infer; +export const TenantEventEnvelopeV2 = EventEnvelope(TenantEventV2); +export type TenantEventEnvelopeV2 = z.infer; -const TenantEventEnvelope = EventEnvelope(TenantEvent); -type TenantEventEnvelope = z.infer; - -export { - tenantEventToBinaryData, - TenantEvent, - TenantEventV1, - TenantEventV2, - TenantEventEnvelope, - TenantEventEnvelopeV1, - TenantEventEnvelopeV2, -}; +export const TenantEventEnvelope = EventEnvelope(TenantEvent); +export type TenantEventEnvelope = z.infer;