All files / tao-utils/src transfer.js

100% Statements 13/13
100% Branches 26/26
100% Functions 5/5
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93    23x                                               9x 9x                     2x         2x 1x   2x 1x   2x                                                         2x             2x 2x            
import { AppCtx } from '@tao.js/core';
 
const IDENTITY = (val) => val;
 
/**
 * Transfer the trigram and datagrams into a new Signal as an AppCtx
 *
 * @param {Object} tao signal trigram that you want to transfer from (short `t`/`a`/`o` keys)
 * @param {Object} data signal datagram that you want to transfer from
 * @param {Object} to signal trigram that you want to transfer to - any missing trigram (t, a, or o) will use the value from the `tao` argument
 * @param {Object} [options] optional functions to transform the datagrams in the new AppCtx
 * @param {function(*): *} [options.transformTerm] transforms the term datagram
 * @param {function(*): *} [options.transformAction] transforms the action datagram
 * @param {function(*): *} [options.transformOrient] transforms the orient datagram
 * @returns {AppCtx} AppCtx with the trigram of the `to` argument and the data from the `data` argument
 */
export function transferToAppCtx(
  tao,
  data,
  to,
  {
    transformTerm = IDENTITY,
    transformAction = IDENTITY,
    transformOrient = IDENTITY,
  } = {},
) {
  const { t, term, a, action, o, orient } = to;
  return new AppCtx(
    t || term || tao.t,
    a || action || tao.a,
    o || orient || tao.o,
    transformTerm(data[tao.t]),
    transformAction(data[tao.a]),
    transformOrient(data[tao.o]),
  );
}
 
function buildErrorDatagram(tao, data, error) {
  const fail = {
    reason: typeof error === 'string' ? error : error.message,
    a: tao.a,
    [tao.a]: data[tao.a],
  };
  if (typeof error !== 'string') {
    fail.error = error;
  }
  if (error.response && error.response.data) {
    fail.response = error.response.data;
  }
  return fail;
}
 
/**
 * Transfer the trigram, datagrams and an error into a new AppCtx that
 * signals the Error
 *
 * @param {Object} tao signal trigram that you want to transfer from (short `t`/`a`/`o` keys)
 * @param {Object} data signal datagram that you want to transfer from
 * @param {(Error|string)} error error that is either an Error or a String message
 * @param {Object} [options] optional functions to transform the datagrams in the new AppCtx
 * @param {string} [options.action='fail'] action used in the new trigram
 * @param {function(*): *} [options.transformTerm] transforms the term datagram
 * @param {function(*): *} [options.transformAction] transforms the error datagram built for the action
 * @param {function(*): *} [options.transformOrient] transforms the orient datagram
 * @returns {AppCtx} AppCtx with the trigram of the { `tao.t`, `action` [`'fail'`], `tao.o` }
 * argument and the datagrams from the `data` argument with the error
 */
export function transferError(
  tao,
  data,
  error,
  {
    action = 'fail',
    transformTerm = IDENTITY,
    transformAction = IDENTITY,
    transformOrient = IDENTITY,
  } = {},
) {
  return transferToAppCtx(
    tao,
    data,
    { action },
    {
      transformTerm,
      transformAction: () => {
        const fail = buildErrorDatagram(tao, data, error);
        return transformAction(fail);
      },
      transformOrient,
    },
  );
}