autobaan/src/ntfy/client.ts

35 lines
813 B
TypeScript
Raw Normal View History

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')
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,
}),
)
}
}