All files / api-build/utils handler.js

26.67% Statements 4/15
0% Branches 0/2
0% Functions 0/1
26.67% Lines 4/15
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 271x 1x 1x   1x                                            
const path = require('path');
const fs = require('fs');
const utils = require('./handler-utils');
 
exports.go = (event, context, callback) => {
  const basePath = event.pathOverride || process.cwd();
  const endpointPath = path.join(basePath, 'endpoints', `${event.name}.js`);
  try {
    fs.statSync(endpointPath).isFile();
  } catch (e) {
    throw new Error('Endpoint does not exist');
  }
  const endpoint = require(endpointPath);
  const parsedData = utils.fixBuffers(event.data);
  try {
    endpoint(parsedData, {
      success: utils.success(callback),
      error: utils.error(event, callback),
      log: utils.log,
      getSecret: utils.getSecret(event.secrets),
    });
  } catch (e) {
    const error = utils.parseErrors(event, e);
    callback(JSON.stringify(error), null);
  }
};