Skip to content

Commit

Permalink
more eventbus tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed May 25, 2024
1 parent 7d3837c commit 1ebcc60
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
21 changes: 17 additions & 4 deletions sdk/js/src/aws/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export module bus {
return `https://events.${region}.amazonaws.com/`;
}

export function handle<Events extends event.Definition>(
export function subscriber<Events extends event.Definition>(
_events: Events | Events[],
cb: (
input: {
Expand All @@ -30,16 +30,29 @@ export module bus {
};
}

export async function publish<Definition extends event.Definition>(
/** @deprecated
* use bus.subscriber instead
* */
export const handler = subscriber;

export async function publish<Definition extends event.Definition = any>(
name: string | { name: string },
def: Definition,
def: Definition | string,
properties: Definition["$input"],
options?: {
metadata?: Definition["$metadata"];
aws?: AwsOptions;
},
): Promise<any> {
const u = url(options?.aws);
const evt = await def.create(properties);
const evt =
typeof def === "string"
? {
type: def,
properties,
metadata: options?.metadata || {},
}
: await def.create(properties);
const res = await client.fetch(u, {
method: "POST",
aws: options?.aws,
Expand Down
47 changes: 24 additions & 23 deletions sdk/js/src/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZodObject, ZodSchema, z } from "zod";
import { ZodSchema, z } from "zod";
import { Prettify } from "../auth/handler.js";

export module event {
Expand All @@ -12,41 +12,44 @@ export module event {
};

export function builder<
MetadataFunction extends (type: string, properties: any) => any,
Validator extends (schema: any) => (input: any) => any,
MetadataSchema extends Parameters<Validator>[0] | undefined,
>(input: {
metadata?: MetadataSchema;
metadataFn?: MetadataFunction;
validator: Validator;
}) {
const validator = input.validator;
const metadataValidator = input.metadata ? validator(input.metadata) : null;
Metadata extends
| ((type: string, properties: any) => any)
| Parameters<Validator>[0],
Validator extends (
schema: any,
) => (input: any) => any = typeof ZodValidator,
>(input: { validator?: Validator; metadata?: Metadata }) {
const validator = input.validator || ZodValidator;
const fn = function event<
Type extends string,
Schema extends Parameters<Validator>[0],
>(type: Type, schema: Schema) {
type MetadataOutput = Metadata extends (
type: string,
properties: any,
) => any
? ReturnType<Metadata>
: // @ts-expect-error
inferParser<Metadata>["out"];
type Payload = Prettify<{
type: Type;
properties: Parsed["out"];
metadata: ReturnType<MetadataFunction>;
metadata: MetadataOutput;
}>;
type Parsed = inferParser<Schema>;
type Create = undefined extends MetadataSchema
type Create = Metadata extends (type: string, properties: any) => any
? (properties: Parsed["in"]) => Promise<Payload>
: (
properties: Parsed["in"],
// @ts-expect-error
metadata: inferParser<MetadataSchema>["in"],
metadata: inferParser<Metadata>["in"],
) => Promise<Payload>;
const validate = validator(schema);
async function create(properties: any, metadata?: any) {
if (metadataValidator) {
metadata = metadataValidator(metadata);
}
if (input.metadataFn) {
metadata = input.metadataFn(type, properties);
}
metadata =
typeof input.metadata === "function"
? input.metadata(type, properties)
: input.metadata(metadata);
properties = validate(properties);
return {
type,
Expand All @@ -60,7 +63,7 @@ export module event {
$input: {} as Parsed["in"],
$output: {} as Parsed["out"],
$payload: {} as Payload,
$metadata: {} as ReturnType<MetadataFunction>,
$metadata: {} as MetadataOutput,
} satisfies Definition;
};
fn.coerce = <Events extends Definition>(
Expand Down Expand Up @@ -142,6 +145,4 @@ export module event {
out: $InOut;
}
: never;

type inferEvent<T extends { shape: ZodObject<any> }> = z.infer<T["shape"]>;
}
18 changes: 18 additions & 0 deletions sdk/js/test/event.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { bus } from "../src/aws/bus.js";
import { event } from "../src/event/index.js";
import { z } from "zod";

const defineEvent = event.builder({
metadata(type, properties) {
return {
traceID: "123",
};
},
});

const MyEvent = defineEvent(
"MyEvent",
z.object({
foo: z.string(),
}),
);

0 comments on commit 1ebcc60

Please sign in to comment.