Files
cozsweet-frontend-nextjs/src/core/net/config/api_config.ts
T

70 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* API 配置管理
*
* 通过环境变量配置后端服务地址与超时时间。
* 原始 Dart: lib/core/net/service_manager.dart
*/
import { AppEnvUtil, type AppEnv } from "@/utils/app-env";
export type { AppEnv };
export interface ApiConfig {
/** 后端 API 基础 URL(含协议) */
baseUrl: string;
/** WebSocket 基础 URL */
wsUrl: string;
/** 连接超时(ms */
connectTimeout: number;
/** 接收超时(ms */
receiveTimeout: number;
/** 发送超时(ms */
sendTimeout: number;
}
/**
* 超时常量
*/
export class TimeoutConstants {
private constructor() {}
static readonly shortTimeout = 30000;
static readonly longTimeout = 60000;
}
/**
* 解析当前环境
*/
export function getAppEnv(): AppEnv {
return AppEnvUtil.current;
}
/**
* 获取当前 API 配置
*/
export function getApiConfig(): ApiConfig {
const baseUrl =
process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://172.16.48.49:3002";
const wsUrl =
process.env.NEXT_PUBLIC_WS_BASE_URL ?? "ws://172.16.48.49:3002/ws";
const connectTimeout = Number.parseInt(
process.env.NEXT_PUBLIC_API_CONNECT_TIMEOUT ?? "30000",
10
);
const receiveTimeout = Number.parseInt(
process.env.NEXT_PUBLIC_API_RECEIVE_TIMEOUT ?? "60000",
10
);
const sendTimeout = Number.parseInt(
process.env.NEXT_PUBLIC_API_SEND_TIMEOUT ?? "30000",
10
);
return {
baseUrl,
wsUrl,
connectTimeout,
receiveTimeout,
sendTimeout,
};
}