-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
130 lines (102 loc) · 2.98 KB
/
index.ts
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
import fs from 'node:fs/promises'
import { ntfyMessagePriorityDefault } from './priorities.js'
import type { FetchHeaders, NtfyMessageOptions } from './types.js'
/**
* The default ntfy server to use.
*/
export const DEFAULT_NTFY_SERVER = 'https://ntfy.sh'
/**
* @deprecated Use `DEFAULT_NTFY_SERVER` instead.
*/
export const DEFAULT_SERVER = DEFAULT_NTFY_SERVER
/**
* The default priority to use when sending a message.
*/
export const DEFAULT_NTFY_PRIORITY = ntfyMessagePriorityDefault
/**
* @deprecated Use `DEFAULT_NTFY_PRIORITY` instead.
*/
export const DEFAULT_PRIORITY = DEFAULT_NTFY_PRIORITY
/**
* Send a message through an ntfy server.
* @param ntfyMessage The message to post.
* @returns `true` if the message was posted.
*/
export default async function publish(
ntfyMessage: NtfyMessageOptions
): Promise<boolean> {
/*
* Set Server
*/
let server = ntfyMessage.server ?? DEFAULT_NTFY_SERVER
if (!server.endsWith('/')) {
server += '/'
}
/*
* Build Headers
*/
const messageHeaders: FetchHeaders = {
Priority: ntfyMessage.priority ?? DEFAULT_NTFY_PRIORITY
}
if (ntfyMessage.title !== undefined) {
messageHeaders.Title = ntfyMessage.title
}
if (ntfyMessage.tags !== undefined) {
messageHeaders.Tags = ntfyMessage.tags.join(',')
}
if (ntfyMessage.clickURL !== undefined) {
messageHeaders.Click = ntfyMessage.clickURL
}
if (ntfyMessage.iconURL !== undefined) {
messageHeaders.Icon = ntfyMessage.iconURL
}
// Attachments
let hasLocalAttachment = false
if (ntfyMessage.fileAttachmentURL !== undefined) {
hasLocalAttachment = !(
ntfyMessage.fileAttachmentURL.toLowerCase().startsWith('http://') ||
ntfyMessage.fileAttachmentURL.toLowerCase().startsWith('https://')
)
if (!hasLocalAttachment) {
messageHeaders.Attach = ntfyMessage.fileAttachmentURL
}
}
const fileData = hasLocalAttachment
? // eslint-disable-next-line security/detect-non-literal-fs-filename
await fs.readFile(ntfyMessage.fileAttachmentURL as string)
: undefined
if (ntfyMessage.fileName !== undefined) {
messageHeaders.Filename = ntfyMessage.fileName
}
// Cache
if (Object.hasOwn(ntfyMessage, 'cache') && !(ntfyMessage.cache ?? true)) {
messageHeaders.Cache = 'no'
}
/*
* Send Message
*/
const response = await fetch(server + ntfyMessage.topic, {
method: 'POST',
body: hasLocalAttachment ? fileData : ntfyMessage.message ?? '',
headers: messageHeaders as Record<string, string>
})
return response.ok
}
export {
ntfyMessagePrioritiesDefault,
ntfyMessagePrioritiesHigh,
ntfyMessagePrioritiesLow,
ntfyMessagePrioritiesMax,
ntfyMessagePrioritiesMin,
ntfyMessagePriorityDefault,
ntfyMessagePriorityHigh,
ntfyMessagePriorityLow,
ntfyMessagePriorityMax,
ntfyMessagePriorityMin
} from './priorities.js'
export type {
FetchHeaders,
NtfyMessageOptions,
NtfyMessagePriority
} from './types.js'
export { ntfyTagEmojis, isSupportedTagEmoji } from './emoji.js'