From 5b3cc4aa1932b754958f84cd0d4cf9a9dea25762 Mon Sep 17 00:00:00 2001 From: 3rd <3rd@users.noreply.github.com> Date: Mon, 26 Feb 2024 15:03:12 +0200 Subject: [PATCH] fix: typings mapping & clean-up --- package.json | 12 +++++++++--- src/bus.ts | 12 ++++++------ src/utils/schema.ts | 4 ++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 68c0fea..3455c67 100644 --- a/package.json +++ b/package.json @@ -17,12 +17,18 @@ "dist" ], "type": "module", - "typings": "dist/index.d.ts", + "types": "dist/index.d.ts", "main": "dist/index.mjs", "exports": { ".": { - "import": "./dist/index.mjs", - "require": "./dist/index.cjs" + "import": { + "default": "./dist/index.mjs", + "types": "./dist/index.d.ts" + }, + "require": { + "default": "./dist/index.cjs", + "types": "./dist/index.d.ts" + } } }, "scripts": { diff --git a/src/bus.ts b/src/bus.ts index 3661d84..af4c72f 100644 --- a/src/bus.ts +++ b/src/bus.ts @@ -30,7 +30,7 @@ interface Bus { function create({ schema, validate = true }: BusOptions): Bus { const subPubPathMap = getSubPubPathMap(schema) as Record, PublishKey[]>; const eventNames = Array.from(new Set(Object.values(subPubPathMap).flat())) as PublishKey[]; - const listeners: Map, Set> = new Map(); + const listeners = new Map, Set>(); const getListenerSetsForSubscriptionKey = (event: SubscriptionKey): Set[] => { if (event === "*") return Array.from(listeners.values()); @@ -46,12 +46,12 @@ function create({ schema, validate = true }: BusOptions): B const validatePayloadOrPanic = (event: string, data: unknown): void => { const pathFragments = event.split("."); - let currentSchema: ZodType | Schema = schema; + let currentSchema: Schema | ZodType = schema; for (const fragment of pathFragments) { - if (typeof (currentSchema as Record)[fragment] === "undefined") { + if ((currentSchema as Record)[fragment] === undefined) { throw new ValidationError(`Invalid event: "${event}". Could not resolve "${fragment}" fragment.`); } - currentSchema = (currentSchema as Record)[fragment]; + currentSchema = (currentSchema as Record)[fragment]; } if (typeof currentSchema.parse === "function") { (currentSchema as ZodType).parse(data); @@ -76,7 +76,7 @@ function create({ schema, validate = true }: BusOptions): B const unsubscribe = >(event: K, listener?: SubscriptionListeners[K]): void => { const eventListeners = getListenerSetsForSubscriptionKey(event); for (const listenerSet of eventListeners) { - if (typeof listener === "undefined") { + if (listener === undefined) { listenerSet.clear(); } else { listenerSet.delete(listener); @@ -167,7 +167,7 @@ function create({ schema, validate = true }: BusOptions): B event: K, options: { timeout?: number; filter?: (data: SubscriptionListenerPayloads[K]) => boolean } = {} ) => { - const { timeout = 5_000, filter } = options; + const { timeout = 5000, filter } = options; return new Promise[K]>((resolve, reject) => { let timeoutId: ReturnType; const listener = (data: unknown) => { diff --git a/src/utils/schema.ts b/src/utils/schema.ts index b725b07..adcc308 100644 --- a/src/utils/schema.ts +++ b/src/utils/schema.ts @@ -40,8 +40,8 @@ export const getSubPubPathMap = (schema: Schema | ZodType): Record { const publishParts = publishPath.split("."); if (publishParts.length !== subscribeParts.length) return false; - for (let i = 0; i < publishParts.length; i++) { - if (subscribeParts[i] !== "*" && publishParts[i] !== subscribeParts[i]) return false; + for (const [i, publishPart] of publishParts.entries()) { + if (subscribeParts[i] !== "*" && publishPart !== subscribeParts[i]) return false; } return true; });