Sample Gunner Strategy implementation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
903 B

const requestor = async (unit, request) => {
switch (unit.method) {
case 'get':
return await request[unit.method](unit.path);
case 'post':
return await (request[unit.method](unit.path)
.type(unit.reqType || 'json')
.send(unit.body));
}
};
const dbAction = async (unit, db) => {
if (!unit.hasOwnProperty('method')) unit.method = 'findOne';
const exec = [ 'create', 'insertMany' ].indexOf(unit.method) === -1;
const action = db[unit.collection][unit.method](
unit.query || unit.insert,
unit.update || unit.queryList,
);
return !exec ? await action : action.exec();
};
module.exports = async (unit, state) => {
const [ request, db ] = state['@start'];
switch (unit.type) {
case 'request':
return await requestor(unit, request);
case 'db':
return await dbAction(unit, db);
default:
throw new Error(
`Unknown before hook type: ${unit.type}`
);
}
};