()
| 237 | } |
| 238 | |
| 239 | function init() { |
| 240 | isSurge = () => { |
| 241 | return undefined !== this.$httpClient |
| 242 | } |
| 243 | isQuanX = () => { |
| 244 | return undefined !== this.$task |
| 245 | } |
| 246 | getdata = (key) => { |
| 247 | if (isSurge()) return $persistentStore.read(key) |
| 248 | if (isQuanX()) return $prefs.valueForKey(key) |
| 249 | } |
| 250 | setdata = (key, val) => { |
| 251 | if (isSurge()) return $persistentStore.write(key, val) |
| 252 | if (isQuanX()) return $prefs.setValueForKey(key, val) |
| 253 | } |
| 254 | msg = (title, subtitle = '', body = '') => { |
| 255 | if (isSurge()) $notification.post(title, subtitle, body) |
| 256 | if (isQuanX()) $notify(title, subtitle, body) |
| 257 | } |
| 258 | log = (title, msg) => { |
| 259 | console.log(`${title}:\n${msg}\n`) |
| 260 | } |
| 261 | get = (options, callback) => { |
| 262 | if (isQuanX()) { |
| 263 | if (typeof options == 'string') options = { url: options } |
| 264 | options['method'] = 'GET' |
| 265 | return $task.fetch(options).then( |
| 266 | response => { |
| 267 | response['status'] = response.statusCode |
| 268 | callback(null, response, response.body) |
| 269 | }, |
| 270 | reason => callback(reason.error, null, null), |
| 271 | ) |
| 272 | } |
| 273 | if (isSurge()) return $httpClient.get(options, callback) |
| 274 | } |
| 275 | post = (options, callback) => { |
| 276 | if (isQuanX()) { |
| 277 | if (typeof options == 'string') options = { url: options } |
| 278 | options['method'] = 'POST' |
| 279 | $task.fetch(options).then( |
| 280 | response => { |
| 281 | response['status'] = response.statusCode |
| 282 | callback(null, response, response.body) |
| 283 | }, |
| 284 | reason => callback(reason.error, null, null), |
| 285 | ) |
| 286 | } |
| 287 | if (isSurge()) $httpClient.post(options, callback) |
| 288 | } |
| 289 | done = (value = {}) => { |
| 290 | $done(value) |
| 291 | } |
| 292 | return { isSurge, isQuanX, msg, log, getdata, setdata, get, post, done } |
| 293 | } |
| 294 | |
| 295 | function RequestFailed(action = '', reason = '', isInvalidToken = false) { |
| 296 | this.action = action |
no test coverage detected