@suprsend/node-sdk 0.0.6 → 0.1.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.
package/src/utils.js CHANGED
@@ -1,187 +1,6 @@
1
1
  import os from "os";
2
2
  import fs from "fs";
3
- import path from "path";
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
- };
3
+ import { v4 as uuidv4 } from "uuid";
185
4
 
186
5
  export function base64Encode(file) {
187
6
  var body = fs.readFileSync(file);
@@ -200,32 +19,37 @@ export function resolveTilde(filePath) {
200
19
  return filePath;
201
20
  }
202
21
 
203
- let __JSON_SCHEMAS = {};
204
-
205
- export function _get_schema(schema_name) {
206
- var schema_body = __JSON_SCHEMAS[schema_name];
207
- if (!schema_body) {
208
- schema_body = schema;
209
- __JSON_SCHEMAS[schema_name] = schema_body;
22
+ export class SuprsendError extends Error {
23
+ constructor(message) {
24
+ super(message);
25
+ this.name = "SuprsendError";
210
26
  }
211
- return schema_body;
212
27
  }
213
28
 
214
- function _load_json_schema(schema_name) {
215
- var file_path = path.join(__dirname, `request_json/${schema_name}.json`);
216
- var json_schema;
217
- try {
218
- var schema_string = fs.readFileSync(file_path, "utf8");
219
- json_schema = JSON.parse(schema_string);
220
- } catch (e) {
221
- throw new SuprsendError("Missing Schema");
222
- }
223
- return json_schema;
29
+ export function is_string(value) {
30
+ return typeof value === "string";
224
31
  }
225
32
 
226
- export class SuprsendError extends Error {
227
- constructor(message) {
228
- super(message);
229
- this.name = "SuprsendError";
33
+ export function is_object(value) {
34
+ return typeof value === "object" && !Array.isArray(value) && value !== null;
35
+ }
36
+
37
+ export function is_empty(value) {
38
+ if (is_object(value)) {
39
+ return Object.keys(value) <= 0;
40
+ } else if (Array.isArray(value)) {
41
+ return value.length <= 0;
230
42
  }
231
43
  }
44
+
45
+ export const has_special_char = (str) => {
46
+ return str.startsWith("$") || str?.toLowerCase()?.startsWith("ss_");
47
+ };
48
+
49
+ export function uuid() {
50
+ return uuidv4();
51
+ }
52
+
53
+ export function epoch_milliseconds() {
54
+ return Math.round(Date.now());
55
+ }
package/src/workflow.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import get_request_signature from "./signature";
2
2
  import { Validator } from "jsonschema";
3
- import { _get_schema, SuprsendError } from "./utils";
3
+ import { SuprsendError } from "./utils";
4
4
  import axios from "axios";
5
5
 
6
+ const workflow_schema = require("./request_json/workflow.json");
7
+
6
8
  class Workflow {
7
9
  constructor(ss_instance, data) {
8
10
  this.ss_instance = ss_instance;
@@ -38,7 +40,7 @@ class Workflow {
38
40
  const signature = get_request_signature(
39
41
  this.url,
40
42
  "POST",
41
- this.data,
43
+ content_text,
42
44
  headers,
43
45
  this.ss_instance.env_secret
44
46
  );
@@ -67,7 +69,7 @@ class Workflow {
67
69
  if (!(this.data.data instanceof Object)) {
68
70
  throw new SuprsendError("data must be a object");
69
71
  }
70
- const schema = _get_schema("workflow");
72
+ const schema = workflow_schema;
71
73
  var v = new Validator();
72
74
  const validated_data = v.validate(this.data, schema);
73
75
  if (validated_data.valid) {