zodbus 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bus.d.ts +27 -0
- package/dist/constants.d.ts +1 -0
- package/dist/errors.d.ts +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/types.d.ts +32 -0
- package/dist/utils/schema.d.ts +6 -0
- package/package.json +2 -3
package/dist/bus.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { PublishKey, Schema, SubscriptionKey, SubscriptionListenerPayloads, SubscriptionListeners } from "./types";
|
|
2
|
+
type BusOptions<T extends Schema> = {
|
|
3
|
+
schema: T;
|
|
4
|
+
validate?: boolean;
|
|
5
|
+
};
|
|
6
|
+
interface Bus<T extends Schema> {
|
|
7
|
+
publish: <K extends PublishKey<T>>(event: K, data: SubscriptionListenerPayloads<T>[K]) => void;
|
|
8
|
+
subscribe: <K extends SubscriptionKey<T>>(event: K, listener: SubscriptionListeners<T>[K]) => {
|
|
9
|
+
event: K;
|
|
10
|
+
listener: SubscriptionListeners<T>[K];
|
|
11
|
+
unsubscribe: () => void;
|
|
12
|
+
};
|
|
13
|
+
subscribeOnce: <K extends SubscriptionKey<T>>(event: K, listener: SubscriptionListeners<T>[K]) => {
|
|
14
|
+
event: K;
|
|
15
|
+
listener: SubscriptionListeners<T>[K];
|
|
16
|
+
unsubscribe: () => void;
|
|
17
|
+
};
|
|
18
|
+
unsubscribe: <K extends SubscriptionKey<T>>(event: K, listener?: SubscriptionListeners<T>[K]) => void;
|
|
19
|
+
getEventNames: () => string[];
|
|
20
|
+
getListeners: <K extends SubscriptionKey<T>>(event?: K) => ((data: unknown, eventName: string) => void)[];
|
|
21
|
+
waitFor: <K extends SubscriptionKey<T>>(event: K, options?: {
|
|
22
|
+
timeout?: number;
|
|
23
|
+
filter?: (data: SubscriptionListenerPayloads<T>[K]) => boolean;
|
|
24
|
+
}) => Promise<SubscriptionListenerPayloads<T>[K]>;
|
|
25
|
+
}
|
|
26
|
+
declare function create<T extends Schema>({ schema, validate }: BusOptions<T>): Bus<T>;
|
|
27
|
+
export { create };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const errorPrefix = "[zodbus] ";
|
package/dist/errors.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./bus";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ZodType } from "zod";
|
|
2
|
+
export type IsZodType<T> = T extends ZodType ? true : false;
|
|
3
|
+
export type IsSchema<T> = T extends Schema ? true : false;
|
|
4
|
+
export type Listener<K extends string = string, V = unknown> = (data: V, event: K) => void;
|
|
5
|
+
export interface Schema {
|
|
6
|
+
[key: string]: ZodType | Schema;
|
|
7
|
+
}
|
|
8
|
+
export type HasWildcard<T extends string> = T extends `*` | `*.${string}` | `${string}.*.${string}` | `${string}.*` ? true : false;
|
|
9
|
+
export type InferSubscriptionListener<T, K extends string, P extends string = ""> = K extends `${infer Left}.${infer Right}` ? HasWildcard<Left & string> extends true ? Listener : Left extends keyof T ? T[Left] extends Schema ? InferSubscriptionListener<T[Left], Right, `${P}${Left}.`> : never : never : K extends keyof T ? T[K] extends ZodType<infer O, any, any> ? Listener<`${P}${K}`, O> : never : HasWildcard<K & string> extends true ? Listener : never;
|
|
10
|
+
export type InferSubscriptionListenerPayload<T, K extends string, P extends string = ""> = K extends `${infer Left}.${infer Right}` ? HasWildcard<Left & string> extends true ? unknown : Left extends keyof T ? T[Left] extends Schema ? InferSubscriptionListenerPayload<T[Left], Right, `${P}${Left}.`> : never : never : K extends keyof T ? T[K] extends ZodType<infer O, any, any> ? O : never : HasWildcard<K & string> extends true ? unknown : never;
|
|
11
|
+
export type ZodTypePath<T, K extends keyof T = keyof T> = K extends string ? IsZodType<T[K]> extends true ? K : never : never;
|
|
12
|
+
export type NamespacePath<T, K extends keyof T = keyof T> = K extends string ? IsSchema<T[K]> extends true ? `${K}.${SchemaPath<T[K], keyof T[K]>}` : never : never;
|
|
13
|
+
export type SchemaPath<T, K extends keyof T = keyof T> = ZodTypePath<T, K> | NamespacePath<T, K>;
|
|
14
|
+
export type WildcardPath<T extends string> = T extends `${infer L}.${infer R}` ? `${L}.${WildcardPath<R>}` | `${L}.*` | `*.${WildcardPath<R>}` : T | "*";
|
|
15
|
+
export type ExcludeDirectlyNestedKeys<T extends string> = T extends `${infer L}.${infer R}` ? L | ExcludeDirectlyNestedKeys<R> : never;
|
|
16
|
+
export type Subscription<T extends Schema> = {
|
|
17
|
+
[K in Exclude<WildcardPath<SchemaPath<T>>, ExcludeDirectlyNestedKeys<SchemaPath<T>>>]: {
|
|
18
|
+
listener: InferSubscriptionListener<T, K>;
|
|
19
|
+
payload: InferSubscriptionListenerPayload<T, K>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export type Subscriptions<T extends Schema> = {
|
|
23
|
+
[K in keyof Subscription<T>]: Subscription<T>[K];
|
|
24
|
+
};
|
|
25
|
+
export type SubscriptionListeners<T extends Schema> = {
|
|
26
|
+
[K in keyof Subscriptions<T>]: Subscriptions<T>[K]["listener"];
|
|
27
|
+
};
|
|
28
|
+
export type SubscriptionListenerPayloads<T extends Schema> = {
|
|
29
|
+
[K in keyof Subscriptions<T>]: Subscriptions<T>[K]["payload"];
|
|
30
|
+
};
|
|
31
|
+
export type SubscriptionKey<T extends Schema> = Extract<keyof SubscriptionListeners<T>, string>;
|
|
32
|
+
export type PublishKey<T extends Schema> = Extract<SubscriptionKey<T>, SchemaPath<T>>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ZodType } from "zod";
|
|
2
|
+
import { Schema } from "../types";
|
|
3
|
+
export declare const hasWildcard: (path: string) => boolean;
|
|
4
|
+
export declare const getPublishPaths: (schema: Schema | ZodType, prefix?: string) => string[];
|
|
5
|
+
export declare const getSubscribePaths: (schema: Schema | ZodType, prefix?: string) => string[];
|
|
6
|
+
export declare const getSubPubPathMap: (schema: Schema | ZodType) => Record<string, string[]>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zodbus",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"repository": "https://github.com/3rd/zodbus",
|
|
5
5
|
"description": "Type-safe event bus built around Zod.",
|
|
6
6
|
"keywords": [
|
|
@@ -26,8 +26,7 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
|
-
"build": "npm run clean && node src/scripts/build.mjs",
|
|
30
|
-
"postbuild": "tsc -p tsconfig.build.json",
|
|
29
|
+
"build": "npm run clean && node src/scripts/build.mjs && tsc -p tsconfig.build.json",
|
|
31
30
|
"test": "jest",
|
|
32
31
|
"tsc": "tsc",
|
|
33
32
|
"benchmark": "cd benchmark && npm run benchmark",
|