Repository navigation
Expand file tree
/
Copy pathntfy.js
More file actions
189 lines (176 loc) · 5.59 KB
/
Copy pathntfy.js
File metadata and controls
189 lines (176 loc) · 5.59 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Creates a new Ntfy notification builder instance
* @constructor
* @param {string} url - The ntfy server URL (e.g., 'https://ntfy.sh/mytopic')
* @example
* const ntfy = new Ntfy('https://ntfy.sh/thisismytopic');
*/
function Ntfy(url) {
this.config = {
topic: '',
tags: [],
priority: 3,
title: '',
message: '',
actions: []
};
const urlParts = url.split('/');
if (urlParts.length >= 4) {
this.server = urlParts.slice(0, 3).join('/');
this.config.topic = urlParts.slice(3).join('/');
} else {
this.server = url;
this.config.topic = '';
}
}
/**
* Sends the notification with all configured settings
* @returns {Ntfy} Returns this instance for method chaining
* @example
* // Basic usage
* ntfy.message('Hello', 'World').send();
*
* // With method chaining
* ntfy
* .tags(['server', 'alert'])
* .message('Server Alert', 'High CPU usage detected')
* .priority(4)
* .actionView('Check Server', 'https://dashboard.example.com')
* .send();
*
* // Multiple notifications with persistent tags
* const alertNtfy = new Ntfy('https://ntfy.sh/alerts').tags(['production']);
* alertNtfy.message('Deploy Started', 'Version 1.2.3').send();
* alertNtfy.message('Deploy Complete', 'All services online').send();
*/
Ntfy.prototype.send = function () {
// Build the notification payload
var payload = {
...this.config
};
$http.send({
url: this.server,
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
});
return this;
};
/**
* Resets the notification builder to a fresh state with the same URL
* @returns {Ntfy} Returns a new Ntfy instance with the same URL
* @example
* const freshNtfy = ntfy.reset();
*/
Ntfy.prototype.reset = function () {
return new Ntfy(this.url);
};
/**
* Adds the tags for the notification
* @param {string[]} tagArray - Array of tags to apply to the notification
* @returns {Ntfy} Returns this instance for method chaining
* @example
* ntfy.tags(['server', 'alert', 'urgent']);
*/
Ntfy.prototype.tags = function (tagArray) {
this.config.tags = [tagArray, ...this.config.tags];
return this;
};
/**
* Sets the tags for the notification (replace existing tags)
* @param {string[]} tagArray - Array of tags to apply to the notification
* @returns {Ntfy} Returns this instance for method chaining
* @example
* ntfy.tags(['server', 'alert', 'urgent']);
*/
Ntfy.prototype.setTags = function (tagArray) {
this.config.tags = tagArray || [];
return this;
};
/**
* Sets the message title and body for the notification
* @param {string} title - The notification title
* @param {string} body - The notification message body
* @returns {Ntfy} Returns this instance for method chaining
* @example
* ntfy.message('Server Alert', 'Database connection failed');
*/
Ntfy.prototype.message = function (title = "", body = "") {
this.config.title = title;
this.config.message = body;
return this;
};
/**
* Sets the priority level for the notification (1=min, 3=default, 5=max)
* @param {number} level - Priority level (1-5)
* @returns {Ntfy} Returns this instance for method chaining
* @example
* ntfy.priority(5); // Maximum priority
*/
Ntfy.prototype.priority = function (level) {
this.config.priority = level;
return this;
};
/**
* Adds a click action for the notification (shorthand for actionView)
* @param {string} url - The URL to open when notification is clicked
* @returns {Ntfy} Returns this instance for method chaining
* @example
* ntfy.click('https://example.com');
* ntfy.click('geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA');
* ntfy.click('mailto:phil@example.com');
*/
Ntfy.prototype.click = function (url) {
this.config.actions.push({
action: 'view',
url: url,
clear: clear || false
});
return this;
};
/**
* Adds a view action button to the notification
* @param {string} label - The text to display on the action button
* @param {string} url - The URL to open when the button is clicked
* @param {boolean} [clear=false] - Whether to clear the notification when action is triggered
* @returns {Ntfy} Returns this instance for method chaining
* @example
* ntfy.actionView('Visit Site', 'https://example.com', true);
* ntfy.actionView('Maps to Google', 'geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA');
*/
Ntfy.prototype.actionView = function (label, url, clear) {
this.config.actions.push({
action: 'view',
label: label,
url: url,
clear: clear || false
});
return this;
};
/**
* Adds an HTTP action button to the notification (like an API call)
* @param {string} label - The text to display on the action button
* @param {string} url - The HTTP endpoint to call when the button is clicked
* @param {boolean} [clear=false] - Whether to clear the notification when action is triggered
* @returns {Ntfy} Returns this instance for method chaining
* @example
* ntfy.actionHTTP('Restart Service', 'https://api.example.com/restart', true);
* ntfy.actionHTTP('Restart Service with auth', 'https://api.example.com/restart', true, {"headers": {"Authorization": "Bearer zAzsx1sk.."}});
* ntfy.actionHTTP('with Auth and Body', 'https://api.example.com/restart', true, {"headers": {"Authorization": "Bearer zAzsx1sk.."}, "body": {"force": true}});
*/
Ntfy.prototype.actionHTTP = function (label, url, clear = false, otherParams = {}) {
this.config.actions.push({
action: 'http',
label: label,
url: url,
clear: clear || false,
...otherParams
});
return this;
};
// Export for PocketBase require() usage
if (typeof module !== 'undefined' && module.exports) {
module.exports = Ntfy;
}