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

@tuoyuan/gateway-request-lib

v1.0.3

Published

## 1. 简介

Downloads

267

Readme

网关请求工具基础库

1. 简介

网关接口请求基础库,基于 axios 封装,支持动态注册 API、自定义勾子、全局消息、超时配置、自定义请求适配器等功能。

2. 安装

npm install @tuoyuan/gateway-request-lib

3. 使用

3.1 全局初始化请求工具实例

  • 调用init方法初始化请求工具实例且只能存在一个实例
import * as gatewayRequest from '@tuoyuan/gateway-request-lib';

// 初始化
gatewayRequest.init({
  /** 网关服务地址 */
  base_url: 'base_url',
  /** 应用ID */
  app_id: 'app_id',
  /** 应用KEY */
  app_key: 'app_key',
  /** 获取access_token方法 */
  getToken: () => {},
  // 更多配置见类型定义
});

3.2 封装 API 方法

  • 调用request方法封装 API 方法
import { request } from '@tuoyuan/gateway-request-lib';

export async function apiFunc() {
  return request<RequestData, ResponseData>({
    api: '/api/path',
    config: {
      access_token: true,
    },
  });
}

3.3 注册 API

  • 调用registerApis方法注册新的 api 方法,多次调用会进行深度合并,重复时会覆盖之前的 api 方法
import * as gatewayRequest from '@tuoyuan/gateway-request-lib';
import apis from 'your-apis-path';

// 注册API
gatewayTool.registerApis(apis);
  • 重写 TS 类型
// types.d.ts

declare module '@tuoyuan/gateway-request-lib' {
  import type * as GatewayTypes from '@tuoyuan/gateway-request-lib';
  import * as NewApis from 'your-apis-path';

  const init: GatewayTypes.GatewayTool['init'];
  const request: GatewayTypes.GatewayTool['request'];
  const registerApis: GatewayTypes.GatewayTool['registerApis'];
  const apis: GatewayTypes.MergeApiType<NewApis.IApis>;

  const Exports = {
    init,
    request,
    apis,
    registerApis,
  };
  export = Exports;
}

3.4 调用 API

import { apis } from '@tuoyuan/gateway-request-lib';

// 调用API
apis.apiFunc();