-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCommonUtils.js
More file actions
62 lines (51 loc) · 2 KB
/
CommonUtils.js
File metadata and controls
62 lines (51 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict'
import * as fs from 'fs'
class CommonUtils {
static validateString(name, val) {
if (!val || Object.prototype.toString.call(val) !== '[object String]')
throw new Error(`${name} must be a String!`)
}
static validateInteger(name, val) {
if (!Number.isInteger(val))
throw new Error(`${name} must be an integer!`)
}
static validateNumber(name, val) {
if (!val || !Number(val))
throw new Error(`${name} must be a number!`)
}
static validateObject(name, val) {
if (!val || Object.prototype.toString.call(val) !== '[object Object]')
throw new Error(`${name} must be an Object!`)
}
static generateMethodURL(params, method) {
if (method === "sendFileByUpload" || method === "uploadFile") {
return `${params.media}/waInstance${params.idInstance}/${method}/${params.apiTokenInstance}`
} else {
return `${params.host}/waInstance${params.idInstance}/${method}/${params.apiTokenInstance}`
}
}
static generateMethodURLWithQuery(params, method, queryParams) {
if (method === "sendFileByUpload" || method === "uploadFile") {
return `${params.media}/waInstance${params.idInstance}/${method}/${params.apiTokenInstance}${queryParams}`
} else {
return `${params.host}/waInstance${params.idInstance}/${method}/${params.apiTokenInstance}${queryParams}`
}
}
static validateChatIdPhoneNumber(chatId, phoneNumber) {
if (!chatId) {
CommonUtils.validateInteger('phoneNumber', phoneNumber)
}
if (!phoneNumber) {
CommonUtils.validateString('chatId', chatId)
}
}
static validateArray(name, val) {
if (!val || !Array.isArray(val))
throw new Error(`${name} must be an Array!`)
}
static validatePath(name, val) {
if (!val || !fs.existsSync(val))
throw new Error(`${name} not found!`)
}
}
export default CommonUtils