( data: UsageLimitData, fallback = "Your usage limit has been reached.", )
| 87 | * fields are missing or invalid. |
| 88 | */ |
| 89 | export function formatUsageLimitMessage( |
| 90 | data: UsageLimitData, |
| 91 | fallback = "Your usage limit has been reached.", |
| 92 | ): string { |
| 93 | const { spent_micros, limit_micros, resets_at } = data; |
| 94 | |
| 95 | // All structured fields must be present and valid for the |
| 96 | // detailed message. |
| 97 | if ( |
| 98 | typeof spent_micros !== "number" || |
| 99 | typeof limit_micros !== "number" || |
| 100 | typeof resets_at !== "string" || |
| 101 | !resets_at |
| 102 | ) { |
| 103 | return fallback; |
| 104 | } |
| 105 | |
| 106 | const spent = formatCostMicros(spent_micros); |
| 107 | const limit = formatCostMicros(limit_micros); |
| 108 | const resetDate = formatResetDate(resets_at); |
| 109 | |
| 110 | if (!resetDate) { |
| 111 | return `You've used ${spent} of your ${limit} limit.`; |
| 112 | } |
| 113 | |
| 114 | return `You've used ${spent} of your ${limit} limit. Resets ${resetDate}.`; |
| 115 | } |
no test coverage detected