Skip to content

Commit fa5fd8e

Browse files
committed
feat(jike-client): add loginWithPassword
1 parent 26b565e commit fa5fd8e

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

src/client/client.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ export class JikeClient {
110110
)!
111111
}
112112

113+
/**
114+
* 密码登录
115+
* @param areaCode 区号,如 `+86`
116+
* @param mobile 手机号
117+
* @param password 密码
118+
* @throws {@link RequestFailureError} 请求失败错误
119+
*/
120+
async loginWithPassword(
121+
areaCode: string | number,
122+
mobile: string,
123+
password: string
124+
) {
125+
const result = await this.#client.users.loginWithPhoneAndPassword(
126+
resolveAreaCode(areaCode),
127+
mobile,
128+
password
129+
)
130+
if (!isSuccess(result)) throwRequestFailureError(result, '登录')
131+
this.#refreshToken = result.headers.get(
132+
`x-${this.#config.endpointId}-refresh-token`
133+
)!
134+
this.accessToken = result.headers.get(
135+
`x-${this.#config.endpointId}-access-token`
136+
)!
137+
}
138+
113139
/**
114140
* 获取用户
115141
* @param username 用户名

src/request.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export interface ApiConfigResolved {
1616
endpointUrl: string
1717
/** APP ID */
1818
bundleId: string
19+
/** App 版本 */
20+
appVersion: string
1921
/** 构建 ID */
2022
buildNo: string
2123
/** `access-token` */
@@ -32,7 +34,12 @@ export interface ApiConfigResolved {
3234
export type ApiConfig = Partial<ApiConfigResolved> &
3335
Pick<
3436
ApiConfigResolved,
35-
'endpointId' | 'endpointUrl' | 'bundleId' | 'buildNo' | 'userAgent'
37+
| 'endpointId'
38+
| 'endpointUrl'
39+
| 'bundleId'
40+
| 'appVersion'
41+
| 'buildNo'
42+
| 'userAgent'
3643
>
3744

3845
/**
@@ -100,6 +107,7 @@ export const resolveKyOptions = (): Options => {
100107
`x-${apiConfig.endpointId}-device-properties`,
101108
JSON.stringify({ idfv: apiConfig.idfv })
102109
)
110+
req.headers.set(`App-Version`, apiConfig.appVersion)
103111
;(req as any).highWaterMark = 1024 * 1024
104112
},
105113
],

tests/jike-client/auth.ts renamed to tests/jike-client/auth.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ import { config, refreshToken } from '../config'
55
describe('login should work', () => {
66
const client = new JikeClient(config)
77
const mobile = process.env.MOBILE
8+
const code = process.env.CODE
89

910
it('sendSmsCode should work', async () => {
10-
if (!mobile) return
11-
client.sendSmsCode(86, mobile)
11+
if (!mobile || code) return
12+
await client.sendSmsCode(86, mobile)
13+
})
14+
15+
it('loginWithSmsCode should work', async () => {
16+
if (!mobile || !code) return
17+
await client.loginWithSmsCode(86, mobile, code)
1218
})
1319

1420
it('loginWithSmsCode should throw an error', async () => {
1521
if (!mobile) return
16-
try {
17-
await client.loginWithSmsCode(86, mobile, '123123')
18-
} catch (err: unknown) {
19-
expect(err).instanceOf(RequestFailureError)
20-
if (err instanceof RequestFailureError)
21-
expect(err.message).oneOf([
22-
'验证码已失效',
23-
'错误登录次数过多,请稍后再试',
24-
])
25-
}
22+
23+
const err: RequestFailureError = await client
24+
.loginWithSmsCode(86, mobile, '123123')
25+
.catch((err) => err)
26+
expect(err).instanceOf(RequestFailureError)
27+
expect(err.message).oneOf(['验证码已失效', '错误登录次数过多,请稍后再试'])
2628
})
2729
})
2830

0 commit comments

Comments
 (0)