Skip to content

Commit

Permalink
feat: throw an timeout error for when an timeout is happened
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderKoen committed Aug 20, 2024
1 parent 4753ba3 commit eea9212
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 23 deletions.
17 changes: 4 additions & 13 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 11 additions & 4 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/errors/timeoutError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class TimeoutError extends Error {
constructor(message: string, public readonly retryAfter: Date) {
super(message);
this.name = "TimeoutError";
}
}
7 changes: 6 additions & 1 deletion src/moneybird.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.";
Expand Down
28 changes: 25 additions & 3 deletions src/salesInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ export class SalesInvoice {
* @returns A sales invoice
*/
public async send(data: ISalesInvoiceSending = {}): Promise<SalesInvoice> {
return this.HTTP.PATCH<SalesInvoice>("send_invoice", {
sales_invoice_sending: data,
});
return new SalesInvoice(
this.moneybird,
this.administration,
await this.HTTP.PATCH<ISalesInvoice>("send_invoice", {
sales_invoice_sending: data,
})
);
}

/**
Expand Down Expand Up @@ -142,4 +146,22 @@ export class SalesInvoice {
responseType: "arraybuffer",
});
}

/**
* Update the sales invoice
*/
public async update(data: Partial<ISalesInvoice>): Promise<SalesInvoice> {
return new SalesInvoice(
this.moneybird,
this.administration,
await this.HTTP.PATCH<ISalesInvoice>("", { sales_invoice: data })
);
}

/**
* Delete the sales invoice
*/
public async delete(): Promise<void> {
return this.HTTP.DELETE<void>("");
}
}

0 comments on commit eea9212

Please sign in to comment.