2023-09-05 09:12:04 +02:00
|
|
|
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')
|
2023-09-13 20:52:25 +02:00
|
|
|
const token = this.configService.getOrThrow<string>('NTFY_TOKEN')
|
2023-09-05 09:12:04 +02:00
|
|
|
this.httpClient = new Axios({
|
|
|
|
|
baseURL: `https://${host}`,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
2023-09-13 20:52:25 +02:00
|
|
|
Authorization: `Bearer ${token}`,
|
2023-09-05 09:12:04 +02:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async publish(message: Omit<MessageConfig, 'topic'>) {
|
|
|
|
|
return await this.httpClient.post(
|
|
|
|
|
'/',
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
topic: this.topic,
|
|
|
|
|
...message,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|