UNPKG

all-pusher-api

Version:
100 lines (93 loc) 2.38 kB
import axios, { AxiosRequestConfig } from 'axios'; import { queryStringify, proxy2httpsAgent, proxy, result, sendOptions } from './tool'; interface IyuuConfig { key?: { token: string } token?: string proxy?: proxy } interface IyuuOptions { text: string desp?: string [name: string]: any } class Iyuu { protected _KEY: string; readonly baseURL = 'https://iyuu.cn/'; httpsAgent?: AxiosRequestConfig['httpsAgent']; constructor({ token, key, proxy }: IyuuConfig) { 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 iyuuOptions: IyuuOptions; if (sendOptions.customOptions) { iyuuOptions = sendOptions.customOptions; } else { iyuuOptions = { text: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10), desp: sendOptions.message }; } if (sendOptions.extraOptions) { iyuuOptions = { ...iyuuOptions, ...sendOptions.extraOptions }; } const axiosOptions: AxiosRequestConfig = { url: `${this.baseURL}${this._KEY}.send`, method: 'POST', headers: { 'Content-type': 'application/x-www-form-urlencoded' }, data: queryStringify(iyuuOptions) }; if (this.httpsAgent) { axiosOptions.httpsAgent = this.httpsAgent; } return axios(axiosOptions).then((response) => { if (response.data) { if (response.data.errcode === 0) { 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 { Iyuu };