feat(core-events): EventDescriptor + defineEvent
TDD: red (test-only), then green. defineEvent(name, schema) returns a typed EventDescriptor<TName, TSchema>. Also added @repo/core-testing to devDeps (required for nodeVitestConfig's setupFiles). 3 tests passing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@repo/core-eslint": "workspace:*",
|
"@repo/core-eslint": "workspace:*",
|
||||||
|
"@repo/core-testing": "workspace:*",
|
||||||
"@repo/core-typescript": "workspace:*",
|
"@repo/core-typescript": "workspace:*",
|
||||||
"typescript": "^5.8.0",
|
"typescript": "^5.8.0",
|
||||||
"vitest": "^3.0.0"
|
"vitest": "^3.0.0"
|
||||||
|
|||||||
24
packages/core-events/src/event-descriptor.test.ts
Normal file
24
packages/core-events/src/event-descriptor.test.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { defineEvent } from "@/event-descriptor";
|
||||||
|
|
||||||
|
describe("defineEvent", () => {
|
||||||
|
it("returns a descriptor with name and schema", () => {
|
||||||
|
const schema = z.object({ id: z.string() }).strict();
|
||||||
|
const descriptor = defineEvent("test.thing.happened", schema);
|
||||||
|
expect(descriptor.name).toBe("test.thing.happened");
|
||||||
|
expect(descriptor.schema).toBe(schema);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("descriptor.schema parses valid payloads", () => {
|
||||||
|
const schema = z.object({ id: z.string() }).strict();
|
||||||
|
const d = defineEvent("test.evt", schema);
|
||||||
|
expect(() => d.schema.parse({ id: "abc" })).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("descriptor.schema rejects invalid payloads", () => {
|
||||||
|
const schema = z.object({ id: z.string() }).strict();
|
||||||
|
const d = defineEvent("test.evt", schema);
|
||||||
|
expect(() => d.schema.parse({ id: 123 })).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
13
packages/core-events/src/event-descriptor.ts
Normal file
13
packages/core-events/src/event-descriptor.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import type { z } from "zod";
|
||||||
|
|
||||||
|
export type EventDescriptor<TName extends string, TSchema extends z.ZodType> = {
|
||||||
|
readonly name: TName;
|
||||||
|
readonly schema: TSchema;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function defineEvent<TName extends string, TSchema extends z.ZodType>(
|
||||||
|
name: TName,
|
||||||
|
schema: TSchema,
|
||||||
|
): EventDescriptor<TName, TSchema> {
|
||||||
|
return { name, schema };
|
||||||
|
}
|
||||||
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@@ -531,6 +531,9 @@ importers:
|
|||||||
'@repo/core-eslint':
|
'@repo/core-eslint':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../core-eslint
|
version: link:../core-eslint
|
||||||
|
'@repo/core-testing':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../core-testing
|
||||||
'@repo/core-typescript':
|
'@repo/core-typescript':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../core-typescript
|
version: link:../core-typescript
|
||||||
|
|||||||
Reference in New Issue
Block a user