apiUtils.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Wrap operations for consumption in Node
  3. *
  4. * @author d98762625 [d98762625@gmail.com]
  5. * @copyright Crown Copyright 2018
  6. * @license Apache-2.0
  7. */
  8. import Dish from "../core/Dish";
  9. /**
  10. * Extract default arg value from operation argument
  11. * @param {Object} arg - an arg from an operation
  12. */
  13. function extractArg(arg) {
  14. if (arg.type === "option" || arg.type === "editableOption") {
  15. return arg.value[0];
  16. }
  17. return arg.value;
  18. }
  19. /**
  20. * Wrap an operation to be consumed by node API.
  21. * new Operation().run() becomes operation()
  22. * Perform type conversion on input
  23. * @param {Operation} Operation
  24. * @returns {Function} The operation's run function, wrapped in
  25. * some type conversion logic
  26. */
  27. export function wrap(Operation) {
  28. /**
  29. * Wrapped operation run function
  30. */
  31. return async (input, args=null, callback) => {
  32. if (callback && typeof callback !== "function") {
  33. throw TypeError("Expected callback to be a function");
  34. }
  35. if (!callback && typeof args === "function") {
  36. callback = args;
  37. args = null;
  38. }
  39. const operation = new Operation();
  40. const dish = new Dish();
  41. const type = Dish.typeEnum(input.constructor.name);
  42. dish.set(input, type);
  43. if (!args) {
  44. args = operation.args.map(extractArg);
  45. } else {
  46. // Allows single arg ops to have arg defined not in array
  47. if (!(args instanceof Array)) {
  48. args = [args];
  49. }
  50. }
  51. const transformedInput = await dish.get(operation.inputType);
  52. // Allow callback or promsise / async-await
  53. if (callback) {
  54. try {
  55. const out = operation.run(transformedInput, args);
  56. callback(null, out);
  57. } catch (e) {
  58. callback(e);
  59. }
  60. } else {
  61. return operation.run(transformedInput, args);
  62. }
  63. };
  64. }
  65. /**
  66. * First draft
  67. * @param input
  68. * @param type
  69. */
  70. export async function translateTo(input, type) {
  71. const dish = new Dish();
  72. const initialType = Dish.typeEnum(input.constructor.name);
  73. dish.set(input, initialType);
  74. return await dish.get(type);
  75. }
  76. /**
  77. *
  78. * @param searchTerm
  79. */
  80. export function search(searchTerm) {
  81. }
  82. /**
  83. * Extract properties from an operation by instantiating it and
  84. * returning some of its properties for reference.
  85. * @param {Operation} Operation - the operation to extract info from
  86. * @returns {Object} operation properties
  87. */
  88. function extractOperationInfo(Operation) {
  89. const operation = new Operation();
  90. return {
  91. name: operation.name,
  92. module: operation.module,
  93. description: operation.description,
  94. inputType: operation.inputType,
  95. outputType: operation.outputType,
  96. args: Object.assign([], operation.args),
  97. };
  98. }
  99. /**
  100. * @param {Object} operations - an object filled with operations.
  101. * @param {String} searchTerm - the name of the operation to get help for.
  102. * Case and whitespace are ignored in search.
  103. * @returns {Object} listing properties of function
  104. */
  105. export function help(operations, searchTerm) {
  106. if (typeof searchTerm === "string") {
  107. const operation = operations[Object.keys(operations).find(o =>
  108. o.toLowerCase() === searchTerm.replace(/ /g, "").toLowerCase())];
  109. if (operation) {
  110. return extractOperationInfo(operation);
  111. }
  112. }
  113. return null;
  114. }
  115. /**
  116. * SomeName => someName
  117. * @param {String} name - string to be altered
  118. * @returns {String} decapitalised
  119. */
  120. export function decapitalise(name) {
  121. return `${name.charAt(0).toLowerCase()}${name.substr(1)}`;
  122. }