@dawntech/blip-tools 0.1.10 → 0.2.0

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.
@@ -1,12 +1,10 @@
1
- function normalizeValue(value) {
2
- return String(value).replaceAll(' ', '%20');
3
- }
1
+ import encodeString from './encode-strings.js';
4
2
  export default function encodeBlipParams(params) {
5
3
  //This function does not uses any of the standard methods of URI parsing because Blip does not follow any of them
6
4
  const parsedParams = [];
7
5
  for (const [key, value] of Object.entries(params)) {
8
6
  if (value && key) {
9
- parsedParams.push(`${normalizeValue(key)}=${normalizeValue(value)}`);
7
+ parsedParams.push(`${encodeString(key)}=${encodeString(value)}`);
10
8
  }
11
9
  }
12
10
  return parsedParams.join('&');
@@ -0,0 +1 @@
1
+ export default function encodeString(value: unknown): string;
@@ -0,0 +1,3 @@
1
+ export default function encodeString(value) {
2
+ return String(value).replaceAll(' ', '%20');
3
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { AxiosInstance } from 'axios';
2
1
  import { Template } from './types/template.js';
3
2
  import { Contact } from './types/contact.js';
4
3
  import { BlipConstructor } from './types/blip-constructor.js';
@@ -7,9 +6,11 @@ import { AttendanceHourContainer } from './types/attendance-hour-container.js';
7
6
  import { Ticket } from './types/ticket.js';
8
7
  import { CampaignNotification } from './types/campaign-notification.js';
9
8
  import { CampaignNotificationStatus } from './types/campaign-notification-status.js';
9
+ import BlipBucket from './modules/blip-bucket.js';
10
10
  export { BlipError, type Ticket, type AttendanceHourContainer, type Contact, type Template };
11
11
  export default class Blip {
12
- api: AxiosInstance;
12
+ private api;
13
+ bucket: BlipBucket;
13
14
  constructor(params: BlipConstructor);
14
15
  /**
15
16
  * Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
@@ -48,6 +49,11 @@ export default class Blip {
48
49
  identity: string;
49
50
  content: string;
50
51
  }): Promise<void>;
52
+ sendMessageWithButtons({ identity, message, buttons, }: {
53
+ identity: string;
54
+ message: string;
55
+ buttons: string[];
56
+ }): Promise<void>;
51
57
  updateContact({ identity, contactData }: {
52
58
  identity: string;
53
59
  contactData: Record<string, unknown>;
package/dist/index.js CHANGED
@@ -6,9 +6,11 @@ import formatTemplateParams from './helpers/format-template-params.js';
6
6
  import validateInstance from './helpers/validate-instance.js';
7
7
  import BlipError from './exceptions/blip-error.js';
8
8
  import encodeBlipParams from './helpers/encode-blip-params.js';
9
+ import BlipBucket from './modules/blip-bucket.js';
9
10
  export { BlipError };
10
11
  export default class Blip {
11
12
  api;
13
+ bucket;
12
14
  constructor(params) {
13
15
  validateInstance(params);
14
16
  this.api = axios.create({
@@ -18,6 +20,7 @@ export default class Blip {
18
20
  },
19
21
  timeout: 30000,
20
22
  });
23
+ this.bucket = new BlipBucket(this.api);
21
24
  }
22
25
  /**
23
26
  * Sends a template-based message to any user. For sending messages to clients who are active, use `sendTemplateMessage` instead.
@@ -142,6 +145,24 @@ export default class Blip {
142
145
  handleError(error);
143
146
  }
144
147
  }
148
+ async sendMessageWithButtons({ identity, message, buttons, }) {
149
+ try {
150
+ const response = await this.api.post('/messages', {
151
+ id: randomUUID(),
152
+ to: identity,
153
+ type: 'application/vnd.lime.select+json',
154
+ content: {
155
+ scope: buttons.length <= 3 ? 'immediate' : null,
156
+ text: message,
157
+ options: buttons.map((button, index) => ({ order: index + 1, text: button })),
158
+ },
159
+ });
160
+ validateResponse(response);
161
+ }
162
+ catch (error) {
163
+ handleError(error);
164
+ }
165
+ }
145
166
  async updateContact({ identity, contactData }) {
146
167
  try {
147
168
  const response = await this.api.post('/commands', {
@@ -233,11 +254,9 @@ export default class Blip {
233
254
  resource: {
234
255
  category,
235
256
  action,
257
+ contact: identity ? { identity } : undefined,
236
258
  },
237
259
  };
238
- if (identity && body.resource) {
239
- body.resource.contact = { identity };
240
- }
241
260
  const response = await this.api.post('/commands', body);
242
261
  validateResponse(response);
243
262
  }
@@ -0,0 +1,21 @@
1
+ import { AxiosInstance } from 'axios';
2
+ export default class BlipBucket {
3
+ private api;
4
+ constructor(api: AxiosInstance);
5
+ /**
6
+ Stores JSON content in a bucket provided by Blip. The content can be updaded or retrieved using the same id used in the update.
7
+ You can optionally provide a expiration time (in milliseconds) to delete the document after a certain amount of time.
8
+ */
9
+ storeContentInBucket(id: string, content: unknown, opts?: {
10
+ expiresInMs?: number;
11
+ }): Promise<void>;
12
+ /**
13
+ Retrieves a document from the Blip bucket.
14
+ */
15
+ getContentFromBucket<T>(id: string): Promise<T>;
16
+ getAllContentIdsFromBucket(): Promise<string[]>;
17
+ /**
18
+ Deletes a JSON document from the Blip bucket.
19
+ */
20
+ deleteContentFromBucket(id: string): Promise<void>;
21
+ }
@@ -0,0 +1,88 @@
1
+ import encodeString from '../helpers/encode-strings.js';
2
+ import { randomUUID } from 'crypto';
3
+ import encodeBlipParams from '../helpers/encode-blip-params.js';
4
+ import validateResponse from '../helpers/validate-response.js';
5
+ import handleError from '../helpers/handle-error.js';
6
+ export default class BlipBucket {
7
+ api;
8
+ constructor(api) {
9
+ this.api = api;
10
+ }
11
+ /**
12
+ Stores JSON content in a bucket provided by Blip. The content can be updaded or retrieved using the same id used in the update.
13
+ You can optionally provide a expiration time (in milliseconds) to delete the document after a certain amount of time.
14
+ */
15
+ async storeContentInBucket(id, content, opts) {
16
+ try {
17
+ let uri = `/buckets/${encodeString(id)}`;
18
+ const params = {};
19
+ if (opts?.expiresInMs) {
20
+ params.expiration = opts?.expiresInMs;
21
+ uri += `?${encodeBlipParams(params)}`;
22
+ }
23
+ const body = {
24
+ id: randomUUID(),
25
+ method: 'set',
26
+ type: 'application/json',
27
+ uri,
28
+ resource: content,
29
+ };
30
+ const response = await this.api.post('/commands', body);
31
+ validateResponse(response);
32
+ }
33
+ catch (error) {
34
+ throw handleError(error);
35
+ }
36
+ }
37
+ /**
38
+ Retrieves a document from the Blip bucket.
39
+ */
40
+ async getContentFromBucket(id) {
41
+ try {
42
+ const body = {
43
+ id: randomUUID(),
44
+ method: 'get',
45
+ uri: `/buckets/${encodeString(id)}`,
46
+ };
47
+ const response = await this.api.post('/commands', body);
48
+ validateResponse(response);
49
+ return response.data.resource;
50
+ }
51
+ catch (error) {
52
+ throw handleError(error);
53
+ }
54
+ }
55
+ async getAllContentIdsFromBucket() {
56
+ try {
57
+ const body = {
58
+ id: randomUUID(),
59
+ method: 'get',
60
+ uri: `/buckets`,
61
+ };
62
+ const response = await this.api.post('/commands', body);
63
+ validateResponse(response);
64
+ return response.data.resource.items;
65
+ }
66
+ catch (error) {
67
+ throw handleError(error);
68
+ }
69
+ }
70
+ /**
71
+ Deletes a JSON document from the Blip bucket.
72
+ */
73
+ async deleteContentFromBucket(id) {
74
+ try {
75
+ const body = {
76
+ id: randomUUID(),
77
+ method: 'delete',
78
+ uri: `/buckets/${encodeString(id)}`,
79
+ to: 'postmaster@msging.net',
80
+ };
81
+ const response = await this.api.post('/commands', body);
82
+ validateResponse(response);
83
+ }
84
+ catch (error) {
85
+ throw handleError(error);
86
+ }
87
+ }
88
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dawntech/blip-tools",
3
- "version": "0.1.10",
3
+ "version": "0.2.0",
4
4
  "description": "Node package for Blip API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",