-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
75 lines (65 loc) · 2.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import fetch from 'cross-fetch'
/**
* @param {string} apikey
* @param {object} options
* @param {string} options.baseURL - openai api base url, need http/https prefix, default to 'https://api.openai.com'
* @returns {Promise<{ total: number, used: number, remain: number, expiresTime: number } | { error: { code: string, message: string } }>}
*/
export async function fetchBilling(
apikey,
{ baseURL = 'https://api.openai.com' } = {}
) {
const headers = {
'Authorization': 'Bearer ' + apikey,
'Content-Type': 'application/json',
}
try {
const subscription = await fetch(`${baseURL}/dashboard/billing/subscription`, { headers }).then(res => res.json())
if (subscription.error) {
return {
error: {
code: subscription.error.code,
message: subscription.error.message,
},
}
}
const now = new Date()
const expiresTime = subscription.access_until * 1000
if (now > expiresTime) {
return {
error: {
code: 'grant_expired',
message: 'The grant for the account has expired',
},
}
}
// 如果用户绑卡,额度每月会刷新,则起始日期为本月第一天,否则为三个月前
const startDate = subscription.has_payment_method
? new Date(now.getFullYear(), now.getMonth())
: new Date(now - 90 * 24 * 60 * 60 * 1000)
const endDate = new Date(now.getTime() + 24 * 60 * 60 * 1000)
const usage = await fetch(`${baseURL}/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`, { headers }).then(res => res.json())
const total = subscription.hard_limit_usd
const used = usage.total_usage / 100
const remain = total - used
return {
total,
used,
remain,
expiresTime
}
} catch (err) {
return {
error: {
code: err.code ?? 'unknown',
message: err.message ?? err.stack,
},
}
}
}
/**
* @param {Date} date
*/
function formatDate(date) {
return date.toISOString().slice(0, 10)
}