baseService.js (1124B)
1 import {fetch} from '~/lib/xhr-promise'; 2 3 class BaseService { 4 constructor(url) { 5 this.origUrl = url; 6 } 7 8 isOurUrl() { 9 throw new Error('Method not implemented'); 10 } 11 12 requestOptions() { 13 throw new Error('Method not implemented'); 14 } 15 16 parseResponse() { 17 throw new Error('Method not implemented'); 18 } 19 20 async geoData() { 21 if (!this.isOurUrl()) { 22 throw new Error('Unsupported url'); 23 } 24 const requests = this.requestOptions().map((it) => fetch(it.url, it.options)); 25 let responses; 26 try { 27 responses = await Promise.all(requests); 28 } catch (e) { 29 return [{name: this.origUrl, error: 'NETWORK'}]; 30 } 31 32 return this.parseResponse(responses); 33 } 34 35 nameFromUrl(url) { 36 try { 37 url = decodeURIComponent(url); 38 } catch (e) { 39 // leave url as is 40 } 41 42 return url 43 .split('#')[0] 44 .split('?')[0] 45 .replace(/\/*$/u, '') 46 .split('/') 47 .pop(); 48 } 49 } 50 export default BaseService;