|
1 |
| -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; |
| 1 | +const fetch = require('isomorphic-unfetch') |
2 | 2 |
|
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 |
6 | 7 | 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' } } |
9 | 21 | }
|
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' } } |
13 | 24 | }
|
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' } } |
17 | 27 | }
|
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' } } |
21 | 30 | }
|
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' } } |
25 | 33 | }
|
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' } } |
29 | 36 | }
|
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 |
33 | 45 | }
|
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 = { |
38 | 48 | method: 'POST',
|
39 |
| - uri: options.url, |
40 |
| - body: { |
| 49 | + headers: { |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + }, |
| 52 | + body: JSON.stringify({ |
41 | 53 | chatId: options.chat.id,
|
42 | 54 | chatSecret: options.chat.secret,
|
43 |
| - namespace: namespace, |
44 |
| - level: level, |
45 |
| - text: text, |
| 55 | + namespace, |
| 56 | + level, |
| 57 | + text, |
46 | 58 | 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' } } |
51 | 67 | }
|
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 } |
58 | 73 | }
|
59 | 74 |
|
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) |
64 | 77 | }
|
| 78 | + |
| 79 | +module.exports = pnotice |
0 commit comments