UNPKG

ih-black-lion

Version:

State handler for Arus projects

47 lines (43 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_URL__, catchURL: process.env.PAGELET_URL, }; export default class NavigationCollectionConnector 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) => { let jRes = res.data; if (typeof jRes === 'string') { const re = /(\{"NavCollection[\s\S]*\})/; jRes = JSON.parse(re.exec(jRes)[0]); } const fault = interceptFault(jRes); if (fault) { reject(new Fault(jRes)); } else { const navCollection = new this.Model(jRes); resolve(navCollection); } }).catch(reject); }); } }