Skip to content

Commit 011ca54

Browse files
committed
Fix base URL normalization and README examples
1 parent 37bb0e5 commit 011ca54

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm i uapi-sdk-typescript
1818
```ts
1919
import { UapiClient } from 'uapi-sdk-typescript'
2020

21-
const client = new UapiClient('https://uapis.cn/api/v1')
21+
const client = new UapiClient('https://uapis.cn', 'YOUR_API_KEY')
2222
const result = await client.social.getSocialQqUserinfo({ qq: '10001' })
2323
console.log(result)
2424
```
@@ -46,7 +46,7 @@ HTTP 封装基于 Axios,默认 15 秒超时并自动携带 `Authorization` 头
4646
```ts
4747
import { UapiClient, UapiError } from 'uapi-sdk-typescript'
4848

49-
const client = new UapiClient('https://uapis.cn/api/v1')
49+
const client = new UapiClient('https://uapis.cn', 'YOUR_API_KEY')
5050

5151
// 成功路径
5252
const result = await client.social.getSocialQqUserinfo({ qq: '10001' })

‎src/index.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ export type { RateLimitPolicyEntry, RateLimitStateEntry, ResponseMeta } from './
66

77
type Config = { baseURL: string; token?: string; timeout?: number }
88

9+
const API_PREFIX = '/api/v1'
10+
11+
function normalizeBaseURL(baseURL: string): string {
12+
const trimmed = baseURL.replace(/\/+$/, '')
13+
return trimmed.endsWith(API_PREFIX) ? trimmed.slice(0, -API_PREFIX.length) : trimmed
14+
}
15+
916
class HTTP {
1017
private cli: AxiosInstance
1118
private lastMeta?: ResponseMeta
1219

1320
constructor(cfg: Config) {
14-
this.cli = axios.create({ baseURL: cfg.baseURL, timeout: cfg.timeout ?? 15000 })
21+
this.cli = axios.create({ baseURL: normalizeBaseURL(cfg.baseURL), timeout: cfg.timeout ?? 15000 })
1522
if (cfg.token) this.cli.defaults.headers.common['Authorization'] = `Bearer ${cfg.token}`
1623
}
1724

0 commit comments

Comments
 (0)