@iap Packages
    Preparing search index...

    Function createHttp

    • 创建 HTTP 请求实例

      Parameters

      • instanceConfig: HttpInstanceConfig = {}

        实例级配置,包含 baseUrl、超时、拦截器、logger 等

      Returns {
          delete: <T>(
              url: string,
              config?: Omit<HttpConfig, "method" | "url">,
          ) => Promise<HttpResponse<T>>;
          get: <T>(
              url: string,
              config?: Omit<HttpConfig, "method" | "url">,
          ) => Promise<HttpResponse<T>>;
          patch: <T>(
              url: string,
              body?: unknown,
              config?: Omit<HttpConfig, "body" | "method" | "url">,
          ) => Promise<HttpResponse<T>>;
          post: <T>(
              url: string,
              body?: unknown,
              config?: Omit<HttpConfig, "body" | "method" | "url">,
          ) => Promise<HttpResponse<T>>;
          put: <T>(
              url: string,
              body?: unknown,
              config?: Omit<HttpConfig, "body" | "method" | "url">,
          ) => Promise<HttpResponse<T>>;
          request: <T>(config: HttpConfig) => Promise<HttpResponse<T>>;
      }

      包含 request、get、post、put、patch、delete 方法的实例

      • delete: <T>(
            url: string,
            config?: Omit<HttpConfig, "method" | "url">,
        ) => Promise<HttpResponse<T>>
      • get: <T>(
            url: string,
            config?: Omit<HttpConfig, "method" | "url">,
        ) => Promise<HttpResponse<T>>
      • patch: <T>(
            url: string,
            body?: unknown,
            config?: Omit<HttpConfig, "body" | "method" | "url">,
        ) => Promise<HttpResponse<T>>
      • post: <T>(
            url: string,
            body?: unknown,
            config?: Omit<HttpConfig, "body" | "method" | "url">,
        ) => Promise<HttpResponse<T>>
      • put: <T>(
            url: string,
            body?: unknown,
            config?: Omit<HttpConfig, "body" | "method" | "url">,
        ) => Promise<HttpResponse<T>>
      • request: <T>(config: HttpConfig) => Promise<HttpResponse<T>>
      const http = createHttp({
      baseUrl: '/api',
      timeout: 5000,
      onRequest: (config) => ({
      ...config,
      headers: { ...config.headers, Authorization: `Bearer ${token}` },
      }),
      onResponse: (res) => {
      if (res.code !== 200) throw new Error(res.message)
      return res
      },
      })

      const res = await http.get<User[]>('/users')