Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions docs/lib/demo-credits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,21 @@ function hasPaymentRequiredCode(value: unknown, depth = 0): boolean {
return false;
}

function hasCreditsExhaustedMessage(value: unknown, depth = 0): boolean {
if (depth > 4 || value == null) return false;

if (typeof value === "string") {
return /credit|quota|rate[\s_-]*limit|too many requests|insufficient funds/i.test(value);
}

if (typeof value !== "object") return false;

return Object.values(value).some((child) => hasCreditsExhaustedMessage(child, depth + 1));
}

export function isDemoCreditsExhaustedError(error: unknown, status?: number): boolean {
if (status === 402) return true;
return hasPaymentRequiredCode(error);
if (status === 402 || status === 429) return true;
return hasPaymentRequiredCode(error) || hasCreditsExhaustedMessage(error);
}

export function createDemoCreditsExhaustedResponse(): Response {
Expand Down