example.ts 1.07 KB
/**
 * API 使用示例
 * 展示如何在子应用中使用 GSC Request 发起请求
 */
import { gscPost, gscGet } from '@/utils/gscRequest';

/**
 * 示例:用户登录
 */
export async function login(username: string, password: string) {
  try {
    const response = await gscPost('/login', {
      username,
      password,
    });
    return response;
  } catch (error) {
    console.error('登录失败:', error);
    throw error;
  }
}

/**
 * 示例:获取用户信息
 */
export async function getUserInfo() {
  try {
    const response = await gscGet('/user/info');
    return response;
  } catch (error) {
    console.error('获取用户信息失败:', error);
    throw error;
  }
}

/**
 * 示例:带配置的 POST 请求
 */
export async function createData(data: any) {
  try {
    const response = await gscPost(
      '/api/data',
      data,
      {
        returnAll: true, // 返回完整响应
        timeout: 10000, // 超时时间
      },
    );
    return response;
  } catch (error) {
    console.error('创建数据失败:', error);
    throw error;
  }
}