1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| import axios from 'axios'; import * as bgHttp from 'nativescript-background-http'; import * as mime from 'mime-types';
export default { data() { return { apiBase: 'https://path/to/api', } }, methods: { async requestBgHttp(url, method, params = null, config = null) { let request = { url, method, headers: { 'Content-Type': 'application/octet-stream', }, description: 'Request with files', }; if (config) { Object.assign(request, config); }
let session, task; if (params.file) { session = bgHttp.session(`upload: ${Math.random()}`); request.headers['File-Name'] = params.file.name; task = session.uploadFile(params.file.path, request); } else { session = bgHttp.session(`multipart: ${Math.random()}`); const _params = Object.entries(params).map(e => { if (typeof e[1] === 'object' && e[1].file) { return { name: e[0], filename: e[1].file.path, mimeType: mime.lookup(e[1].file.path), }; } else { return { name: e[0], value: e[1] }; } }); task = session.multipartUpload(_params, request); }
const taskPromise = new Promise((resolve, reject) => { task.on('progress', e => console.log({ e })); task.on('responded', e => resolve(e)); task.on('complete', e => resolve(e)); task.on('cancelled', e => resolve(e)); task.on('error', e => reject(e)); });
const res = await Promise.resolve(taskPromise) .catch(error => { console.error({error}); throw error; });
switch (res.eventName) { case 'responded': case 'complete': case 'cancelled': return res;
default: break; } return res; }, async requestAxios(url, method, params = null, config = null) { let options = { url, method }; if (params) { if (method === 'GET') { options.params = params; } else { options.data = params; } } if (config) { Object.assign(options, config); } try { const res = await axios.request(options); console.log({res}); return res; } catch (error) { console.error({ error, request: error.request, response: error.response, config: error.config, message: error.message, }); throw error; } }, async request(url, method, params = null, hasFile = false, config = null) { if (hasFile) { return await this.requestBgHttp(url, method, params, config); } else { return await this.requestAxios(url, method, params, config); } }, async get(path, params = null, config = null) { return await this.request(`${this.apiBase}/${path}`, 'GET', params, false, config); }, async post(path, params, hasFile=false, config = null) { return await this.request(`${this.apiBase}/${path}`, 'POST', params, hasFile, config); }, async put(path, params, hasFile=false, config = null) { return await this.request(`${this.apiBase}/${path}`, 'PUT', params, hasFile, config); }, async patch(path, params, hasFile=false, config = null) { return await this.request(`${this.apiBase}/${path}`, 'PATCH', params, hasFile, config); }, async delete(path, params, config = null) { return await this.request(`${this.apiBase}/${path}`, 'DELETE', params, false, config); }, }, }
|