All files / api-build api.js

70.83% Statements 34/48
56.25% Branches 9/16
62.5% Functions 5/8
72.34% Lines 34/47
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 931x 1x 1x   1x 1x 1x 1x 1x   1x   1x   1x 4x     3x 3x   4x     1x   3x 1x 1x 1x 1x 1x 1x   1x         2x 2x     2x     2x         2x 2x                                               2x 2x 2x                 1x          
const path = require('path');
const maybe = require('call-me-maybe');
require('colors');
 
const { baseLinks } = require('./commands/link');
const utils = require('./utils/utils');
const { parseFileResponse, convertToFileType, file } = require('./utils/file-utils');
const console = require('./utils/console');
const invoke = require('./lib/invoke');
 
const linksPath = path.join(utils.sharedDirectoryPath(), 'links.json');
 
const scope = {};
 
module.exports.config = (apiKey) => {
  scope.key = apiKey;
 
  function setService(service) {
    scope.service = service;
    return api;
  }
  return Object.assign(setService.bind(scope), api);
};
 
const api = {
  run(action, d, cb) {
    if (scope.key && scope.key.startsWith('demo')) {
      return maybe(cb, new Promise((resolve) => {
        console.log('This is a demo API key!'.red);
        console.log('To get your own, run:');
        console.log('');
        console.log(`  ${'$'.grey} ${'api signup'.yellow}`);
        console.log('');
 
        return resolve();
      }));
    }
 
    // Can't reassign params
    let data = d;
    let callback = cb;
 
    // If no data is passed in, default to {}
    Iif (typeof data === 'function') {
      callback = data;
      data = {};
    } else Iif (!data) {
      data = {};
    }
 
    // Don't call api if there is a local link
    const localLinks = utils.fileExists(linksPath) ? require(linksPath) : baseLinks;
    Iif (localLinks.services[scope.service] && localLinks.directories[process.cwd()].length) {
      const handler = require(path.join(process.cwd(), 'node_modules/api/utils/handler.js'));
      const errors = utils.buildErrors(localLinks.services[scope.service]);
 
      const event = {
        name: action,
        data,
        pathOverride: localLinks.services[scope.service],
        errors,
      };
 
      console.log(`Running ${scope.service} from ${localLinks.services[scope.service]}`.yellow);
 
      return maybe(callback, new Promise(async (resolve, reject) => {
        event.data = await convertToFileType(event.data);
 
        handler.go(event, undefined, (err, response) => {
          if (err) return reject(err);
 
          return resolve(parseFileResponse(JSON.stringify(response)));
        });
      }));
    }
 
    return maybe(callback, new Promise((resolve, reject) => {
      return invoke(scope.key, scope.service, action, data).then((response) => {
        return resolve(response);
      }).catch((err) => {
        return reject(err);
      });
    }));
  },
  file,
};
 
module.exports.file = file;
 
/*
 * docsTest: This is an example for docs.test.js
 */