Skip to content

Commit c9ebcb7

Browse files
committed
Upgrade to new request library unfetch (node-fetch in node.js env).
Harden the system to more strictly check input values. Update Author details. Start adding testing suite. Tests not yet included.
1 parent a9af359 commit c9ebcb7

File tree

2 files changed

+74
-51
lines changed

2 files changed

+74
-51
lines changed

index.js

+62-47
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,79 @@
1-
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
1+
const fetch = require('isomorphic-unfetch')
22

3-
const request = require('request-promise-native');
4-
5-
function sendRequest(namespace, text, level='INFO', options={}) {
3+
function checkInputs(namespace, text, level, options) {
4+
if (options.url === undefined) options.url = 'https://pushnotice.chat/api/v1/chatnotice'
5+
if (options.debug === undefined) options.debug = false
6+
if (options.env === undefined) options.env = undefined
67
if (options.disabled === true) {
7-
if (options.debug) console.log('PushNotice: disabled not sending the notification');
8-
return;
8+
return { error: { title: 'Disabled', message: 'disabled not sending the notification' } }
9+
}
10+
if (options.chat === undefined) {
11+
return { error: { title: 'MissingChatObject', message: 'chat object has to be provided' } }
12+
}
13+
if (options.chat?.id === undefined) {
14+
return { error: { title: 'MissingChatId', message: 'chat id has to be provided' } }
15+
}
16+
if (options.chat?.secret === undefined) {
17+
return { error: { title: 'MissingChatSecret', message: 'chat secret has to be provided' } }
18+
}
19+
if (namespace === undefined) {
20+
return { error: { title: 'MissingNamespace', message: 'namespace has to be provided' } }
921
}
10-
if (options.chat == undefined) {
11-
if (options.debug) console.error('PushNotice: chat object has to be provided');
12-
return;
22+
if (typeof namespace !== 'string' && !(namespace instanceof String)) {
23+
return { error: { title: 'NamespaceMustBeString', message: 'namespace must be a String' } }
1324
}
14-
if (options.chat && options.chat.id == undefined) {
15-
if (options.debug) console.error('PushNotice: chat id has to be provided');
16-
return;
25+
if (text === undefined && text === '') {
26+
return { error: { title: 'MissingText', message: 'text has to be provided' } }
1727
}
18-
if (options.chat && options.chat.secret == undefined) {
19-
if (options.debug) console.error('PushNotice: chat secret has to be provided');
20-
return;
28+
if (typeof text !== 'string' && !(text instanceof String)) {
29+
return { error: { title: 'TextMustBeString', message: 'text must be a String' } }
2130
}
22-
if (namespace == undefined) {
23-
if (options.debug) console.error('PushNotice: namespace has to be provided');
24-
return;
31+
if (level === undefined) {
32+
return { error: { title: 'MissingLevel', message: 'level has to be provided' } }
2533
}
26-
if (text == undefined) {
27-
if (options.debug) console.error('PushNotice: text has to be provided');
28-
return;
34+
if (typeof level !== 'string' && !(level instanceof String)) {
35+
return { error: { title: 'LevelMustBeString', message: 'level must be a String' } }
2936
}
30-
if (level == undefined) {
31-
if (options.debug) console.error('PushNotice: level has to be provided');
32-
return;
37+
}
38+
39+
async function sendRequest(namespace, text, level = 'INFO', options = {}) {
40+
const error = checkInputs(namespace, text, level, options)
41+
42+
if (error && options.debug) {
43+
console.error('PushNotice:', error?.error?.message || error)
44+
return error
3345
}
34-
if (options.url == undefined) options.url = 'https://pushnotice.chat/api/v1/chatnotice';
35-
if (options.debug == undefined) options.debug = false;
36-
if (options.env == undefined) options.env = undefined;
37-
let requestOptions = {
46+
47+
const request = {
3848
method: 'POST',
39-
uri: options.url,
40-
body: {
49+
headers: {
50+
'Content-Type': 'application/json',
51+
},
52+
body: JSON.stringify({
4153
chatId: options.chat.id,
4254
chatSecret: options.chat.secret,
43-
namespace: namespace,
44-
level: level,
45-
text: text,
55+
namespace,
56+
level,
57+
text,
4658
env: options.env,
47-
},
48-
json: true,
49-
headers: {
50-
'content-type': 'application/json',
59+
}),
60+
}
61+
62+
try {
63+
const response = await fetch(options.url, request)
64+
const body = await response.json()
65+
if (!body.ok && body.name !== 'notificationQueued') {
66+
return { error: { title: 'ApiRequestFailed', message: 'Error sending Notification to PushNotice API' } }
5167
}
52-
};
53-
request(requestOptions).then((res) => {
54-
if (options.debug) console.log("PushNotice: Successfully sent Notification to PushNotice API");
55-
}).catch((err) => {
56-
if (options.debug) console.error("PushNotice: Error sending Notification to PushNotice API", err);
57-
});
68+
} catch (err) {
69+
return { error: { title: 'ApiRequestFailed', message: 'Error sending Notification to PushNotice API' } }
70+
}
71+
72+
return { success: true }
5873
}
5974

60-
module.exports = (namespace, options={}) => {
61-
return (text, level='INFO') => {
62-
sendRequest(namespace, text, level, options);
63-
}
75+
function pnotice(namespace, options = {}) {
76+
return (text, level = 'INFO') => sendRequest(namespace, text, level, options)
6477
}
78+
79+
module.exports = pnotice

package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
"license": "ISC",
1313
"main": "index.js",
1414
"scripts": {
15-
"test": "echo \"Error: no test specified\" && exit 1"
15+
"test": "xo && ava -v",
16+
"ava": "ava -v --watch"
1617
},
1718
"files": [
1819
"index.js"
1920
],
20-
"author": "Chris Spiegl <chris@spiegl.io>",
21+
"author": {
22+
"name": "Chris Spiegl",
23+
"email": "chris@chrisspiegl.com",
24+
"url": "https://ChrisSpiegl.com"
25+
},
2126
"repository": {
2227
"type": "git",
2328
"url": "https://github.com/spieglio/pushnotice-node-api-client.git"
@@ -27,7 +32,10 @@
2732
},
2833
"homepage": "https://pushnotice.chat",
2934
"dependencies": {
30-
"request": "^2.83.0",
31-
"request-promise-native": "^1.0.5"
35+
"isomorphic-unfetch": "^3.1.0"
36+
},
37+
"devDependencies": {
38+
"ava": "^3.15.0",
39+
"xo": "^0.38.2"
3240
}
3341
}

0 commit comments

Comments
 (0)