index.js 765 Bytes
'use strict';

const TTL_SECS = 30;

const coreapi = require('komodo-sdk/coreapi');
const NodeCache = require( "node-cache" );

const cache = new NodeCache({
    stdTTL: TTL_SECS,    
});

async function lookup(number) {
    if (!number) return;

    if (cache.get(number)) {
        return cache.get(number);
    }

    const [err, lookupResult] = await coreapi({
        path: '/prefixes/lookup',
        method: 'GET',
        qs: {
            number,
        }
    });

    if (err || !lookupResult || lookupResult.error) {
        cache.del(number);
        return;
    }

    if (lookupResult.prefix) {
        cache.set(number, lookupResult.prefix);
    } else {
        cache.del(number);
    }

    return lookupResult.prefix;
}

exports.lookup = lookup;