All files / src/utils Tools.ts

15.91% Statements 7/44
0% Branches 0/15
0% Functions 0/12
16.28% Lines 7/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1035x 5x 5x   5x 5x 5x     5x                                                                                                                                                                                          
const Promise = require('bluebird');
const queryString = require('qs');
const xml2js = require('xml2js').parseString;
 
const errors = require('../errors')
    ,AppError = errors.AppError
    ,MalResultError = errors.MalResultError;
 
/** Random tools that have nowhere else to go */
export class Tools {
    constructor() {
    }
 
    /**
     * [getJSONParser pick the approriate JSON parser based on the content-type, dafault to dummy parser (return data as its)]
     * @type {[type]}
     */
    public getJSONParser = (contentType, cb = undefined) => {
        const my = this;
        return new Promise(function (resolve, reject) {
            let parser = my.defaultParser;
            
            if(!contentType) return reject(new AppError("No Content Type"));
 
            if(contentType.indexOf("text/xml") > -1)
                parser = my.xmlJSONParser;
             else if(contentType.indexOf("application/json") > -1)
                 parser = my.jsonParser;
            else if(contentType.indexOf("application/x-www-form-urlencoded") > -1)
                parser = my.formUrlEncodedJSONParser;
 
            return resolve(parser);
        }).nodeify(cb);
    }
 
    /**
     * [defaultParser default parsing data toString]
     * @type {[type]}
     */
    private defaultParser = (data, cb = undefined) => {
        return new Promise((resolve, reject) => {
            try{
                const parsedData = data.toString();
                return resolve(parsedData);
            }catch(err){
                return reject(new MalResultError("Unable to parse results: " + err.mesage));
            }
        }).nodeify(cb); 
    }
    /**
     * [xmlJSONParser parse JSON for content-type text/xml]
     * @return Promise resolving JSON parsed data
     *  or reject with AppError MalResult when fail to parse
     *  Support callback fallback
     */
    private xmlJSONParser = (data, cb = undefined) => {
 
        return new Promise((resolve, reject) => {
            xml2js(data, function (err, finalVal) {
                if(err) {
                    return reject(new MalResultError("Unable to parse results: " + err.mesage));
                }
                resolve(finalVal);
            })
        }).nodeify(cb);     
    }
 
    /**
     * [jsonParser parse JSON for content-type application/json]
     * @return Promise resolving JSON parsed data
     *  or reject with AppError MalResult when fail to parse
     *  Support callback fallback
     */
    private jsonParser = (data, cb = undefined) => {
        return new Promise((resolve, reject) => {
            try{
                const parsedData = JSON.parse(data)
                return resolve(parsedData);
            }catch(err){
                return reject(new MalResultError("Unable to parse results: " + err.mesage));
            }
        }).nodeify(cb);
    }
 
    /**
     * [formUrlEncodedJSONParser parse JSON for content-type application/x-www-form-urlencoded]
     * @return Promise resolving JSON parsed data
     *  or reject with AppError MalResult when fail to parse
     *  Support callback fallback
     */
    private formUrlEncodedJSONParser = (data, cb = undefined) => {
        return new Promise((resolve, reject) => {
            try{
                const parsedData = queryString.parse(data)
                return resolve(parsedData);
            }catch(err){
                return reject(new MalResultError("Unable to parse results: " + err.mesage));
            }
        }).nodeify(cb);
    }
 
}