uapi-browser-sdk 0.1.14 → 0.1.15
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/README.md +1 -60
- package/dist/src/errors.d.ts +0 -16
- package/dist/src/errors.js +0 -22
- package/dist/src/index.d.ts +471 -2
- package/dist/src/index.js +504 -91
- package/package.json +1 -1
- package/src/errors.ts +0 -39
- package/src/index.ts +901 -4
package/README.md
CHANGED
|
@@ -19,12 +19,10 @@ npm i uapi-browser-sdk
|
|
|
19
19
|
import { UapiClient } from 'uapi-browser-sdk'
|
|
20
20
|
|
|
21
21
|
const client = new UapiClient('https://uapis.cn', 'YOUR_API_KEY')
|
|
22
|
-
const result = await client.
|
|
22
|
+
const result = await client.social.getSocialQqUserinfo({ qq: '10001' })
|
|
23
23
|
console.log(result)
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
这个接口默认只要传 `type` 就可以拿当前热榜。`time`、`keyword`、`timeStart`、`timeEnd`、`limit`、`sources` 都是按场景再传的可选参数。
|
|
27
|
-
|
|
28
26
|
## CDN 引入
|
|
29
27
|
|
|
30
28
|
`uapi-browser-sdk` 发布在 npm,因而可以直接通过常见 CDN 以 ES Module 方式加载。建议在生产环境固定版本号(`@latest` 仅用于快速试用)。
|
|
@@ -72,63 +70,6 @@ SDK 采用原生 `fetch`,自动补上 `Authorization` 头且不依赖任何 No
|
|
|
72
70
|
|
|
73
71
|
如果你需要查看字段细节或内部逻辑,仓库中的 `./internal` 目录同步保留了由 `openapi-generator` 生成的完整结构体,随时可供参考。
|
|
74
72
|
|
|
75
|
-
## 响应元信息
|
|
76
|
-
|
|
77
|
-
每次请求完成后,SDK 会自动把响应 Header 解析成结构化的 `ResponseMeta`,你不用自己拆原始字符串。
|
|
78
|
-
|
|
79
|
-
成功时可以通过 `client.lastResponseMeta` 读取,失败时可以通过 `err.meta` 读取,两条路径拿到的是同一套字段。
|
|
80
|
-
|
|
81
|
-
```ts
|
|
82
|
-
import { UapiClient, UapiError } from 'uapi-browser-sdk'
|
|
83
|
-
|
|
84
|
-
const client = new UapiClient('https://uapis.cn', 'YOUR_API_KEY')
|
|
85
|
-
|
|
86
|
-
// 成功路径
|
|
87
|
-
await client.social.getSocialQqUserinfo({ qq: '10001' })
|
|
88
|
-
const meta = client.lastResponseMeta
|
|
89
|
-
if (meta) {
|
|
90
|
-
console.log('这次请求原价:', meta.creditsRequested ?? 0, '积分')
|
|
91
|
-
console.log('这次实际扣费:', meta.creditsCharged ?? 0, '积分')
|
|
92
|
-
console.log('特殊计价:', meta.creditsPricing ?? '原价')
|
|
93
|
-
console.log('余额剩余:', meta.balanceRemainingCents ?? 0, '分')
|
|
94
|
-
console.log('资源包剩余:', meta.quotaRemainingCredits ?? 0, '积分')
|
|
95
|
-
console.log('当前有效额度桶:', meta.activeQuotaBuckets ?? 0)
|
|
96
|
-
console.log('额度用空即停:', meta.stopOnEmpty ?? false)
|
|
97
|
-
console.log('Key QPS:', meta.billingKeyRateRemaining ?? 0, '/', meta.billingKeyRateLimit ?? 0, meta.billingKeyRateUnit ?? 'req')
|
|
98
|
-
console.log('Request ID:', meta.requestId)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// 失败路径
|
|
102
|
-
try {
|
|
103
|
-
await client.social.getSocialQqUserinfo({ qq: '10001' })
|
|
104
|
-
} catch (err) {
|
|
105
|
-
if (err instanceof UapiError && err.meta) {
|
|
106
|
-
console.log('Retry-After 秒数:', err.meta.retryAfterSeconds ?? null)
|
|
107
|
-
console.log('Retry-After 原始值:', err.meta.retryAfterRaw ?? null)
|
|
108
|
-
console.log('访客 QPS:', err.meta.visitorRateRemaining ?? 0, '/', err.meta.visitorRateLimit ?? 0)
|
|
109
|
-
console.log('Request ID:', err.meta.requestId)
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
常用字段一览:
|
|
115
|
-
|
|
116
|
-
| 字段 | 说明 |
|
|
117
|
-
|------|------|
|
|
118
|
-
| `creditsRequested` | 这次请求原本要扣多少积分,也就是请求价 |
|
|
119
|
-
| `creditsCharged` | 这次请求实际扣了多少积分 |
|
|
120
|
-
| `creditsPricing` | 特殊计价原因,例如缓存半价 `cache-hit-half-price` |
|
|
121
|
-
| `balanceRemainingCents` | 账户余额剩余(分) |
|
|
122
|
-
| `quotaRemainingCredits` | 资源包剩余积分 |
|
|
123
|
-
| `activeQuotaBuckets` | 当前还有多少个有效额度桶参与计费 |
|
|
124
|
-
| `stopOnEmpty` | 额度耗尽后是否直接停止服务 |
|
|
125
|
-
| `retryAfterSeconds` / `retryAfterRaw` | 限流后的等待时长;当服务端返回 HTTP 时间字符串时看 `retryAfterRaw` |
|
|
126
|
-
| `requestId` | 请求唯一 ID,排障时使用 |
|
|
127
|
-
| `billingKeyRateLimit` / `billingKeyRateRemaining` | Billing Key 当前 QPS 规则的上限与剩余 |
|
|
128
|
-
| `billingIpRateLimit` / `billingIpRateRemaining` | Billing Key 单 IP 当前 QPS 规则的上限与剩余 |
|
|
129
|
-
| `visitorRateLimit` / `visitorRateRemaining` | 访客当前 QPS 规则的上限与剩余 |
|
|
130
|
-
| `rateLimitPolicies` / `rateLimits` | 完整结构化限流策略数据 |
|
|
131
|
-
|
|
132
73
|
## 错误模型概览
|
|
133
74
|
|
|
134
75
|
| HTTP 状态码 | SDK 错误类型 | 附加信息 |
|
package/dist/src/errors.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ export interface RateLimitStateEntry {
|
|
|
12
12
|
}
|
|
13
13
|
export interface ResponseMeta {
|
|
14
14
|
requestId?: string;
|
|
15
|
-
retryAfterRaw?: string;
|
|
16
15
|
retryAfterSeconds?: number;
|
|
17
16
|
debitStatus?: string;
|
|
18
17
|
creditsRequested?: number;
|
|
@@ -30,21 +29,6 @@ export interface ResponseMeta {
|
|
|
30
29
|
quotaRemainingCredits?: number;
|
|
31
30
|
visitorQuotaLimitCredits?: number;
|
|
32
31
|
visitorQuotaRemainingCredits?: number;
|
|
33
|
-
billingKeyRateLimit?: number;
|
|
34
|
-
billingKeyRateRemaining?: number;
|
|
35
|
-
billingKeyRateUnit?: string;
|
|
36
|
-
billingKeyRateWindowSeconds?: number;
|
|
37
|
-
billingKeyRateResetAfterSeconds?: number;
|
|
38
|
-
billingIpRateLimit?: number;
|
|
39
|
-
billingIpRateRemaining?: number;
|
|
40
|
-
billingIpRateUnit?: string;
|
|
41
|
-
billingIpRateWindowSeconds?: number;
|
|
42
|
-
billingIpRateResetAfterSeconds?: number;
|
|
43
|
-
visitorRateLimit?: number;
|
|
44
|
-
visitorRateRemaining?: number;
|
|
45
|
-
visitorRateUnit?: string;
|
|
46
|
-
visitorRateWindowSeconds?: number;
|
|
47
|
-
visitorRateResetAfterSeconds?: number;
|
|
48
32
|
rawHeaders: Record<string, string>;
|
|
49
33
|
}
|
|
50
34
|
export declare class UapiError extends Error {
|
package/dist/src/errors.js
CHANGED
|
@@ -161,15 +161,8 @@ export function extractMetaFromHeaders(headers) {
|
|
|
161
161
|
resetAfterSeconds: parseNumber(item.params.t),
|
|
162
162
|
};
|
|
163
163
|
}
|
|
164
|
-
const billingKeyRatePolicy = rateLimitPolicies['billing-key-rate'];
|
|
165
|
-
const billingKeyRateState = rateLimits['billing-key-rate'];
|
|
166
|
-
const billingIpRatePolicy = rateLimitPolicies['billing-ip-rate'];
|
|
167
|
-
const billingIpRateState = rateLimits['billing-ip-rate'];
|
|
168
|
-
const visitorRatePolicy = rateLimitPolicies['visitor-rate'];
|
|
169
|
-
const visitorRateState = rateLimits['visitor-rate'];
|
|
170
164
|
return {
|
|
171
165
|
requestId: rawHeaders['x-request-id'],
|
|
172
|
-
retryAfterRaw: rawHeaders['retry-after'],
|
|
173
166
|
retryAfterSeconds: parseNumber(rawHeaders['retry-after']),
|
|
174
167
|
debitStatus: rawHeaders['uapi-debit-status'],
|
|
175
168
|
creditsRequested: parseNumber(rawHeaders['uapi-credits-requested']),
|
|
@@ -187,21 +180,6 @@ export function extractMetaFromHeaders(headers) {
|
|
|
187
180
|
quotaRemainingCredits: rateLimits['billing-quota']?.remaining,
|
|
188
181
|
visitorQuotaLimitCredits: rateLimitPolicies['visitor-quota']?.quota,
|
|
189
182
|
visitorQuotaRemainingCredits: rateLimits['visitor-quota']?.remaining,
|
|
190
|
-
billingKeyRateLimit: billingKeyRatePolicy?.quota,
|
|
191
|
-
billingKeyRateRemaining: billingKeyRateState?.remaining,
|
|
192
|
-
billingKeyRateUnit: billingKeyRatePolicy?.unit ?? billingKeyRateState?.unit,
|
|
193
|
-
billingKeyRateWindowSeconds: billingKeyRatePolicy?.windowSeconds,
|
|
194
|
-
billingKeyRateResetAfterSeconds: billingKeyRateState?.resetAfterSeconds,
|
|
195
|
-
billingIpRateLimit: billingIpRatePolicy?.quota,
|
|
196
|
-
billingIpRateRemaining: billingIpRateState?.remaining,
|
|
197
|
-
billingIpRateUnit: billingIpRatePolicy?.unit ?? billingIpRateState?.unit,
|
|
198
|
-
billingIpRateWindowSeconds: billingIpRatePolicy?.windowSeconds,
|
|
199
|
-
billingIpRateResetAfterSeconds: billingIpRateState?.resetAfterSeconds,
|
|
200
|
-
visitorRateLimit: visitorRatePolicy?.quota,
|
|
201
|
-
visitorRateRemaining: visitorRateState?.remaining,
|
|
202
|
-
visitorRateUnit: visitorRatePolicy?.unit ?? visitorRateState?.unit,
|
|
203
|
-
visitorRateWindowSeconds: visitorRatePolicy?.windowSeconds,
|
|
204
|
-
visitorRateResetAfterSeconds: visitorRateState?.resetAfterSeconds,
|
|
205
183
|
rawHeaders,
|
|
206
184
|
};
|
|
207
185
|
}
|