UNPKG

all-pusher-api

Version:
103 lines (96 loc) 2.43 kB
import axios, { AxiosRequestConfig } from 'axios'; import { proxy2httpsAgent, proxy, result, sendOptions } from './tool'; interface PushConfig { key?: { token: string baseURL?: string } baseURL?: string token?: string proxy?: proxy } interface PushOptions { title: string body: string [name: string]: any } class Push { protected _KEY: string; protected _BASE_URL = 'https://push.techulus.com/api/v1/notify/'; httpsAgent?: AxiosRequestConfig['httpsAgent']; constructor({ token, baseURL, key, proxy }: PushConfig) { const $key = { token, baseURL, ...key }; if (!$key.token) { throw new Error('Missing Parameter: token'); } this._KEY = $key.token; 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 pushOptions: PushOptions; if (sendOptions.customOptions) { pushOptions = sendOptions.customOptions; } else { pushOptions = { title: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10), body: sendOptions.message }; } if (sendOptions.extraOptions) { pushOptions = { ...pushOptions, ...sendOptions.extraOptions }; } const axiosOptions: AxiosRequestConfig = { url: `${this._BASE_URL}${this._KEY}`, method: 'POST', headers: { 'Content-type': 'application/json' }, data: pushOptions }; 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 { Push };