@@ -154,6 +154,10 @@ export class JikeClient {
154
154
return new JikeUser < M > ( this , username )
155
155
}
156
156
157
+ /**
158
+ * 获取自身用户
159
+ * @returns {@link JikeUser } 实例
160
+ */
157
161
getSelf ( ) {
158
162
return new JikeUser < true > ( this , undefined )
159
163
}
@@ -187,6 +191,9 @@ export class JikeClient {
187
191
)
188
192
}
189
193
194
+ /**
195
+ * 刷新 access token
196
+ */
190
197
async renewToken ( ) {
191
198
if ( ! this . #refreshToken)
192
199
throw new Error ( '登录状态已失效,请重新获取 access-token!' )
@@ -202,4 +209,45 @@ export class JikeClient {
202
209
result . data [ `x-${ this . #config. endpointId } -refresh-token` ]
203
210
this . accessToken = result . data [ `x-${ this . #config. endpointId } -access-token` ]
204
211
}
212
+
213
+ /**
214
+ * 转换为 JSON 数据
215
+ */
216
+ async toJSON ( ) : Promise < JikeClientJSON > {
217
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
218
+ const { beforeRetry, ...config } = this . #config
219
+ const profile = await this . getSelf ( ) . queryProfile ( )
220
+ return {
221
+ ...config ,
222
+ accessToken : this . accessToken ,
223
+ refreshToken : this . refreshToken ,
224
+ userId : profile . user . id ,
225
+ username : profile . user . username ,
226
+ screenName : profile . user . screenName ,
227
+ }
228
+ }
229
+
230
+ /**
231
+ * 序列化
232
+ * @param space 缩进空格数
233
+ */
234
+ async serialize ( space = 0 ) : Promise < string > {
235
+ return JSON . stringify ( await this . toJSON ( ) , undefined , space )
236
+ }
237
+
238
+ /**
239
+ * 反序列化
240
+ * @param data 数据
241
+ */
242
+ static deserialize ( data : string ) : JikeClient {
243
+ const json : JikeClientJSON = JSON . parse ( data )
244
+ return new JikeClient ( { ...json } )
245
+ }
246
+ }
247
+
248
+ export interface JikeClientJSON extends Omit < ApiConfigResolved , 'beforeRetry' > {
249
+ refreshToken : string
250
+ userId : string
251
+ username : string
252
+ screenName : string
205
253
}
0 commit comments