Adding ntfy module

This commit is contained in:
Collin Duncan 2023-09-05 09:12:04 +02:00
parent f8699080a8
commit 2f7d5c68ef
No known key found for this signature in database
6 changed files with 188 additions and 5 deletions

36
package-lock.json generated
View file

@ -17,6 +17,7 @@
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"@nestjs/schedule": "^2.2.2", "@nestjs/schedule": "^2.2.2",
"@nestjs/typeorm": "^9.0.1", "@nestjs/typeorm": "^9.0.1",
"axios": "^1.5.0",
"bull": "^4.10.4", "bull": "^4.10.4",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"class-validator": "^0.14.0", "class-validator": "^0.14.0",
@ -3451,8 +3452,17 @@
"node_modules/asynckit": { "node_modules/asynckit": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
"dev": true },
"node_modules/axios": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.5.0.tgz",
"integrity": "sha512-D4DdjDo5CY50Qms0qGQTTw6Q44jl7zRwY7bthds06pUGfChBCTcQs+N743eFWGEd6pRTMd6A+I87aWyFV5wiZQ==",
"dependencies": {
"follow-redirects": "^1.15.0",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
}, },
"node_modules/babel-jest": { "node_modules/babel-jest": {
"version": "29.5.0", "version": "29.5.0",
@ -4300,7 +4310,6 @@
"version": "1.0.8", "version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dev": true,
"dependencies": { "dependencies": {
"delayed-stream": "~1.0.0" "delayed-stream": "~1.0.0"
}, },
@ -4611,7 +4620,6 @@
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"dev": true,
"engines": { "engines": {
"node": ">=0.4.0" "node": ">=0.4.0"
} }
@ -5741,6 +5749,25 @@
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==",
"dev": true "dev": true
}, },
"node_modules/follow-redirects": {
"version": "1.15.2",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz",
"integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/fork-ts-checker-webpack-plugin": { "node_modules/fork-ts-checker-webpack-plugin": {
"version": "8.0.0", "version": "8.0.0",
"resolved": "https://registry.npmjs.org/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-8.0.0.tgz", "resolved": "https://registry.npmjs.org/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-8.0.0.tgz",
@ -5865,7 +5892,6 @@
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dev": true,
"dependencies": { "dependencies": {
"asynckit": "^0.4.0", "asynckit": "^0.4.0",
"combined-stream": "^1.0.8", "combined-stream": "^1.0.8",

View file

@ -33,6 +33,7 @@
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"@nestjs/schedule": "^2.2.2", "@nestjs/schedule": "^2.2.2",
"@nestjs/typeorm": "^9.0.1", "@nestjs/typeorm": "^9.0.1",
"axios": "^1.5.0",
"bull": "^4.10.4", "bull": "^4.10.4",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"class-validator": "^0.14.0", "class-validator": "^0.14.0",

34
src/ntfy/client.ts Normal file
View file

@ -0,0 +1,34 @@
import { Inject, Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Axios } from 'axios'
import { MessageConfig } from './types'
@Injectable()
export class NtfyClient {
private readonly httpClient: Axios
private readonly topic: string
constructor(
@Inject(ConfigService)
private readonly configService: ConfigService,
) {
const host = this.configService.getOrThrow<string>('NTFY_HOST')
this.topic = this.configService.getOrThrow<string>('NTFY_TOPIC')
this.httpClient = new Axios({
baseURL: `https://${host}`,
headers: {
'Content-Type': 'application/json',
},
})
}
async publish(message: Omit<MessageConfig, 'topic'>) {
return await this.httpClient.post(
'/',
JSON.stringify({
topic: this.topic,
...message,
}),
)
}
}

10
src/ntfy/module.ts Normal file
View file

@ -0,0 +1,10 @@
import { Module } from '@nestjs/common'
import { NtfyClient } from './client'
import { NtfyProvider } from './provider'
@Module({
providers: [NtfyProvider, NtfyClient],
exports: [NtfyProvider, NtfyClient],
})
export class NtfyModule {}

38
src/ntfy/provider.ts Normal file
View file

@ -0,0 +1,38 @@
import { Inject, Injectable } from '@nestjs/common'
import { NtfyClient } from './client'
import { MessagePriority, MessageTags } from './types'
@Injectable()
export class NtfyProvider {
constructor(
@Inject(NtfyClient)
private readonly ntfyClient: NtfyClient,
) {}
async sendErrorNotification(
title: string,
message: string,
priority = MessagePriority.default,
) {
await this.ntfyClient.publish({
title,
message,
tags: [MessageTags.red_x],
priority,
})
}
async sendInfoNotification(
title: string,
message: string,
priority = MessagePriority.low,
) {
await this.ntfyClient.publish({
title,
message,
tags: [MessageTags.info],
priority,
})
}
}

74
src/ntfy/types.ts Normal file
View file

@ -0,0 +1,74 @@
export enum MessageTags {
thumbs_up = '+1',
thumbs_down = '-1',
warning = 'warning',
rotating_light = 'rotating-light',
skull_and_crossbones = 'skull_and_crossbones',
tada = 'tada',
outbox_tray = 'outbox_tray',
inbox_tray = 'inbox_tray',
date = 'date',
no_entry = 'no_entry',
exclamation = 'exclamation',
question = 'question',
white_check_mark = 'white_check_mark',
red_x = 'x',
zero = 'zero',
one = 'zero',
two = 'zero',
three = 'zero',
four = 'zero',
five = 'zero',
six = 'zero',
seven = 'zero',
eight = 'zero',
nine = 'zero',
new = 'new',
ok = 'ok',
sos = 'sos',
clock12 = 'clock12',
clock1230 = 'clock1230',
clock1 = 'clock1',
clock130 = 'clock130',
clock2 = 'clock2',
clock230 = 'clock230',
clock3 = 'clock3',
clock330 = 'clock330',
clock4 = 'clock4',
clock430 = 'clock430',
clock5 = 'clock5',
clock530 = 'clock530',
clock6 = 'clock6',
clock630 = 'clock630',
clock7 = 'clock7',
clock730 = 'clock730',
clock8 = 'clock8',
clock830 = 'clock830',
clock9 = 'clock9',
clock930 = 'clock930',
clock10 = 'clock10',
clock1030 = 'clock1030',
clock11 = 'clock11',
clock1130 = 'clock1130',
badminton = 'badminton',
info = 'information_source',
}
export enum MessagePriority {
min = '1',
low = '2',
default = '3',
high = '4',
max = '5',
}
export interface MessageConfig {
topic: string
message?: string
title?: string
tags?: MessageTags[]
priority?: MessagePriority
actions?: object[]
markdown?: boolean
icon?: string
}