UNPKG

prostgles-client

Version:

Reactive client for Postgres

32 lines (31 loc) 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getMethods = void 0; const prostgles_types_1 = require("prostgles-types"); const getMethods = ({ onDebug, methods, socket }) => { let methodsObj = {}; const _methods = JSON.parse(JSON.stringify(methods)); _methods.map(method => { /** New method def */ const isBasic = typeof method === "string"; const methodName = isBasic ? method : method.name; const onRun = async function (...params) { await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "method", command: methodName, data: { params } })); return new Promise((resolve, reject) => { socket.emit(prostgles_types_1.CHANNELS.METHOD, { method: methodName, params }, (err, res) => { if (err) reject(err); else resolve(res); }); }); }; methodsObj[methodName] = isBasic ? onRun : { ...method, run: onRun }; }); methodsObj = Object.freeze(methodsObj); return { methodsObj }; }; exports.getMethods = getMethods;