From eea9212aaf9038e4f384d694b4db9bad169f602d Mon Sep 17 00:00:00 2001 From: UnderKoen Date: Tue, 20 Aug 2024 09:28:46 +0200 Subject: [PATCH] feat: throw an timeout error for when an timeout is happened --- example/example.js | 17 ++++------------- example/package-lock.json | 15 +++++++++++---- example/package.json | 3 ++- src/common.ts | 2 +- src/errors/timeoutError.ts | 6 ++++++ src/moneybird.ts | 7 ++++++- src/salesInvoice.ts | 28 +++++++++++++++++++++++++--- 7 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 src/errors/timeoutError.ts diff --git a/example/example.js b/example/example.js index 120d96b..e15488b 100644 --- a/example/example.js +++ b/example/example.js @@ -1,25 +1,16 @@ /* eslint-disable @typescript-eslint/no-var-requires */ const moneybird = require("@print-one/moneybird-js"); const fs = require("fs"); +const _ = require("lodash"); moneybird.instance.setOptions({ api_token: process.env.MONEYBIRD_API_KEY, }); (async () => { - const administrations = await moneybird.instance.administrations(); - for (const administration of administrations) { - const invoices = await administration.salesInvoices(); - for (const invoice of invoices) { - if (invoice.data.id === "391346733960922236") { - for (let attachment of invoice.data.attachments) { - console.log(await invoice.downloadAttachment(attachment)); - await invoice.deleteAttachment(attachment); - } - - const file = fs.readFileSync("./dummy.pdf"); - await invoice.addAttachment(file); - } + while (true) { + const administrations = await moneybird.instance.administrations(); + for (const administration of administrations) { } } })().catch(console.log); diff --git a/example/package-lock.json b/example/package-lock.json index 6f32c68..b5ca4cd 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -13,16 +13,17 @@ "devDependencies": { "@types/node": "^18.15.12", "@under_koen/bsm": "^0.0.6", - "env-cmd": "^10.1.0" + "env-cmd": "^10.1.0", + "lodash": "^4.17.21" } }, "..": { "name": "@print-one/moneybird-js", - "version": "1.5.0", + "version": "1.8.1", "license": "MIT", "dependencies": { - "debug": "^4.3.4", - "gaxios": "^5.1.0" + "axios": "^1.5.1", + "debug": "^4.3.4" }, "devDependencies": { "@semantic-release/changelog": "^6.0.3", @@ -110,6 +111,12 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "node_modules/path-key": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", diff --git a/example/package.json b/example/package.json index b2bba83..41978c7 100644 --- a/example/package.json +++ b/example/package.json @@ -10,6 +10,7 @@ "devDependencies": { "@types/node": "^18.15.12", "@under_koen/bsm": "^0.0.6", - "env-cmd": "^10.1.0" + "env-cmd": "^10.1.0", + "lodash": "^4.17.21" } } diff --git a/src/common.ts b/src/common.ts index b94912f..a2fba23 100644 --- a/src/common.ts +++ b/src/common.ts @@ -288,7 +288,7 @@ export interface IPayment { export interface IPaymentCreate { payment_date: string; - price: number; + price: number | string; price_base?: number; financial_account_id?: string; financial_mutation_id?: string; diff --git a/src/errors/timeoutError.ts b/src/errors/timeoutError.ts new file mode 100644 index 0000000..aa0fc5b --- /dev/null +++ b/src/errors/timeoutError.ts @@ -0,0 +1,6 @@ +export class TimeoutError extends Error { + constructor(message: string, public readonly retryAfter: Date) { + super(message); + this.name = "TimeoutError"; + } +} diff --git a/src/moneybird.ts b/src/moneybird.ts index 86a3cdd..3be22fa 100644 --- a/src/moneybird.ts +++ b/src/moneybird.ts @@ -3,6 +3,7 @@ import { IAdministration, MoneybirdOptions } from "./common"; import { Administration } from "./administration"; import { HTTP } from "./httpHandler"; import debug from "debug"; +import { TimeoutError } from "./errors/timeoutError"; export class Moneybird implements HTTP { private readonly client: Axios; @@ -186,7 +187,11 @@ export class Moneybird implements HTTP { break; case 429: error = "Too many requests - See Throttling information"; - break; + + throw new TimeoutError( + error, + new Date(response.headers["retry-after"] * 1000) + ); case 500: error = "Internal server error - Something went wrong while processing the request. This is unexpected behaviour and requires Moneybird to fix the scenario."; diff --git a/src/salesInvoice.ts b/src/salesInvoice.ts index 7a6eb6c..67b10fa 100644 --- a/src/salesInvoice.ts +++ b/src/salesInvoice.ts @@ -36,9 +36,13 @@ export class SalesInvoice { * @returns A sales invoice */ public async send(data: ISalesInvoiceSending = {}): Promise { - return this.HTTP.PATCH("send_invoice", { - sales_invoice_sending: data, - }); + return new SalesInvoice( + this.moneybird, + this.administration, + await this.HTTP.PATCH("send_invoice", { + sales_invoice_sending: data, + }) + ); } /** @@ -142,4 +146,22 @@ export class SalesInvoice { responseType: "arraybuffer", }); } + + /** + * Update the sales invoice + */ + public async update(data: Partial): Promise { + return new SalesInvoice( + this.moneybird, + this.administration, + await this.HTTP.PATCH("", { sales_invoice: data }) + ); + } + + /** + * Delete the sales invoice + */ + public async delete(): Promise { + return this.HTTP.DELETE(""); + } }