UNPKG

facturapi-es6

Version:

FacturAPI makes it easy for developers to generate valid Invoices in Mexico (known as Factura Electrónica or CFDI).

285 lines (284 loc) • 9.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var axios_1 = __importDefault(require("axios")); var constants_1 = __importDefault(require("./constants")); var FormData = require("form-data"); axios_1.default.defaults.baseURL = constants_1.default.BASE_URL; axios_1.default.defaults.headers.post["Content-Type"] = "application/json"; axios_1.default.defaults.headers.put["Content-Type"] = "application/json"; var responseInterceptor = function (response) { return response.data; }; var errorInterceptor = function (error) { return Promise.reject(new Error(error.response.data.message)); }; function encodeStringToBase64(text) { // Make sure text is a string text = text.toString(); return Buffer.from(text).toString("base64"); } var Wrapper = /** @class */ (function () { function Wrapper(apiKey) { this.client = axios_1.default.create(); this.client.interceptors.response.use(responseInterceptor, errorInterceptor); this.t = 23; this.client.defaults.headers.common["Authorization"] = "Basic " + encodeStringToBase64(apiKey + ":"); } Wrapper.prototype.listCustomers = function (params) { if (!params) params = {}; return this.client.get("/customers", { params: params }); }; /** * Gets a single customer object * @param {string} id */ Wrapper.prototype.retrieveCustomer = function (id) { if (!id) return Promise.reject(new Error("id is required")); return this.client.get("/customers/" + id); }; /** * Creates a new customer in your organization * @param {Object} data */ Wrapper.prototype.createCustomer = function (data) { return this.client.post("/customers", data); }; /** * Updates a customer * @param {string} id * @param {Object} data */ Wrapper.prototype.updateCustomer = function (id, data) { return this.client.put("/customers/" + id, data); }; /** * Permanently removes a customer from your organization. * @param {string} id */ Wrapper.prototype.removeCustomer = function (id) { return this.client.delete("/customers/" + id); }; /** * Gets a paginated list of products that belong to your organization * @param {Object} params */ Wrapper.prototype.listProducts = function (params) { if (!params) params = {}; return this.client.get("/products", { params: params }); }; /** * Gets a single product object * @param {string} id */ Wrapper.prototype.retrieveProduct = function (id) { if (!id) return Promise.reject(new Error("id is required")); return this.client.get("/products/" + id); }; /** * Creates a new product in your organization * @param {Object} data */ Wrapper.prototype.createProduct = function (data) { return this.client.post("/products", data); }; /** * Updates a product * @param {string} id * @param {object} data */ Wrapper.prototype.updateProduct = function (id, data) { return this.client.put("/products/" + id, data); }; /** * Permanently removes a product from your organization * @param {string} id */ Wrapper.prototype.removeProduct = function (id) { return this.client.delete("/products/" + id); }; /** * Searches product keys by criteria "q" * @param {string} q */ Wrapper.prototype.keys = function (q) { return this.client.get("/products/keys", { q: q }); }; /** * Searches product units by criteria "q" * @param {string} q */ Wrapper.prototype.units = function (q) { return this.client.get("/products/units", { q: q }); }; /** * Gets a paginated list of invoices created by your organization * @param {Object} params */ Wrapper.prototype.listInvoices = function (params) { if (!params) params = {}; return this.client.get("/invoices", { params: params }); }; /** * Gets a single invoice object * @param {string} id * @returns {Promise<Object>} Invoice object */ Wrapper.prototype.retrieveInvoice = function (id) { if (!id) return Promise.reject(new Error("id is required")); return this.client.get("/invoices/" + id); }; /** * Creates a new valid invoice (CFDI). * @param {Object} data */ Wrapper.prototype.createInvoice = function (data) { return this.client.post("/invoices", data); }; /** * Cancels an invoice. The invoice will not be valid anymore and will change its status to canceled. * @param {string} id */ Wrapper.prototype.cancelInvoice = function (id) { return this.client.delete("/invoices/" + id); }; /** * Sends the invoice to the customer's email * @param {string} id Invoice Id * @param {Object} data * @param {string} data.email Email address to send the invoice to */ Wrapper.prototype.sendInvoiceByEmail = function (id, data) { return this.client.post("/invoices/" + id + "/email", data); }; /** * Downloads the specified invoice in PDF format * @param {string} id Invoice Id * @returns {Promise<ReadStream>} PDF file in a stream */ Wrapper.prototype.downloadPdf = function (id) { return this.client.get("/invoices/" + id + "/pdf", { responseType: "stream" }); }; /** * Downloads the specified invoice in XML format * @param {string} id Invoice Id * @returns {Promise<ReadStream>} XML file in a stream */ Wrapper.prototype.downloadXml = function (id) { return this.client.get("/invoices/" + id + "/xml", { responseType: "stream" }); }; /** * Downloads the specified invoice in a ZIP package containing both PDF and XML files * @param {string} id Invoice Id * @returns {Promise<ReadStream>} ZIP file in a stream */ Wrapper.prototype.downloadZip = function (id) { return this.client.get("/invoices/" + id + "/zip", { responseType: "stream" }); }; /** * Creates a new organization in your account * @param {Object} data */ Wrapper.prototype.createOrganization = function (data) { return this.client.post("/organizations", data); }; /** * Gets a paginated list of organizations that belong to your account * @param {Object} params */ Wrapper.prototype.listOrganizations = function (params) { if (!params) params = {}; return this.client.get("/organizations", { params: params }); }; /** * Gets a single organization object * @param {string} id */ Wrapper.prototype.retrieveOrganization = function (id) { if (!id) return Promise.reject(new Error("id is required")); return this.client.get("/organizations/" + id); }; /** * Gets the api keys for an organization * @param {string} id */ Wrapper.prototype.getOrganizationApiKeys = function (id) { if (!id) return Promise.reject(new Error("id is required")); return this.client.get("/organizations/" + id + "/apikeys"); }; /** * Permanently removes an organization from your account. * @param {string} id */ Wrapper.prototype.removeOrganization = function (id) { if (!id) return Promise.reject(new Error("id is required")); return this.client.delete("/organizations/" + id); }; /** * Updates the organization's legal information * @param {string} id * @param {object} data */ Wrapper.prototype.updateOrganizationLegal = function (id, data) { if (!id) return Promise.reject(new Error("id is required")); return this.client.put("/organizations/" + id + "/legal", data); }; /** * Updates the organization's customization information * @param {string} id * @param {object} data */ Wrapper.prototype.updateOrganizationCustomization = function (id, data) { return this.client.put("/organizations/" + id + "/customization", data); }; /** * Uploads the organization's logo * @param {string} id * @param {ReadableStream} file */ Wrapper.prototype.uploadOrganizationLogo = function (id, file) { var formData = new FormData(); formData.append("file", file); return this.client.put("/organizations/" + id + "/logo", formData, { headers: formData.getHeaders() }); }; /** * Uploads the organization's certificate (CSD) * @param {string} id * @param {ReadableStream} cerFile * @param {ReadableStream} keyFile * @param {string} password */ Wrapper.prototype.uploadOrganizationCertificate = function (id, cerFile, keyFile, password) { var formData = new FormData(); formData.append("cer", cerFile); formData.append("key", keyFile); formData.append("password", password); return this.client.put("/organizations/" + id + "/certificate", formData, { headers: formData.getHeaders() }); }; return Wrapper; }()); exports.default = Wrapper;