@charmland/pi-hyper-provider 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/credits.ts +17 -8
package/package.json
CHANGED
package/src/credits.ts
CHANGED
|
@@ -20,21 +20,30 @@ const CREDITS_RETRY_DELAY_MS_INITIAL = 5_000;
|
|
|
20
20
|
const CREDITS_RETRY_DELAY_MS_MAX = 5 * 60_000;
|
|
21
21
|
const CREDITS_RETRY_EXPONENT_MAX = 6;
|
|
22
22
|
|
|
23
|
-
const CreditsPayloadSchema = Type.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
const CreditsPayloadSchema = Type.Union([
|
|
24
|
+
Type.Object(
|
|
25
|
+
{
|
|
26
|
+
balance: Type.Number(),
|
|
27
|
+
},
|
|
28
|
+
{ additionalProperties: false },
|
|
29
|
+
),
|
|
30
|
+
Type.Object(
|
|
31
|
+
{
|
|
32
|
+
balance_usd: Type.Number(),
|
|
33
|
+
},
|
|
34
|
+
{ additionalProperties: false },
|
|
35
|
+
),
|
|
36
|
+
]);
|
|
29
37
|
const CreditsPayloadValidator = Compile(CreditsPayloadSchema);
|
|
30
38
|
|
|
31
|
-
async function fetchCredits(apiKey: string, signal: AbortSignal): Promise<number> {
|
|
39
|
+
async function fetchCredits(apiKey: string, signal: AbortSignal): Promise<number | undefined> {
|
|
32
40
|
const payload = await httpClient.fetchJson(`${HYPER_API_BASE_URL}/credits`, {
|
|
33
41
|
headers: hyperJsonHeaders({ Authorization: `Bearer ${apiKey}` }),
|
|
34
42
|
signal,
|
|
35
43
|
timeoutMs: CREDITS_FETCH_TIMEOUT_MS,
|
|
36
44
|
});
|
|
37
|
-
|
|
45
|
+
const credits = parseSchema(CreditsPayloadValidator, payload, "Hyper /credits response");
|
|
46
|
+
return "balance" in credits ? credits.balance : undefined;
|
|
38
47
|
}
|
|
39
48
|
|
|
40
49
|
function isTransientCreditError(error: unknown): boolean {
|