Comparing version 0.0.6 to 0.0.7
@@ -26,4 +26,3 @@ import { JSON } from './json'; | ||
*/ | ||
export interface PubSubMessage<S extends string, T> { | ||
subscription: S; | ||
export interface PubSubMessage<T> { | ||
message: { | ||
@@ -66,7 +65,8 @@ messageId: MessageId; | ||
declare type Validator<T> = (json: JSON) => T | undefined; | ||
interface SubscriptionHandler<S extends string, T> { | ||
validator: Validator<T>; | ||
handler: (data: PubSubMessage<S, T>) => Promise<boolean>; | ||
interface SubscriptionHandler<D extends {} = {}> { | ||
validator: Validator<D>; | ||
handler: (subscription: string, data: PubSubMessage<D>) => Promise<boolean>; | ||
} | ||
export declare type DecodingTable<S extends string, T> = Map<S, SubscriptionHandler<S, T>>; | ||
export interface DecodingTable extends Record<string, SubscriptionHandler<any>> { | ||
} | ||
export declare const getSubscription: <S extends string>(rawSubscription: string) => S | null; | ||
@@ -77,3 +77,3 @@ export declare class PubSub<S extends string, T> { | ||
private decoders; | ||
constructor(projectId: string, decodingTable: DecodingTable<S, T>, customStateManager?: StateManager); | ||
constructor(projectId: string, decodingTable: DecodingTable, customStateManager?: StateManager); | ||
publish<J extends {}>(topic: string, data: J): Promise<void>; | ||
@@ -80,0 +80,0 @@ handlePubSubMessage(rawMsg: UnprocessedPubSubMessage): Promise<SubscriptionError | undefined>; |
@@ -133,3 +133,3 @@ "use strict"; | ||
const updatedCachedMessage = yield this.stateManager.recordMessageReceived(rawMsg, subscription, cachedMessage); | ||
const subscriptionHandler = this.decoders.get(subscription); | ||
const subscriptionHandler = this.decoders[subscription]; | ||
if (!subscriptionHandler) { | ||
@@ -144,3 +144,2 @@ return SubscriptionError.MissingHandlerForTopic; | ||
const pubSubMessage = { | ||
subscription, | ||
message: { | ||
@@ -151,3 +150,3 @@ messageId: rawMsg.message.messageId, | ||
}; | ||
const succeeded = yield handler(pubSubMessage).catch(() => { | ||
const succeeded = yield handler(subscription, pubSubMessage).catch(() => { | ||
// catching in case handler didn't catch its own errors | ||
@@ -154,0 +153,0 @@ return false; |
{ | ||
"name": "a1pubsub", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "GCP PubSub wrapper to add idempotency", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
22708
255