JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 88
  • Score
    100M100P100Q54132F
  • License MIT

react fetch by isomorphic-fetch

Package Exports

  • isomorphic-fetch-http

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (isomorphic-fetch-http) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

isomorphic-fetch-http

NPM version NPM downloads

Install

npm install isomorphic-fetch-http --save

Usage

import { http } from 'isomorphic-fetch-http'

http.setup({
  cookies: true,
});

return http.get('url', param, header);

Options

setup

全局配置

prefix

请求 url 前缀

headers

配置请求头

cookies

是否携带cookies

filter

过滤函数,发生在请求发出之前,可以对url, headers,fetch option进行更改,类型为 Promise function。

http.setup({
  filter: data => new Promise((resolve, reject) => {
    // 获取
    const { url, headers, option } = data;
    
    // 修改
    const newHeaders = {
      ...headers,
      token: 'zhnagsan'
    };
    
    // 返回
    resolve({ url, headers: newHeaders, option });
  })
});

callback

回调数据函数,对请求成功获取到的数据进行预处理

http.setup({
  callback: data => {
    const { status, data, messgae } = data;
    if (status) {
      return data;
    } else {
      throw new Erroe(message);
    }
  }

get

get 方法,一般用于数据查询

return http.get('/get', {currentPage: 1}, {token: 'zhangsan'}); // 此处的headers优先级最高

post

post 方法,一般用于数据提交

return http.post('/add', {username: 'zhangsan'});

put

put 方法,一般用于数据修改

return http.put('/update/1', {username: 'lisi'});

delete

delete 方法,一般用于数据删除

return http.put('/delete/1');

json

请求头 "Content-Type" 为 "application/json" 的 post 方法

return http.post('/add', {username: 'zhangsan'});

options

option 方法,一般用于请求时的预检

Contributing

You can submit any ideas as pull requests or as a Github issue.