UNPKG

@rechnungs-api/client

Version:

This is the official JavaScript and TypeScript client for [RechnungsAPI](https://www.rechnungs-api.de), a powerful API for generating German invoices, including e-invoices with ZUGFeRD and XRechnung. The API also supports automated double-entry bookkeepin

214 lines (213 loc) 7.49 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { ApiError: () => ApiError, Client: () => Client }); module.exports = __toCommonJS(index_exports); var ApiError = class _ApiError { constructor(status, body) { this.status = status; this.body = body; } static async fromResponse(response) { return new _ApiError(response.status, await response.json()); } }; function queryParamsToString(params) { if (!params) return ""; return new URLSearchParams( Object.fromEntries( Object.entries(params).flatMap(([key, value]) => { if (value === null) return []; return [[key, value.toString()]]; }) ) ).toString(); } var Client = class { constructor({ apiKey, baseUrl }) { this.apiKey = apiKey; this.baseUrl = baseUrl != null ? baseUrl : "https://www.rechnungs-api.de/api/v1"; } get headers() { return { Authorization: `ApiKey ${this.apiKey}`, "Content-Type": "application/json" }; } /** * Create a new document * * Generate a new invoice, credit note, purchase order or other document type. * * @param document Information about the document that is to be created. Consult the API documentation * for more information. */ async createDocument(document) { const response = await fetch(`${this.baseUrl}/documents`, { method: "POST", headers: this.headers, body: JSON.stringify(document) }); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } async readDocument(id, format = "json") { const response = await fetch( `${this.baseUrl}/documents/${id}?format=${format}`, { headers: this.headers } ); if (!response.ok) throw await ApiError.fromResponse(response); if (format === "xml") return await response.text(); if (format === "pdf") return await response.arrayBuffer(); return await response.json(); } /** * Lists all ledgers. */ async listLedgers(queryParams) { const response = await fetch( `${this.baseUrl}/ledgers?${queryParamsToString(queryParams)}`, { headers: this.headers } ); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Creates a new ledger. This can be used to implement automated double-entry bookkeeping into your application. */ async createLedger(ledger) { const response = await fetch(`${this.baseUrl}/ledgers`, { method: "POST", headers: this.headers, body: JSON.stringify(ledger != null ? ledger : {}) }); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Deletes a given ledger together with all associated accounts and transactions. This cannot be undone. * * @param ledgerId ID of the ledger to delete. */ async deleteLedger(ledgerId) { const response = await fetch(`${this.baseUrl}/ledgers/${ledgerId}`, { method: "DELETE", headers: this.headers }); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Lists all accounts associated with a given ledger. */ async listLedgerAccounts(ledgerId, queryParams) { const response = await fetch( `${this.baseUrl}/ledgers/${ledgerId}/accounts?${queryParamsToString(queryParams)}`, { headers: this.headers } ); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Creates a new account on a given ledger. Each transaction on the ledger has exactly one debit and one credit account. German companies may want to use a scheme such as DATEV's SKR04. Note: Once an account has been created it can no longer be deleted. * * @param ledgerId ID of the ledger to create a new account for. * @param account Details about the new account to be created. */ async createLedgerAccount(ledgerId, account) { const response = await fetch( `${this.baseUrl}/ledgers/${ledgerId}/accounts`, { method: "POST", headers: this.headers, body: JSON.stringify(account) } ); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Lists transactions associated with a given ledger. */ async listLedgerTransactions(ledgerId, queryParams) { const response = await fetch( `${this.baseUrl}/ledgers/${ledgerId}/transactions?${queryParamsToString(queryParams)}`, { headers: this.headers } ); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Creates a new transaction on a given ledger. Once a transaction has been created it can no longer be deleted. */ async createLedgerTransaction(ledgerId, transaction) { const response = await fetch( `${this.baseUrl}/ledgers/${ledgerId}/transactions`, { method: "POST", headers: this.headers, body: JSON.stringify(transaction) } ); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Archives a transaction by creating a new transaction that reverses the effect of the archived transaction. Observably, this is similar to deleting a transaction, but it complies with the [GoBD](https://de.wikipedia.org/wiki/Grunds%C3%A4tze_zur_ordnungsm%C3%A4%C3%9Figen_F%C3%BChrung_und_Aufbewahrung_von_B%C3%BCchern,_Aufzeichnungen_und_Unterlagen_in_elektronischer_Form_sowie_zum_Datenzugriff). * * @returns The newly created transaction that cancels out the existing transaction. */ async archiveLedgerTransaction(ledgerId, transactionNumber) { const response = await fetch( `${this.baseUrl}/ledgers/${ledgerId}/transactions/${transactionNumber}`, { method: "DELETE", headers: this.headers } ); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } /** * Lists balances of the accounts on a given ledger when taking into account transactions of a given timeframe. */ async listLedgerBalances(ledgerId, queryParams) { const response = await fetch( `${this.baseUrl}/ledgers/${ledgerId}/balances?${queryParamsToString(queryParams)}`, { headers: this.headers } ); if (!response.ok) throw await ApiError.fromResponse(response); return await response.json(); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ApiError, Client }); //# sourceMappingURL=index.cjs.map