UNPKG

shadow-function

Version:

ioing lib - shadow Function, worker Function

38 lines (31 loc) 988 B
'use strict' import { getShadowModule } from '../getShadowModule/index' import { object2params } from '../url/index' type options = { url: string data: object callbackKey: string callbackName: string timeout: number, reportUrl: string } const jsonp = function (options: options) { return new Promise(function (resolve, reject) { let { url, data, callbackKey, callbackName, timeout, reportUrl } = options let payload = '' data = data || {} timeout = timeout || 30000 callbackKey = callbackKey || 'callback' callbackName = callbackName || 'JSONP' data[callbackKey] = callbackName payload = object2params(data) if (!url || typeof url !== 'string') return reject('params url is not defined') url += (url.indexOf('?') !== -1 ? '&' : '?') + payload getShadowModule(url, { receiver: callbackName, timeout, reportUrl }).then((data) => { resolve(JSON.parse(JSON.stringify(data))) }).catch(reject) }) } export { jsonp }