GithubHelp home page GithubHelp logo

rufetch's Introduction

intro

a lightweight fetch lib with middleware inspired by koa

quick start

yarn add @jarzzzi/rufetch

example

const http = new Rufetch({
  baseURL: 'https://github.com/api',
})

const getUser = (name) => {
  return http.get(`/user/${name}`)
}

const getReps = (name, page) => {
  return http.get(`/user/${name}`, {params: {page}})
}

const createReps = (name, data) => {
  return http.post(`/user/${name}`, {data})
}

middleware

  • built in middleware

    • cache
    • fetch
  • error middleware

http.use(async (ctx: any, next: any) => {
  await next()
  const {error} = ctx.res

  if (error?.status >= 400 && error?.status < 500) {
    console.error('client error')
  }
  if (error?.status >= 500) {
    console.error('server error')
  }
  if (error === 'fetch timeout') {
    console.error('fetch timeout')
  }
})
  • token middleware
http.use(async (ctx: any, next: any) => {
  const token = await getToken()
  ctx.req.requestInit.setHeaders(token, token)
  await next()
})

api

  • options
    • baseURL
    • timeout: default 15000
    • params: url search params
    • data: body alias
    • responseType: default json, arrayBuffer, blob, json, text, formData, stream...
    • cacheOptions
      • cache: boolean
      • expiredAge: default 5000
    • body
    • cache
    • credentials
    • headers
    • integrity
    • keepalive
    • method
    • mode
    • redirect
    • referrer
    • referrerPolicy
    • signal
    • window
  • new (options)
  • instance.get
  • instance.post
  • instance.put
  • instance.delete
  • instance.head
  • instance.use
    • add middleware
    • ctx
      • req
      • request
      • res
      • response

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.