mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-08 19:16:37 +08:00
Update requests.ts
This commit is contained in:
parent
ffa3f33401
commit
06e072c779
@ -52,6 +52,7 @@ export function requestOpenaiClient(path: string) {
|
|||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
"Cache-Control": "no-cache",
|
||||||
path,
|
path,
|
||||||
...getHeaders(),
|
...getHeaders(),
|
||||||
},
|
},
|
||||||
@ -73,21 +74,28 @@ export async function requestChat(messages: Message[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function requestUsage() {
|
export async function requestUsage() {
|
||||||
|
const formatDate = (d: Date) =>
|
||||||
|
`${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, "0")}-${d
|
||||||
|
.getDate()
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0")}`;
|
||||||
|
const ONE_DAY = 24 * 60 * 60 * 1000;
|
||||||
|
const now = new Date(Date.now() + ONE_DAY);
|
||||||
|
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
|
||||||
|
const startDate = formatDate(startOfMonth);
|
||||||
|
const endDate = formatDate(now);
|
||||||
const res = await requestOpenaiClient(
|
const res = await requestOpenaiClient(
|
||||||
"dashboard/billing/credit_grants?_vercel_no_cache=1",
|
`dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`,
|
||||||
)(null, "GET");
|
)(null, "GET");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = (await res.json()) as {
|
const response = (await res.json()) as {
|
||||||
total_available: number;
|
total_usage: number;
|
||||||
total_granted: number;
|
|
||||||
total_used: number;
|
|
||||||
};
|
};
|
||||||
return response;
|
return Math.round(response.total_usage) / 100;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[Request usage] ", error, res.body);
|
console.error("[Request usage] ", error, res.body);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export async function requestChatStream(
|
export async function requestChatStream(
|
||||||
messages: Message[],
|
messages: Message[],
|
||||||
|
Loading…
Reference in New Issue
Block a user