@dawntech/blip-tools 0.1.5 → 0.1.6

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) {
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 { 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,48 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { Template } from './types/template.js';
3
+ import { Contact } from './types/contact.js';
1
4
  export default class Blip {
2
- baseUrl: string;
3
- constructor(baseUrl: string);
4
- someMethod(): void;
5
- fetchData(endpoint: string): Promise<unknown>;
5
+ api: AxiosInstance;
6
+ constructor({ contract, authToken }: {
7
+ contract: string;
8
+ authToken: string;
9
+ });
10
+ sendTemplate({ campaignName, campaignType, masterState, flowId, stateId, channelType, phone, templateName, params, }: {
11
+ campaignName: string;
12
+ campaignType: string;
13
+ masterState: string;
14
+ flowId: string;
15
+ stateId: string;
16
+ channelType: string;
17
+ phone: string;
18
+ templateName: string;
19
+ params: string[];
20
+ }): Promise<void>;
21
+ getContextVariable({ identity, variableName }: {
22
+ identity: string;
23
+ variableName: string;
24
+ }): Promise<unknown>;
25
+ getAvailableContextVariables({ identity }: {
26
+ identity: string;
27
+ }): Promise<Record<string, unknown>>;
28
+ sendTemplateMessage({ identity, template }: {
29
+ identity: string;
30
+ template: Template;
31
+ }): Promise<void>;
32
+ sendSimpleMessage({ identity, content }: {
33
+ identity: string;
34
+ content: string;
35
+ }): Promise<void>;
36
+ updateContact({ identity, contactData }: {
37
+ identity: string;
38
+ contactData: Record<string, unknown>;
39
+ }): Promise<void>;
40
+ getContact({ identity }: {
41
+ identity: string;
42
+ }): Promise<Contact>;
43
+ setVariable({ value, variableName, identity }: {
44
+ variableName: string;
45
+ value: unknown;
46
+ identity: string;
47
+ }): Promise<void>;
6
48
  }
package/dist/index.js CHANGED
@@ -1,17 +1,161 @@
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';
1
6
  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;
7
+ api;
8
+ constructor({ contract, authToken }) {
9
+ this.api = axios.create({
10
+ baseURL: `https://${contract}.http.msging.net`,
11
+ headers: {
12
+ Authorization: `Key ${authToken}`,
13
+ },
14
+ timeout: 30000,
15
15
  });
16
16
  }
17
+ async sendTemplate({ campaignName, campaignType, masterState, flowId, stateId, channelType, phone, templateName, params, }) {
18
+ try {
19
+ const response = await this.api.post('/commands', {
20
+ id: randomUUID(),
21
+ to: 'postmaster@activecampaign.msging.net',
22
+ method: 'set',
23
+ uri: '/campaign/full',
24
+ type: 'application/vnd.iris.activecampaign.full-campaign+json',
25
+ resource: {
26
+ campaign: {
27
+ name: campaignName,
28
+ campaignType,
29
+ masterState,
30
+ flowId,
31
+ stateId,
32
+ channelType,
33
+ },
34
+ audience: {
35
+ recipient: `+${phone}`,
36
+ messageParams: formatTemplateParams(params),
37
+ },
38
+ message: {
39
+ messageTemplate: templateName,
40
+ messageParams: params.map((_param, index) => String(index + 1)),
41
+ channelType,
42
+ },
43
+ },
44
+ });
45
+ validateResponse(response);
46
+ }
47
+ catch (error) {
48
+ handleError(error);
49
+ }
50
+ }
51
+ async getContextVariable({ identity, variableName }) {
52
+ try {
53
+ const response = await this.api.post('/commands', {
54
+ id: randomUUID(),
55
+ to: 'postmaster@msging.net',
56
+ method: 'get',
57
+ uri: `/contexts/${identity}/${variableName}`,
58
+ });
59
+ validateResponse(response);
60
+ return response.data.resource;
61
+ }
62
+ catch (error) {
63
+ throw handleError(error);
64
+ }
65
+ }
66
+ async getAvailableContextVariables({ identity }) {
67
+ try {
68
+ const response = await this.api.post('/commands', {
69
+ id: randomUUID(),
70
+ to: 'postmaster@msging.net',
71
+ method: 'get',
72
+ uri: `/contexts/${identity}`,
73
+ });
74
+ validateResponse(response);
75
+ return response.data.resource;
76
+ }
77
+ catch (error) {
78
+ throw handleError(error);
79
+ }
80
+ }
81
+ async sendTemplateMessage({ identity, template }) {
82
+ try {
83
+ const response = await this.api.post('/messages', {
84
+ id: randomUUID(),
85
+ to: identity,
86
+ type: 'application/json',
87
+ content: {
88
+ type: 'template',
89
+ template,
90
+ },
91
+ });
92
+ validateResponse(response);
93
+ }
94
+ catch (error) {
95
+ handleError(error);
96
+ }
97
+ }
98
+ async sendSimpleMessage({ identity, content }) {
99
+ try {
100
+ const response = await this.api.post('/messages', {
101
+ id: randomUUID(),
102
+ to: identity,
103
+ type: 'text/plain',
104
+ content,
105
+ });
106
+ validateResponse(response);
107
+ }
108
+ catch (error) {
109
+ handleError(error);
110
+ }
111
+ }
112
+ async updateContact({ identity, contactData }) {
113
+ try {
114
+ const response = await this.api.post('/commands', {
115
+ id: randomUUID(),
116
+ to: 'postmaster@msging.net',
117
+ method: 'merge',
118
+ uri: '/contacts',
119
+ type: 'application/vnd.lime.contact+json',
120
+ resource: {
121
+ identity,
122
+ ...contactData,
123
+ },
124
+ });
125
+ validateResponse(response);
126
+ }
127
+ catch (error) {
128
+ handleError(error);
129
+ }
130
+ }
131
+ async getContact({ identity }) {
132
+ try {
133
+ const response = await this.api.post('/commands', {
134
+ id: randomUUID(),
135
+ to: 'postmaster@crm.msging.net',
136
+ method: 'get',
137
+ uri: `/contacts/${identity}`,
138
+ });
139
+ validateResponse(response);
140
+ return response.data.resource;
141
+ }
142
+ catch (error) {
143
+ throw handleError(error);
144
+ }
145
+ }
146
+ async setVariable({ value, variableName, identity }) {
147
+ try {
148
+ const response = await this.api.post('/commands', {
149
+ id: randomUUID(),
150
+ method: 'set',
151
+ uri: `/contexts/${identity}/${variableName}`,
152
+ type: 'application/vnd.lime.contact+json',
153
+ resource: value,
154
+ });
155
+ validateResponse(response);
156
+ }
157
+ catch (error) {
158
+ handleError(error);
159
+ }
160
+ }
17
161
  }
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.6",
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
  }