Source

cryptoForage.esm.js

import { _ as __awaiter, a as __generator } from './_tslib-ca2dd4fc.js';
import 'tweetnacl';
import 'tweetnacl-util';
import { crypto } from './cryptoPrimitives.esm.js';
import { handler } from './handler.esm.js';

/**
 * @description Use currying process to inject
 * @namespace
 * @category forage
 */
var cryptoForage = {
    /**
     * @description encrypt a functional
     * @name enBox
     * @category forage
     * @uses forage.mergeItem
     * @memberof cryptoForage
     * @param {string} key - encryption key
     * @param {array} logger - loglevel of errors (forage)
     * @param {array} returner - return type (forage)
     * @returns {object|*} - the JSON results of our box
     * @function
     * @example
     *  const boxFn = await cryptoForage.enBox({
     *    key:key,
     *    loggerType: 'throw',
     *    returnType: 7
     *  })(new Date().now())
     */
    enBox: function (_a) {
        var _b = _a === void 0 ? {} : _a, key = _b.key, loggerType = _b.loggerType, returnType = _b.returnType;
        return function (val) {
            return __awaiter(this, void 0, void 0, function () {
                var _a, _b, _c, _d, _e;
                return __generator(this, function (_f) {
                    switch (_f.label) {
                        case 0:
                            _b = (_a = handler).returner;
                            _d = (_c = crypto.secretBox).encrypt;
                            _e = {};
                            return [4 /*yield*/, val];
                        case 1: return [4 /*yield*/, _d.apply(_c, [(_e.json = _f.sent(),
                                    _e.key = key,
                                    _e)]).catch(function (err) {
                                /* istanbul ignore next */
                                return handler.logger(err)(loggerType);
                            })];
                        case 2: return [2 /*return*/, _b.apply(_a, [_f.sent()])(returnType)];
                    }
                });
            });
        };
    },
    /**
     * @name deBox
     * @description use the "box" system to decrypt something from the storage
     * @category forage
     * @memberof cryptoForage
     * @uses forage.getKeyValue
     * @param {string} key - encryption key
     * @param {array} model - array of acceptable properties (validate)
     * @param {string} maxlen - forage name of key (validate)
     * @param {string} row - forage row (forage)
     * @param {string} name - forage name of key (forage)
     * @param {string} logger - loglevel of errors (forage)
     * @param {number} returner - return type (forage)
     * @returns {object|*} - the JSON results of our box
     * @function
     * @example
     *  const curry = async function (val) {
     *    const box = await crypto.secretBox.encrypt({
     *      json: val,
     *      key: key
     *    })
     *    console.log(box)
     *    return box
     *  }
     */
    deBox: function (_a) {
        var _b = _a === void 0 ? {} : _a, key = _b.key, model = _b.model, maxLen = _b.maxLen, loggerType = _b.loggerType, returnType = _b.returnType;
        return function (val) {
            return __awaiter(this, void 0, void 0, function () {
                var result, _a, _b, _c, _d, _e;
                return __generator(this, function (_f) {
                    switch (_f.label) {
                        case 0:
                            _b = (_a = handler).returner;
                            _d = (_c = crypto.secretBox).decrypt;
                            _e = {};
                            return [4 /*yield*/, val];
                        case 1: return [4 /*yield*/, _d.apply(_c, [(_e.msg = _f.sent(),
                                    _e.key = key,
                                    _e)]).catch(function (err) {
                                /* istanbul ignore next */
                                return handler.logger(err)(loggerType);
                            })
                            /* istanbul ignore */
                        ];
                        case 2:
                            result = _b.apply(_a, [_f.sent()
                                /* istanbul ignore */
                            ])(returnType);
                            if (model && typeof model === 'object') {
                                return [2 /*return*/, handler.jsonPurify({
                                        model: model,
                                        maxLen: maxLen || null
                                    })(JSON.stringify(result))];
                            }
                            else {
                                return [2 /*return*/, result];
                            }
                    }
                });
            });
        };
    }
};

export { cryptoForage };
//# sourceMappingURL=cryptoForage.esm.js.map