|
| 1 | +'use strict'; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 12 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 13 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 14 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 15 | + function step(op) { |
| 16 | + if (f) throw new TypeError("Generator is already executing."); |
| 17 | + while (_) try { |
| 18 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 19 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 20 | + switch (op[0]) { |
| 21 | + case 0: case 1: t = op; break; |
| 22 | + case 4: _.label++; return { value: op[1], done: false }; |
| 23 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 24 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 25 | + default: |
| 26 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 27 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 28 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 29 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 30 | + if (t[2]) _.ops.pop(); |
| 31 | + _.trys.pop(); continue; |
| 32 | + } |
| 33 | + op = body.call(thisArg, _); |
| 34 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 35 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 36 | + } |
| 37 | +}; |
| 38 | +/* |
| 39 | +* Author: Faisal Sami |
| 40 | +* mail: faisalsami78@gmail.com |
| 41 | +* https://github.com/faisalsami/odoo-xmlrpc |
| 42 | +* |
| 43 | +* Modify by nguyenvantien2009 |
| 44 | +* http://nguyenvantien2009.com |
| 45 | +*/ |
| 46 | +var xmlrpc = require('xmlrpc'); |
| 47 | +var url = require('url'); |
| 48 | +var Odoo = /** @class */ (function () { |
| 49 | + function Odoo(config) { |
| 50 | + var _this = this; |
| 51 | + config = config || {}; |
| 52 | + var urlparts = url.parse(config.url); |
| 53 | + this.host = urlparts.hostname; |
| 54 | + this.port = config.port || urlparts.port; |
| 55 | + this.db = config.db; |
| 56 | + this.username = config.username; |
| 57 | + this.password = config.password; |
| 58 | + this.secure = true; |
| 59 | + if (urlparts.protocol !== 'https:') { |
| 60 | + this.secure = false; |
| 61 | + } |
| 62 | + var uid = 0; |
| 63 | + /** |
| 64 | + * connect to XML-RPC of Odoo base on setting in config. |
| 65 | + */ |
| 66 | + this.connect = function () { return __awaiter(_this, void 0, void 0, function () { |
| 67 | + var clientOptions, client, params; |
| 68 | + return __generator(this, function (_a) { |
| 69 | + clientOptions = { |
| 70 | + host: this.host, |
| 71 | + port: this.port, |
| 72 | + path: '/xmlrpc/2/common' |
| 73 | + }; |
| 74 | + client = this.secure === false |
| 75 | + ? xmlrpc.createClient(clientOptions) |
| 76 | + : xmlrpc.createSecureClient(clientOptions); |
| 77 | + params = [ |
| 78 | + this.db, |
| 79 | + this.username, |
| 80 | + this.password, |
| 81 | + {} // empty object. |
| 82 | + ]; |
| 83 | + return [2 /*return*/, new Promise(function (resolve, reject) { |
| 84 | + client.methodCall('authenticate', params, function (error, value) { |
| 85 | + if (error) { |
| 86 | + reject(error); |
| 87 | + } |
| 88 | + if (!value) { |
| 89 | + reject({ message: "No UID returned from authentication." }); |
| 90 | + } |
| 91 | + uid = value; |
| 92 | + resolve(value); |
| 93 | + }); |
| 94 | + })]; |
| 95 | + }); |
| 96 | + }); }; |
| 97 | + /** |
| 98 | + * Execute a method from model in Odoo. |
| 99 | + * |
| 100 | + * @param {String} model Model name in Odoo. |
| 101 | + * @param {String} method Name of method in Odoo. |
| 102 | + * @param {Array<Object>} params Params in to execute from Odoo. |
| 103 | + */ |
| 104 | + this.execute_kw = function (model, method, params) { return __awaiter(_this, void 0, void 0, function () { |
| 105 | + var clientOptions, client, fparams; |
| 106 | + return __generator(this, function (_a) { |
| 107 | + clientOptions = { |
| 108 | + host: this.host, |
| 109 | + port: this.port, |
| 110 | + path: '/xmlrpc/2/object' |
| 111 | + }; |
| 112 | + client = this.secure === false |
| 113 | + ? xmlrpc.createClient(clientOptions) |
| 114 | + : xmlrpc.createSecureClient(clientOptions); |
| 115 | + fparams = [ |
| 116 | + this.db, |
| 117 | + uid, |
| 118 | + this.password, |
| 119 | + model, |
| 120 | + method, |
| 121 | + params |
| 122 | + ]; |
| 123 | + return [2 /*return*/, new Promise(function (resolve, reject) { |
| 124 | + client.methodCall('execute_kw', fparams, function (error, value) { |
| 125 | + error ? reject(error) : resolve(value); |
| 126 | + }); |
| 127 | + })]; |
| 128 | + }); |
| 129 | + }); }; |
| 130 | + /** |
| 131 | + * call workflow in odoo. |
| 132 | + * |
| 133 | + * @param {String} model Model name in Odoo |
| 134 | + * @param {String} method Method name in Odoo |
| 135 | + * @param {Object[]} params List parameters to call xml-rpc. |
| 136 | + */ |
| 137 | + this.exec_workflow = function (model, method, params) { return __awaiter(_this, void 0, void 0, function () { |
| 138 | + var clientOptions, client, fparams; |
| 139 | + return __generator(this, function (_a) { |
| 140 | + clientOptions = { |
| 141 | + host: this.host, |
| 142 | + port: this.port, |
| 143 | + path: '/xmlrpc/2/object' |
| 144 | + }; |
| 145 | + client = this.secure === false |
| 146 | + ? xmlrpc.createClient(clientOptions) |
| 147 | + : xmlrpc.createSecureClient(clientOptions); |
| 148 | + fparams = [ |
| 149 | + this.db, |
| 150 | + uid, |
| 151 | + this.password, |
| 152 | + model, |
| 153 | + method, |
| 154 | + params |
| 155 | + ]; |
| 156 | + return [2 /*return*/, new Promise(function (resolve, reject) { |
| 157 | + client.methodCall('exec_workflow', fparams, function (error, value) { |
| 158 | + error ? reject(error) : resolve(value); |
| 159 | + }); |
| 160 | + })]; |
| 161 | + }); |
| 162 | + }); }; |
| 163 | + this.render_report = function (report, params) { return __awaiter(_this, void 0, void 0, function () { |
| 164 | + var clientOptions, client, fparams; |
| 165 | + return __generator(this, function (_a) { |
| 166 | + clientOptions = { |
| 167 | + host: this.host, |
| 168 | + port: this.port, |
| 169 | + path: '/xmlrpc/2/report' |
| 170 | + }; |
| 171 | + client = this.secure === false |
| 172 | + ? xmlrpc.createClient(clientOptions) |
| 173 | + : xmlrpc.createSecureClient(clientOptions); |
| 174 | + fparams = [ |
| 175 | + this.db, |
| 176 | + uid, |
| 177 | + this.password, |
| 178 | + report, |
| 179 | + params |
| 180 | + ]; |
| 181 | + return [2 /*return*/, new Promise(function (resolve, reject) { |
| 182 | + client.methodCall('render_report', fparams, function (error, value) { |
| 183 | + error ? reject(error) : resolve(value); |
| 184 | + }); |
| 185 | + })]; |
| 186 | + }); |
| 187 | + }); }; |
| 188 | + } |
| 189 | + return Odoo; |
| 190 | +}()); |
| 191 | +module.exports = Odoo; |
0 commit comments