generic-interceptor
Version:
Provide proxy handler for getting properties and executing functions
79 lines • 5.02 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.interceptor = exports.ProcessingResult = exports.ProcessingType = void 0;
const uuid_1 = require("uuid");
var ProcessingType;
(function (ProcessingType) {
ProcessingType["synchronous"] = "synchronous";
ProcessingType["promise"] = "promise async";
ProcessingType["callbackEnding"] = "callback ending";
})(ProcessingType = exports.ProcessingType || (exports.ProcessingType = {}));
var ProcessingResult;
(function (ProcessingResult) {
ProcessingResult["succeed"] = "succeed";
ProcessingResult["failed"] = "failed";
})(ProcessingResult = exports.ProcessingResult || (exports.ProcessingResult = {}));
const interceptor = (options) => ({
get: (target, key) => {
const fieldValueType = typeof target[key];
const commonCallbackPayload = {
fieldKey: String(key),
fieldValueType,
fieldValue: target[key],
uniqueAccessId: options.passId === true ? (0, uuid_1.v4)() : undefined,
};
if (fieldValueType !== "function") {
options.onNonFunction(commonCallbackPayload);
return target[key];
}
return (...functionArgs) => {
try {
const maybeNewFunc = options.onBefore(Object.assign(Object.assign({}, commonCallbackPayload), { functionArgs }));
const syncResult = maybeNewFunc === undefined
? target[key](...functionArgs)
: maybeNewFunc(...functionArgs);
const callbackEnding = options.callbackEnding;
if (callbackEnding !== undefined &&
fieldValueType === "function" &&
syncResult instanceof Object &&
typeof syncResult[callbackEnding] === "function") {
return Object.assign(Object.assign({}, syncResult), { [callbackEnding]: (...functionArgs2) => __awaiter(void 0, void 0, void 0, function* () {
try {
const asyncResult = yield syncResult[callbackEnding](...functionArgs2);
return (options.onSuccess(Object.assign(Object.assign({}, commonCallbackPayload), { functionArgs, processingResult: ProcessingResult.succeed, functionResult: asyncResult, processingStrategy: ProcessingType.callbackEnding })) || asyncResult);
}
catch (error) {
throw (options.onError(Object.assign(Object.assign({}, commonCallbackPayload), { functionArgs, processingResult: ProcessingResult.failed, functionError: error, processingStrategy: ProcessingType.callbackEnding })) || error);
}
}) });
}
if (syncResult instanceof Promise) {
return (() => __awaiter(void 0, void 0, void 0, function* () {
try {
const asyncResult = yield syncResult;
return (options.onSuccess(Object.assign(Object.assign({}, commonCallbackPayload), { functionArgs, processingResult: ProcessingResult.succeed, functionResult: asyncResult, processingStrategy: ProcessingType.promise })) || asyncResult);
}
catch (error) {
throw (options.onError(Object.assign(Object.assign({}, commonCallbackPayload), { functionArgs, processingResult: ProcessingResult.failed, functionError: error, processingStrategy: ProcessingType.promise })) || error);
}
}))();
}
return (options.onSuccess(Object.assign(Object.assign({}, commonCallbackPayload), { functionArgs, processingResult: ProcessingResult.succeed, functionResult: syncResult, processingStrategy: ProcessingType.synchronous })) || syncResult);
}
catch (error) {
throw (options.onError(Object.assign(Object.assign({}, commonCallbackPayload), { functionArgs, processingResult: ProcessingResult.failed, functionError: error, processingStrategy: ProcessingType.synchronous })) || error);
}
};
},
});
exports.interceptor = interceptor;
//# sourceMappingURL=index.js.map