npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sqin/http-client-uni

v1.0.3

Published

uni app 的 网络请求封装

Downloads

1

Readme

readme

uni app 的 网络请求封装

// api/index.ts
import { HttpClient, type HttpClientHooks } from "@sqin/http-client-uni";

const SOTRAGE_KEY_API_TOKEN = "api_token";
const SOTRAGE_KEY_LOGIN_TOKEN = "refresh_token";
export const setApiToken = (token: string) =>
  uni.setStorageSync(SOTRAGE_KEY_API_TOKEN, token);
export const setLoginToken = (token: string) =>
  uni.setStorageSync(SOTRAGE_KEY_LOGIN_TOKEN, token);
export const getApiToken = () => uni.getStorageSync(SOTRAGE_KEY_API_TOKEN);
export const getLoginToken = () => uni.getStorageSync(SOTRAGE_KEY_LOGIN_TOKEN);



type UniResponse = UniNamespace.RequestSuccessCallbackResult;

class HttpClientError extends Error {
  showToast: boolean;
  constructor(message: string, showToast = true) {
    super(message);
    this.showToast = showToast;
  }
}

// ----------------------------------------------------------------

const hooks: HttpClientHooks = {
  async urlHook(url) {
    return "http://127.0.0.1:3000" + url;
  },
  async beforeRequest(option) {
    const api_token = getApiToken();
    if (api_token) {
      option.header = {
        Authorization: `Bearer ${api_token}`,
        ...option.header,
      };
    }
    return option;
  },
  async responsePass(res, info) {
    console.log("response pass", res);
    const { statusCode } = res;
    try {
      if (statusCode >= 200 && statusCode < 300) {
        return onResponseHandler.two(res, info);
      }

      if (statusCode >= 400 && statusCode < 500) {
        return onResponseHandler.four(res, info);
      }

      if (statusCode >= 500 && statusCode < 600) {
        return onResponseHandler.five(res, info);
      }

      throw new HttpClientError("未处理的 http code" + statusCode, false);
    } catch (e) {
      if (e instanceof HttpClientError) {
        if (e.showToast)
          uni.showToast({
            title: e.message,
            icon: "error",
          });
      }
    }
  },
  async responseFail(err) {
    // {errno: 600009, errMsg: "request:fail invalid url "/api/v1/login""}

    console.log("response faild", err);

    uni.showToast({
      title: err?.errMsg || "无法访问服务器,请稍后在试",
    });
    throw err;
  },
};

const onResponseHandler = {
  async two(res: UniResponse, info: any) {
    if (res.statusCode === 200) {
      // 对 Response 数据进行一次校准,避免数据异常
      if (typeof res?.data !== "object") {
        // 说明格式可能错误
        console.log("异常 res.data ", res.data);

        throw new HttpClientError("数据异常");
      }

      return res.data;
    }

    throw new HttpClientError("未处理的 Http Code:" + res.statusCode, false);
  },
  async four(res: UniResponse, info: any) {
    const { errMsg } = res;

    // 鉴权
    if (res.statusCode === 401) {
      return Unauthorized(res, info);
    }

    let msg = res?.data?.msg || errMsg || "Code 400:操作失败";

    throw new HttpClientError(msg);
  },

  five(res: UniResponse, info: any) {
    const { errMsg } = res;

    let msg = res.data?.msg || errMsg || "Code 500:服务器异常,请稍后在尝试";

    throw new HttpClientError(msg);
  },
};

const Unauthorized = async (res: UniResponse, info: any) => {
  const token = getLoginToken();
  if (!token) throw new HttpClientError("未登录");

  const url = await hooks.urlHook?.("/api/v1/renew");
  if (!url) throw new HttpClientError("url构建失败", false);

  const renew = await defaultHttpClient.request({
    url,
    method: "POST",
    data: {
      refresh: token,
    },
  });

  if (renew.statusCode !== 200)
    throw new HttpClientError("自动刷新 Token 失败", false);

  const { data } = renew.data as any;
  const { api, refresh } = data;

  // save 逻辑应该放在 pinia
  if (api) setApiToken(api);
  if (refresh) setLoginToken(refresh);

  const { url: reqUrl, data: reqData } = info.config;
  if (info.method === "GET") {
    return defaultHttpClient.get(reqUrl, reqData);
  } else {
    return defaultHttpClient.post(reqUrl, reqData);
  }
};

// ----------------------------------------------------------------

const defaultHttpClient = new HttpClient(hooks);

// ----------------------------------------------------------------

export const post = defaultHttpClient.post;
export const get = defaultHttpClient.get;
import { post } from "./index";

interface IHttpResponse<T = any> {
  code: number;
  ok: boolean;
  msg: string;
  data?: T;
  [key: string]: any;
}

interface IResLogin {
  api: string;
  refresh: string;
}
export const reqLogin = (user = "王五") =>
  post<IHttpResponse<IResLogin>>("/api/v1/login", {
    user,
  });