项目作者: hubgit

项目描述 :
A minimal interface for fetching resources from the web
高级语言: JavaScript
项目地址: git://github.com/hubgit/fetch-resource.git
创建时间: 2017-03-28T23:36:49Z
项目社区:https://github.com/hubgit/fetch-resource

开源协议:

下载


fetch-resource

A wrapper for isomorphic-fetch that makes fetching JSON easier and handles query parameters.

Install

  1. npm install fetch-resource

or

  1. yarn add fetch-resource

Usage

  1. import resource from 'fetch-resource'
  2. const params = {
  3. q: 'language:javascript',
  4. sort: 'stars',
  5. order: 'desc'
  6. }
  7. resource('https://api.github.com/search/repositories', params)
  8. .fetch('json')
  9. .then(data => { })

Methods: fetch, update, create, kill

  1. import resource from 'fetch-resource'
  2. (async function () {
  3. // a resource representing the collection
  4. const collection = resource('https://example.com/items')
  5. // create a new resource
  6. const item = await collection.create({
  7. title: 'foo'
  8. })
  9. // update the item
  10. await item.update({
  11. title: 'bar'
  12. })
  13. // fetch the updated data
  14. const data = await item.fetch('json')
  15. // delete the item
  16. await item.kill()
  17. })()

Usage in Next.js

  1. import resource from 'fetch-resource'
  2. const Page = ({items}) => (
  3. <div>
  4. {items.map(item => (
  5. <div><a href={item.html_url}>{item.name}</a>: {item.stargazers_count}</div>
  6. ))}
  7. </div>
  8. )
  9. Page.getInitialProps = () => {
  10. return resource('https://api.github.com/search/repositories', {
  11. q: 'language:javascript',
  12. sort: 'stars',
  13. order: 'desc'
  14. }).fetch('json')
  15. }