@suprsend/node-sdk 0.0.3 → 0.0.4

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.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # suprsend-node-sdk
2
+ This package can be included in a node project to easily integrate with `Suprsend` platform.
3
+
4
+ We're working towards creating SDK in other languages as well.
5
+
6
+ ### Suprsend SDKs available in following languages
7
+ * node (`suprsend-node-sdk`)
8
+ * python3 >= 3.7 (`suprsend-py-sdk`)
9
+
10
+ ### Installation
11
+ `suprsend-node-sdk` is available as npm package. You can install using npm or yarn.
12
+
13
+ Using npm:
14
+ ```bash
15
+ npm install @suprsend/node-sdk
16
+ ```
17
+ Using yarn:
18
+ ```bash
19
+ yarn add @suprsend/node-sdk
20
+ ```
21
+
22
+ ### Usage
23
+ Initialize the Suprsend SDK
24
+ ```node
25
+ const Suprsend = require("@suprsend/node-sdk");
26
+
27
+ // Initialize SDK
28
+ const supr_client = new Suprsend("env_key", "env_secret");
29
+ ```
30
+
31
+ Following example shows a sample request for triggering a workflow.
32
+ It triggers a notification to a user with id: `distinct_id`,
33
+ email: `user@example.com` & androidpush-token: `__android_push_token__`
34
+ using template `purchase-made` and notification_category `system`
35
+
36
+ ```node
37
+ // Prepare Workflow body
38
+ const workflow_body = {
39
+ "name": "Purchase Workflow",
40
+ "template": "purchase-made",
41
+ "notification_category": "system",
42
+ "delay": "15m",
43
+ "users": [
44
+ {
45
+ "distinct_id": "0f988f74-6982-41c5-8752-facb6911fb08",
46
+ "$email": ["user@example.com"],
47
+ "$androidpush": ["__android_push_token__"],
48
+ }
49
+ ],
50
+ "data": {
51
+ "template": {
52
+ "first_name": "User",
53
+ "spend_amount": "$10"
54
+ },
55
+ }
56
+ }
57
+
58
+ // Trigger workflow
59
+ const response = supr_client.trigger_workflow(workflow_body); // returns promise
60
+ response.then((res) => console.log("response", res));
61
+ ```
62
+
63
+ When you call `supr_client.trigger_workflow`, the SDK internally makes an HTTP call to SuprSend
64
+ Platform to register this request, and you'll receive a promise which resolve to response indicating
65
+ the acceptance status.
66
+
67
+ Note: The actual processing/execution of workflow happens asynchronously.
68
+
69
+ ```node
70
+ // If the call succeeds, response will looks like:
71
+ {
72
+ "success": true,
73
+ "status_code": 202,
74
+ "message": "Accepted",
75
+ }
76
+
77
+ // In case the call fails. You will receive a response with success=false
78
+ {
79
+ "success": false,
80
+ "status": 400,
81
+ "message": "error message",
82
+ }
83
+ ```
84
+
85
+ ### Add attachments
86
+
87
+ To add one or more Attachments to a Notification (viz. Email, Whatsapp),
88
+ call `supr_client.add_attachment(...)` for each file.
89
+ Ensure that file_path is proper, otherwise it will raise error.
90
+ ```node
91
+ // this snippet can be used to add attachment to workflow_body.
92
+ const file_path = "/home/user/billing.pdf"
93
+ supr_client.add_attachment(workflow_body, file_path);
94
+ ```
95
+
96
+ #### Attachment structure
97
+ The `add_attachment(...)` call appends below structure to `data->'$attachments'`
98
+
99
+ ```json
100
+ {
101
+ "filename": "billing.pdf",
102
+ "contentType": "application/pdf",
103
+ "data": "Q29uZ3JhdHVsYXRpb25zLCB5b3UgY2FuIGJhc2U2NCBkZWNvZGUh",
104
+ }
105
+ ```
106
+ Where
107
+ * `filename` - name of file.
108
+ * `contentType` - MIME-type of file content.
109
+ * `data` - base64-encoded content of file.
package/dist/workflow.js CHANGED
@@ -130,7 +130,8 @@ var Workflow = /*#__PURE__*/function () {
130
130
  if (validated_data.valid) {
131
131
  return this.data;
132
132
  } else {
133
- var error_msg = validated_data.errors[0].message;
133
+ var error_obj = validated_data.errors[0];
134
+ var error_msg = "".concat(error_obj.property, " ").concat(error_obj.message);
134
135
  throw new _utils.SuprsendError(error_msg);
135
136
  }
136
137
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suprsend/node-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Suprsend Node SDK to trigger workflow from backend",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/workflow.js CHANGED
@@ -73,7 +73,8 @@ class Workflow {
73
73
  if (validated_data.valid) {
74
74
  return this.data;
75
75
  } else {
76
- const error_msg = validated_data.errors[0].message;
76
+ const error_obj = validated_data.errors[0];
77
+ const error_msg = `${error_obj.property} ${error_obj.message}`;
77
78
  throw new SuprsendError(error_msg);
78
79
  }
79
80
  }