Skip to content
Open
Show file tree
Hide file tree
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
94 changes: 94 additions & 0 deletions docs/examples/api-key-quota-extractor-compatible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
({
request: {
url: "{{baseUrl}}/api/actions/my-usage/getMyQuota",
Comment on lines +1 to +3
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Duplicate extractor files across docs/examples and public/examples

docs/examples has 6 extractor variants while public/examples carries only 3 of them (compatible, direct, main), with identical content. Any future update that touches one location must also be manually applied to the other, which is an easy source of drift. Consider keeping a single source of truth (e.g., only public/examples) and generating or symlinking the docs copies, or at minimum document why two locations are needed.

Prompt To Fix With AI
This is a comment left during a code review.
Path: docs/examples/api-key-quota-extractor-compatible.js
Line: 1-3

Comment:
**Duplicate extractor files across `docs/examples` and `public/examples`**

`docs/examples` has 6 extractor variants while `public/examples` carries only 3 of them (compatible, direct, main), with identical content. Any future update that touches one location must also be manually applied to the other, which is an easy source of drift. Consider keeping a single source of truth (e.g., only `public/examples`) and generating or symlinking the `docs` copies, or at minimum document why two locations are needed.

How can I resolve this? If you propose a fix, please make it concise.

method: "POST",
headers: {
"Authorization": "Bearer {{apiKey}}",
"Content-Type": "application/json",
"User-Agent": "cc-switch/1.0"
},
body: "{}"
},

extractor: function(response) {
const data = response && response.ok === true && response.data && typeof response.data === "object"
? response.data
: {};

const toNumber = function(value, fallback) {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
};

const formatPercent = function(value) {
return typeof value === "number" && Number.isFinite(value) ? value + "%" : "-";
};

const toBoolean = function(value, fallback) {
return typeof value === "boolean" ? value : fallback;
};

const round2 = function(value) {
return Math.round(value * 100) / 100;
};

const percent = function(used, total) {
return total > 0 ? round2((used / total) * 100) : null;
};

const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object"
? data.quotaWindows
: {};
const fiveHour = quotaWindows.fiveHour || {};
const daily = quotaWindows.daily || {};
const weekly = quotaWindows.weekly || {};
const monthly = quotaWindows.monthly || {};
const total = quotaWindows.total || {};

const limitMonthlyUsd = toNumber(data.limitMonthlyUsd, null);
const limitTotalUsd = toNumber(data.limitTotalUsd, limitMonthlyUsd);
const usedTotalUsd = toNumber(data.usedTotalUsd, toNumber(data.usedMonthlyUsd, 0));
const remainingTotalUsd = limitTotalUsd === null ? null : round2(Math.max(limitTotalUsd - usedTotalUsd, 0));

const limitDailyUsd = toNumber(data.limitDailyUsd, null);
const usedDailyUsd = toNumber(data.usedDailyUsd, 0);
const remainingDailyUsd = limitDailyUsd === null ? null : round2(Math.max(limitDailyUsd - usedDailyUsd, 0));

const limit5hUsd = toNumber(data.limit5hUsd, null);
const used5hUsd = toNumber(data.used5hUsd, 0);
const remaining5hUsd = limit5hUsd === null ? null : round2(Math.max(limit5hUsd - used5hUsd, 0));

const isValid =
response &&
response.ok === true &&
toBoolean(data.keyIsEnabled, true) &&
toBoolean(data.userIsEnabled, true);

return {
isValid: !!isValid,
invalidMessage: isValid ? undefined : "套餐不可用",
planName: "Total Quota",
unit: typeof data.unit === "string" ? data.unit : "USD",
remaining: toNumber(total.remainingUsd, remainingTotalUsd),
total: toNumber(total.limitUsd, limitTotalUsd),
used: toNumber(total.usedUsd, usedTotalUsd),
todayUsed: toNumber(data.todayUsedUsd, toNumber(daily.usedUsd, usedDailyUsd)),
todayRemaining: toNumber(data.todayRemainingUsd, toNumber(daily.remainingUsd, remainingDailyUsd)),
todayUsedPercent: toNumber(data.todayUsedPercent, toNumber(daily.usedPercent, percent(usedDailyUsd, limitDailyUsd))),
todayRemainingPercent: toNumber(
data.todayRemainingPercent,
toNumber(daily.remainingPercent, percent(remainingDailyUsd || 0, limitDailyUsd))
),
remaining5h: toNumber(fiveHour.remainingUsd, remaining5hUsd),
remainingDaily: toNumber(daily.remainingUsd, remainingDailyUsd),
remainingWeekly: toNumber(weekly.remainingUsd, toNumber(data.remainingWeeklyUsd, null)),
remainingMonthly: toNumber(monthly.remainingUsd, toNumber(data.remainingMonthlyUsd, null)),
remainingTotal: toNumber(total.remainingUsd, remainingTotalUsd),
remainingPercent: toNumber(total.remainingPercent, data.remainingPercent),
extra: "5H剩余:" + formatPercent(toNumber(fiveHour.remainingPercent, percent(remaining5hUsd || 0, limit5hUsd)))
+ "/日剩余:" + formatPercent(toNumber(daily.remainingPercent, percent(remainingDailyUsd || 0, limitDailyUsd)))
+ "/周剩余:" + formatPercent(weekly.remainingPercent)
+ "/月剩余:" + formatPercent(monthly.remainingPercent)
+ "/总剩余:" + formatPercent(toNumber(total.remainingPercent, data.remainingPercent))
};
}
})
48 changes: 48 additions & 0 deletions docs/examples/api-key-quota-extractor-daily.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
({
request: {
url: "{{baseUrl}}/api/actions/my-usage/getMyQuota",
method: "POST",
headers: {
"Authorization": "Bearer {{apiKey}}",
"Content-Type": "application/json",
"User-Agent": "cc-switch/1.0"
},
body: "{}"
},

extractor: function(response) {
const data = response && response.ok === true && response.data && typeof response.data === "object"
? response.data
: {};

const toNumber = function(value, fallback) {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
};

const toBoolean = function(value, fallback) {
return typeof value === "boolean" ? value : fallback;
};

const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object"
? data.quotaWindows
: {};
const daily = quotaWindows.daily || {};

return {
ok: response && response.ok === true,
isValid: toBoolean(data.keyIsEnabled, true) && toBoolean(data.userIsEnabled, true),
planName: "Daily Quota",
remaining: toNumber(daily.remainingUsd, toNumber(data.remainingDailyUsd, null)),
total: toNumber(daily.limitUsd, toNumber(data.limitDailyUsd, null)),
used: toNumber(daily.usedUsd, toNumber(data.usedDailyUsd, 0)),
usedPercent: toNumber(daily.usedPercent, toNumber(data.todayUsedPercent, null)),
remainingPercent: toNumber(daily.remainingPercent, toNumber(data.todayRemainingPercent, null)),
unit: typeof data.unit === "string" ? data.unit : "USD",
keyName: typeof data.keyName === "string" ? data.keyName : null,
userName: typeof data.userName === "string" ? data.userName : null,
providerGroup: typeof data.providerGroup === "string" ? data.providerGroup : null,
resetMode: typeof data.resetMode === "string" ? data.resetMode : null,
resetTime: typeof data.resetTime === "string" ? data.resetTime : null
};
}
})
64 changes: 64 additions & 0 deletions docs/examples/api-key-quota-extractor-direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
({
request: {
url: "{{baseUrl}}/api/actions/my-usage/getMyQuota",
method: "POST",
headers: {
"Authorization": "Bearer {{apiKey}}",
"Content-Type": "application/json",
"User-Agent": "cc-switch/1.0"
},
body: "{}"
},

extractor: function(response) {
const data = response && response.ok === true && response.data && typeof response.data === "object"
? response.data
: {};
const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object"
? data.quotaWindows
: {};
const fiveHour = quotaWindows.fiveHour || {};
const daily = quotaWindows.daily || {};
const weekly = quotaWindows.weekly || {};
const monthly = quotaWindows.monthly || {};
const total = quotaWindows.total || {};
const formatPercent = function(value) {
return typeof value === "number" && Number.isFinite(value) ? value + "%" : "-";
};

const toBoolean = function(value, fallback) {
return typeof value === "boolean" ? value : fallback;
};

const isValid =
response &&
response.ok === true &&
toBoolean(data.keyIsEnabled, true) &&
toBoolean(data.userIsEnabled, true);

return {
isValid: !!isValid,
invalidMessage: isValid ? undefined : "套餐不可用",
planName: "Total Quota",
unit: typeof data.unit === "string" ? data.unit : "USD",
remaining: total.remainingUsd,
total: total.limitUsd,
used: total.usedUsd,
todayUsed: data.todayUsedUsd,
todayRemaining: data.todayRemainingUsd,
todayUsedPercent: data.todayUsedPercent,
todayRemainingPercent: data.todayRemainingPercent,
remaining5h: fiveHour.remainingUsd,
remainingDaily: daily.remainingUsd,
remainingWeekly: weekly.remainingUsd,
remainingMonthly: monthly.remainingUsd,
remainingTotal: total.remainingUsd,
remainingPercent: data.remainingPercent,
extra: "5H剩余:" + formatPercent(fiveHour.remainingPercent)
+ "/日剩余:" + formatPercent(daily.remainingPercent)
+ "/周剩余:" + formatPercent(weekly.remainingPercent)
+ "/月剩余:" + formatPercent(monthly.remainingPercent)
+ "/总剩余:" + formatPercent(total.remainingPercent)
};
}
})
48 changes: 48 additions & 0 deletions docs/examples/api-key-quota-extractor-total.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
({
request: {
url: "{{baseUrl}}/api/actions/my-usage/getMyQuota",
method: "POST",
headers: {
"Authorization": "Bearer {{apiKey}}",
"Content-Type": "application/json",
"User-Agent": "cc-switch/1.0"
},
body: "{}"
},

extractor: function(response) {
const data = response && response.ok === true && response.data && typeof response.data === "object"
? response.data
: {};

const toNumber = function(value, fallback) {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
};

const toBoolean = function(value, fallback) {
return typeof value === "boolean" ? value : fallback;
};

const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object"
? data.quotaWindows
: {};
const total = quotaWindows.total || {};

return {
ok: response && response.ok === true,
isValid: toBoolean(data.keyIsEnabled, true) && toBoolean(data.userIsEnabled, true),
planName: "Total Quota",
remaining: toNumber(total.remainingUsd, toNumber(data.remainingTotalUsd, null)),
total: toNumber(total.limitUsd, toNumber(data.limitTotalUsd, null)),
used: toNumber(total.usedUsd, toNumber(data.usedTotalUsd, 0)),
usedPercent: toNumber(total.usedPercent, null),
remainingPercent: toNumber(total.remainingPercent, toNumber(data.remainingPercent, null)),
unit: typeof data.unit === "string" ? data.unit : "USD",
keyName: typeof data.keyName === "string" ? data.keyName : null,
userName: typeof data.userName === "string" ? data.userName : null,
providerGroup: typeof data.providerGroup === "string" ? data.providerGroup : null,
resetMode: typeof data.resetMode === "string" ? data.resetMode : null,
resetTime: typeof data.resetTime === "string" ? data.resetTime : null
};
}
})
52 changes: 52 additions & 0 deletions docs/examples/api-key-quota-extractor-weekly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
({
request: {
url: "{{baseUrl}}/api/actions/my-usage/getMyQuota",
method: "POST",
headers: {
"Authorization": "Bearer {{apiKey}}",
"Content-Type": "application/json",
"User-Agent": "cc-switch/1.0"
},
body: "{}"
},

extractor: function(response) {
const data = response && response.ok === true && response.data && typeof response.data === "object"
? response.data
: {};

const toNumber = function(value, fallback) {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
};

const toBoolean = function(value, fallback) {
return typeof value === "boolean" ? value : fallback;
};

const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object"
? data.quotaWindows
: {};
const weekly = quotaWindows.weekly || {};

return {
isValid: !!(
response &&
response.ok === true &&
toBoolean(data.keyIsEnabled, true) &&
toBoolean(data.userIsEnabled, true)
),
planName: "Weekly Quota",
remaining: toNumber(weekly.remainingUsd, toNumber(data.remainingWeeklyUsd, null)),
total: toNumber(weekly.limitUsd, toNumber(data.limitWeeklyUsd, null)),
used: toNumber(weekly.usedUsd, toNumber(data.usedWeeklyUsd, 0)),
usedPercent: toNumber(weekly.usedPercent, null),
remainingPercent: toNumber(weekly.remainingPercent, null),
unit: typeof data.unit === "string" ? data.unit : "USD",
keyName: typeof data.keyName === "string" ? data.keyName : null,
userName: typeof data.userName === "string" ? data.userName : null,
providerGroup: typeof data.providerGroup === "string" ? data.providerGroup : null,
resetMode: typeof data.resetMode === "string" ? data.resetMode : null,
resetTime: typeof data.resetTime === "string" ? data.resetTime : null
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
})
67 changes: 67 additions & 0 deletions docs/examples/api-key-quota-extractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
({
request: {
url: "{{baseUrl}}/api/actions/my-usage/getMyQuota",
method: "POST",
headers: {
"Authorization": "Bearer {{apiKey}}",
"Content-Type": "application/json",
"User-Agent": "cc-switch/1.0"
},
body: "{}"
},

extractor: function(response) {
const data = response && response.ok === true && response.data && typeof response.data === "object"
? response.data
: {};

const toNumber = function(value, fallback) {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
};

const formatPercent = function(value) {
return typeof value === "number" && Number.isFinite(value) ? value + "%" : "-";
};

const toBoolean = function(value, fallback) {
return typeof value === "boolean" ? value : fallback;
};

const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object"
? data.quotaWindows
: {};
const fiveHour = quotaWindows.fiveHour || {};
const daily = quotaWindows.daily || {};
const weekly = quotaWindows.weekly || {};
const monthly = quotaWindows.monthly || {};
const total = quotaWindows.total || {};

const isValid =
response &&
response.ok === true &&
toBoolean(data.keyIsEnabled, true) &&
toBoolean(data.userIsEnabled, true);

return {
isValid: !!isValid,
invalidMessage: isValid ? undefined : "套餐不可用",
remaining: toNumber(total.remainingUsd, toNumber(data.remainingTotalUsd, null)),
unit: typeof data.unit === "string" ? data.unit : "USD",
planName: "Total Quota",
total: toNumber(total.limitUsd, toNumber(data.limitTotalUsd, null)),
used: toNumber(total.usedUsd, toNumber(data.usedTotalUsd, 0)),
todayUsed: toNumber(data.todayUsedUsd, toNumber(daily.usedUsd, 0)),
todayRemaining: toNumber(data.todayRemainingUsd, toNumber(daily.remainingUsd, null)),
remainingWeekly: toNumber(weekly.remainingUsd, toNumber(data.remainingWeeklyUsd, null)),
remainingMonthly: toNumber(monthly.remainingUsd, toNumber(data.remainingMonthlyUsd, null)),
remainingTotal: toNumber(total.remainingUsd, toNumber(data.remainingTotalUsd, null)),
remaining5h: toNumber(fiveHour.remainingUsd, toNumber(data.remaining5hUsd, null)),
remainingDaily: toNumber(daily.remainingUsd, toNumber(data.remainingDailyUsd, null)),
extra: "5H剩余:" + formatPercent(fiveHour.remainingPercent)
+ "/日剩余:" + formatPercent(toNumber(daily.remainingPercent, data.todayRemainingPercent))
+ "/周剩余:" + formatPercent(weekly.remainingPercent)
+ "/月剩余:" + formatPercent(monthly.remainingPercent)
+ "/总剩余:" + formatPercent(toNumber(total.remainingPercent, data.remainingPercent))
};
}
})
Loading