c22b90c7f4
Add barrelsby as a dev dependency with a `generate-barrels` npm script and a `barrelesby.json` config targeting `./src`. This automates creation of barrel/index files to simplify imports across the project.
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
/**
|
|
* 登录响应
|
|
* 原始 Dart: LoginResponse (lib/data/models/auth/auth_response.dart)
|
|
*/
|
|
import { User } from "../user/user";
|
|
|
|
export class LoginResponse {
|
|
readonly user: User;
|
|
readonly token: string;
|
|
readonly refreshToken: string;
|
|
|
|
constructor(params: { user: User; token: string; refreshToken?: string }) {
|
|
this.user = params.user;
|
|
this.token = params.token;
|
|
this.refreshToken = params.refreshToken ?? "";
|
|
Object.freeze(this);
|
|
}
|
|
|
|
toJson(): Record<string, unknown> {
|
|
return {
|
|
user: this.user.toJson(),
|
|
token: this.token,
|
|
refreshToken: this.refreshToken,
|
|
};
|
|
}
|
|
|
|
static fromJson(json: Record<string, unknown>): LoginResponse {
|
|
const token = json.token;
|
|
if (typeof token !== "string") {
|
|
throw new Error("LoginResponse.token is required and must be a string");
|
|
}
|
|
const userJson = json.user;
|
|
if (typeof userJson !== "object" || userJson === null) {
|
|
throw new Error("LoginResponse.user is required and must be an object");
|
|
}
|
|
return new LoginResponse({
|
|
user: User.fromJson(userJson as Record<string, unknown>),
|
|
token,
|
|
refreshToken:
|
|
typeof json.refreshToken === "string" ? json.refreshToken : undefined,
|
|
});
|
|
}
|
|
}
|