@proca/queue
Version:
This package provides a **robust RabbitMQ consumer** for processing Proca **action** and **event** messages with strict retry, dead-letter, and crash semantics.
205 lines (198 loc) • 5.15 kB
text/typescript
import { PersonalInfo, KeyStore } from '@proca/crypto';
/**
* The format of action + contact data stored as JSON in the processing queue.
* Current format is: V2 - see MessageV2 below
* (V1 is only used by older users) - see MessageV1
*/
type ProcessStage = "confirm" | "deliver";
type ContactV1 = {
email: string;
firstName: string;
ref: string;
payload: string;
signKey?: string;
publicKey?: string;
nonce?: string;
area: string | null;
};
type ContactV2 = {
email: string;
firstName: string;
contactRef: string;
area: string | null;
} & {
[key: string]: any;
};
type Campaign = {
title: string;
name: string;
externalId: number;
id?: number;
};
type ActionPage = {
locale: string;
name: string;
thankYouTemplate: string;
thankYouTemplateRef: string;
id?: number;
};
type ActionV1 = {
actionType: string;
fields: {
[key: string]: string;
};
createdAt: string;
testing: boolean;
};
type ActionV2 = {
id?: number;
actionType: string;
customFields: {
[key: string]: string | number | boolean | string[] | number[];
};
createdAt: string;
testing: boolean;
};
type Organisation = {
name: string;
title: string;
id?: number;
};
type Tracking = {
source: string;
medium: string;
campaign: string;
content: string;
location: string;
};
type PrivacyV1 = {
communication: boolean;
givenAt: string;
};
type PrivacyV2 = {
withConsent: boolean;
optIn?: boolean;
givenAt?: string;
emailStatus: null | "double_opt_in" | "bounce" | "blocked" | "spam" | "unsub";
emailStatusChanged: null | string;
};
type ActionMessage = ActionMessageV1 | ActionMessageV2;
type ActionMessageV1 = {
actionId: number;
actionPageId: number;
campaignId: number;
action: ActionV1;
contact: ContactV1;
campaign: Campaign;
actionPage: ActionPage;
tracking: Tracking;
privacy: PrivacyV1;
schema: "proca:action:1";
stage: ProcessStage;
};
type ActionMessageV2 = {
actionId: number;
actionPageId: number;
campaignId: number;
orgId: number;
org: Organisation;
action: ActionV2;
contact: ContactV2;
personalInfo: PersonalInfo | null;
campaign: Campaign;
actionPage: ActionPage;
tracking: Tracking;
privacy: PrivacyV2;
schema: "proca:action:2";
stage: ProcessStage;
};
declare const actionMessageV1to2: (a1: ActionMessageV1) => ActionMessage;
type EventBase = {
schema: 'proca:event:2';
timestamp: string;
};
type EmailStatusEvent = EventBase & {
eventType: 'email_status';
action?: ActionV2;
actionPage?: ActionPage;
campaign?: Campaign;
supporter: {
contact: ContactV2;
privacy: PrivacyV2;
};
tracking?: Tracking;
};
type Json = Record<string, any>;
type CampaignMessage = {
id: number;
externalId: number;
name: string;
title: string;
contactSchema: string;
config: Json;
org: {
name: string;
title: string;
};
};
type CampaignUpdatedEvent = EventBase & {
eventType: 'campaign_updated';
campaignId: number;
orgId: number;
campaign: CampaignMessage;
};
type ConfirmCreatedEvent = EventBase & {
eventType: 'confirm_created';
confirm: {
acceptLink: string;
rejectLink: string;
code: string;
objectId: number;
subjectId: number;
operation: string;
email: string;
message: string;
campaign: {
name: string;
title: string;
};
creator: {
email: string;
jobTitle: string | null;
};
org: {
name: string;
title: string;
twitter?: {
name: string;
screenName: string;
description: string;
url: string;
picture: string;
followersCount: number;
};
};
};
};
type Event = EmailStatusEvent | CampaignUpdatedEvent | ConfirmCreatedEvent;
type ConsumerOpts = {
concurrency?: number;
prefetch?: number;
keyStore?: KeyStore;
tag?: string;
maxRetries?: number;
};
type SyncCallback = (message: ActionMessage | Event) => Promise<boolean>;
type Counters = {
ack: number;
nack: number;
queued: number | undefined;
};
declare const listenConnection: (fct: any) => any;
declare const count: Counters;
declare const connect: (queueUrl: string) => any;
declare const syncQueue: (queueUrl: string, queueName: string, syncer: SyncCallback, opts?: ConsumerOpts) => Promise<{
close: () => Promise<void>;
}>;
declare const pause: (time?: number) => Promise<any>;
export { type ActionMessage, type ActionMessageV1, type ActionMessageV2, type Campaign, type CampaignUpdatedEvent, type CampaignUpdatedEvent as CampaignUpdatedEventMessage, type ConsumerOpts, type ContactV2 as Contact, type Counters, type Event as EventMessageV2, type ProcessStage, type SyncCallback, actionMessageV1to2, connect, count, listenConnection, pause, syncQueue };