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
- Host: GitHub
- URL: https://github.com/hubgit/fetch-resource
- Owner: hubgit
- Created: 2017-03-28T23:36:49.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-04-26T10:32:10.000Z (about 4 years ago)
- Last Synced: 2026-03-01T19:48:54.011Z (4 months ago)
- Topics: fetch, json, nodejs, resource
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/fetch-resource
- Size: 548 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
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}) => (
)
Page.getInitialProps = () => {
return resource('https://api.github.com/search/repositories', {
q: 'language:javascript',
sort: 'stars',
order: 'desc'
}).fetch('json')
}
```