Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/core/lib/events/DomainEventEmitter.types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
type CommonEventDefinition,
enrichMessageSchemaWithBase,
} from '@message-queue-toolkit/schemas'
import { describe, expectTypeOf, it } from 'vitest'
import { z } from 'zod/v4'

import type { DomainEventEmitter } from './DomainEventEmitter.ts'

const myEvents = {
transformingEvent: {
...enrichMessageSchemaWithBase(
'entity.updated',
z.object({
mode: z.preprocess(
(value) => (value === 'live' ? value : undefined),
z.literal('live').optional(),
),
}),
),
},
} as const satisfies Record<string, CommonEventDefinition>

type Emitter = DomainEventEmitter<[typeof myEvents.transformingEvent]>

describe('DomainEventEmitter types', () => {
it('on() handlers receive the parsed event, with transformed fields as their output type', () => {
type OnHandler = Parameters<Emitter['on']>[1]
type HandledEvent = Parameters<OnHandler['handleEvent']>[0]

expectTypeOf<HandledEvent['payload']['mode']>().toEqualTypeOf<'live' | undefined>()
})

it('onAny() handlers receive the parsed event, with transformed fields as their output type', () => {
type AnyHandler = Parameters<Emitter['onAny']>[0]
type HandledEvent = Parameters<AnyHandler['handleEvent']>[0]

expectTypeOf<HandledEvent['payload']['mode']>().toEqualTypeOf<'live' | undefined>()
})

it('emit() takes the raw event as input and resolves to the parsed event', () => {
type EmitInput = Parameters<Emitter['emit']>[1]
type EmittedEvent = Awaited<ReturnType<Emitter['emit']>>

// The caller passes the raw payload, which emit() parses with the schema
expectTypeOf<EmitInput['payload']['mode']>().toBeUnknown()
// What comes back is the validated event returned by the parse
expectTypeOf<EmittedEvent['payload']['mode']>().toEqualTypeOf<'live' | undefined>()
})
})
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"@lokalise/node-core": "^14.2.0",
"@lokalise/universal-ts-utils": "^4.5.1",
"@message-queue-toolkit/schemas": "^7.0.0",
"@message-queue-toolkit/schemas": "^7.2.0",
"dot-prop": "^10.1.0",
"fast-equals": "^6.0.0",
"json-stream-stringify": "^3.1.6",
Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/queues/HandlerContainer.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ describe('HandlerContainer Types', () => {
})
})

it('should infer the schema output type for transforming schemas', () => {
const JOB_MESSAGE_SCHEMA = z.object({
type: z.literal('job.scheduled'),
// Forward-compatible field: unknown values are dropped instead of failing validation
mode: z.preprocess(
(value) => (value === 'fast' ? value : undefined),
z.literal('fast').optional(),
),
})
type JobMessage = z.output<typeof JOB_MESSAGE_SCHEMA>

const builder = new MessageHandlerConfigBuilder<SupportedMessages | JobMessage, TestContext>()

// Consumers (e.g. SNS/SQS) parse messages with the schema before invoking the
// handler, so the handler message type is the schema output, not its raw input
builder.addConfig(JOB_MESSAGE_SCHEMA, (message, _context) => {
expectTypeOf(message.mode).toEqualTypeOf<'fast' | undefined>()
return Promise.resolve({ result: 'success' as const })
})
})

it('should accept messageType in options', () => {
const builder = new MessageHandlerConfigBuilder<SupportedMessages, TestContext>()

Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading