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

@kordar-lib/request

v2.0.0

Published

基于 axios 的轻量请求封装,提供统一的 Request 接口与 AxiosRequest 实现,便于在业务中注入请求实例(如在模板项目的 init 中统一管理),并通过拦截器完成鉴权、语言、响应解包与错误处理。

Downloads

142

Readme

@kordar-lib/request

基于 axios 的轻量请求封装,提供统一的 Request 接口与 AxiosRequest 实现,便于在业务中注入请求实例(如在模板项目的 init 中统一管理),并通过拦截器完成鉴权、语言、响应解包与错误处理。

特性

  • Request 接口:get/post/put/delete(支持泛型返回值)
  • AxiosRequest:axios.create + 请求/响应拦截器
  • RequestOption 直接复用 AxiosRequestConfig(与 axios 参数保持一致)
  • 内置下载辅助:octetStreamByBlobDownload

安装

npm install @kordar-lib/request axios

基本用法

创建请求实例(配置 + 拦截器):

import {AxiosRequest} from '@kordar-lib/request'
import type {AxiosError, AxiosResponse, InternalAxiosRequestConfig} from 'axios'

export const request = new AxiosRequest(
  { timeout: 30000, baseURL: '/api' },
  (cfg: InternalAxiosRequestConfig) => {
    const headers = cfg.headers
    ;(headers as any).Locale = 'zh-CN'
    ;(headers as any).Token = 'your-token'
    return cfg
  },
  (resp: AxiosResponse<any>) => resp.data,
  (err: AxiosError) => {
    throw err
  }
)

调用:

const data = await request.get<{list: any[]}>('resource/demo/list', {page: 1})

约定的后端响应结构(推荐)

项目模板中普遍使用:

export type ApiResp<T = any> = {
  code: number
  message?: string
  data?: T
  count?: number
}

把响应统一解包为 ApiResp,业务侧更易处理:

import type {AxiosResponse} from 'axios'
import type {ApiResp} from './types'

const beforeResponse = (resp: AxiosResponse<ApiResp>) => resp.data

进一步把 data 直接返回(按你的业务约定自行处理 code):

import type {AxiosResponse} from 'axios'

type ApiResp<T> = { code: number; message?: string; data?: T; count?: number }

const beforeResponse = <T>(resp: AxiosResponse<ApiResp<T>>) => {
  const r = resp.data
  if (r.code !== 200) throw new Error(r.message || 'Request failed')
  return r.data as T
}

自定义并挂载 Request(推荐模式)

应用初始化时创建 request 实例,然后通过你自己的 init 模块统一注入:

// src/init.ts
import type {Request} from '@kordar-lib/request'

let requestInstance: Request

export function loadRequest(request: Request) {
  requestInstance = request
}

export function getRequest(): Request {
  return requestInstance
}

业务 service 中使用:

import {getRequest} from './init'

export function listUsers() {
  return getRequest().post('resource/user/list', { page: 1, rows: 20 })
}

下载辅助

import {octetStreamByBlobDownload} from '@kordar-lib/request'

octetStreamByBlobDownload([new Uint8Array([1,2,3])], 'demo.bin')

构建(Monorepo:npm)

在仓库根目录执行:

npm install
npm run -w @kordar-lib/request build

类型检查:

npm exec -w @kordar-lib/request -- tsc -p tsconfig.json --noEmit