1.4.2 • Published 3 years ago

@fetch-plus/ponyfill v1.4.2

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

fetch-plus

koa style middleware for Fetch API on browser and nodejs

Install

npm install @fetch-plus/ponyfill --save

Usage

import { fetch } from '@fetch-plus/ponyfill'

fetch.use(async (ctx, next) => {
	const { request } = ctx
	request.url = `https://unpkg.com/${request.url}`
	await next()
})

fetch('react', { 
	// method: 'GET'
	// dataType: 'json', // default response return json, ['json', 'text', 'blob']
	// type: 'json', // default request content-type is json, ['json', 'form', 'multipart']
	search: { meta: true } // query params append to url, ex: '?meta=true' 
	// body: {}, // when method is ['POST', 'PUT', 'PATCH'], the body be submited
	// headers: {}, // request headers
	// credentials: 'include', // cors cookie 
}).then(data => {
	console.log(data)
})

// or use base plugin
fetch.base({
	baseUrl: 'https://unpkg.com/',
	search: { meta: true },
	// body: {},
	// headers: {},
	// credentials: 'include',
	// method: 'GET',
	// type: '', // ['json', 'form', 'multipart']
	// dataType: '', // ['json', 'text', 'blob'] 
	// pre(request, ctx, body, req) {  },
	// post(response, ctx, data, req) {  },
	// catch(error) {  }
})

fetch('react').then(data => console.log(data))

Methods

fetch(url, options)

fetch('https://unpkg.com/react', { dataType: 'text' })

fetch.base(options)

fetch.base({
	baseUrl: '/',
	search: {},
	body: {},
	headers: {},
	credentials: 'include',
	method: 'GET',
	type: '', // ['json', 'form', 'multipart']
	dataType: '', // ['json', 'text', 'blob'] 
	pre(request, ctx, body, req) { 
		// const { url, options } = req // request arguments

	},
	post(response, ctx, data, req) { 
		// const { url, options } = req // request arguments
		// ctx.data = data.data // change all response data
		// ctx.interrupt() interrupt promise then or catch invoke
	},
	catch(error) {  }
})

fetch.use(middleware, index)

fetch.use(async (ctx, next) => {
	...
	await next()
	...
}, 0)

index is the order in the middleware list, default will push to the end

Classes

FetchPlusPonyfill

import FetchPlusPonyfill, { fetch } from  '@fetch-plus/ponyfill'

const fetchPlus = new FetchPlusPonyfill()

// use basePlugin to add baseUrl, baseSearch, baseBody and so on.
fetchPlus.base({
	baseUrl: 'https://unpkg.com',
	search: {} 
})

fetchPlus.fetch('react', { dataType: 'text' })

method fetch is the shortcut of the FetchPlusPonyfill instance, with default inner plugins:

  • BasePlugin
  • DataTypePlugin
  • TypeIsPlugin
  • SearchBodyPlugin

FetchPlus

import FetchPlus from '@fetch-plus/core'

base FetchPlus class without fetch-ponyfill and DefaultPlugins

1.4.2

3 years ago

1.4.1

3 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.0

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.1.23

4 years ago

1.1.22

5 years ago

1.1.21

5 years ago

1.1.20

5 years ago

1.1.19

5 years ago

1.1.18

5 years ago

1.1.17

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.4

5 years ago