GithubHelp home page GithubHelp logo

fetra's Introduction

fetch 扩展

主要目的是能够实现拦截器,以及对请求的统一处理,比如请求头的添加,请求参数的添加,请求的统一处理等等。
现在已经实现请求拦截、响应拦截、错误拦截

安装

npm

npm install fetra --save

yarn

yarn add fetra

pnpm

pnpm add fetra

简单使用

import {createFetra} from 'fetra'
const fetra = createFetra()

fetra("/api/user").then(res => {
    console.log(res)
})

拦截请求

import {createFetra, RequestInterceptor} from 'fetra'
const fetra = createFetra()

const reqInterceptor: RequestInterceptor = async (input: RequestInfo | URL, init: RequestInit, fetcher: Fetcher) => {
  // do something
  return [input, {...init, headers: {...init.headers, 'Content-Type': 'application/json'}}]
}

fetra.request.add(reqInterceptor)

拦截响应

import {createFetra, ResponseInterceptor} from 'fetra'
const fetra = createFetra()
const respInterceptor: ResponseInterceptor = async (response: Response, input: RequestInfo | URL, init: RequestInit, fetcher: Fetcher) => {
  // do something
  return response
}

fetra.response.add(respInterceptor)

拦截错误

import {createFetra, ErrorInterceptor} from 'fetra'
const fetra = createFetra()
const errorInterceptor: ErrorInterceptor = async (error: unknown, input: RequestInfo | URL, init: RequestInit, response: Response | null, fetcher: Fetcher) => {
  // do something
  return response
}

为了方便Restful API的使用,提供了Restfull Wrapper

import {createFetra, ErrorInterceptor, wrapRestful} from 'fetra'

const fetra = wrapRestful(createFetra())

//新增了get,post,put,delete,patch方法
fetch.get("/api/user").then(res => {
    console.log(res)
})

fetra's People

Contributors

mingshuisheng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.