UNPKG

all-pusher-api

Version:
105 lines (97 loc) 2.46 kB
import axios, { AxiosRequestConfig } from 'axios'; import { proxy2httpsAgent, proxy, result, sendOptions } from './tool'; interface SimplePushConfig { key?: { token: string } token?: string, proxy?: proxy } interface SimplePushOptions { key: string msg: string title?: string [name: string]: any } class SimplePush { protected _KEY: string; readonly baseURL = 'https://api.simplepush.io/send'; httpsAgent?: AxiosRequestConfig['httpsAgent']; constructor({ token, key, proxy }: SimplePushConfig) { const $key = { token, ...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 simplePushOptions: SimplePushOptions; if (sendOptions.customOptions) { simplePushOptions = sendOptions.customOptions; } else { simplePushOptions = { key: this._KEY, msg: sendOptions.message }; if (sendOptions.title) { simplePushOptions.title = sendOptions.title; } } if (sendOptions.extraOptions) { simplePushOptions = { ...simplePushOptions, ...sendOptions.extraOptions }; } const axiosOptions: AxiosRequestConfig = { url: this.baseURL, method: 'POST', headers: { 'Content-type': 'application/x-www-form-urlencoded' }, data: simplePushOptions }; if (this.httpsAgent) { axiosOptions.httpsAgent = this.httpsAgent; } return axios(axiosOptions).then((response) => { if (response.data) { if (response.data.status === 'OK') { 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 { SimplePush };