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

@jimengio/api-base

v0.3.12

Published

> 公司项目 API 前端复用逻辑

Downloads

60

Readme

API Base

公司项目 API 前端复用逻辑

包含功能:

  • 提供 API 调用简写语法,
  • 提供页面顶部显示的进度条,
  • 生成 API 报错信息(供外部的 UI 组件展示).

注意事项

原先耦合的代码需要手动处理一遍, 搜则对应的逻辑会丢失:

  • 监听 EJimuApiEvent.ErrorUnauthorized 事件, 调用 clearUserInfoCache(),
  • 监听 EJimuApiEvent.ErrorMessage 事件, 调用 message.error(errorHumanized),
  • 监听 EJimuApiEvent.ErrorGotoSignin 调用 gotoSignin(signinPath, location.href).

Usages

yarn add @jimengio/api-base
import { get } from "@jimengio/api-base";

get({
  baseURL: `http://my-api/api`,
  url: `/info`,
});

// 发送其他方法
get({
  method: "POST",
  baseURL: `http://my-api/api`,
  url: `/info`,
});
注册错误信息
import { addGlobalErrorMessages } from "@jimengio/api-base";

addGlobalErrorMessages({
  00001: "信息不合法",
});

对于特定的错误码定制错误信息(覆盖默认注册的错误信息):

put({
  url: `/plants/${plantId}/materials/${materialId}`,
  baseURL: coresBaseUrl,
  data: options,
  errorMessage: {
    [ApiErrorCode.DBDup]: lingual.materialCodeAlreadyExists,
  },
});

通过 false 可以关闭特定错误码的报错提示:

{
  errorMessage: {
    [ApiErrorCode.DBDup]: false,
  },
}

通过 axios 的 paramsSerializer 的参数可以对数组格式的 query 参数进行格式化:

{
  paramsSerializer: (params) => Qs.stringify(params, {arrayFormat: 'brackets'}),
}
监听 API 特殊的事件:
import { EJimuApiEvent } from "@jimengio/api-base";

JimuApisEventBus.on(EJimuApiEvent.ErrorMessage, (error: string) => {
  console.error(error);
});
修改默认多语言
export { changeApisLingual, EApisLangKind } from "@jimengio/api-base";

changeApisLingual(EApisLangKind.enUS);
页面顶部显示进度条

展示网络加载进度(不精确):

import { NetProgress } from "@jimengio/api-base";

<NetProgress />;

API

  • get post put del patch, 基础 HTTP 请求
  • changeApisLingual, EApisLangKind 用于修改多语言配置
  • NetProgress 渲染网络状态的组件
  • addGlobalErrorMessages addGlobalStatusCodeErrorMessages 设置错误文案`
  • ApiError IJimuApiOption API 属性配置类型
  • BuiltinApiErrorCode 内置错误码, 偶尔会用到
  • 私有 generateCancelToken getApiBaseUrl setApiBaseUrl setApiDefaultHeader API 状态修改方法
  • 私有 globalErrorMessages globalStatusCodeErrorMessages(类型为 IErrorMessages) 读取全局错误文案配置
  • 私有 showError 触发错误