@suprsend/node-sdk 0.0.2 → 0.0.3

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/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);
@@ -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/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,23 +120,19 @@ 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_msg = validated_data.errors[0].message;
134
+ throw new _utils.SuprsendError(error_msg);
139
135
  }
140
-
141
- return this.data;
142
136
  }
143
137
  }]);
144
138
  return Workflow;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suprsend/node-sdk",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Suprsend Node SDK to trigger workflow from backend",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
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,17 @@ 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_msg = validated_data.errors[0].message;
77
+ throw new SuprsendError(error_msg);
81
78
  }
82
- return this.data;
83
79
  }
84
80
  }
85
81