UNPKG

frappe-js-sdk

Version:

TypeScript/JavaScript client for Frappe Framework REST API

404 lines (403 loc) 23.3 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { 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; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FrappeDB = void 0; var FrappeDB = /** @class */ (function () { function FrappeDB(appURL, axios, useToken, token, tokenType) { this.appURL = appURL; this.axios = axios; this.useToken = useToken !== null && useToken !== void 0 ? useToken : false; this.token = token; this.tokenType = tokenType; } /** * Get a document from the database * @param {string} doctype Name of the doctype * @param {string} docname Name of the document * @returns Promise which resolves to the document object */ FrappeDB.prototype.getDoc = function (doctype, docname) { if (docname === void 0) { docname = ''; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.axios .get("/api/resource/".concat(doctype, "/").concat(encodeURIComponent(docname))) .then(function (res) { return res.data.data; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while fetching the document.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** * Gets a list of documents from the database for a particular doctype. Add filters, sorting order and pagination to get a filtered and sorted list of documents. * @param {string} doctype Name of the doctype * @param {@type GetDocListArgs} [args] Arguments for the query * @returns Promise which resolves to an array of documents */ FrappeDB.prototype.getDocList = function (doctype, args) { var _a; return __awaiter(this, void 0, void 0, function () { var params, fields, filters, orFilters, orderBy, limit, limit_start, groupBy, _b, asDict, orderByString; return __generator(this, function (_c) { params = {}; if (args) { fields = args.fields, filters = args.filters, orFilters = args.orFilters, orderBy = args.orderBy, limit = args.limit, limit_start = args.limit_start, groupBy = args.groupBy, _b = args.asDict, asDict = _b === void 0 ? true : _b; orderByString = orderBy ? "".concat(String(orderBy === null || orderBy === void 0 ? void 0 : orderBy.field), " ").concat((_a = orderBy === null || orderBy === void 0 ? void 0 : orderBy.order) !== null && _a !== void 0 ? _a : 'asc') : ''; params = { fields: fields ? JSON.stringify(fields) : undefined, filters: filters ? JSON.stringify(filters) : undefined, or_filters: orFilters ? JSON.stringify(orFilters) : undefined, order_by: orderByString, group_by: groupBy, limit: limit, limit_start: limit_start, as_dict: asDict, }; } return [2 /*return*/, this.axios .get("/api/resource/".concat(doctype), { params: params }) .then(function (res) { return res.data.data; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while fetching the documents.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** Creates a new document in the database * @param {string} doctype Name of the doctype * @param {Object} value Contents of the document * @returns Promise which resolves with the complete document object */ FrappeDB.prototype.createDoc = function (doctype, value) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.axios .post("/api/resource/".concat(doctype), __assign({}, value)) .then(function (res) { return res.data.data; }) .catch(function (error) { var _a, _b, _c; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: (_a = error.response.data.message) !== null && _a !== void 0 ? _a : 'There was an error while creating the document.', exception: (_c = (_b = error.response.data.exception) !== null && _b !== void 0 ? _b : error.response.data.exc_type) !== null && _c !== void 0 ? _c : '' }); })]; }); }); }; /** Updates a document in the database * @param {string} doctype Name of the doctype * @param {string} docname Name of the document * @param {Object} value Contents of the document to update (only the fields that are to be updated) * @returns Promise which resolves with the complete document object */ FrappeDB.prototype.updateDoc = function (doctype, docname, value) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.axios .put("/api/resource/".concat(doctype, "/").concat(docname ? encodeURIComponent(docname) : docname), __assign({}, value)) .then(function (res) { return res.data.data; }) .catch(function (error) { var _a, _b, _c; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: (_a = error.response.data.message) !== null && _a !== void 0 ? _a : 'There was an error while updating the document.', exception: (_c = (_b = error.response.data.exception) !== null && _b !== void 0 ? _b : error.response.data.exc_type) !== null && _c !== void 0 ? _c : '' }); })]; }); }); }; /** * Deletes a document in the database * @param {string} doctype Name of the doctype * @param {string} docname Name of the document * @returns Promise which resolves an object with a message "ok" */ FrappeDB.prototype.deleteDoc = function (doctype, docname) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.axios .delete("/api/resource/".concat(doctype, "/").concat(docname ? encodeURIComponent(docname) : docname)) .then(function (res) { return res.data; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while deleting the document.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** * Gets count of documents from the database for a particular doctype with the given filters * @param {string} doctype Name of the doctype * @param {@type Filter[]} [filters] Filters to be applied in the count query * @param {boolean} [cache] Whether to cache the result or not * @param {boolean} [debug] Whether to print debug messages or not * @returns Promise which resolves a number */ FrappeDB.prototype.getCount = function (doctype, filters, cache, debug) { if (cache === void 0) { cache = false; } if (debug === void 0) { debug = false; } return __awaiter(this, void 0, void 0, function () { var params; return __generator(this, function (_a) { params = { doctype: doctype, filters: [], }; if (cache) { params.cache = cache; } if (debug) { params.debug = debug; } if (filters) { params.filters = filters ? JSON.stringify(filters) : undefined; } return [2 /*return*/, this.axios .get('/api/method/frappe.client.get_count', { params: params }) .then(function (res) { return res.data.message; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while getting the count.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** * Get a document from the database * @param {string} doctype Name of the doctype * @param {@type GetLastDocArgs} [args] Arguments for the query * @returns Promise which resolves to the document object */ FrappeDB.prototype.getLastDoc = function (doctype, args) { return __awaiter(this, void 0, void 0, function () { var queryArgs, getDocLists; return __generator(this, function (_a) { switch (_a.label) { case 0: queryArgs = { orderBy: { field: 'creation', order: 'desc', }, }; if (args) { queryArgs = __assign(__assign({}, queryArgs), args); } return [4 /*yield*/, this.getDocList(doctype, __assign(__assign({}, queryArgs), { limit: 1, fields: ['name'] }))]; case 1: getDocLists = _a.sent(); if (getDocLists.length > 0) { return [2 /*return*/, this.getDoc(doctype, getDocLists[0].name)]; } return [2 /*return*/, {}]; } }); }); }; /** * Renames a document from the database * @param {string} doctype Name of the doctype * @param {string} oldname Current name of the document * @param {string} newname The new name that will replace the `oldname` * @param {boolean} merge Merges the old document into the new one if a document with `newname` already exists. * @returns Promise which resolves with the updated document name */ FrappeDB.prototype.renameDoc = function (doctype, oldname, newname, merge) { if (merge === void 0) { merge = false; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.axios .post('/api/method/frappe.client.rename_doc', { doctype: doctype, old_name: oldname, new_name: newname, merge: merge, }) .then(function (res) { return res.data; }) .catch(function (error) { var _a, _b, _c; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: (_a = error.response.data.message) !== null && _a !== void 0 ? _a : 'There was an error while renaming the document.', exception: (_c = (_b = error.response.data.exception) !== null && _b !== void 0 ? _b : error.response.data.exc_type) !== null && _c !== void 0 ? _c : '' }); })]; }); }); }; /** * Retrieves a document's value from the database for a specific doctype using the provided field names and filters. * @param {string} doctype Name of the doctype * @param {FieldName} [fieldname] - Fields to be returned (default `name`) * @param {Filter[]} [filters] Filters to be applied in the get query * @param {boolean} asDict Return as dict(object) or list (array) * @param {boolean} [debug] Whether to print debug messages or not * @param {string} parent Parent doctype name to fetch child table record * @returns Promise which resolves an object with specified fieldnames */ FrappeDB.prototype.getValue = function (doctype, fieldname, filters, asDict, debug, parent) { if (asDict === void 0) { asDict = true; } if (debug === void 0) { debug = false; } if (parent === void 0) { parent = null; } return __awaiter(this, void 0, void 0, function () { var params; return __generator(this, function (_a) { params = { doctype: doctype, fieldname: '[]', filters: [], as_dict: asDict, debug: debug, parent: null, }; if (fieldname) { params.fieldname = typeof fieldname === 'object' ? JSON.stringify(fieldname) : fieldname; } if (filters) { params.filters = filters ? JSON.stringify(filters) : undefined; } if (parent) { params.parent = parent; } return [2 /*return*/, this.axios .get('/api/method/frappe.client.get_value', { params: params }) .then(function (res) { return res.data; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while getting the value.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** * Sets the field values in the database for the specified doctype. * @param {string} doctype Name of the doctype * @param {string} name Name of the document * @param {string | object} fieldname Fieldname(s) whose value(s) need to be set. * @param {any} value Value to be set in fieldname when updating a single field or if `fieldname` is a string. * @returns Promise which resolves an updated docoument */ FrappeDB.prototype.setValue = function (doctype, name, fieldname, value) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { if (fieldname !== null && typeof fieldname === 'object' && !Array.isArray(fieldname)) { value = undefined; } return [2 /*return*/, this.axios .post('/api/method/frappe.client.set_value', { doctype: doctype, name: name, fieldname: fieldname, value: value, }) .then(function (res) { return res.data; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while setting the value.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** * Retrieves the field value from the database for a specific single doctype. * @param {string} doctype Name of the doctype * @param {string} field Name of the field * @returns Promise that resolves to the field's value. */ FrappeDB.prototype.getSingleValue = function (doctype, field) { return __awaiter(this, void 0, void 0, function () { var params; return __generator(this, function (_a) { params = { doctype: doctype, field: field, }; return [2 /*return*/, this.axios .get('/api/method/frappe.client.get_single_value', { params: params }) .then(function (res) { return res.data; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while getting the value of single doctype.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** * Submit a document. * @param {object} doc Document to be submitted * @returns Promise that resolves to a submitted document. */ FrappeDB.prototype.submit = function (doc) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.axios .post('/api/method/frappe.client.submit', { doc: doc }) .then(function (res) { return res.data.message; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while submitting the document.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; /** * Cancel a document. * @param {string} doctype Name of the doctype * @param {string} name Name of the document to be cancelled * @returns Promise that resolves to a canceled document. */ FrappeDB.prototype.cancel = function (doctype, name) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.axios .post('/api/method/frappe.client.cancel', { doctype: doctype, name: name }) .then(function (res) { return res.data; }) .catch(function (error) { var _a, _b; throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: 'There was an error while cancelling the document.', exception: (_b = (_a = error.response.data.exception) !== null && _a !== void 0 ? _a : error.response.data.exc_type) !== null && _b !== void 0 ? _b : '' }); })]; }); }); }; return FrappeDB; }()); exports.FrappeDB = FrappeDB;