Skip to content

Commit

Permalink
add agent interceptor into global fetch instance (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu authored Jun 10, 2024
1 parent cb5aa7f commit f64e898
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 7 deletions.
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.0-alpha.1.ethers.6",
"version": "1.0.0-alpha.2.ethers.6",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import URI from './specs/uri';
import * as utils from './utils';
import {
BaseError,
createAgentAdapter,
createCacheAdapter,
fetch,
getImageURI,
Expand Down Expand Up @@ -36,6 +37,9 @@ export class AvatarResolver implements AvatarResolver {
if (options?.cache && options?.cache > 0) {
createCacheAdapter(fetch, options?.cache);
}
if (options?.agents) {
createAgentAdapter(fetch, options?.agents);
}
}

async getMetadata(ens: string) {
Expand Down
14 changes: 13 additions & 1 deletion src/specs/erc1155.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Contract, Provider } from 'ethers';
import { Buffer } from 'buffer/';
import { fetch, resolveURI } from '../utils';
import {
createAgentAdapter,
createCacheAdapter,
fetch,
resolveURI,
} from '../utils';
import { AvatarResolverOpts } from '../types';

const abi = [
Expand All @@ -26,6 +31,13 @@ export default class ERC1155 {
tokenID: string,
options?: AvatarResolverOpts
) {
if (options?.cache && options?.cache > 0) {
createCacheAdapter(fetch, options?.cache);
}
if (options?.agents) {
createAgentAdapter(fetch, options?.agents);
}

// exclude opensea api which does not follow erc1155 spec
const tokenIDHex = !tokenID.startsWith('https://api.opensea.io/')
? tokenID.replace('0x', '').padStart(64, '0')
Expand Down
14 changes: 13 additions & 1 deletion src/specs/erc721.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Contract, Provider } from 'ethers';
import { Buffer } from 'buffer/';
import { fetch, resolveURI } from '../utils';
import {
createAgentAdapter,
createCacheAdapter,
fetch,
resolveURI,
} from '../utils';
import { AvatarResolverOpts } from '../types';

const abi = [
Expand All @@ -16,6 +21,13 @@ export default class ERC721 {
tokenID: string,
options?: AvatarResolverOpts
) {
if (options?.cache && options?.cache > 0) {
createCacheAdapter(fetch, options?.cache);
}
if (options?.agents) {
createAgentAdapter(fetch, options?.agents);
}

const contract = new Contract(contractAddress, abi, provider);
const [tokenURI, owner] = await Promise.all([
contract.tokenURI(tokenID),
Expand Down
15 changes: 14 additions & 1 deletion src/specs/uri.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { AvatarResolverOpts } from '../types';
import { fetch, isImageURI, resolveURI } from '../utils';
import {
createAgentAdapter,
createCacheAdapter,
fetch,
isImageURI,
resolveURI,
} from '../utils';

export default class URI {
async getMetadata(uri: string, options?: AvatarResolverOpts) {
if (options?.cache && options?.cache > 0) {
createCacheAdapter(fetch, options?.cache);
}
if (options?.agents) {
createAgentAdapter(fetch, options?.agents);
}

const { uri: resolvedURI, isOnChain } = resolveURI(uri, options);
if (isOnChain) {
return resolvedURI;
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ export type MarketplaceAPIKey = Partial<
}
>;

export interface AxiosAgents {
httpAgent?: Function;
httpsAgent?: Function;
}

export interface AvatarResolverOpts {
cache?: number;
ipfs?: string;
arweave?: string;
apiKey?: MarketplaceAPIKey;
urlDenyList?: string[];
agents?: AxiosAgents;
}

export interface AvatarRequestOpts {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/detectPlatform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const isBrowser: boolean =
typeof window !== 'undefined' && typeof window.document !== 'undefined';

export { isBrowser };
27 changes: 25 additions & 2 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import axios, { Axios } from 'axios';
import { isBrowser } from './detectPlatform';
import { AxiosAgents } from '../types';

export function createAgentAdapter(fetch: Axios, agents?: AxiosAgents) {
if (!isBrowser && agents && Object.values(agents || {}).length) {
fetch.interceptors.request.use(config => {
config.httpAgent = agents.httpAgent;
config.httpsAgent = agents.httpsAgent;
return config;
});
}
}

export function createCacheAdapter(fetch: Axios, ttl: number) {
// creates cache adapter for axios
Expand All @@ -8,11 +20,22 @@ export function createCacheAdapter(fetch: Axios, ttl: number) {
});
}

function createFetcher({ ttl }: { ttl?: number }) {
const _fetch = axios.create({ proxy: false });
function createFetcher({
ttl,
agents,
}: {
ttl?: number;
agents?: AxiosAgents;
}) {
const _fetch = axios.create({
proxy: false,
});
if (ttl && ttl > 0) {
createCacheAdapter(_fetch, ttl);
}
if (Object.values(agents || {}).length) {
createAgentAdapter(_fetch, agents);
}
return _fetch;
}

Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { parseNFT } from './parseNFT';
import { BaseError } from './error';
import { convertToRawSVG, getImageURI } from './getImageURI';
import { resolveURI } from './resolveURI';
import { createCacheAdapter, fetch } from './fetch';
import { createAgentAdapter, createCacheAdapter, fetch } from './fetch';
import { isCID } from './isCID';
import { isImageURI } from './isImageURI';

export {
BaseError,
assert,
convertToRawSVG,
createAgentAdapter,
createCacheAdapter,
fetch,
getImageURI,
Expand Down

0 comments on commit f64e898

Please sign in to comment.