Skip to content

Commit

Permalink
Small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Carminepo2 committed Jul 16, 2024
1 parent 1fb1891 commit 3e91d52
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 100 deletions.
28 changes: 9 additions & 19 deletions src/agreement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,23 +26,13 @@ const AgreementEvent = z
return res.data;
});

type AgreementEvent = z.infer<typeof AgreementEvent>;
export type AgreementEvent = z.infer<typeof AgreementEvent>;

const AgreementEventEnvelopeV1 = EventEnvelope(AgreementEventV1);
type AgreementEventEnvelopeV1 = z.infer<typeof AgreementEventEnvelopeV1>;
export const AgreementEventEnvelopeV1 = EventEnvelope(AgreementEventV1);
export type AgreementEventEnvelopeV1 = z.infer<typeof AgreementEventEnvelopeV1>;

const AgreementEventEnvelopeV2 = EventEnvelope(AgreementEventV2);
type AgreementEventEnvelopeV2 = z.infer<typeof AgreementEventEnvelopeV2>;
export const AgreementEventEnvelopeV2 = EventEnvelope(AgreementEventV2);
export type AgreementEventEnvelopeV2 = z.infer<typeof AgreementEventEnvelopeV2>;

const AgreementEventEnvelope = EventEnvelope(AgreementEvent);
type AgreementEventEnvelope = z.infer<typeof AgreementEventEnvelope>;

export {
agreementEventToBinaryData,
AgreementEvent,
AgreementEventV1,
AgreementEventV2,
AgreementEventEnvelope,
AgreementEventEnvelopeV1,
AgreementEventEnvelopeV2,
};
export const AgreementEventEnvelope = EventEnvelope(AgreementEvent);
export type AgreementEventEnvelope = z.infer<typeof AgreementEventEnvelope>;
28 changes: 9 additions & 19 deletions src/eservice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,23 +26,13 @@ const EServiceEvent = z
return res.data;
});

type EServiceEvent = z.infer<typeof EServiceEvent>;
export type EServiceEvent = z.infer<typeof EServiceEvent>;

const EServiceEventEnvelopeV1 = EventEnvelope(EServiceEventV1);
type EServiceEventEnvelopeV1 = z.infer<typeof EServiceEventEnvelopeV1>;
export const EServiceEventEnvelopeV1 = EventEnvelope(EServiceEventV1);
export type EServiceEventEnvelopeV1 = z.infer<typeof EServiceEventEnvelopeV1>;

const EServiceEventEnvelopeV2 = EventEnvelope(EServiceEventV2);
type EServiceEventEnvelopeV2 = z.infer<typeof EServiceEventEnvelopeV2>;
export const EServiceEventEnvelopeV2 = EventEnvelope(EServiceEventV2);
export type EServiceEventEnvelopeV2 = z.infer<typeof EServiceEventEnvelopeV2>;

const EServiceEventEnvelope = EventEnvelope(EServiceEvent);
type EServiceEventEnvelope = z.infer<typeof EServiceEventEnvelope>;

export {
eServiceEventToBinaryData,
EServiceEvent,
EServiceEventV1,
EServiceEventV2,
EServiceEventEnvelope,
EServiceEventEnvelopeV1,
EServiceEventEnvelopeV2,
};
export const EServiceEventEnvelope = EventEnvelope(EServiceEvent);
export type EServiceEventEnvelope = z.infer<typeof EServiceEventEnvelope>;
24 changes: 0 additions & 24 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,6 @@ export type EventEnvelope<TEvent> = z.infer<
ReturnType<typeof EventEnvelope<z.ZodType<TEvent>>>
>;

export const DebeziumCreatePayload = <TEventZodType extends z.ZodType>(
event: TEventZodType
) =>
z.object({
op: z.enum(["c", "r"]),
after: EventEnvelope(event),
});
export type DebeziumCreatePayload<TEvent> = z.infer<
ReturnType<typeof DebeziumCreatePayload<z.ZodType<TEvent>>>
>;

export const Message = <TEventZodType extends z.ZodType>(
event: TEventZodType
) =>
z.object({
value: z.preprocess(
(v) => (v != null ? JSON.parse(v.toString()) : null),
DebeziumCreatePayload(EventEnvelope(event))
),
});
export type Message<TEvent> = z.infer<
ReturnType<typeof Message<z.ZodType<TEvent>>>
>;

export const eventV1 = z
.object({
event_version: z.literal(1),
Expand Down
28 changes: 9 additions & 19 deletions src/purpose/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -25,23 +25,13 @@ const PurposeEvent = z
}
return res.data;
});
type PurposeEvent = z.infer<typeof PurposeEvent>;
export type PurposeEvent = z.infer<typeof PurposeEvent>;

const PurposeEventEnvelopeV1 = EventEnvelope(PurposeEventV1);
type PurposeEventEnvelopeV1 = z.infer<typeof PurposeEventEnvelopeV1>;
export const PurposeEventEnvelopeV1 = EventEnvelope(PurposeEventV1);
export type PurposeEventEnvelopeV1 = z.infer<typeof PurposeEventEnvelopeV1>;

const PurposeEventEnvelopeV2 = EventEnvelope(PurposeEventV2);
type PurposeEventEnvelopeV2 = z.infer<typeof PurposeEventEnvelopeV2>;
export const PurposeEventEnvelopeV2 = EventEnvelope(PurposeEventV2);
export type PurposeEventEnvelopeV2 = z.infer<typeof PurposeEventEnvelopeV2>;

const PurposeEventEnvelope = EventEnvelope(PurposeEvent);
type PurposeEventEnvelope = z.infer<typeof PurposeEventEnvelope>;

export {
purposeEventToBinaryData,
PurposeEvent,
PurposeEventV1,
PurposeEventV2,
PurposeEventEnvelope,
PurposeEventEnvelopeV1,
PurposeEventEnvelopeV2,
};
export const PurposeEventEnvelope = EventEnvelope(PurposeEvent);
export type PurposeEventEnvelope = z.infer<typeof PurposeEventEnvelope>;
28 changes: 9 additions & 19 deletions src/tenant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,23 +26,13 @@ const TenantEvent = z
return res.data;
});

type TenantEvent = z.infer<typeof TenantEvent>;
export type TenantEvent = z.infer<typeof TenantEvent>;

const TenantEventEnvelopeV1 = EventEnvelope(TenantEventV1);
type TenantEventEnvelopeV1 = z.infer<typeof TenantEventEnvelopeV1>;
export const TenantEventEnvelopeV1 = EventEnvelope(TenantEventV1);
export type TenantEventEnvelopeV1 = z.infer<typeof TenantEventEnvelopeV1>;

const TenantEventEnvelopeV2 = EventEnvelope(TenantEventV2);
type TenantEventEnvelopeV2 = z.infer<typeof TenantEventEnvelopeV2>;
export const TenantEventEnvelopeV2 = EventEnvelope(TenantEventV2);
export type TenantEventEnvelopeV2 = z.infer<typeof TenantEventEnvelopeV2>;

const TenantEventEnvelope = EventEnvelope(TenantEvent);
type TenantEventEnvelope = z.infer<typeof TenantEventEnvelope>;

export {
tenantEventToBinaryData,
TenantEvent,
TenantEventV1,
TenantEventV2,
TenantEventEnvelope,
TenantEventEnvelopeV1,
TenantEventEnvelopeV2,
};
export const TenantEventEnvelope = EventEnvelope(TenantEvent);
export type TenantEventEnvelope = z.infer<typeof TenantEventEnvelope>;

0 comments on commit 3e91d52

Please sign in to comment.