Skip to content

Commit

Permalink
FetchWrapper (Node & non-React support) (#46)
Browse files Browse the repository at this point in the history
FetchWrapper is a class that allows to use the library with other frontend libraries than React,
or with NodeJS. Unline the `useNft()` hook, `FetchWrapper#fetchNft()` does not retry, cache,
or do anything else than attempting to fetch the NFT data once.
  • Loading branch information
gretzke authored Apr 27, 2021
1 parent 5bb26c9 commit 4f76ce5
Show file tree
Hide file tree
Showing 11 changed files with 10,251 additions and 7 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The useNft() hook requires two arguments: the NFT `contract` address, and its to
The returned value is an object containing information about the loading state:

```tsx
const result = useNft("0xd07dc4262bcdbf85190c01c996b4c06a461d2430", "90473")
const result = useNft("0xd07dc4262bcdbf85190c01c996b4c06a461d2430", "90473")

// one of "error", "loading" and "done"
result.status
Expand Down Expand Up @@ -151,6 +151,27 @@ type NftMetadata = {
See the implementation of the [Ethers](https://github.com/spectrexyz/use-nft/blob/38bd803f20e778b9bb684d682c194a812a94a05c/src/fetchers/ethers/index.tsx#L12-L42) and [Ethereum](https://github.com/spectrexyz/use-nft/blob/38bd803f20e778b9bb684d682c194a812a94a05c/src/fetchers/ethereum/index.tsx#L12-L42) fetchers for more details.
### FetchWrapper
`FetchWrapper` is a class that allows to use the library with other frontend libraries than React, or with NodeJS. Unline the `useNft()` hook, `FetchWrapper#fetchNft()` does not retry, cache, or do anything else than attempting to fetch the NFT data once.
```js
import { FetchWrapper } from "use-nft"
```

Pass the fetcher declaration to the `FetchWrapper` and call the `fetchNft` function to retreive the NFT data.

```js
const fetcher = ["ethers", { ethers, provider: ethers.getDefaultProvider() }]
const fetchWrapper = new FetchWrapper(fetcher)
const result = await fetchWrapper.fetchNft(
"0xd07dc4262bcdbf85190c01c996b4c06a461d2430",
"90473"
)
```

The `fetchNft()` function returns a promise which resolves to an `NftMetadata` object.

## License

[MIT](LICENSE)
1 change: 1 addition & 0 deletions examples/fetchWrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.yarn/
23 changes: 23 additions & 0 deletions examples/fetchWrapper/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const ethers = require("ethers")
const FetchWrapper = require("use-nft").FetchWrapper

// nodejs does not have a fetch function, set it here
// not required for applications that run in a browser
const fetch = require("node-fetch")
if (!globalThis.fetch) {
globalThis.fetch = fetch
}

const fetchNfts = async () => {
const fetcher = ["ethers", { ethers, provider: ethers.getDefaultProvider() }]
const fetchWrapper = new FetchWrapper(fetcher)

const nft = await fetchWrapper.fetchNft(
"0xd07dc4262bcdbf85190c01c996b4c06a461d2430",
"90473"
)

console.log(nft)
}

fetchNfts()
29 changes: 29 additions & 0 deletions examples/fetchWrapper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "fetchwrapper",
"version": "1.0.0",
"description": "Example for FetchWrapper",
"main": "index.js",
"scripts": {
"dev": "esnext run index.js --watch",
"build": "esnext build index.js dist/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/spectrexyz/use-nft.git"
},
"author": "Daniel Gretzke",
"license": "ISC",
"bugs": {
"url": "https://github.com/spectrexyz/use-nft/issues"
},
"homepage": "https://github.com/spectrexyz/use-nft#readme",
"dependencies": {
"ethers": "^5.1.3",
"node-fetch": "^2.6.1",
"use-nft": "portal:../.."
},
"devDependencies": {
"esnext-scripts": "^2.1.1",
"nodemon": "^2.0.7"
}
}
Loading

0 comments on commit 4f76ce5

Please sign in to comment.