UNPKG

tm-notification

Version:

This is the package exposing methods for the notification service

65 lines (53 loc) 1.5 kB
"use strict"; const axios = require("axios"); class SMSBuilder{ constructor(clientId) { this._axios = axios.create({ baseURL: process.env.API_GATEWAY_URL }); this.startBuild(); } startBuild(){ this.props = {}; return this; } setClientId(clientId){ this.props["clientId"] = clientId; return this; } setSender(sender){ this.props["sender"] = sender; return this; } setRecipients(recipients){ this.props["recipients"] = recipients; return this; } setProvider(provider){ this.props["provider"] = provider; return this; } setMessage(message){ this.props["message"] = message; return this; } build(){ return this.props; } async send(){ try{ const config ={ headers: { "client-id": this.props.clientId || process.env.CLIENT_ID } }; if(process.env.NOTIFICATION_SERVICE_URL) return {data: (await this._axios.post(`${process.env.NOTIFICATION_SERVICE_URL}/v1/sms`, this.build(), config)).data.data}; return {data: (await this._axios.post("/notifications/v1/sms", this.build(), config)).data.data}; }catch (e) { const error = resolveAxiosError(e); return {error: error?.raw?.message || error}; } } } module.exports = SMSBuilder;