@suprsend/node-sdk 0.0.1 → 0.0.5

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/.babelrc CHANGED
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "presets": ["@babel/preset-env"],
3
- "plugins": [["@babel/transform-runtime"]]
3
+ "plugins": ["@babel/transform-runtime", "add-module-exports"]
4
4
  }
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/config.js CHANGED
@@ -9,4 +9,5 @@ var config = {
9
9
  prod: "https://hub.suprsend.com/"
10
10
  };
11
11
  var _default = config;
12
- exports["default"] = _default;
12
+ exports["default"] = _default;
13
+ module.exports = exports.default;
package/dist/index.js CHANGED
@@ -42,11 +42,11 @@ var Suprsend = /*#__PURE__*/function () {
42
42
  key: "_validate",
43
43
  value: function _validate() {
44
44
  if (!this.env_key) {
45
- throw new Error("SuprsendError: Missing Mandatory WORKSPACE_ENVIRONEMENT");
45
+ throw new _utils.SuprsendError("Missing Mandatory WORKSPACE_ENVIRONEMENT");
46
46
  } else if (!this.env_secret) {
47
- throw new Error("SuprsendError: Missing Mandatory WORKSPACE_SECRET");
47
+ throw new _utils.SuprsendError("Missing Mandatory WORKSPACE_SECRET");
48
48
  } else if (!this.base_url) {
49
- throw new Error("SuprsendError: Missing Mandatory base url");
49
+ throw new _utils.SuprsendError("Missing Mandatory base url");
50
50
  }
51
51
  }
52
52
  }, {
@@ -80,7 +80,7 @@ var Suprsend = /*#__PURE__*/function () {
80
80
  }
81
81
 
82
82
  if (!body.data instanceof Object) {
83
- throw new Error("SuprsendError: data must be an object");
83
+ throw new _utils.SuprsendError("data must be an object");
84
84
  }
85
85
 
86
86
  var attachment = this._get_attachment_json_for_file(file_path);
@@ -115,4 +115,5 @@ var Suprsend = /*#__PURE__*/function () {
115
115
  }();
116
116
 
117
117
  var _default = Suprsend;
118
- exports["default"] = _default;
118
+ exports["default"] = _default;
119
+ module.exports = exports.default;
@@ -27,15 +27,13 @@
27
27
  "$ref": "#/definitions/non_empty_string",
28
28
  "description": "timestamp in ISO-8601 format. e.g 2021-08-27T20:14:51.643Z"
29
29
  },
30
- "priority_algorithm": {
31
- "type": "boolean"
32
- },
30
+ "priority_algorithm": { "type": "boolean" },
33
31
  "users": {
34
32
  "type": "array",
35
- "items": {
36
- "$ref": "#/definitions/user_setting"
37
- },
38
- "description": "user object to run workflow for"
33
+ "items": { "$ref": "#/definitions/user_setting" },
34
+ "minItems": 1,
35
+ "maxItems": 100,
36
+ "description": "user object to run workflow for. At least 1 user is required"
39
37
  },
40
38
  "data": {
41
39
  "type": "object",
@@ -46,141 +44,126 @@
46
44
  "definitions": {
47
45
  "non_empty_string": {
48
46
  "type": "string",
49
- "minLength": 1
47
+ "minLength": 2
48
+ },
49
+ "mobile_number_pattern": {
50
+ "type": "string",
51
+ "minLength": 8,
52
+ "pattern": "^\\+[0-9\\s]+",
53
+ "message": {
54
+ "required": "Either a mobile-number or an array of mobile-numbers. e.g [\"41446681800\"]",
55
+ "pattern": "number must start with + and must contain country code. e.g. +41446681800"
56
+ }
57
+ },
58
+ "email_pattern": {
59
+ "type": "string",
60
+ "format": "email",
61
+ "pattern": "^\\S+@\\S+\\.\\S+$",
62
+ "description": "email of an user",
63
+ "minLength": 6,
64
+ "maxLength": 127,
65
+ "message": {
66
+ "required": "",
67
+ "pattern": "value in email format required. e.g. user@example.com"
68
+ }
50
69
  },
51
70
  "user_setting": {
52
71
  "type": "object",
53
72
  "properties": {
54
- "distinct_id": {
55
- "$ref": "#/definitions/non_empty_string"
56
- },
73
+ "distinct_id": { "$ref": "#/definitions/non_empty_string" },
57
74
  "$email": {
58
75
  "oneOf": [
59
- {
60
- "$ref": "#/definitions/non_empty_string"
61
- },
76
+ { "$ref": "#/definitions/email_pattern" },
62
77
  {
63
78
  "type": "array",
64
79
  "uniqueItems": true,
65
80
  "maxItems": 10,
66
81
  "minItems": 1,
67
- "items": {
68
- "$ref": "#/definitions/non_empty_string"
69
- }
82
+ "items": { "$ref": "#/definitions/email_pattern" }
70
83
  }
71
84
  ]
72
85
  },
73
86
  "$sms": {
74
87
  "oneOf": [
75
- {
76
- "$ref": "#/definitions/non_empty_string"
77
- },
88
+ { "$ref": "#/definitions/mobile_number_pattern" },
78
89
  {
79
90
  "type": "array",
80
91
  "uniqueItems": true,
81
92
  "maxItems": 10,
82
93
  "minItems": 1,
83
- "items": {
84
- "$ref": "#/definitions/non_empty_string"
85
- }
94
+ "items": { "$ref": "#/definitions/mobile_number_pattern" }
86
95
  }
87
96
  ]
88
97
  },
89
98
  "$androidpush": {
90
99
  "oneOf": [
91
- {
92
- "$ref": "#/definitions/non_empty_string"
93
- },
100
+ { "$ref": "#/definitions/non_empty_string" },
94
101
  {
95
102
  "type": "array",
96
103
  "uniqueItems": true,
97
104
  "maxItems": 10,
98
105
  "minItems": 1,
99
- "items": {
100
- "$ref": "#/definitions/non_empty_string"
101
- }
106
+ "items": { "$ref": "#/definitions/non_empty_string" }
102
107
  }
103
108
  ]
104
109
  },
105
110
  "$iospush": {
106
111
  "oneOf": [
107
- {
108
- "$ref": "#/definitions/non_empty_string"
109
- },
112
+ { "$ref": "#/definitions/non_empty_string" },
110
113
  {
111
114
  "type": "array",
112
115
  "uniqueItems": true,
113
116
  "maxItems": 10,
114
117
  "minItems": 1,
115
- "items": {
116
- "$ref": "#/definitions/non_empty_string"
117
- }
118
+ "items": { "$ref": "#/definitions/non_empty_string" }
118
119
  }
119
120
  ]
120
121
  },
121
122
  "$whatsapp": {
122
123
  "oneOf": [
123
- {
124
- "$ref": "#/definitions/non_empty_string"
125
- },
124
+ { "$ref": "#/definitions/mobile_number_pattern" },
126
125
  {
127
126
  "type": "array",
128
127
  "uniqueItems": true,
129
128
  "maxItems": 10,
130
129
  "minItems": 1,
131
- "items": {
132
- "$ref": "#/definitions/non_empty_string"
133
- }
130
+ "items": { "$ref": "#/definitions/mobile_number_pattern" }
134
131
  }
135
132
  ]
136
133
  },
137
134
  "$webpush": {
138
135
  "oneOf": [
139
- {
140
- "type": "object",
141
- "minProperties": 1
142
- },
136
+ { "type": "object", "minProperties": 1 },
143
137
  {
144
138
  "type": "array",
145
139
  "uniqueItems": true,
146
140
  "maxItems": 10,
147
141
  "minItems": 1,
148
- "items": {
149
- "type": "object",
150
- "minProperties": 1
151
- }
142
+ "items": { "type": "object", "minProperties": 1 }
152
143
  }
153
144
  ]
154
145
  },
155
146
  "$inbox": {
156
147
  "oneOf": [
157
- {
158
- "$ref": "#/definitions/non_empty_string"
159
- },
148
+ { "$ref": "#/definitions/non_empty_string" },
160
149
  {
161
150
  "type": "array",
162
151
  "uniqueItems": true,
163
152
  "maxItems": 10,
164
153
  "minItems": 1,
165
- "items": {
166
- "$ref": "#/definitions/non_empty_string"
167
- }
154
+ "items": { "$ref": "#/definitions/non_empty_string" }
168
155
  }
169
156
  ]
170
157
  },
171
158
  "$messenger": {
172
159
  "oneOf": [
173
- {
174
- "$ref": "#/definitions/non_empty_string"
175
- },
160
+ { "$ref": "#/definitions/non_empty_string" },
176
161
  {
177
162
  "type": "array",
178
163
  "uniqueItems": true,
179
164
  "maxItems": 10,
180
165
  "minItems": 1,
181
- "items": {
182
- "$ref": "#/definitions/non_empty_string"
183
- }
166
+ "items": { "$ref": "#/definitions/non_empty_string" }
184
167
  }
185
168
  ]
186
169
  }
package/dist/signature.js CHANGED
@@ -27,4 +27,6 @@ function get_request_signature(url, http_verb, content, headers, secret) {
27
27
  var hash = _crypto["default"].createHmac("sha256", secret).update(sign_string);
28
28
 
29
29
  return hash.digest("base64");
30
- }
30
+ }
31
+
32
+ module.exports = exports.default;
package/dist/utils.js CHANGED
@@ -5,16 +5,31 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
+ exports.SuprsendError = void 0;
8
9
  exports._get_schema = _get_schema;
9
10
  exports.base64Encode = base64Encode;
10
11
  exports.resolveTilde = resolveTilde;
11
12
 
13
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
14
+
15
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
16
+
17
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
18
+
19
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
20
+
21
+ var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper"));
22
+
12
23
  var _os = _interopRequireDefault(require("os"));
13
24
 
14
25
  var _fs = _interopRequireDefault(require("fs"));
15
26
 
16
27
  var _path = _interopRequireDefault(require("path"));
17
28
 
29
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
30
+
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
+
18
33
  function base64Encode(file) {
19
34
  var body = _fs["default"].readFileSync(file);
20
35
 
@@ -56,8 +71,27 @@ function _load_json_schema(schema_name) {
56
71
 
57
72
  json_schema = JSON.parse(schema_string);
58
73
  } catch (e) {
59
- throw new Error("SuprsendError: Missing Schema");
74
+ throw new SuprsendError("Missing Schema");
60
75
  }
61
76
 
62
77
  return json_schema;
63
- }
78
+ }
79
+
80
+ var SuprsendError = /*#__PURE__*/function (_Error) {
81
+ (0, _inherits2["default"])(SuprsendError, _Error);
82
+
83
+ var _super = _createSuper(SuprsendError);
84
+
85
+ function SuprsendError(message) {
86
+ var _this;
87
+
88
+ (0, _classCallCheck2["default"])(this, SuprsendError);
89
+ _this = _super.call(this, message);
90
+ _this.name = "SuprsendError";
91
+ return _this;
92
+ }
93
+
94
+ return SuprsendError;
95
+ }( /*#__PURE__*/(0, _wrapNativeSuper2["default"])(Error));
96
+
97
+ exports.SuprsendError = SuprsendError;
package/dist/workflow.js CHANGED
@@ -17,8 +17,6 @@ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/creat
17
17
 
18
18
  var _signature = _interopRequireDefault(require("./signature"));
19
19
 
20
- var _https = _interopRequireDefault(require("https"));
21
-
22
20
  var _jsonschema = require("jsonschema");
23
21
 
24
22
  var _utils = require("./utils");
@@ -122,27 +120,25 @@ var Workflow = /*#__PURE__*/function () {
122
120
  }
123
121
 
124
122
  if (!(this.data.data instanceof Object)) {
125
- throw new Error("SuprsendError: data must be a object");
123
+ throw new _utils.SuprsendError("data must be a object");
126
124
  }
127
125
 
128
126
  var schema = (0, _utils._get_schema)("workflow");
129
-
130
- try {
131
- var v = new _jsonschema.Validator();
132
- v.validate(this.data, schema);
133
- } catch (e) {
134
- if (e instanceof _jsonschema.SchemaError) {
135
- throw new Error("SuprsendSchemaError:".concat(e.message));
136
- } else if (e instanceof _jsonschema.ValidationError) {
137
- throw new Error("SuprsendValidationError:".concat(e.message));
138
- }
127
+ var v = new _jsonschema.Validator();
128
+ var validated_data = v.validate(this.data, schema);
129
+
130
+ if (validated_data.valid) {
131
+ return this.data;
132
+ } else {
133
+ var error_obj = validated_data.errors[0];
134
+ var error_msg = "".concat(error_obj.property, " ").concat(error_obj.message);
135
+ throw new _utils.SuprsendError(error_msg);
139
136
  }
140
-
141
- return this.data;
142
137
  }
143
138
  }]);
144
139
  return Workflow;
145
140
  }();
146
141
 
147
142
  var _default = Workflow;
148
- exports["default"] = _default;
143
+ exports["default"] = _default;
144
+ module.exports = exports.default;
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@suprsend/node-sdk",
3
- "version": "0.0.1",
3
+ "version": "0.0.5",
4
4
  "description": "Suprsend Node SDK to trigger workflow from backend",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
- "build": "rm -rf dist && babel ./src --out-dir dist && mkdir ./dist/request_json && cp ./src/request_json/workflow.json ./dist/request_json/"
7
+ "build": "rm -rf dist && babel ./src --out-dir dist && mkdir ./dist/request_json && cp ./src/request_json/workflow.json ./dist/request_json/",
8
+ "publish_sdk": "npm run build && npm publish"
8
9
  },
9
10
  "keywords": [
10
11
  "suprsend-node-sdk",
11
12
  "workflow"
12
13
  ],
13
14
  "author": "Sivaram Katta",
14
- "license": "ISC",
15
+ "license": "MIT",
15
16
  "repository": {
16
17
  "type": "git",
17
18
  "url": "https://github.com/suprsend/suprsend-node-sdk.git"
@@ -29,6 +30,7 @@
29
30
  "@babel/cli": "^7.16.0",
30
31
  "@babel/core": "^7.16.0",
31
32
  "@babel/plugin-transform-runtime": "^7.16.4",
32
- "@babel/preset-env": "^7.16.4"
33
+ "@babel/preset-env": "^7.16.4",
34
+ "babel-plugin-add-module-exports": "^1.0.4"
33
35
  }
34
36
  }
package/src/index.js CHANGED
@@ -2,7 +2,7 @@ import config from "./config";
2
2
  import Workflow from "./workflow";
3
3
  import path from "path";
4
4
  import mime from "mime-types";
5
- import { base64Encode, resolveTilde } from "./utils";
5
+ import { base64Encode, resolveTilde, SuprsendError } from "./utils";
6
6
 
7
7
  const package_json = require("../package.json");
8
8
 
@@ -22,13 +22,11 @@ class Suprsend {
22
22
 
23
23
  _validate() {
24
24
  if (!this.env_key) {
25
- throw new Error(
26
- "SuprsendError: Missing Mandatory WORKSPACE_ENVIRONEMENT"
27
- );
25
+ throw new SuprsendError("Missing Mandatory WORKSPACE_ENVIRONEMENT");
28
26
  } else if (!this.env_secret) {
29
- throw new Error("SuprsendError: Missing Mandatory WORKSPACE_SECRET");
27
+ throw new SuprsendError("Missing Mandatory WORKSPACE_SECRET");
30
28
  } else if (!this.base_url) {
31
- throw new Error("SuprsendError: Missing Mandatory base url");
29
+ throw new SuprsendError("Missing Mandatory base url");
32
30
  }
33
31
  }
34
32
 
@@ -55,7 +53,7 @@ class Suprsend {
55
53
  body.data = {};
56
54
  }
57
55
  if (!body.data instanceof Object) {
58
- throw new Error("SuprsendError: data must be an object");
56
+ throw new SuprsendError("data must be an object");
59
57
  }
60
58
  const attachment = this._get_attachment_json_for_file(file_path);
61
59
  if (!body.data["$attachments"]) {
@@ -27,15 +27,13 @@
27
27
  "$ref": "#/definitions/non_empty_string",
28
28
  "description": "timestamp in ISO-8601 format. e.g 2021-08-27T20:14:51.643Z"
29
29
  },
30
- "priority_algorithm": {
31
- "type": "boolean"
32
- },
30
+ "priority_algorithm": { "type": "boolean" },
33
31
  "users": {
34
32
  "type": "array",
35
- "items": {
36
- "$ref": "#/definitions/user_setting"
37
- },
38
- "description": "user object to run workflow for"
33
+ "items": { "$ref": "#/definitions/user_setting" },
34
+ "minItems": 1,
35
+ "maxItems": 100,
36
+ "description": "user object to run workflow for. At least 1 user is required"
39
37
  },
40
38
  "data": {
41
39
  "type": "object",
@@ -46,141 +44,126 @@
46
44
  "definitions": {
47
45
  "non_empty_string": {
48
46
  "type": "string",
49
- "minLength": 1
47
+ "minLength": 2
48
+ },
49
+ "mobile_number_pattern": {
50
+ "type": "string",
51
+ "minLength": 8,
52
+ "pattern": "^\\+[0-9\\s]+",
53
+ "message": {
54
+ "required": "Either a mobile-number or an array of mobile-numbers. e.g [\"41446681800\"]",
55
+ "pattern": "number must start with + and must contain country code. e.g. +41446681800"
56
+ }
57
+ },
58
+ "email_pattern": {
59
+ "type": "string",
60
+ "format": "email",
61
+ "pattern": "^\\S+@\\S+\\.\\S+$",
62
+ "description": "email of an user",
63
+ "minLength": 6,
64
+ "maxLength": 127,
65
+ "message": {
66
+ "required": "",
67
+ "pattern": "value in email format required. e.g. user@example.com"
68
+ }
50
69
  },
51
70
  "user_setting": {
52
71
  "type": "object",
53
72
  "properties": {
54
- "distinct_id": {
55
- "$ref": "#/definitions/non_empty_string"
56
- },
73
+ "distinct_id": { "$ref": "#/definitions/non_empty_string" },
57
74
  "$email": {
58
75
  "oneOf": [
59
- {
60
- "$ref": "#/definitions/non_empty_string"
61
- },
76
+ { "$ref": "#/definitions/email_pattern" },
62
77
  {
63
78
  "type": "array",
64
79
  "uniqueItems": true,
65
80
  "maxItems": 10,
66
81
  "minItems": 1,
67
- "items": {
68
- "$ref": "#/definitions/non_empty_string"
69
- }
82
+ "items": { "$ref": "#/definitions/email_pattern" }
70
83
  }
71
84
  ]
72
85
  },
73
86
  "$sms": {
74
87
  "oneOf": [
75
- {
76
- "$ref": "#/definitions/non_empty_string"
77
- },
88
+ { "$ref": "#/definitions/mobile_number_pattern" },
78
89
  {
79
90
  "type": "array",
80
91
  "uniqueItems": true,
81
92
  "maxItems": 10,
82
93
  "minItems": 1,
83
- "items": {
84
- "$ref": "#/definitions/non_empty_string"
85
- }
94
+ "items": { "$ref": "#/definitions/mobile_number_pattern" }
86
95
  }
87
96
  ]
88
97
  },
89
98
  "$androidpush": {
90
99
  "oneOf": [
91
- {
92
- "$ref": "#/definitions/non_empty_string"
93
- },
100
+ { "$ref": "#/definitions/non_empty_string" },
94
101
  {
95
102
  "type": "array",
96
103
  "uniqueItems": true,
97
104
  "maxItems": 10,
98
105
  "minItems": 1,
99
- "items": {
100
- "$ref": "#/definitions/non_empty_string"
101
- }
106
+ "items": { "$ref": "#/definitions/non_empty_string" }
102
107
  }
103
108
  ]
104
109
  },
105
110
  "$iospush": {
106
111
  "oneOf": [
107
- {
108
- "$ref": "#/definitions/non_empty_string"
109
- },
112
+ { "$ref": "#/definitions/non_empty_string" },
110
113
  {
111
114
  "type": "array",
112
115
  "uniqueItems": true,
113
116
  "maxItems": 10,
114
117
  "minItems": 1,
115
- "items": {
116
- "$ref": "#/definitions/non_empty_string"
117
- }
118
+ "items": { "$ref": "#/definitions/non_empty_string" }
118
119
  }
119
120
  ]
120
121
  },
121
122
  "$whatsapp": {
122
123
  "oneOf": [
123
- {
124
- "$ref": "#/definitions/non_empty_string"
125
- },
124
+ { "$ref": "#/definitions/mobile_number_pattern" },
126
125
  {
127
126
  "type": "array",
128
127
  "uniqueItems": true,
129
128
  "maxItems": 10,
130
129
  "minItems": 1,
131
- "items": {
132
- "$ref": "#/definitions/non_empty_string"
133
- }
130
+ "items": { "$ref": "#/definitions/mobile_number_pattern" }
134
131
  }
135
132
  ]
136
133
  },
137
134
  "$webpush": {
138
135
  "oneOf": [
139
- {
140
- "type": "object",
141
- "minProperties": 1
142
- },
136
+ { "type": "object", "minProperties": 1 },
143
137
  {
144
138
  "type": "array",
145
139
  "uniqueItems": true,
146
140
  "maxItems": 10,
147
141
  "minItems": 1,
148
- "items": {
149
- "type": "object",
150
- "minProperties": 1
151
- }
142
+ "items": { "type": "object", "minProperties": 1 }
152
143
  }
153
144
  ]
154
145
  },
155
146
  "$inbox": {
156
147
  "oneOf": [
157
- {
158
- "$ref": "#/definitions/non_empty_string"
159
- },
148
+ { "$ref": "#/definitions/non_empty_string" },
160
149
  {
161
150
  "type": "array",
162
151
  "uniqueItems": true,
163
152
  "maxItems": 10,
164
153
  "minItems": 1,
165
- "items": {
166
- "$ref": "#/definitions/non_empty_string"
167
- }
154
+ "items": { "$ref": "#/definitions/non_empty_string" }
168
155
  }
169
156
  ]
170
157
  },
171
158
  "$messenger": {
172
159
  "oneOf": [
173
- {
174
- "$ref": "#/definitions/non_empty_string"
175
- },
160
+ { "$ref": "#/definitions/non_empty_string" },
176
161
  {
177
162
  "type": "array",
178
163
  "uniqueItems": true,
179
164
  "maxItems": 10,
180
165
  "minItems": 1,
181
- "items": {
182
- "$ref": "#/definitions/non_empty_string"
183
- }
166
+ "items": { "$ref": "#/definitions/non_empty_string" }
184
167
  }
185
168
  ]
186
169
  }
package/src/utils.js CHANGED
@@ -37,7 +37,14 @@ function _load_json_schema(schema_name) {
37
37
  var schema_string = fs.readFileSync(file_path, "utf8");
38
38
  json_schema = JSON.parse(schema_string);
39
39
  } catch (e) {
40
- throw new Error("SuprsendError: Missing Schema");
40
+ throw new SuprsendError("Missing Schema");
41
41
  }
42
42
  return json_schema;
43
43
  }
44
+
45
+ export class SuprsendError extends Error {
46
+ constructor(message) {
47
+ super(message);
48
+ this.name = "SuprsendError";
49
+ }
50
+ }
package/src/workflow.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import get_request_signature from "./signature";
2
- import https from "https";
3
- import { Validator, SchemaError, ValidationError } from "jsonschema";
4
- import { _get_schema } from "./utils";
2
+ import { Validator } from "jsonschema";
3
+ import { _get_schema, SuprsendError } from "./utils";
5
4
  import axios from "axios";
6
5
 
7
6
  class Workflow {
@@ -66,20 +65,18 @@ class Workflow {
66
65
  this.data.data = {};
67
66
  }
68
67
  if (!(this.data.data instanceof Object)) {
69
- throw new Error("SuprsendError: data must be a object");
68
+ throw new SuprsendError("data must be a object");
70
69
  }
71
70
  const schema = _get_schema("workflow");
72
- try {
73
- var v = new Validator();
74
- v.validate(this.data, schema);
75
- } catch (e) {
76
- if (e instanceof SchemaError) {
77
- throw new Error(`SuprsendSchemaError:${e.message}`);
78
- } else if (e instanceof ValidationError) {
79
- throw new Error(`SuprsendValidationError:${e.message}`);
80
- }
71
+ var v = new Validator();
72
+ const validated_data = v.validate(this.data, schema);
73
+ if (validated_data.valid) {
74
+ return this.data;
75
+ } else {
76
+ const error_obj = validated_data.errors[0];
77
+ const error_msg = `${error_obj.property} ${error_obj.message}`;
78
+ throw new SuprsendError(error_msg);
81
79
  }
82
- return this.data;
83
80
  }
84
81
  }
85
82