packages/api-gateway/src/index.js
function loadApis() {
  // Shortcut if apis already have been loaded
  if (apis !== undefined) {
    return Promise.resolve(apis);
  }

  const apiSpecsPath = path.join(process.cwd(), plugin.config.apisPath);

  // This event allows to inject code before loading all APIs
  return plugin.myrmex.fire('beforeApisLoad')
  .then(() => {
    // Retrieve configuration path of all API specifications
    return Promise.promisify(fs.readdir)(apiSpecsPath);
  })
  .then(subdirs => {
    // Load all the API specifications
    const apiPromises = [];
    _.forEach(subdirs, (subdir) => {
      const apiSpecPath = path.join(apiSpecsPath, subdir, 'spec');
      // subdir is the identifier of the API, so we pass it as the second argument
      apiPromises.push(loadApi(apiSpecPath, subdir));
    });
    return Promise.all(apiPromises);
  })
  .then(apis => {
    // This event allows to inject code to add or delete or alter API specifications
    return plugin.myrmex.fire('afterApisLoad', apis);
  })
  .spread(loadedApis => {
    apis = loadedApis;
    return Promise.resolve(apis);
  })
  .catch(e => {
    if (e.code === 'ENOENT' && path.basename(e.path) === path.basename(plugin.config.apisPath)) {
      return Promise.resolve([]);
    }
    return Promise.reject(e);
  });
}
function loadApis() {
  // Shortcut if apis already have been loaded
  if (apis !== undefined) {
    return Promise.resolve(apis);
  }

  const apiSpecsPath = path.join(process.cwd(), plugin.config.apisPath);

  // This event allows to inject code before loading all APIs
  return plugin.myrmex.fire('beforeApisLoad')
  .then(() => {
    // Retrieve configuration path of all API specifications
    return Promise.promisify(fs.readdir)(apiSpecsPath);
  })
  .then(subdirs => {
    // Load all the API specifications
    const apiPromises = [];
    _.forEach(subdirs, (subdir) => {
      const apiSpecPath = path.join(apiSpecsPath, subdir, 'spec');
      // subdir is the identifier of the API, so we pass it as the second argument
      apiPromises.push(loadApi(apiSpecPath, subdir));
    });
    return Promise.all(apiPromises);
  })
  .then(apis => {
    // This event allows to inject code to add or delete or alter API specifications
    return plugin.myrmex.fire('afterApisLoad', apis);
  })
  .spread(loadedApis => {
    apis = loadedApis;
    return Promise.resolve(apis);
  })
  .catch(e => {
    if (e.code === 'ENOENT' && path.basename(e.path) === path.basename(plugin.config.apisPath)) {
      return Promise.resolve([]);
    }
    return Promise.reject(e);
  });
}
function loadApis ( ) { // Shortcut if apis already have been loaded if ( apis !== undefined ) { return Promise . resolve ( apis ) ; } const apiSpecsPath = path . join ( process . cwd ( ) , plugin . config . apisPath ) ; // This event allows to inject code before loading all APIs return plugin . myrmex . fire ( 'beforeApisLoad' ) . then ( ( ) => { // Retrieve configuration path of all API specifications return Promise . promisify ( fs . readdir ) ( apiSpecsPath ) ; } ) . then ( subdirs => { // Load all the API specifications const apiPromises = [ ] ; _ . forEach ( subdirs , ( subdir ) => { const apiSpecPath = path . join ( apiSpecsPath , subdir , 'spec' ) ; // subdir is the identifier of the API, so we pass it as the second argument apiPromises . push ( loadApi ( apiSpecPath , subdir ) ) ; } ) ; return Promise . all ( apiPromises ) ; } ) . then ( apis => { // This event allows to inject code to add or delete or alter API specifications return plugin . myrmex . fire ( 'afterApisLoad' , apis ) ; } ) . spread ( loadedApis => { apis = loadedApis ; return Promise . resolve ( apis ) ; } ) . catch ( e => { if ( e . code === 'ENOENT' && path . basename ( e . path ) === path . basename ( plugin . config . apisPath ) ) { return Promise . resolve ( [ ] ) ; } return Promise . reject ( e ) ; } ) ; }
Load all API specifications
@returns {Promise<[Api]>} - the promise of an array containing all APIs
Load all API specifications