UNPKG

ih-black-lion

Version:

State handler for Arus projects

50 lines (44 loc) 1.5 kB
import Connector from './Connector'; import { Fault } from '../models'; import Request from '../Request'; import { toCamelCase, interceptFault } from './utils'; const URLS = { tryURL: __PAGELET_BUILDER_URL__, catchURL: process.env.PAGELET_BUILDER_URL, }; export default class NewsConnector extends Connector { constructor(requestMethod, requestParams, Model, pageletName, extraParam = undefined) { super(requestMethod, requestParams, Model, URLS, extraParam); this.pageletName = pageletName; this.params.url += this.pageletName; } checkTypes() { super.checkTypes(); if (typeof this.pageletName !== 'string') { return Promise.reject(new TypeError(`Type of pageletName is ${typeof this.pageletName}. Expected a string\n\tpageletName = ${this.pageletName}`)); } return true; } get Promise() { this.checkTypes(); // Checking the types here to simplify the usage of the Connectors. return new Promise((resolve, reject) => { Request.get(this.params) .then((res) => { const jRes = res.data; // should be an error if (typeof jRes === 'string') { const re = /(\{"body[\s\S]*\})/; reject(new Fault(re.exec(jRes)[0])); } const fault = interceptFault(jRes); if (fault) { reject(new Fault(jRes)); } else { const news = new this.Model(jRes); resolve(news); } }).catch(reject); }); } }