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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@js-lion/api

v0.2.3

Published

简化 Http 请求, 不会对 Axios 做任何改变

Downloads

58

Readme

功能

  • 支持 URL 动态变量解析
  • 支持使用装饰器进行 Http 请求

安装

Npm

$ npm install @js-lion/api lodash-es axios reflect-metadata

yarn

$ yarn add @js-lion/api lodash-es axios reflect-metadata

pnpm

$ pnpm install @js-lion/api lodash-es axios reflect-metadata

可以通过 import 导入方式使用

import { API, Get, Post } from "@js-lion/api";

或则通过 require 导入方式使用

const { API, Get, Post } = require("@js-lion/api");

案例

import { API, Get } from "@js-lion/api";

interface UserInfo {
  id?: string | number;
  nikeName?: string;
}

class HttpApi extends API {
  // get 注解(装饰器)
  @Get("/user/:id")
  getUserInfo<T = UserInfo>(userId: string | number): Promise<T> {
    // 准备请求参数
    const params = { id: userId };
    // 该返回值被注解接收用于发起 http 请求, 然后在将结果正常返回
    // @ts-ignore
    return { params };
  }

  @Post("/user/create")
  createUser(data: UserInfo): Promise<boolean> {
    // 处理接口返回的数据,按业务需求进行处理
    const callback = function(result: object) {
      // todo
      // 该返回值会直接返回给 createUser 方法
      return result ? true : false;
    };
    // 注解在发起 http 请求会,会将结果传给 callback 进行处理
    // @ts-ignore
    return { data, callback };
  }

  // 其余情况
  other<T>(query: object): Promise<T> {
    const params = {
      // todo
    };
    // 可以使用 Axios 的所有方法 [get / post / put / delete ...]
    return this.delete("/xxx", { params });
  }
}

const http = new Http();
const getUser = async function() {
  const userId = 1;
  const userInfo = await http.getUserInfo(userId);
  console.log(userInfo);
}

const createUser = async function() {
  const user: UserInfo = {
    nikeName: "xx"
  };
  const status = await http.createUser(user);
  console.log(status);
}

配置

import { API } from "@js-lion/api";
import type { AxiosRequestConfig, AxiosResponse } from "axios";

API.setEnv({
  // 配置全局的变量,用于 url 宏变量替换
});

API.setConfig({
  baseURL: "/",
  // 配置全局的 Axios Config
});

API.addRequest(function(req: AxiosRequestConfig) {
  // 请求前处理
  return req;
}, function(error: any) {
  // 处理异常,可省略
  return error;
});

API.addResponse(function(res: AxiosResponse) {
  // 请求完成时处理
  return res;
}, function(error: any) {
  // 处理异常,可省略
  return error;
});

class HttpApi extends API {
  constructor() {
    // 配置 Axios 默认参数
    const config: AxiosRequestConfig = {
      // todo
    };
    super(config);
  }
}

装饰器列表

装饰器名称 | 描述 -- | -- @Get | 创建一个 Http Get 请求 @Post | 创建一个 Http Post 请求 @Put | 创建一个 Http Put 请求 @Delete | 创建一个 Http Delete 请求 @Gql | 创建一个 Http Post 请求 并以 graphql 规范传输数据 @Http | 自定义创建一个 Http 请求 @validate | 对方法的参数断言 @required | 设置参数为必填,不允许为空 @tryError | 监听方法异常,异常时返回默认值

import { Get, Post,  validate, required, tryError } from "@js-lion/api";

class HttpApi {
  @Get("/user/:id")
  @validate
  getUserInfo<T = UserInfo>(@required userId: string | number): Promise<T> {
    const params = { id: userId };
    // @ts-ignore
    return { params };
  }

  @tryError(false)
  @Post("/user/create")
  createUser(data: UserInfo): Promise<boolean> {
    // 处理接口返回的数据,按业务需求进行处理
    const callback = function(result: object) {
      // todo
      return true;
    };
    // @ts-ignore
    return { data, callback };
  }

  @Http("Get", "/xxxx")
  @validate
  getUserInfo<T = UserInfo>(@required userId: string | number): Promise<T> {
    // 同 Get 案例
  }
}