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

1 lines 12.6 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n\tCursor,\n\tDocument,\n\tDocumentCreateRequest,\n\tLedger,\n\tLedgerAccount,\n\tLedgerAccountCreateRequest,\n\tLedgerAccountListResponse,\n\tLedgerCreateRequest,\n\tLedgerListResponse,\n\tLedgerTransaction,\n\tLedgerTransactionCreateRequest,\n\tLedgerTransactionListResponse,\n\tLimit,\n\tListLedgerBalancesResponse,\n} from \"./generated\";\n\nexport * from \"./generated/types.gen\";\n\nexport class ApiError {\n\tconstructor(\n\t\tpublic status: number,\n\t\tpublic body: unknown,\n\t) {}\n\n\tstatic async fromResponse(response: Response) {\n\t\treturn new ApiError(response.status, await response.json());\n\t}\n}\n\nfunction queryParamsToString(\n\tparams: Record<string, string | number | null> | undefined,\n) {\n\tif (!params) return \"\";\n\treturn new URLSearchParams(\n\t\tObject.fromEntries(\n\t\t\tObject.entries(params).flatMap(([key, value]) => {\n\t\t\t\tif (value === null) return [];\n\t\t\t\treturn [[key, value.toString()]];\n\t\t\t}),\n\t\t),\n\t).toString();\n}\n\n/**\n * RechnungsAPI API client.\n *\n * See [https://www.rechnungs-api.de/docs] for more information.\n */\nexport class Client {\n\tconstructor({\n\t\tapiKey,\n\t\tbaseUrl,\n\t}: {\n\t\tapiKey: string;\n\t\tbaseUrl?: string;\n\t}) {\n\t\tthis.apiKey = apiKey;\n\t\tthis.baseUrl = baseUrl ?? \"https://www.rechnungs-api.de/api/v1\";\n\t}\n\n\tprivate apiKey: string;\n\tprivate baseUrl: string;\n\n\tprivate get headers() {\n\t\treturn {\n\t\t\tAuthorization: `ApiKey ${this.apiKey}`,\n\t\t\t\"Content-Type\": \"application/json\",\n\t\t};\n\t}\n\n\t/**\n\t * Create a new document\n\t *\n\t * Generate a new invoice, credit note, purchase order or other document type.\n\t *\n\t * @param document Information about the document that is to be created. Consult the API documentation\n\t * for more information.\n\t */\n\tpublic async createDocument(document: DocumentCreateRequest) {\n\t\tconst response = await fetch(`${this.baseUrl}/documents`, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: this.headers,\n\t\t\tbody: JSON.stringify(document),\n\t\t});\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as Document;\n\t}\n\n\t/**\n\t * Retrieve a document\n\t *\n\t * Loads the document as a machine readable JSON object, or downloads the document file (PDF or XML).\n\t *\n\t * @param id The ID of the document to read.\n\t * @param format Which format to read. For e-invoices of type XRechnung, this must be XML or JSON.\n\t * For all others, it must be PDF or JSON.\n\t */\n\tpublic async readDocument(id: string): Promise<ArrayBuffer>;\n\tpublic async readDocument(id: string, format: \"json\"): Promise<Document>;\n\tpublic async readDocument(id: string, format: \"xml\"): Promise<string>;\n\tpublic async readDocument(id: string, format: \"pdf\"): Promise<ArrayBuffer>;\n\tpublic async readDocument(\n\t\tid: string,\n\t\tformat: \"json\" | \"xml\" | \"pdf\" = \"json\",\n\t) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/documents/${id}?format=${format}`,\n\t\t\t{ headers: this.headers },\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\tif (format === \"xml\") return await response.text();\n\t\tif (format === \"pdf\") return await response.arrayBuffer();\n\t\treturn (await response.json()) as Document;\n\t}\n\n\t/**\n\t * Lists all ledgers.\n\t */\n\tpublic async listLedgers(queryParams: {\n\t\tlimit?: Limit;\n\t\tcursor?: Cursor | null;\n\t}) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/ledgers?${queryParamsToString(queryParams)}`,\n\t\t\t{\n\t\t\t\theaders: this.headers,\n\t\t\t},\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as LedgerListResponse;\n\t}\n\n\t/**\n\t * Creates a new ledger. This can be used to implement automated double-entry bookkeeping into your application.\n\t */\n\tpublic async createLedger(ledger?: LedgerCreateRequest) {\n\t\tconst response = await fetch(`${this.baseUrl}/ledgers`, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: this.headers,\n\t\t\tbody: JSON.stringify(ledger ?? {}),\n\t\t});\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as Ledger;\n\t}\n\n\t/**\n\t * Deletes a given ledger together with all associated accounts and transactions. This cannot be undone.\n\t *\n\t * @param ledgerId ID of the ledger to delete.\n\t */\n\tpublic async deleteLedger(ledgerId: string) {\n\t\tconst response = await fetch(`${this.baseUrl}/ledgers/${ledgerId}`, {\n\t\t\tmethod: \"DELETE\",\n\t\t\theaders: this.headers,\n\t\t});\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as Ledger;\n\t}\n\n\t/**\n\t * Lists all accounts associated with a given ledger.\n\t */\n\tpublic async listLedgerAccounts(\n\t\tledgerId: string,\n\t\tqueryParams: {\n\t\t\tlimit?: Limit;\n\t\t\tcursor?: Cursor | null;\n\t\t},\n\t) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/ledgers/${ledgerId}/accounts?${queryParamsToString(queryParams)}`,\n\t\t\t{ headers: this.headers },\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as LedgerAccountListResponse;\n\t}\n\n\t/**\n\t * 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.\n\t *\n\t * @param ledgerId ID of the ledger to create a new account for.\n\t * @param account Details about the new account to be created.\n\t */\n\tpublic async createLedgerAccount(\n\t\tledgerId: string,\n\t\taccount: LedgerAccountCreateRequest,\n\t) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/ledgers/${ledgerId}/accounts`,\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: this.headers,\n\t\t\t\tbody: JSON.stringify(account),\n\t\t\t},\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as LedgerAccount;\n\t}\n\n\t/**\n\t * Lists transactions associated with a given ledger.\n\t */\n\tpublic async listLedgerTransactions(\n\t\tledgerId: string,\n\t\tqueryParams: {\n\t\t\tlimit?: Limit;\n\t\t\tcursor?: Cursor | null;\n\t\t},\n\t) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/ledgers/${ledgerId}/transactions?${queryParamsToString(queryParams)}`,\n\t\t\t{ headers: this.headers },\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as LedgerTransactionListResponse;\n\t}\n\n\t/**\n\t * Creates a new transaction on a given ledger. Once a transaction has been created it can no longer be deleted.\n\t */\n\tpublic async createLedgerTransaction(\n\t\tledgerId: string,\n\t\ttransaction: LedgerTransactionCreateRequest,\n\t) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/ledgers/${ledgerId}/transactions`,\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: this.headers,\n\t\t\t\tbody: JSON.stringify(transaction),\n\t\t\t},\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as LedgerTransaction;\n\t}\n\n\t/**\n\t * 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).\n\t *\n\t * @returns The newly created transaction that cancels out the existing transaction.\n\t */\n\tpublic async archiveLedgerTransaction(\n\t\tledgerId: string,\n\t\ttransactionNumber: number,\n\t) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/ledgers/${ledgerId}/transactions/${transactionNumber}`,\n\t\t\t{\n\t\t\t\tmethod: \"DELETE\",\n\t\t\t\theaders: this.headers,\n\t\t\t},\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as LedgerTransaction;\n\t}\n\n\t/**\n\t * Lists balances of the accounts on a given ledger when taking into account transactions of a given timeframe.\n\t */\n\tpublic async listLedgerBalances(\n\t\tledgerId: string,\n\t\tqueryParams?: {\n\t\t\tstartDate?: string;\n\t\t\tendDate?: string;\n\t\t},\n\t) {\n\t\tconst response = await fetch(\n\t\t\t`${this.baseUrl}/ledgers/${ledgerId}/balances?${queryParamsToString(queryParams)}`,\n\t\t\t{ headers: this.headers },\n\t\t);\n\t\tif (!response.ok) throw await ApiError.fromResponse(response);\n\t\treturn (await response.json()) as ListLedgerBalancesResponse;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBO,IAAM,WAAN,MAAM,UAAS;AAAA,EACrB,YACQ,QACA,MACN;AAFM;AACA;AAAA,EACL;AAAA,EAEH,aAAa,aAAa,UAAoB;AAC7C,WAAO,IAAI,UAAS,SAAS,QAAQ,MAAM,SAAS,KAAK,CAAC;AAAA,EAC3D;AACD;AAEA,SAAS,oBACR,QACC;AACD,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,IAAI;AAAA,IACV,OAAO;AAAA,MACN,OAAO,QAAQ,MAAM,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AAChD,YAAI,UAAU,KAAM,QAAO,CAAC;AAC5B,eAAO,CAAC,CAAC,KAAK,MAAM,SAAS,CAAC,CAAC;AAAA,MAChC,CAAC;AAAA,IACF;AAAA,EACD,EAAE,SAAS;AACZ;AAOO,IAAM,SAAN,MAAa;AAAA,EACnB,YAAY;AAAA,IACX;AAAA,IACA;AAAA,EACD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