simplified API

This commit is contained in:
d98762625 2018-04-20 10:55:17 +01:00
parent 59877b5138
commit fca4ed7013
2 changed files with 37 additions and 93 deletions

View File

@ -10,90 +10,45 @@ import Dish from "../core/Dish";
import log from "loglevel"; import log from "loglevel";
/** /**
* * Extract default arg value from operation argument
* @param {Object} arg - an arg from an operation
*/ */
export default class Wrapper { function extractArg(arg) {
/**
*
* @param arg
*/
extractArg(arg) {
if (arg.type === "option" || arg.type === "editableOption") { if (arg.type === "option" || arg.type === "editableOption") {
return arg.value[0]; return arg.value[0];
} }
return arg.value; return arg.value;
} }
/** /**
* * Wrap an operation to be consumed by node API.
*/ * new Operation().run() becomes operation()
wrap(operation) { * @param Operation
this.operation = new operation(); */
// This for just exposing run function: export default function wrap(Operation) {
// return this.run.bind(this); /**
*
/** */
* return async (input, args=null) => {
* @param input const operation = new Operation();
* @param args const dish = new Dish(input);
*/
const _run = async(input, args=null) => { try {
const dish = new Dish(input); dish.findType();
} catch (e) {
try { log.debug(e);
dish.findType(); }
} catch (e) {
log.debug(e); if (!args) {
} args = operation.args.map(extractArg);
} else {
if (!args) { // Allows single arg ops to have arg defined not in array
args = this.operation.args.map(this.extractArg); if (!(args instanceof Array)) {
} else { args = [args];
// Allows single arg ops to have arg defined not in array }
if (!(args instanceof Array)) { }
args = [args]; const transformedInput = await dish.get(Dish.typeEnum(operation.inputType));
} return operation.run(transformedInput, args);
} };
const transformedInput = await dish.get(Dish.typeEnum(this.operation.inputType));
return this.operation.innerRun(transformedInput, args);
};
// There's got to be a nicer way to do this!
this.operation.innerRun = this.operation.run;
this.operation.run = _run;
return this.operation;
}
/**
*
* @param input
*/
async run(input, args = null) {
const dish = new Dish(input);
try {
dish.findType();
} catch (e) {
log.debug(e);
}
if (!args) {
args = this.operation.args.map(this.extractArg);
} else {
// Allows single arg ops to have arg defined not in array
if (!(args instanceof Array)) {
args = [args];
}
}
const transformedInput = await dish.get(Dish.typeEnum(this.operation.inputType));
return this.operation.run(transformedInput, args);
}
} }

View File

@ -18,36 +18,25 @@ global.ENVIRONMENT_IS_WEB = function() {
return typeof window === "object"; return typeof window === "object";
}; };
// import Chef from "../core/Chef";
// const CyberChef = { import wrap from "./Wrapper";
// bake: function(input, recipeConfig) {
// this.chef = new Chef();
// return this.chef.bake(
// input,
// recipeConfig,
// {},
// 0,
// false
// );
// }
// };
// export default CyberChef;
// export {CyberChef};
import Wrapper from "./Wrapper";
import * as operations from "../core/operations/index"; import * as operations from "../core/operations/index";
const cyberChef = { /**
base32: { *
from: new Wrapper().wrap(operations.FromBase32), * @param name
to: new Wrapper().wrap(operations.ToBase32), */
} function decapitalise(name) {
}; return `${name.charAt(0).toLowerCase()}${name.substr(1)}`;
}
export default cyberChef;
export {cyberChef}; // console.log(operations);
const chef = {};
Object.keys(operations).forEach(op =>
chef[decapitalise(op)] = wrap(operations[op]));
export default chef;
export {chef};