refactor: 封装请求
This commit is contained in:
18
src/types/api/envelope.ts
Normal file
18
src/types/api/envelope.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/** 后端 `{ code, msg, data }`;约定 `code === 0` 表示成功 */
|
||||
export type ApiEnvelope<T = unknown> = {
|
||||
code: number;
|
||||
msg: string;
|
||||
data: T;
|
||||
};
|
||||
|
||||
export function isApiEnvelope(v: unknown): v is ApiEnvelope {
|
||||
if (v === null || typeof v !== "object") {
|
||||
return false;
|
||||
}
|
||||
const o = v as Record<string, unknown>;
|
||||
return (
|
||||
typeof o.code === "number" &&
|
||||
typeof o.msg === "string" &&
|
||||
"data" in o
|
||||
);
|
||||
}
|
||||
20
src/types/api/errors.ts
Normal file
20
src/types/api/errors.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/** HTTP 非成功或其 body 为业务信封且 `code !== 0` 时抛出 */
|
||||
export class LotteryApiBizError extends Error {
|
||||
readonly code: number;
|
||||
readonly data: unknown;
|
||||
|
||||
constructor(message: string, code: number, data: unknown) {
|
||||
super(message);
|
||||
this.name = "LotteryApiBizError";
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
/** body 不是约定的信封结构 */
|
||||
export class LotteryApiEnvelopeError extends Error {
|
||||
constructor(message = "响应不是约定的 { code, msg, data }") {
|
||||
super(message);
|
||||
this.name = "LotteryApiEnvelopeError";
|
||||
}
|
||||
}
|
||||
7
src/types/api/health.ts
Normal file
7
src/types/api/health.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
/** `GET /api/v1/health` → `data` */
|
||||
export type HealthData = {
|
||||
app: string;
|
||||
default_currency: string;
|
||||
/** 仅 `APP_DEBUG=true` 时出现 */
|
||||
laravel?: string;
|
||||
};
|
||||
11
src/types/api/index.ts
Normal file
11
src/types/api/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export type { ApiEnvelope } from "./envelope";
|
||||
export { isApiEnvelope } from "./envelope";
|
||||
export {
|
||||
LotteryApiBizError,
|
||||
LotteryApiEnvelopeError,
|
||||
} from "./errors";
|
||||
|
||||
export type { HealthData } from "./health";
|
||||
export type { ScopePingData } from "./ping";
|
||||
export type { PlayerMeData } from "./player-me";
|
||||
export type { WalletBalanceData } from "./wallet-balance";
|
||||
4
src/types/api/ping.ts
Normal file
4
src/types/api/ping.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
/** `GET /api/v1/player/ping` → `data` */
|
||||
export type ScopePingData = {
|
||||
scope: string;
|
||||
};
|
||||
10
src/types/api/player-me.ts
Normal file
10
src/types/api/player-me.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/** `GET /api/v1/player/me` → `data`(需 `lottery.player`) */
|
||||
export type PlayerMeData = {
|
||||
id: number;
|
||||
site_code: string;
|
||||
site_player_id: string;
|
||||
username: string;
|
||||
nickname: string | null;
|
||||
default_currency: string;
|
||||
status: number;
|
||||
};
|
||||
9
src/types/api/wallet-balance.ts
Normal file
9
src/types/api/wallet-balance.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/** `GET /api/v1/wallet/balance` → `data`(需 `lottery.player`) */
|
||||
export type WalletBalanceData = {
|
||||
/** 最小货币单位 bigint,序列化可能为 string */
|
||||
balance: string | number;
|
||||
main_balance: null;
|
||||
currency_code: string;
|
||||
wallet_type: string;
|
||||
frozen_balance: string | number;
|
||||
};
|
||||
2
src/types/index.ts
Normal file
2
src/types/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
/** 对外类型出口;API 契约与接口 DTO 见 `./api` */
|
||||
export * from "./api";
|
||||
Reference in New Issue
Block a user