From 0d5392e4500080854ed97b085a465cf36d2dfed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Thu, 7 Nov 2024 17:53:20 +0100 Subject: [PATCH] Port the `Usage` class --- src/usage.ts => lib/usage.d.ts | 16 ++-------------- src/usage.coffee | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 14 deletions(-) rename src/usage.ts => lib/usage.d.ts (69%) create mode 100644 src/usage.coffee diff --git a/src/usage.ts b/lib/usage.d.ts similarity index 69% rename from src/usage.ts rename to lib/usage.d.ts index 0ddd220..580a57d 100644 --- a/src/usage.ts +++ b/lib/usage.d.ts @@ -27,26 +27,14 @@ export class Usage { * Creates a new usage. * @param options An object providing values to initialize this instance. */ - constructor(options: UsageOptions = {}) { - this.limit = options.limit ?? -1; - this.percentage = options.percentage ?? 0; - this.throttled = options.throttled ?? false; - this.usage = options.usage ?? 0; - } + constructor(options?: UsageOptions); /** * Creates a new usage from the specified JSON object. * @param json A JSON object representing a usage. * @returns The instance corresponding to the specified JSON object. */ - static fromJson(json: Record): Usage { - return new this({ - limit: Number.isInteger(json.limit) ? json.limit as number : -1, - percentage: typeof json.percentage == "number" ? json.percentage : 0, - throttled: typeof json.throttled == "boolean" ? json.throttled : false, - usage: Number.isInteger(json.usage) ? json.usage as number : 0 - }); - } + static fromJson(json: Record): Usage; } /** diff --git a/src/usage.coffee b/src/usage.coffee new file mode 100644 index 0000000..7ecbcc7 --- /dev/null +++ b/src/usage.coffee @@ -0,0 +1,24 @@ +# Provides API usage for a given month. +export class Usage + + # Creates a new usage. + constructor: (options = {}) -> + + # The number of monthly API calls your plan entitles you to. + @limit = options.limit ? (-1) + + # The percentage of the limit used since the beginning of the month. + @percentage = options.percentage ? 0 + + # Value indicating whether the requests are being throttled for having consistently gone over the limit. + @throttled = options.throttled ? no + + # The number of calls (spam + ham) since the beginning of the month. + @usage = options.usage ? 0 + + # Creates a new usage from the specified JSON object. + @fromJson: (json) -> new @ + limit: if Number.isInteger(json.limit) then json.limit else -1 + percentage: if typeof json.percentage == "number" then json.percentage else 0 + throttled: if typeof json.throttled == "boolean" then json.throttled else no + usage: if Number.isInteger(json.usage) then json.usage else 0