34 lines
888 B
Plaintext
34 lines
888 B
Plaintext
import { HttpClient, ApiResult, ApiError } from 'common';
|
||
|
||
/**
|
||
* 后端用户接口 DTO(与后端字段约定对齐)。
|
||
*/
|
||
export class UserDto {
|
||
id: string = '';
|
||
name: string = '';
|
||
email: string = '';
|
||
}
|
||
|
||
/**
|
||
* 用户相关网络请求服务:负责发请求、解析统一响应、返回 DTO。
|
||
* TODO: BASE_URL 应改为环境注入(debug/release 分离)。
|
||
*/
|
||
export class UserApi {
|
||
private static readonly BASE_URL: string = 'https://api.example.com';
|
||
|
||
private readonly http: HttpClient;
|
||
|
||
constructor(http: HttpClient) {
|
||
this.http = http;
|
||
}
|
||
|
||
async fetchUser(id: string): Promise<UserDto> {
|
||
const raw = await this.http.get(`${UserApi.BASE_URL}/api/user/${id}`);
|
||
const result = JSON.parse(raw) as ApiResult<UserDto>;
|
||
if (!result.isSuccess()) {
|
||
throw ApiError.business(result.code, result.message);
|
||
}
|
||
return result.data;
|
||
}
|
||
}
|