New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

axios-interface

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-interface

create interface from axios

  • 2.0.0-alpha.1
  • next
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

axios-interface

version npm downloads codecov MIT License

快速开始

yarn add axios-interface

GetStarted

基本用法

axios-interface 分为三个阶段:接口工厂 => 接口定义 => 接口调用,每个阶段传入的配置项都会覆盖前一阶段的同名配置项。

import {createFactory} from 'axios-interface';

// 接口工厂
const {createInterface} = createFactory(options);

// 接口定义
interface Params {
    companyId: string;
    keyword?: string;
}

interface User {
    name: string;
}

const getUsers = createInterface<Params, User[]>('GET', '/rest/companies/{companyId}/users', options);

// 接口调用
const result = await getUsers({companyId: '1', keyword: 'jack'}); // GET /rest/companies/1/users?keyword=jack

配置 Options

interface Options extends AxiosRequestConfig {
    onPending?: OnPending;
    onResolve?: OnResolve;
    onReject?: OnReject;
    interpolate?: RegExp; // 默认为 /{(\w+)}/g
    encodePathVariable?: boolean; // 是否转译 path 上的变量。如把 a/b 转译为 a%2fb。默认为 false
    enableUrlTemplateHeaders?: boolean; // 是否把 urlTemplate 注入 headers['x-url-template']。默认为 false
    transformDeleteParamsIntoBody?: boolean; // 改变 DELETE 是,对参数的处理方式,默认 DELETE 是不传 body 的,有需要时开启
    // 一些 axios 的配置项,常用的如 headers
    [whatever: string]: any;
}

// 其中,以下类型都可以通过 import {OnPending} from 'axios-interface' 导入
type OnPending = <TParams>(params: TParams, options: Options) => Options | Promise<Options>;
type OnResolve = <TParams>(response: AxiosResponse, params: TParams, options: Options) => any;
type OnReject = <TParams>(response: AxiosError, params: TParams, options: Options) => any;

NOTE: 你可以使用 axios 的所有配置项

// 注意以下的所有 options,均为后者覆盖前者
const {createInterface} = createFactory(options);
const getUsers = createInterface(method, urlTemplate, options);
const result = getUsers(params);

onPending, onResolve, onReject 的生命周期说明

函数 call => onPending => transformRequest(axios) => 浏览器 => transformResponse(axios) => onResolve | onReject => 函数返回

whatever_you_want_to_custom 的使用说明

你可以在任何时候,在 options 里注入你想要的数据,在 onPending, onResolve, onReject 三个钩子,都会把 options 重新给你,此时,你可以根据 options 处理多种情况

参数声明时机

  • 以下参数仅在 createFactory 时声明有效

    • interpolate,可以修改 urlTemplate 解析
  • 以下参数在 createFactory 与 createInterface 时声明有效,request 时声明无效

    • encodePathVariable, enableUrlTemplateHeaders
  • 以下参数在 createFactory、 createInterface 和 request 时声明有效

    • onPending, onResolve, onReject, transformDeleteParamsIntoBody

文档

Best Practice

FAQ

FAQs

Package last updated on 14 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc