From 2f7d5c68ef87bb39c77fde895adcde0d481cd807 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Tue, 5 Sep 2023 09:12:04 +0200 Subject: [PATCH] Adding ntfy module --- package-lock.json | 36 ++++++++++++++++++--- package.json | 1 + src/ntfy/client.ts | 34 ++++++++++++++++++++ src/ntfy/module.ts | 10 ++++++ src/ntfy/provider.ts | 38 +++++++++++++++++++++++ src/ntfy/types.ts | 74 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 188 insertions(+), 5 deletions(-) create mode 100644 src/ntfy/client.ts create mode 100644 src/ntfy/module.ts create mode 100644 src/ntfy/provider.ts create mode 100644 src/ntfy/types.ts diff --git a/package-lock.json b/package-lock.json index 2270dc2..1e93c54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@nestjs/platform-express": "^9.0.0", "@nestjs/schedule": "^2.2.2", "@nestjs/typeorm": "^9.0.1", + "axios": "^1.5.0", "bull": "^4.10.4", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", @@ -3451,8 +3452,17 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "dev": true + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "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": { "version": "29.5.0", @@ -4300,7 +4310,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -4611,7 +4620,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "dev": true, "engines": { "node": ">=0.4.0" } @@ -5741,6 +5749,25 @@ "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", "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": { "version": "8.0.0", "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", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dev": true, "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", diff --git a/package.json b/package.json index f497b0b..4d62338 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@nestjs/platform-express": "^9.0.0", "@nestjs/schedule": "^2.2.2", "@nestjs/typeorm": "^9.0.1", + "axios": "^1.5.0", "bull": "^4.10.4", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", diff --git a/src/ntfy/client.ts b/src/ntfy/client.ts new file mode 100644 index 0000000..d90dc42 --- /dev/null +++ b/src/ntfy/client.ts @@ -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('NTFY_HOST') + this.topic = this.configService.getOrThrow('NTFY_TOPIC') + this.httpClient = new Axios({ + baseURL: `https://${host}`, + headers: { + 'Content-Type': 'application/json', + }, + }) + } + + async publish(message: Omit) { + return await this.httpClient.post( + '/', + JSON.stringify({ + topic: this.topic, + ...message, + }), + ) + } +} diff --git a/src/ntfy/module.ts b/src/ntfy/module.ts new file mode 100644 index 0000000..05f4c4e --- /dev/null +++ b/src/ntfy/module.ts @@ -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 {} diff --git a/src/ntfy/provider.ts b/src/ntfy/provider.ts new file mode 100644 index 0000000..98db5f9 --- /dev/null +++ b/src/ntfy/provider.ts @@ -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, + }) + } +} diff --git a/src/ntfy/types.ts b/src/ntfy/types.ts new file mode 100644 index 0000000..1d1795b --- /dev/null +++ b/src/ntfy/types.ts @@ -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 +}