Skip to content

Commit

Permalink
fix mediakey type, update readme examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Sep 26, 2024
1 parent 1b234bb commit 07f8e4e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

Avatar resolver library for both nodejs and browser.

## Note!: ENS-Avatar >= 1.0.0 is only compatible with ethers v6. If your project is using v5, keep your ens-avatar on latest 0.x version.
## Note!: ENS-Avatar >= 1.0.0 is only compatible with ethers v6. If your project is using v5, keep your ens-avatar on latest 0.x version.

## Getting started

### Prerequisites

- Have your web3 provider ready (web3.js, ethers.js)
- [Only for node env] Have jsdom installed.

Expand Down Expand Up @@ -35,72 +36,92 @@ const provider = new StaticJsonRpcProvider(
);
...
async function getAvatar() {
const avt = new AvatarResolver(provider);
const avatarURI = await avt.getAvatar('tanrikulu.eth', { /* jsdomWindow: jsdom (on nodejs) */ });
const resolver = new AvatarResolver(provider);
const avatarURI = await resolver.getAvatar('tanrikulu.eth', { /* jsdomWindow: jsdom (on nodejs) */ });
// avatarURI = https://ipfs.io/ipfs/QmUShgfoZQSHK3TQyuTfUpsc8UfeNfD8KwPUvDBUdZ4nmR
}

async function getHeader() {
const resolver = new AvatarResolver(provider);
const headerURI = await resolver.getHeader('tanrikulu.eth', { /* jsdomWindow: jsdom (on nodejs) */ });
// headerURI = https://ipfs.io/ipfs/QmRFnn6c9rj6NuHenFVyKXb6tuKxynAvGiw7yszQJ2EsjN
}

async function getAvatarMetadata() {
const avt = new AvatarResolver(provider);
const avatarMetadata = await avt.getMetadata('tanrikulu.eth');
const resolver = new AvatarResolver(provider);
const avatarMetadata = await resolver.getMetadata('tanrikulu.eth');
// avatarMetadata = { image: ... , uri: ... , name: ... , description: ... }
const avatarURI = avtUtils.getImageURI({ metadata /*, jsdomWindow: jsdom (on nodejs) */ });
const headerMetadata = await resolver.getMetadata('tanrikulu.eth', 'header');
// headerMetadata = { image: ... , uri: ... , name: ... , description: ... }
const avatarURI = avtUtils.getImageURI({ metadata: avatarMetadata /*, jsdomWindow: jsdom (on nodejs) */ });
// avatarURI = https://ipfs.io/ipfs/QmUShgfoZQSHK3TQyuTfUpsc8UfeNfD8KwPUvDBUdZ4nmR
}
```

## Supported avatar specs

### NFTs

- ERC721
- ERC1155

### URIs

- HTTP
- Base64
- IPFS

## Options

### Cache _(Default: Disabled)_

```js
const avt = new AvatarResolver(provider, { cache: 300 }); // 5 min response cache in memory
```

### Custom IPFS Gateway _(Default: https://ipfs.io)_

```js
const avt = new AvatarResolver(provider, { ipfs: 'https://dweb.link' });
```

### Custom Arweave Gateway _(Default: https://arweave.net)_

```js
const avt = new AvatarResolver(provider, { arweave: 'https://arweave.net' });
```

### Marketplace Api Keys _(Default: {})_

```js
const avt = new AvatarResolver(provider, {
apiKey: {
opensea: 'YOUR_API_KEY'
}
const avt = new AvatarResolver(provider, {
apiKey: {
opensea: 'YOUR_API_KEY',
},
});
```

### URL DenyList _(Default: [])_

```js
const avt = new AvatarResolver(provider, { urlDenyList: ['https://maliciouswebsite.com'] });
const avt = new AvatarResolver(provider, {
urlDenyList: ['https://maliciouswebsite.com'],
});
```

## Demo

- Create .env file with INFURA_KEY env variable
- Build the library

- Node example

```bash
node example/node.js ENS_NAME
```

- Browser example

```bash
yarn build:demo
http-server example
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.1",
"version": "1.0.2",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AvatarRequestOpts,
AvatarResolverOpts,
HeaderRequestOpts,
MediaKey,
Spec,
} from './types';

Expand All @@ -36,7 +37,7 @@ export interface AvatarResolver {
options?: AvatarResolverOpts;
getAvatar(ens: string, data: AvatarRequestOpts): Promise<string | null>;
getHeader(ens: string, data: HeaderRequestOpts): Promise<string | null>;
getMetadata(ens: string): Promise<any | null>;
getMetadata(ens: string, key?: MediaKey): Promise<any | null>;
}

export class AvatarResolver implements AvatarResolver {
Expand All @@ -55,7 +56,7 @@ export class AvatarResolver implements AvatarResolver {
}
}

async getMetadata(ens: string, key: string = 'avatar') {
async getMetadata(ens: string, key: MediaKey = 'avatar') {
// retrieve registrar address and resolver object from ens name
const [resolvedAddress, resolver] = await handleSettled([
this.provider.resolveName(ens),
Expand Down Expand Up @@ -122,7 +123,7 @@ export class AvatarResolver implements AvatarResolver {

async _getMedia(
ens: string,
mediaKey: string = 'avatar',
mediaKey: MediaKey = 'avatar',
data?: HeaderRequestOpts
) {
const metadata = await this.getMetadata(ens, mediaKey);
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface AxiosAgents {
httpsAgent?: Function;
}

export type MediaKey = 'avatar' | 'header' | 'banner';

export interface AvatarResolverOpts {
cache?: number;
ipfs?: string;
Expand All @@ -38,7 +40,7 @@ export interface AvatarRequestOpts {

export interface HeaderRequestOpts {
jsdomWindow?: any;
mediaKey?: 'header' | 'banner';
mediaKey?: Exclude<MediaKey, 'avatar'>;
}

export type Gateways = {
Expand Down

0 comments on commit 07f8e4e

Please sign in to comment.