@codematic/opencdp 5.0.13

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.
@@ -0,0 +1,2 @@
1
+ export { CDPClient } from './client';
2
+ export { CDPConfig, SendEmailRequestOptions, SendEmailRequestWithTemplate, SendEmailRequestWithoutTemplate, SendEmailRequest, SendPushRequest, SendSmsRequest, Identifiers } from './types';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SendEmailRequest = exports.CDPClient = void 0;
4
+ var client_1 = require("./client");
5
+ Object.defineProperty(exports, "CDPClient", { enumerable: true, get: function () { return client_1.CDPClient; } });
6
+ var types_1 = require("./types");
7
+ Object.defineProperty(exports, "SendEmailRequest", { enumerable: true, get: function () { return types_1.SendEmailRequest; } });
@@ -0,0 +1,122 @@
1
+ export interface CDPConfig {
2
+ /**
3
+ * API key for the CDP.
4
+ * This is required for authentication.
5
+ */
6
+ cdpApiKey: string;
7
+ /**
8
+ * Base URL for the OpenCDP API.
9
+ * If not provided, defaults to cdp server.
10
+ */
11
+ cdpEndpoint?: string;
12
+ /**
13
+ * Logger for OpenCDP operations.
14
+ * If not provided, defaults to console logging.
15
+ */
16
+ cdpLogger?: Logger;
17
+ /**
18
+ * Maximum number of concurrent OpenCDP requests.
19
+ * Default: 10. Hard-capped internally at 30.
20
+ */
21
+ maxConcurrentRequests?: number;
22
+ /**
23
+ * Request timeout in milliseconds.
24
+ * Default: 10000 (10 seconds).
25
+ */
26
+ timeout?: number;
27
+ /**
28
+ * Customer.io config for dual-write
29
+ */
30
+ customerIo?: {
31
+ siteId: string;
32
+ apiKey: string;
33
+ region?: 'us' | 'eu';
34
+ };
35
+ /**
36
+ * Optional: If true, sends events to both OpenCDP and Customer.io.
37
+ * This is useful for dual-write scenarios.
38
+ */
39
+ sendToCustomerIo?: boolean;
40
+ debug?: boolean;
41
+ /**
42
+ * Optional: Opt-in to throwing errors from the SDK.
43
+ * Default: false. When false, the SDK logs errors and resolves without throwing.
44
+ * When true, validation and request failures will throw so callers can catch them.
45
+ */
46
+ failOnException?: boolean;
47
+ }
48
+ export interface Logger {
49
+ debug(message: string): void;
50
+ error(message: string, context?: Record<string, any>): void;
51
+ warn(message: string): void;
52
+ }
53
+ export type Identifiers = {
54
+ id: string | number;
55
+ } | {
56
+ email: string;
57
+ };
58
+ export type SendEmailRequestRequiredOptions = {
59
+ to: string;
60
+ identifiers: Identifiers;
61
+ };
62
+ export type SendEmailRequestOptionalOptions = Partial<{
63
+ message_data: Record<string, any>;
64
+ headers: Record<string, any>;
65
+ preheader: string;
66
+ reply_to: string;
67
+ bcc: string[];
68
+ cc: string[];
69
+ plaintext_body: string;
70
+ amp_body: string;
71
+ fake_bcc: boolean;
72
+ disable_message_retention: boolean;
73
+ send_to_unsubscribed: boolean;
74
+ tracked: boolean;
75
+ queue_draft: boolean;
76
+ send_at: number;
77
+ disable_css_preprocessing: boolean;
78
+ language: string;
79
+ attachments: Record<string, string>;
80
+ }>;
81
+ export type SendEmailRequestWithTemplate = SendEmailRequestRequiredOptions & SendEmailRequestOptionalOptions & {
82
+ transactional_message_id: string | number;
83
+ };
84
+ export type SendEmailRequestWithoutTemplate = SendEmailRequestRequiredOptions & SendEmailRequestOptionalOptions & {
85
+ body: string;
86
+ subject: string;
87
+ from: string;
88
+ };
89
+ export type SendEmailRequestOptions = SendEmailRequestWithTemplate | SendEmailRequestWithoutTemplate;
90
+ export type Message = Partial<SendEmailRequestWithTemplate & SendEmailRequestWithoutTemplate> & {
91
+ attachments?: Record<string, string>;
92
+ };
93
+ export declare class SendEmailRequest {
94
+ message: Message;
95
+ constructor(opts: SendEmailRequestOptions);
96
+ attach(name: string, data: any, options?: {
97
+ encode?: boolean | undefined;
98
+ }): void;
99
+ }
100
+ export interface SendPushRequest {
101
+ identifiers: {
102
+ id?: string;
103
+ email?: string;
104
+ cdp_id?: string;
105
+ };
106
+ transactional_message_id: string | number;
107
+ title?: string;
108
+ body?: string;
109
+ message_data?: Record<string, any>;
110
+ }
111
+ export interface SendSmsRequest {
112
+ identifiers: {
113
+ id?: string;
114
+ email?: string;
115
+ cdp_id?: string;
116
+ };
117
+ transactional_message_id?: string | number;
118
+ to?: string;
119
+ from?: string;
120
+ body?: string;
121
+ message_data?: Record<string, any>;
122
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SendEmailRequest = void 0;
4
+ class SendEmailRequest {
5
+ constructor(opts) {
6
+ this.message = {
7
+ to: opts.to,
8
+ identifiers: opts.identifiers,
9
+ message_data: opts.message_data,
10
+ send_at: opts.send_at,
11
+ disable_message_retention: opts.disable_message_retention,
12
+ send_to_unsubscribed: opts.send_to_unsubscribed,
13
+ queue_draft: opts.queue_draft,
14
+ bcc: opts.bcc,
15
+ cc: opts.cc,
16
+ fake_bcc: opts.fake_bcc,
17
+ reply_to: opts.reply_to,
18
+ preheader: opts.preheader,
19
+ headers: opts.headers,
20
+ disable_css_preprocessing: opts.disable_css_preprocessing,
21
+ tracked: opts.tracked,
22
+ language: opts.language,
23
+ attachments: opts.attachments,
24
+ // Template-based fields
25
+ transactional_message_id: 'transactional_message_id' in opts ? opts.transactional_message_id : undefined,
26
+ body: 'body' in opts ? opts.body : undefined,
27
+ amp_body: opts.amp_body,
28
+ plaintext_body: opts.plaintext_body,
29
+ subject: 'subject' in opts ? opts.subject : undefined,
30
+ from: 'from' in opts ? opts.from : undefined
31
+ };
32
+ }
33
+ attach(name, data, options) {
34
+ if (!this.message.attachments) {
35
+ this.message.attachments = {};
36
+ }
37
+ this.message.attachments[name] = data;
38
+ }
39
+ }
40
+ exports.SendEmailRequest = SendEmailRequest;
@@ -0,0 +1,87 @@
1
+ import { CDPConfig, SendEmailRequest, SendPushRequest, SendSmsRequest } from "./types";
2
+ interface DeviceRegistrationParameters {
3
+ deviceId: string;
4
+ name?: string;
5
+ platform: "android" | "ios" | "web";
6
+ osVersion?: string;
7
+ model?: string;
8
+ fcmToken: string;
9
+ apnToken?: string;
10
+ appVersion?: string;
11
+ last_active_at?: string;
12
+ attributes?: Record<string, any>;
13
+ }
14
+ export declare class CDPClient {
15
+ private config;
16
+ private readonly customerIoClient;
17
+ private readonly apiRoot;
18
+ private readonly sendToCustomerIo;
19
+ private readonly logger;
20
+ private limit;
21
+ private timeout;
22
+ private readonly axiosInstance;
23
+ constructor(config: CDPConfig);
24
+ /**
25
+ * Tests the connection to the OpenCDP API server.
26
+ * Sends a ping request to verify that the configured endpoint is reachable and valid.
27
+ *
28
+ * This method ensures that credentials, and network access are configured correctly.
29
+ * It does NOT establish a persistent connection.
30
+ *
31
+ * Do not ping before sending each request
32
+ * @throws Error only when config.failOnException === true and the connection fails due to invalid credentials, network issues, or timeouts.
33
+ */
34
+ ping(): Promise<void>;
35
+ validateConnection(): Promise<void>;
36
+ private limited;
37
+ /**
38
+ * Identify a person in the CDP
39
+ * This method is concurrency-limited using p-limit to avoid overwhelming traffic external traffic.
40
+ * @param identifier The person identifier
41
+ * @param properties Additional properties for the person
42
+ * @throws Error only when config.failOnException === true (e.g., when the identifier is empty or the request fails)
43
+ */
44
+ identify(identifier: string, properties?: Record<string, any>): Promise<void>;
45
+ /**
46
+ * Track an event for a person.
47
+ * @param identifier The person identifier
48
+ * @param eventName The event name
49
+ * @param properties Additional properties for the event
50
+ * @throws Error only when config.failOnException === true (e.g., when validation or request fails)
51
+ */
52
+ track(identifier: string, eventName: string, properties?: Record<string, any>): Promise<void>;
53
+ /**
54
+ * Register a device for a person. A device must be registered to send push notifications
55
+ * @param identifier
56
+ * @param deviceRegistrationParameters
57
+ * @throws Error only when config.failOnException === true (e.g., when validation or request fails)
58
+ */
59
+ registerDevice(identifier: string, deviceRegistrationParameters: DeviceRegistrationParameters): Promise<void>;
60
+ /**
61
+ * Send an email using the CDP transactional email service
62
+ * @param request The send email request parameters
63
+ * @returns Promise that resolves when the email is sent
64
+ * @throws Error only when config.failOnException === true and validation or the request fails
65
+ */
66
+ sendEmail(request: SendEmailRequest): Promise<Record<string, any>>;
67
+ /**
68
+ * Warns about unsupported fields that are accepted but not processed by the backend
69
+ * @param request The send email request parameters
70
+ */
71
+ private warnUnsupportedFields;
72
+ /**
73
+ * Send a push notification using the OpenCDP transactional push service
74
+ * @param request The send push request parameters
75
+ * @returns Promise that resolves when the push notification is sent
76
+ * @throws Error only when config.failOnException === true and validation or the request fails
77
+ */
78
+ sendPush(request: SendPushRequest): Promise<any>;
79
+ /**
80
+ * Send an SMS using the OpenCDP transactional SMS service
81
+ * @param request The send SMS request parameters
82
+ * @returns Promise that resolves when the SMS is sent
83
+ * @throws Error only when config.failOnException === true and validation or the request fails
84
+ */
85
+ sendSms(request: SendSmsRequest): Promise<any>;
86
+ }
87
+ export {};