UNPKG

all-pusher-api

Version:
96 lines (90 loc) 2.26 kB
import axios, { AxiosRequestConfig } from 'axios'; import { proxy2httpsAgent, proxy, result, sendOptions } from './tool'; interface RocketChatConfig { key?: { webhook: string } webhook?: string proxy?: proxy } interface RocketChatOptions { text: string [name: string]: any } class RocketChat { protected _WEBHOOK: string; httpsAgent?: AxiosRequestConfig['httpsAgent']; constructor({ webhook, key, proxy }: RocketChatConfig) { const $key = { webhook, ...key }; if (!$key.webhook) { throw new Error('Missing Parameter: webhook'); } this._WEBHOOK = $key.webhook; if (proxy && proxy.enable) { this.httpsAgent = proxy2httpsAgent(proxy); } } async send(sendOptions: sendOptions): Promise<result> { if (!sendOptions.message && !sendOptions.customOptions) { return { status: 0, statusText: 'Missing Parameter: message', extraMessage: null }; } let rocketChatOptions: RocketChatOptions; if (sendOptions.customOptions) { rocketChatOptions = sendOptions.customOptions; } else { rocketChatOptions = { text: sendOptions.message }; } if (sendOptions.extraOptions) { rocketChatOptions = { ...rocketChatOptions, ...sendOptions.extraOptions }; } const axiosOptions: AxiosRequestConfig = { url: this._WEBHOOK, method: 'POST', headers: { 'Content-type': 'application/json' }, data: rocketChatOptions }; if (this.httpsAgent) { axiosOptions.httpsAgent = this.httpsAgent; } return axios(axiosOptions).then((response) => { if (response.data) { if (response.data.success === true) { return { status: 200, statusText: 'Success', extraMessage: response }; } return { status: 100, statusText: 'Error', extraMessage: response }; } return { status: 101, statusText: 'No Response Data', extraMessage: response }; }).catch((error) => ({ status: 102, statusText: 'Request Error', extraMessage: error })); } } export { RocketChat };