Skip to content

a fetch wrapper like axios, support middlewares

Notifications You must be signed in to change notification settings

tonylua/QuickFetch

Repository files navigation

QuickFetch

a fetch wrapper like axios, support middlewares & abort

1. How to install

npm i quickfetch --save

2. How to use

see test/QuickFetch.test.js

OR vue-cli-3-preset

to eliminate the "the request of a dependency is an expression" warning

about the issue

add below to your webpack config:

// quickfetch uses parcel-bundler which has require statements
new webpack.ContextReplacementPlugin(/quickfetch/, data => {
  delete data.dependencies[0].critical;
  return data;
})

3. API

Classes

QuickFetchMiddlewareHolder

QuickFetch

MiddlewareHolderEventTarget

MiddlewareHolder

Typedefs

option : Object | Null

an optional object for Request API

QuickFetch ⇐ MiddlewareHolder

QuickFetch

Kind: global class
Extends: MiddlewareHolder

new QuickFetch([option])

a fetch-based HTTP request tool

Param Type
[option] option

quickFetch.get(url, [params], [option]) ⇒ Promise

make a GET fetch

Kind: instance method of QuickFetch
Returns: Promise -

a Promise that resolves to a Response object


See: QuickFetch#constuctor

Param Type Description
url string
[params] Object | null

an optional params object

[option] option

quickFetch.post(url, [params], [option]) ⇒ Promise

make a POST fetch

Kind: instance method of QuickFetch
Returns: Promise -

a Promise that resolves to a Response object

Param Type Description
url string
[params] Object | null

an optional params object

[option] option

quickFetch.delete(url, [params], [option]) ⇒ Promise

make a DELETE fetch

Kind: instance method of QuickFetch
Returns: Promise -

a Promise that resolves to a Response object

Param Type Description
url string
[params] Object | null

an optional params object

[option] option

quickFetch.put(url, [params], [option]) ⇒ Promise

make a PUT fetch

Kind: instance method of QuickFetch
Returns: Promise -

a Promise that resolves to a Response object

Param Type Description
url string
[params] Object | null

an optional params object

[option] option

quickFetch.patch(url, [params], [option]) ⇒ Promise

make a PATCH fetch

Kind: instance method of QuickFetch
Returns: Promise -

a Promise that resolves to a Response object

Param Type Description
url string
[params] Object | null

an optional params object

[option] option

quickFetch.sequence(requestPromiseArr) ⇒ Array.<Response>

make batch requests

Kind: instance method of QuickFetch

Param Type
requestPromiseArr Array.<Promise>

quickFetch.ping(url, [params]) ⇒ boolean | Promise

send a beacon

Kind: instance method of QuickFetch
Returns: boolean | Promise -

send result

Param Type Description
url string
[params] Object | null

an optional params object

quickFetch._getMiddlewares(type, [option])

Kind: instance method of QuickFetch
Overrides: _getMiddlewares
Access: protected

Param Type Description
type String

QuickFetch.REQUEST

[option] Object

quickFetch._parseMiddlewares(mids, target, [params])

Kind: instance method of QuickFetch
Overrides: _parseMiddlewares
Access: protected

Param Type Default
mids Array
target Request | Response | JSON | Blob
[params] Object

quickFetch.use(type, middleware, [fetchId]) ⇒ object

regist a middleware

Kind: instance method of QuickFetch
Overrides: use
Returns: object -

actions - { unuse, pause, resume }

Param Type Description
type string

QuickFetch.REQUEST

middleware function

a function looks like (req

[fetchId] string | number

a optional id for special requests

quickFetch._unuse(id)

Kind: instance method of QuickFetch
Overrides: _unuse
Access: protected

Param Type
id number

quickFetch._pause(id, [fetchId])

Kind: instance method of QuickFetch
Overrides: _pause
Access: protected

Param Type Description
id number
[fetchId] string | number

a optional id for special requests

quickFetch._resume(id, [fetchId])

Kind: instance method of QuickFetch
Overrides: _resume
Access: protected

Param Type Description
id number
[fetchId] string | number

a optional id for special requests

QuickFetch.abort(id)

cancel a fetch action

Kind: static method of QuickFetch

Param Type Description
id string | number | symbol

fetchId

MiddlewareHolder ⇐ EventTarget

MiddlewareHolder

Kind: global class
Extends: EventTarget

middlewareHolder._getMiddlewares(type, [option])

Kind: instance method of MiddlewareHolder
Access: protected

Param Type Description
type String

QuickFetch.REQUEST

[option] Object

middlewareHolder._parseMiddlewares(mids, target, [params])

Kind: instance method of MiddlewareHolder
Access: protected

Param Type Default
mids Array
target Request | Response | JSON | Blob
[params] Object

middlewareHolder.use(type, middleware, [fetchId]) ⇒ object

regist a middleware

Kind: instance method of MiddlewareHolder
Returns: object -

actions - { unuse, pause, resume }

Param Type Description
type string

QuickFetch.REQUEST

middleware function

a function looks like (req

[fetchId] string | number

a optional id for special requests

middlewareHolder._unuse(id)

Kind: instance method of MiddlewareHolder
Access: protected

Param Type
id number

middlewareHolder._pause(id, [fetchId])

Kind: instance method of MiddlewareHolder
Access: protected

Param Type Description
id number
[fetchId] string | number

a optional id for special requests

middlewareHolder._resume(id, [fetchId])

Kind: instance method of MiddlewareHolder
Access: protected

Param Type Description
id number
[fetchId] string | number

a optional id for special requests

option : Object | Null

an optional object for Request API

Kind: global typedef
Properties

Name Type Default Description
[method] string

standard fetch init option

[credentials] string "include"

standard fetch init option

[mode] string "cors"

standard fetch init option

[cache] string "reload"

standard fetch init option

[headers] Object

standard fetch init option

[endpoint] string

optional, e.g. https://xxx.com:8090

[baseURL] string

optional, an url prefix, e.g. /myapi

[timeout] string 30000

optional, timeout

[catchError] boolean true

optional, if true then just parse error in middleware, otherwise throw it to endpoint

[ignoreBodyMethods] Array ['get', 'head']

optional

[forceJSON] boolean false

optional, send body with JSON.stringify()

[fetchId] string | number | symbol

optional, an unique ID of every fetch request

[signal] AbortSignal

optional, a given signal to cancel the fetch request

About

a fetch wrapper like axios, support middlewares

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published