Skip to content

Commit 0cbb89d

Browse files
committed
Add ability to send silent notifications (and add options on each call.
1 parent 90b48ff commit 0cbb89d

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

index.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ async function sendRequest(namespace, text, level = 'INFO', options = {}) {
6868
'Content-Type': 'application/json',
6969
},
7070
body: JSON.stringify({
71-
idChat: options.chat.id,
72-
secret: options.chat.secret,
71+
idChat: options?.chat?.id || undefined,
72+
secret: options?.chat?.secret || undefined,
7373
namespace,
7474
level,
7575
text,
76-
env: options.env,
76+
env: options.env || undefined,
77+
silent: options.silent || false,
7778
}),
7879
}
7980

@@ -96,8 +97,22 @@ async function sendRequest(namespace, text, level = 'INFO', options = {}) {
9697
}
9798
}
9899

100+
function isString(value) {
101+
return typeof value === 'string' || value instanceof String
102+
}
103+
99104
function pnotice(namespace, options = {}) {
100-
return (text, level = 'INFO') => sendRequest(namespace, text, level, options)
105+
return (text, level = 'INFO', optionsInner = {}) => {
106+
if (!isString(level)) {
107+
optionsInner = level
108+
}
109+
110+
if (optionsInner.level) {
111+
level = optionsInner.level
112+
}
113+
114+
return sendRequest(namespace, text, level, { ...options, ...optionsInner })
115+
}
101116
}
102117

103118
export default pnotice

test.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,36 @@ if (config.chat.id) {
8585
t.is(response?.error?.title, 'NamespaceMustBeString')
8686
})
8787

88+
test('handle level and optionsInner sent', async (t) => {
89+
const pn = pnotice('pntest', {
90+
disabled: false,
91+
debug: true,
92+
chat: config.chat,
93+
env: 'test',
94+
})
95+
const response = await pn('handle level and optionsInner sent', 'WARNING', { silent: true })
96+
t.is(response?.status, 'ok')
97+
})
98+
99+
test('handle no level but optionsInner as second argument', async (t) => {
100+
const pn = pnotice('pntest', {
101+
disabled: false,
102+
debug: true,
103+
chat: config.chat,
104+
env: 'test',
105+
})
106+
const response = await pn('handle no level but optionsInner as second argument', { level: 'TESTING', silent: true })
107+
t.is(response?.status, 'ok')
108+
})
109+
88110
test('handle successfully sent', async (t) => {
89111
const pn = pnotice('pntest', {
90112
disabled: false,
91113
debug: true,
92114
chat: config.chat,
93115
env: 'test',
94116
})
95-
const response = await pn('hello world', 'INFO')
117+
const response = await pn('handle successfully sent', 'INFO')
96118
t.is(response?.status, 'ok')
97119
})
98120
}

0 commit comments

Comments
 (0)