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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gopowerteam/http-request

v0.4.7

Published

---

Downloads

110

Readme

@gopowerteam/http-request


npm version install size

基于rxjs,axios的网络请求封装,返回Observable对象,便于操作.

目录


特性


  • 基于axios进行封装操作
  • 请求流程基于rxjs进行封装
  • 使用Controller,Service进行配置,便于重复调用
  • 支持自定义拦截器操作
  • 支持请求服务扩展

安装


// yarn use
# yarn add @gopowerteam/http-request

// npm use
# npm install @gopowerteam/http-request --save

示例


// config.ts
import { RequestService } from '@gopowerteam/http-request';

// 配置服务端信息
RequestService.setConfig({
    server: 'http://api.test.com/',     // 设置请求地址
    timeout: 3000                       // 设置请求超时
})
// user.controller.ts

import { RequestMethod } from '@gopowerteam/http-request'

export const UserController = {
    login: {
        controller:'user',
        path: '/api/user/login',
        action: 'login',
        type: RequestMethod.Post
    }
}
// user.service.ts
import { Request, RequestParams } from '@gopowerteam/http-request'
import { Observable } from 'rxjs'
import { UserController } from '../controllers/test.controller'

export class UserService {
    @Request({
        server: UserController.login
    })
    public login(
        requestParams: RequestParams
    ): Observable<any> {
        return requestParams.request()
    }
}
// index.ts
const userService = new UserService()

userService.login(new RequestParams()).subscribe({
    next: data => {
        console.log(data)
    }
})

完整示例

自定义拦截器


状态拦截器

状态拦截器函数负责判断网络请求是否成功,如state为200data.success为true,

// 配置状态拦截器
RequestService.interceptors.status.use(respone => {
    return true
})

成功拦截器

成功拦截器用于确认网络请求成功后返回的数据结构

// 添加成功拦截器
RequestService.interceptors.success.use(respone => {
    return respone.data
})

失败拦截器

失败拦截器用于确认网络请求失败后返回的数据结构

// 添加失败拦截器
RequestService.interceptors.error.use(respone => {
    return respone
})

网络异常处理


通过设置requestCatchHandle可以设置全局异常处理

RequestService.requestCatchHandle = respone => {
    const defaultError = '服务通讯连接失败'
    const messageList = {
        400: '请求参数错误',
        405: '请求服务方法错误',
        500: '服务器内部错误',
        403: '没有权限,请重新登陆'
    }

    if (respone) {
        const responseMessage = (respone.data || {}).message
        const errorMessage =
            responseMessage || messageList[respone.status] || defaultError

        if (respone.status === 403) {
            console.error(respone.data)
        }

    } else {
    }
}

扩展服务


扩展服务支持对请求进行前置或后置的修改,可以通过定义beforeafter来进行前置或后置的操作,可以对发送数据进行修改.扩展服务支持全局安装和单请求安装.

import { ExtendService } from '@gopowerteam/http-request'

export class TokenService extends ExtendService {
    public before = (params: any) => {
        const userid = '001'

        if (userid) {
            params.options.header = params.options.header || {}
            params.options.header['X-UserID'] = userid
        }
    }

    public after = (data: any, params) => {
        //
    }
}

// 全局安装扩展服务
RequestService.installExtendService(new TokenService())

// 单请求安装扩展服务
userService.login(new RequestParams({},{
    token:new TokenService()
}}})).subscribe({
    next: data => {
        console.log(data)
    }
})