-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (96 loc) · 3.21 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import fetch from 'isomorphic-unfetch'
function checkInputs(namespace, text, level, options) {
if (options.url === undefined) {
options.url = 'https://pushnotice.chat/api/v1/chatnotice'
}
if (options.debug === undefined) {
options.debug = false
}
if (options.env === undefined) {
options.env = undefined
}
if (options.disabled === true) {
return { error: { title: 'Disabled', message: 'disabled not sending the notification' } }
}
if (options.chat === undefined) {
return { error: { title: 'MissingChatObject', message: 'chat object has to be provided' } }
}
if (options.chat?.id === undefined) {
return { error: { title: 'MissingChatId', message: 'chat id has to be provided' } }
}
if (options.chat?.secret === undefined) {
return { error: { title: 'MissingChatSecret', message: 'chat secret has to be provided' } }
}
if (namespace === undefined) {
return { error: { title: 'MissingNamespace', message: 'namespace has to be provided' } }
}
if (typeof namespace !== 'string' && !(namespace instanceof String)) {
return { error: { title: 'NamespaceMustBeString', message: 'namespace must be a String' } }
}
if (text === undefined && text === '') {
return { error: { title: 'MissingText', message: 'text has to be provided' } }
}
if (typeof text !== 'string' && !(text instanceof String)) {
return { error: { title: 'TextMustBeString', message: 'text must be a String' } }
}
if (level === undefined) {
return { error: { title: 'MissingLevel', message: 'level has to be provided' } }
}
if (typeof level !== 'string' && !(level instanceof String)) {
return { error: { title: 'LevelMustBeString', message: 'level must be a String' } }
}
}
async function sendRequest(namespace, text, level = 'INFO', options = {}) {
const error = checkInputs(namespace, text, level, options)
if (error && options.debug) {
console.error('PushNotice:', error?.error?.message || error)
return error
}
const request = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
idChat: options?.chat?.id || undefined,
secret: options?.chat?.secret || undefined,
namespace,
level,
text,
env: options.env || undefined,
silent: options.silent || false,
}),
}
try {
const response = await fetch(options.url, request)
const body = await response.json()
if (body.status !== 'ok') {
return {
error: {
title: 'ApiRequestFailed',
message: 'Error sending Notification to PushNotice API',
body,
},
}
}
return body
} catch {
return { error: { title: 'ApiRequestFailed', message: 'Error sending Notification to PushNotice API' } }
}
}
function isString(value) {
return typeof value === 'string' || value instanceof String
}
function pnotice(namespace, options = {}) {
return (text, level = 'INFO', optionsInner = {}) => {
if (!isString(level)) {
optionsInner = { ...level }
level = undefined
}
if (optionsInner.level) {
level = optionsInner.level
}
return sendRequest(namespace, text, level, { ...options, ...optionsInner })
}
}
export default pnotice