An open API service indexing awesome lists of open source software.

https://github.com/hubgit/fetch-resource

A minimal interface for fetching resources from the web
https://github.com/hubgit/fetch-resource

fetch json nodejs resource

Last synced: about 2 months ago
JSON representation

A minimal interface for fetching resources from the web

Awesome Lists containing this project

README

          

# fetch-resource

A wrapper for [isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch) that makes fetching JSON easier and handles query parameters.

## Install

```sh
npm install fetch-resource
```

or

```sh
yarn add fetch-resource
```

## Usage

```js
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

```js
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

```jsx
import resource from 'fetch-resource'

const Page = ({items}) => (


{items.map(item => (

{item.name}: {item.stargazers_count}

))}

)

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