fetch-resource

1.0.0 • Public • Published

fetch-resource

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

Install

npm install fetch-resource

or

yarn add fetch-resource

Usage

import resource from 'fetch-resource'

const params = {
  q: 'language:javascript',
  sort: 'stars',
  order: 'desc'
}

resource('https://api.github.com/search/repositories', params)
  .fetch('json')
  .then(data => { })

Methods: fetch, update, create, kill

import resource from 'fetch-resource'

(async function () {
  // a resource representing the collection
  const collection = resource('https://example.com/items')
  
  // create a new resource
  const item = await collection.create({
    title: 'foo'
  })
  
  // update the item
  await item.update({
    title: 'bar'
  })
  
  // fetch the updated data
  const data = await item.fetch('json')
  
  // delete the item
  await item.kill()
})()

Usage in Next.js

import resource from 'fetch-resource'

const Page = ({items}) => (
  <div>
    {items.map(item => (
      <div><a href={item.html_url}>{item.name}</a>: {item.stargazers_count}</div>
    ))}
  </div>
)

Page.getInitialProps = () => {
  return resource('https://api.github.com/search/repositories', {
    q: 'language:javascript',
    sort: 'stars',
    order: 'desc'
  }).fetch('json')
}

Readme

Keywords

none

Package Sidebar

Install

npm i fetch-resource

Weekly Downloads

3

Version

1.0.0

License

ISC

Unpacked Size

5.02 kB

Total Files

3

Last publish

Collaborators

  • aeaton