@suprsend/node-sdk 0.0.3 → 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021, SuprSend
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # suprsend-node-sdk
2
+ This package can be included in a node project to easily integrate with `Suprsend` platform.
3
+
4
+ Refer full documentation [here](https://docs.suprsend.com/docs/node)
5
+
6
+ We're working towards creating SDK in other languages as well.
7
+
8
+ ### Suprsend SDKs available in following languages
9
+ * node (`suprsend-node-sdk`)
10
+ * python3 >= 3.7 (`suprsend-py-sdk`)
11
+
12
+ ### Installation
13
+ `suprsend-node-sdk` is available as npm package. You can install using npm or yarn.
14
+
15
+ Using npm:
16
+ ```bash
17
+ npm install @suprsend/node-sdk
18
+ ```
19
+ Using yarn:
20
+ ```bash
21
+ yarn add @suprsend/node-sdk
22
+ ```
23
+
24
+ ### Usage
25
+ Initialize the Suprsend SDK
26
+ ```node
27
+ const Suprsend = require("@suprsend/node-sdk");
28
+
29
+ // Initialize SDK
30
+ const supr_client = new Suprsend("env_key", "env_secret");
31
+ ```
32
+
33
+ Following example shows a sample request for triggering a workflow.
34
+ It triggers a notification to a user with id: `distinct_id`,
35
+ email: `user@example.com` & androidpush-token: `__android_push_token__`
36
+ using template `purchase-made` and notification_category `system`
37
+
38
+ ```node
39
+ // Prepare Workflow body
40
+ const workflow_body = {
41
+ "name": "Purchase Workflow",
42
+ "template": "purchase-made",
43
+ "notification_category": "system",
44
+ "delay": "15m",
45
+ "users": [
46
+ {
47
+ "distinct_id": "0f988f74-6982-41c5-8752-facb6911fb08",
48
+ "$email": ["user@example.com"],
49
+ "$androidpush": ["__android_push_token__"],
50
+ }
51
+ ],
52
+ "data": {
53
+ "template": {
54
+ "first_name": "User",
55
+ "spend_amount": "$10"
56
+ },
57
+ }
58
+ }
59
+
60
+ // Trigger workflow
61
+ const response = supr_client.trigger_workflow(workflow_body); // returns promise
62
+ response.then((res) => console.log("response", res));
63
+ ```
64
+
65
+ When you call `supr_client.trigger_workflow`, the SDK internally makes an HTTP call to SuprSend
66
+ Platform to register this request, and you'll receive a promise which resolve to response indicating
67
+ the acceptance status.
68
+
69
+ Note: The actual processing/execution of workflow happens asynchronously.
70
+
71
+ ```node
72
+ // If the call succeeds, response will looks like:
73
+ {
74
+ "success": true,
75
+ "status_code": 202,
76
+ "message": "Accepted",
77
+ }
78
+
79
+ // In case the call fails. You will receive a response with success=false
80
+ {
81
+ "success": false,
82
+ "status": 400,
83
+ "message": "error message",
84
+ }
85
+ ```
86
+
87
+ ### Add attachments
88
+
89
+ To add one or more Attachments to a Notification (viz. Email, Whatsapp),
90
+ call `supr_client.add_attachment(...)` for each file.
91
+ Ensure that file_path is proper, otherwise it will raise error.
92
+ ```node
93
+ // this snippet can be used to add attachment to workflow_body.
94
+ const file_path = "/home/user/billing.pdf"
95
+ supr_client.add_attachment(workflow_body, file_path);
96
+ ```
97
+
98
+ #### Attachment structure
99
+ The `add_attachment(...)` call appends below structure to `data->'$attachments'`
100
+
101
+ ```json
102
+ {
103
+ "filename": "billing.pdf",
104
+ "contentType": "application/pdf",
105
+ "data": "Q29uZ3JhdHVsYXRpb25zLCB5b3UgY2FuIGJhc2U2NCBkZWNvZGUh",
106
+ }
107
+ ```
108
+ Where
109
+ * `filename` - name of file.
110
+ * `contentType` - MIME-type of file content.
111
+ * `data` - base64-encoded content of file.
package/dist/utils.js CHANGED
@@ -30,6 +30,198 @@ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflec
30
30
 
31
31
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
32
32
 
33
+ var schema = {
34
+ $schema: "http://json-schema.org/draft-07/schema#",
35
+ $id: "http://github.com/suprsend/suprsend-node-sdk/request_json/workflow.json",
36
+ title: "workflow_request",
37
+ description: "Json schema for workflow request",
38
+ $comment: "Json schema for workflow request",
39
+ type: "object",
40
+ properties: {
41
+ name: {
42
+ $ref: "#/definitions/non_empty_string",
43
+ description: "name of workflow"
44
+ },
45
+ template: {
46
+ $ref: "#/definitions/non_empty_string",
47
+ description: "slug of Template"
48
+ },
49
+ notification_category: {
50
+ $ref: "#/definitions/non_empty_string",
51
+ description: "slug of Notification category"
52
+ },
53
+ delay: {
54
+ type: ["string", "integer"],
55
+ minimum: 0,
56
+ description: "If string: format [XX]d[XX]h[XX]m[XX]s e.g 1d2h30m10s(for 1day 2hours 30minutes 10sec). If integer: value in number of seconds"
57
+ },
58
+ trigger_at: {
59
+ $ref: "#/definitions/non_empty_string",
60
+ description: "timestamp in ISO-8601 format. e.g 2021-08-27T20:14:51.643Z"
61
+ },
62
+ priority_algorithm: {
63
+ type: "boolean"
64
+ },
65
+ users: {
66
+ type: "array",
67
+ items: {
68
+ $ref: "#/definitions/user_setting"
69
+ },
70
+ minItems: 1,
71
+ maxItems: 100,
72
+ description: "user object to run workflow for. At least 1 user is required"
73
+ },
74
+ data: {
75
+ type: "object",
76
+ description: "variables to be used in workflow. e.g replacing templates variables."
77
+ }
78
+ },
79
+ required: ["name", "template", "notification_category", "users", "data"],
80
+ definitions: {
81
+ non_empty_string: {
82
+ type: "string",
83
+ minLength: 2
84
+ },
85
+ mobile_number_pattern: {
86
+ type: "string",
87
+ minLength: 8,
88
+ pattern: "^\\+[0-9\\s]+",
89
+ message: {
90
+ required: 'Either a mobile-number or an array of mobile-numbers. e.g ["41446681800"]',
91
+ pattern: "number must start with + and must contain country code. e.g. +41446681800"
92
+ }
93
+ },
94
+ email_pattern: {
95
+ type: "string",
96
+ format: "email",
97
+ pattern: "^\\S+@\\S+\\.\\S+$",
98
+ description: "email of an user",
99
+ minLength: 6,
100
+ maxLength: 127,
101
+ message: {
102
+ required: "",
103
+ pattern: "value in email format required. e.g. user@example.com"
104
+ }
105
+ },
106
+ user_setting: {
107
+ type: "object",
108
+ properties: {
109
+ distinct_id: {
110
+ $ref: "#/definitions/non_empty_string"
111
+ },
112
+ $email: {
113
+ oneOf: [{
114
+ $ref: "#/definitions/email_pattern"
115
+ }, {
116
+ type: "array",
117
+ uniqueItems: true,
118
+ maxItems: 10,
119
+ minItems: 1,
120
+ items: {
121
+ $ref: "#/definitions/email_pattern"
122
+ }
123
+ }]
124
+ },
125
+ $sms: {
126
+ oneOf: [{
127
+ $ref: "#/definitions/mobile_number_pattern"
128
+ }, {
129
+ type: "array",
130
+ uniqueItems: true,
131
+ maxItems: 10,
132
+ minItems: 1,
133
+ items: {
134
+ $ref: "#/definitions/mobile_number_pattern"
135
+ }
136
+ }]
137
+ },
138
+ $androidpush: {
139
+ oneOf: [{
140
+ $ref: "#/definitions/non_empty_string"
141
+ }, {
142
+ type: "array",
143
+ uniqueItems: true,
144
+ maxItems: 10,
145
+ minItems: 1,
146
+ items: {
147
+ $ref: "#/definitions/non_empty_string"
148
+ }
149
+ }]
150
+ },
151
+ $iospush: {
152
+ oneOf: [{
153
+ $ref: "#/definitions/non_empty_string"
154
+ }, {
155
+ type: "array",
156
+ uniqueItems: true,
157
+ maxItems: 10,
158
+ minItems: 1,
159
+ items: {
160
+ $ref: "#/definitions/non_empty_string"
161
+ }
162
+ }]
163
+ },
164
+ $whatsapp: {
165
+ oneOf: [{
166
+ $ref: "#/definitions/mobile_number_pattern"
167
+ }, {
168
+ type: "array",
169
+ uniqueItems: true,
170
+ maxItems: 10,
171
+ minItems: 1,
172
+ items: {
173
+ $ref: "#/definitions/mobile_number_pattern"
174
+ }
175
+ }]
176
+ },
177
+ $webpush: {
178
+ oneOf: [{
179
+ type: "object",
180
+ minProperties: 1
181
+ }, {
182
+ type: "array",
183
+ uniqueItems: true,
184
+ maxItems: 10,
185
+ minItems: 1,
186
+ items: {
187
+ type: "object",
188
+ minProperties: 1
189
+ }
190
+ }]
191
+ },
192
+ $inbox: {
193
+ oneOf: [{
194
+ $ref: "#/definitions/non_empty_string"
195
+ }, {
196
+ type: "array",
197
+ uniqueItems: true,
198
+ maxItems: 10,
199
+ minItems: 1,
200
+ items: {
201
+ $ref: "#/definitions/non_empty_string"
202
+ }
203
+ }]
204
+ },
205
+ $messenger: {
206
+ oneOf: [{
207
+ $ref: "#/definitions/non_empty_string"
208
+ }, {
209
+ type: "array",
210
+ uniqueItems: true,
211
+ maxItems: 10,
212
+ minItems: 1,
213
+ items: {
214
+ $ref: "#/definitions/non_empty_string"
215
+ }
216
+ }]
217
+ }
218
+ },
219
+ required: ["distinct_id"],
220
+ additionalProperties: false
221
+ }
222
+ }
223
+ };
224
+
33
225
  function base64Encode(file) {
34
226
  var body = _fs["default"].readFileSync(file);
35
227
 
@@ -54,7 +246,7 @@ function _get_schema(schema_name) {
54
246
  var schema_body = __JSON_SCHEMAS[schema_name];
55
247
 
56
248
  if (!schema_body) {
57
- schema_body = _load_json_schema(schema_name);
249
+ schema_body = schema;
58
250
  __JSON_SCHEMAS[schema_name] = schema_body;
59
251
  }
60
252
 
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.6",
4
4
  "description": "Suprsend Node SDK to trigger workflow from backend",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "workflow"
13
13
  ],
14
14
  "author": "Sivaram Katta",
15
- "license": "ISC",
15
+ "license": "MIT",
16
16
  "repository": {
17
17
  "type": "git",
18
18
  "url": "https://github.com/suprsend/suprsend-node-sdk.git"
package/src/utils.js CHANGED
@@ -2,6 +2,187 @@ import os from "os";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
4
 
5
+ const schema = {
6
+ $schema: "http://json-schema.org/draft-07/schema#",
7
+ $id: "http://github.com/suprsend/suprsend-node-sdk/request_json/workflow.json",
8
+ title: "workflow_request",
9
+ description: "Json schema for workflow request",
10
+ $comment: "Json schema for workflow request",
11
+ type: "object",
12
+ properties: {
13
+ name: {
14
+ $ref: "#/definitions/non_empty_string",
15
+ description: "name of workflow",
16
+ },
17
+ template: {
18
+ $ref: "#/definitions/non_empty_string",
19
+ description: "slug of Template",
20
+ },
21
+ notification_category: {
22
+ $ref: "#/definitions/non_empty_string",
23
+ description: "slug of Notification category",
24
+ },
25
+ delay: {
26
+ type: ["string", "integer"],
27
+ minimum: 0,
28
+ description:
29
+ "If string: format [XX]d[XX]h[XX]m[XX]s e.g 1d2h30m10s(for 1day 2hours 30minutes 10sec). If integer: value in number of seconds",
30
+ },
31
+ trigger_at: {
32
+ $ref: "#/definitions/non_empty_string",
33
+ description: "timestamp in ISO-8601 format. e.g 2021-08-27T20:14:51.643Z",
34
+ },
35
+ priority_algorithm: { type: "boolean" },
36
+ users: {
37
+ type: "array",
38
+ items: { $ref: "#/definitions/user_setting" },
39
+ minItems: 1,
40
+ maxItems: 100,
41
+ description:
42
+ "user object to run workflow for. At least 1 user is required",
43
+ },
44
+ data: {
45
+ type: "object",
46
+ description:
47
+ "variables to be used in workflow. e.g replacing templates variables.",
48
+ },
49
+ },
50
+ required: ["name", "template", "notification_category", "users", "data"],
51
+ definitions: {
52
+ non_empty_string: {
53
+ type: "string",
54
+ minLength: 2,
55
+ },
56
+ mobile_number_pattern: {
57
+ type: "string",
58
+ minLength: 8,
59
+ pattern: "^\\+[0-9\\s]+",
60
+ message: {
61
+ required:
62
+ 'Either a mobile-number or an array of mobile-numbers. e.g ["41446681800"]',
63
+ pattern:
64
+ "number must start with + and must contain country code. e.g. +41446681800",
65
+ },
66
+ },
67
+ email_pattern: {
68
+ type: "string",
69
+ format: "email",
70
+ pattern: "^\\S+@\\S+\\.\\S+$",
71
+ description: "email of an user",
72
+ minLength: 6,
73
+ maxLength: 127,
74
+ message: {
75
+ required: "",
76
+ pattern: "value in email format required. e.g. user@example.com",
77
+ },
78
+ },
79
+ user_setting: {
80
+ type: "object",
81
+ properties: {
82
+ distinct_id: { $ref: "#/definitions/non_empty_string" },
83
+ $email: {
84
+ oneOf: [
85
+ { $ref: "#/definitions/email_pattern" },
86
+ {
87
+ type: "array",
88
+ uniqueItems: true,
89
+ maxItems: 10,
90
+ minItems: 1,
91
+ items: { $ref: "#/definitions/email_pattern" },
92
+ },
93
+ ],
94
+ },
95
+ $sms: {
96
+ oneOf: [
97
+ { $ref: "#/definitions/mobile_number_pattern" },
98
+ {
99
+ type: "array",
100
+ uniqueItems: true,
101
+ maxItems: 10,
102
+ minItems: 1,
103
+ items: { $ref: "#/definitions/mobile_number_pattern" },
104
+ },
105
+ ],
106
+ },
107
+ $androidpush: {
108
+ oneOf: [
109
+ { $ref: "#/definitions/non_empty_string" },
110
+ {
111
+ type: "array",
112
+ uniqueItems: true,
113
+ maxItems: 10,
114
+ minItems: 1,
115
+ items: { $ref: "#/definitions/non_empty_string" },
116
+ },
117
+ ],
118
+ },
119
+ $iospush: {
120
+ oneOf: [
121
+ { $ref: "#/definitions/non_empty_string" },
122
+ {
123
+ type: "array",
124
+ uniqueItems: true,
125
+ maxItems: 10,
126
+ minItems: 1,
127
+ items: { $ref: "#/definitions/non_empty_string" },
128
+ },
129
+ ],
130
+ },
131
+ $whatsapp: {
132
+ oneOf: [
133
+ { $ref: "#/definitions/mobile_number_pattern" },
134
+ {
135
+ type: "array",
136
+ uniqueItems: true,
137
+ maxItems: 10,
138
+ minItems: 1,
139
+ items: { $ref: "#/definitions/mobile_number_pattern" },
140
+ },
141
+ ],
142
+ },
143
+ $webpush: {
144
+ oneOf: [
145
+ { type: "object", minProperties: 1 },
146
+ {
147
+ type: "array",
148
+ uniqueItems: true,
149
+ maxItems: 10,
150
+ minItems: 1,
151
+ items: { type: "object", minProperties: 1 },
152
+ },
153
+ ],
154
+ },
155
+ $inbox: {
156
+ oneOf: [
157
+ { $ref: "#/definitions/non_empty_string" },
158
+ {
159
+ type: "array",
160
+ uniqueItems: true,
161
+ maxItems: 10,
162
+ minItems: 1,
163
+ items: { $ref: "#/definitions/non_empty_string" },
164
+ },
165
+ ],
166
+ },
167
+ $messenger: {
168
+ oneOf: [
169
+ { $ref: "#/definitions/non_empty_string" },
170
+ {
171
+ type: "array",
172
+ uniqueItems: true,
173
+ maxItems: 10,
174
+ minItems: 1,
175
+ items: { $ref: "#/definitions/non_empty_string" },
176
+ },
177
+ ],
178
+ },
179
+ },
180
+ required: ["distinct_id"],
181
+ additionalProperties: false,
182
+ },
183
+ },
184
+ };
185
+
5
186
  export function base64Encode(file) {
6
187
  var body = fs.readFileSync(file);
7
188
  return body.toString("base64");
@@ -24,7 +205,7 @@ let __JSON_SCHEMAS = {};
24
205
  export function _get_schema(schema_name) {
25
206
  var schema_body = __JSON_SCHEMAS[schema_name];
26
207
  if (!schema_body) {
27
- schema_body = _load_json_schema(schema_name);
208
+ schema_body = schema;
28
209
  __JSON_SCHEMAS[schema_name] = schema_body;
29
210
  }
30
211
  return schema_body;
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
  }