@dawntech/blip-tools 0.1.5 → 0.1.7

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,4 @@
1
+ export default class BlipError extends Error {
2
+ code: number;
3
+ constructor(message: string, code?: number);
4
+ }
@@ -0,0 +1,7 @@
1
+ export default class BlipError extends Error {
2
+ code;
3
+ constructor(message, code = 0) {
4
+ super(message);
5
+ this.code = code;
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ export default function formatTemplateParams(params: string[]): Record<string, string>;
@@ -0,0 +1,6 @@
1
+ export default function formatTemplateParams(params) {
2
+ return params.reduce((prev, current, index) => {
3
+ prev[index + 1] = current;
4
+ return prev;
5
+ }, {});
6
+ }
@@ -0,0 +1 @@
1
+ export default function handleError(error: unknown): void;
@@ -0,0 +1,14 @@
1
+ import { AxiosError } from 'axios';
2
+ import BlipError from '../exceptions/blip-error.js';
3
+ export default function handleError(error) {
4
+ if (error instanceof BlipError) {
5
+ throw error;
6
+ }
7
+ if (error instanceof AxiosError) {
8
+ if (error.response?.data.code) {
9
+ throw new BlipError(error.response?.data.description, error.response?.data.code);
10
+ }
11
+ throw new BlipError(error.message, 0);
12
+ }
13
+ throw error;
14
+ }
@@ -0,0 +1,2 @@
1
+ import { BlipConstructor } from '../types/blip-constructor.js';
2
+ export default function validateInstance(params: BlipConstructor): void;
@@ -0,0 +1,9 @@
1
+ import BlipError from '../exceptions/blip-error.js';
2
+ export default function validateInstance(params) {
3
+ if (!params.contract) {
4
+ throw new BlipError('A contract must be specified.');
5
+ }
6
+ if (!params.authToken) {
7
+ throw new BlipError('An authToken must be provided.');
8
+ }
9
+ }
@@ -0,0 +1,2 @@
1
+ import { AxiosResponse } from 'axios';
2
+ export default function validateResponse(response: AxiosResponse): void;
@@ -0,0 +1,6 @@
1
+ import BlipError from '../exceptions/blip-error.js';
2
+ export default function validateResponse(response) {
3
+ if (response.data.status === 'failure') {
4
+ throw new BlipError(response.data.reason.description, response.data.reason.code);
5
+ }
6
+ }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,58 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { Template } from './types/template.js';
3
+ import { Contact } from './types/contact.js';
4
+ import { BlipConstructor } from './types/blip-constructor.js';
5
+ import BlipError from './exceptions/blip-error.js';
6
+ export { BlipError };
1
7
  export default class Blip {
2
- baseUrl: string;
3
- constructor(baseUrl: string);
4
- someMethod(): void;
5
- fetchData(endpoint: string): Promise<unknown>;
8
+ api: AxiosInstance;
9
+ constructor(params: BlipConstructor);
10
+ /**
11
+ * Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
12
+ */
13
+ sendIndividualActiveCampaign({ campaignName, masterState, flowId, stateId, channelType, phone, templateName, params, }: {
14
+ campaignName: string;
15
+ masterState: string;
16
+ flowId: string;
17
+ stateId: string;
18
+ channelType: string;
19
+ phone: string;
20
+ templateName: string;
21
+ params: string[];
22
+ }): Promise<void>;
23
+ getContextVariable({ identity, variableName }: {
24
+ identity: string;
25
+ variableName: string;
26
+ }): Promise<unknown>;
27
+ getAvailableContextVariables({ identity }: {
28
+ identity: string;
29
+ }): Promise<Record<string, unknown>>;
30
+ /**
31
+ * Sends a template-based message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
32
+ */
33
+ sendTemplateMessage({ identity, template }: {
34
+ identity: string;
35
+ template: Template;
36
+ }): Promise<void>;
37
+ /**
38
+ * Sends a simple text message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
39
+ */
40
+ sendSimpleMessage({ identity, content }: {
41
+ identity: string;
42
+ content: string;
43
+ }): Promise<void>;
44
+ updateContact({ identity, contactData }: {
45
+ identity: string;
46
+ contactData: Record<string, unknown>;
47
+ }): Promise<void>;
48
+ getContact({ identity }: {
49
+ identity: string;
50
+ }): Promise<Contact>;
51
+ getWhatsAppTemplates(): Promise<Contact>;
52
+ setContextVariable({ value, variableName, identity, }: {
53
+ variableName: string;
54
+ value: unknown;
55
+ identity: string;
56
+ }): Promise<void>;
57
+ getWhatsAppIdentity(phone: string): Promise<string>;
6
58
  }
package/dist/index.js CHANGED
@@ -1,17 +1,205 @@
1
+ import axios from 'axios';
2
+ import { randomUUID } from 'crypto';
3
+ import validateResponse from './helpers/validate-response.js';
4
+ import handleError from './helpers/handle-error.js';
5
+ import formatTemplateParams from './helpers/format-template-params.js';
6
+ import validateInstance from './helpers/validate-instance.js';
7
+ import BlipError from './exceptions/blip-error.js';
8
+ export { BlipError };
1
9
  export default class Blip {
2
- baseUrl;
3
- constructor(baseUrl) {
4
- this.baseUrl = baseUrl;
5
- }
6
- someMethod() {
7
- // Example method
8
- }
9
- fetchData(endpoint) {
10
- return fetch(endpoint)
11
- .then((response) => response.json())
12
- .catch((error) => {
13
- console.error('Error fetching data:', error);
14
- throw error;
10
+ api;
11
+ constructor(params) {
12
+ validateInstance(params);
13
+ this.api = axios.create({
14
+ baseURL: `https://${params.contract}.http.msging.net`,
15
+ headers: {
16
+ Authorization: `Key ${params.authToken}`,
17
+ },
18
+ timeout: 30000,
15
19
  });
16
20
  }
21
+ /**
22
+ * Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
23
+ */
24
+ async sendIndividualActiveCampaign({ campaignName, masterState, flowId, stateId, channelType, phone, templateName, params, }) {
25
+ try {
26
+ const response = await this.api.post('/commands', {
27
+ id: randomUUID(),
28
+ to: 'postmaster@activecampaign.msging.net',
29
+ method: 'set',
30
+ uri: '/campaign/full',
31
+ type: 'application/vnd.iris.activecampaign.full-campaign+json',
32
+ resource: {
33
+ campaign: {
34
+ name: campaignName,
35
+ campaignType: 'Individual',
36
+ masterState,
37
+ flowId,
38
+ stateId,
39
+ channelType,
40
+ },
41
+ audience: {
42
+ recipient: `+${phone}`,
43
+ messageParams: formatTemplateParams(params),
44
+ },
45
+ message: {
46
+ messageTemplate: templateName,
47
+ messageParams: params.map((_param, index) => String(index + 1)),
48
+ channelType,
49
+ },
50
+ },
51
+ });
52
+ validateResponse(response);
53
+ }
54
+ catch (error) {
55
+ handleError(error);
56
+ }
57
+ }
58
+ async getContextVariable({ identity, variableName }) {
59
+ try {
60
+ const response = await this.api.post('/commands', {
61
+ id: randomUUID(),
62
+ to: 'postmaster@msging.net',
63
+ method: 'get',
64
+ uri: `/contexts/${identity}/${variableName}`,
65
+ });
66
+ validateResponse(response);
67
+ return response.data.resource;
68
+ }
69
+ catch (error) {
70
+ throw handleError(error);
71
+ }
72
+ }
73
+ async getAvailableContextVariables({ identity }) {
74
+ try {
75
+ const response = await this.api.post('/commands', {
76
+ id: randomUUID(),
77
+ to: 'postmaster@msging.net',
78
+ method: 'get',
79
+ uri: `/contexts/${identity}`,
80
+ });
81
+ validateResponse(response);
82
+ return response.data.resource;
83
+ }
84
+ catch (error) {
85
+ throw handleError(error);
86
+ }
87
+ }
88
+ /**
89
+ * Sends a template-based message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
90
+ */
91
+ async sendTemplateMessage({ identity, template }) {
92
+ try {
93
+ const response = await this.api.post('/messages', {
94
+ id: randomUUID(),
95
+ to: identity,
96
+ type: 'application/json',
97
+ content: {
98
+ type: 'template',
99
+ template,
100
+ },
101
+ });
102
+ validateResponse(response);
103
+ }
104
+ catch (error) {
105
+ handleError(error);
106
+ }
107
+ }
108
+ /**
109
+ * Sends a simple text message to an active user. For sending messages to clients who have never interacted or are inactive, use `sendTemplate` instead.
110
+ */
111
+ async sendSimpleMessage({ identity, content }) {
112
+ try {
113
+ const response = await this.api.post('/messages', {
114
+ id: randomUUID(),
115
+ to: identity,
116
+ type: 'text/plain',
117
+ content,
118
+ });
119
+ validateResponse(response);
120
+ }
121
+ catch (error) {
122
+ handleError(error);
123
+ }
124
+ }
125
+ async updateContact({ identity, contactData }) {
126
+ try {
127
+ const response = await this.api.post('/commands', {
128
+ id: randomUUID(),
129
+ to: 'postmaster@msging.net',
130
+ method: 'merge',
131
+ uri: '/contacts',
132
+ type: 'application/vnd.lime.contact+json',
133
+ resource: {
134
+ identity,
135
+ ...contactData,
136
+ },
137
+ });
138
+ validateResponse(response);
139
+ }
140
+ catch (error) {
141
+ handleError(error);
142
+ }
143
+ }
144
+ async getContact({ identity }) {
145
+ try {
146
+ const response = await this.api.post('/commands', {
147
+ id: randomUUID(),
148
+ to: 'postmaster@crm.msging.net',
149
+ method: 'get',
150
+ uri: `/contacts/${identity}`,
151
+ });
152
+ validateResponse(response);
153
+ return response.data.resource;
154
+ }
155
+ catch (error) {
156
+ throw handleError(error);
157
+ }
158
+ }
159
+ async getWhatsAppTemplates() {
160
+ try {
161
+ const response = await this.api.post('/commands', {
162
+ id: randomUUID(),
163
+ to: 'postmaster@wa.gw.msging.net',
164
+ method: 'get',
165
+ uri: '/message-templates',
166
+ });
167
+ validateResponse(response);
168
+ return response.data.resource.data;
169
+ }
170
+ catch (error) {
171
+ throw handleError(error);
172
+ }
173
+ }
174
+ async setContextVariable({ value, variableName, identity, }) {
175
+ try {
176
+ const resourceType = typeof value === 'object' ? 'application/vnd.lime.contact+json' : 'text/plain';
177
+ const response = await this.api.post('/commands', {
178
+ id: randomUUID(),
179
+ method: 'set',
180
+ uri: `/contexts/${identity}/${variableName}`,
181
+ type: resourceType,
182
+ resource: value,
183
+ });
184
+ validateResponse(response);
185
+ }
186
+ catch (error) {
187
+ handleError(error);
188
+ }
189
+ }
190
+ async getWhatsAppIdentity(phone) {
191
+ try {
192
+ const response = await this.api.post('/commands', {
193
+ id: randomUUID(),
194
+ to: 'postmaster@wa.gw.msging.net',
195
+ method: 'get',
196
+ uri: `lime://wa.gw.msging.net/accounts/+${phone}`,
197
+ });
198
+ validateResponse(response);
199
+ return response.data.resource.identity;
200
+ }
201
+ catch (error) {
202
+ throw handleError(error);
203
+ }
204
+ }
17
205
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dawntech/blip-tools",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Node package for Blip API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -30,10 +30,14 @@
30
30
  "license": "MIT",
31
31
  "devDependencies": {
32
32
  "@eslint/js": "^9.22.0",
33
+ "@types/node": "^22.13.10",
33
34
  "@typescript-eslint/eslint-plugin": "^8.26.0",
34
35
  "eslint": "^9.22.0",
35
36
  "prettier": "^3.5.3",
36
37
  "typescript": "^5.8.2",
37
38
  "typescript-eslint": "^8.26.0"
39
+ },
40
+ "dependencies": {
41
+ "axios": "^1.8.3"
38
42
  }
39
43
  }