opensea-js
Version:
TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs and our marketplace data
122 lines (112 loc) • 2.76 kB
text/typescript
import {
getListNFTsByCollectionPath,
getListNFTsByContractPath,
getNFTPath,
getRefreshMetadataPath,
getListNFTsByAccountPath,
getContractPath,
} from "./apiPaths";
import { ListNFTsResponse, GetNFTResponse, GetContractResponse } from "./types";
import { Chain } from "../types";
import { Fetcher } from "./fetcher";
/**
* NFT-related API operations
*/
export class NFTsAPI {
constructor(
private fetcher: Fetcher,
private chain: Chain,
) {}
/**
* Fetch multiple NFTs for a collection.
*/
async getNFTsByCollection(
slug: string,
limit: number | undefined = undefined,
next: string | undefined = undefined,
): Promise<ListNFTsResponse> {
const response = await this.fetcher.get<ListNFTsResponse>(
getListNFTsByCollectionPath(slug),
{
limit,
next,
},
);
return response;
}
/**
* Fetch multiple NFTs for a contract.
*/
async getNFTsByContract(
address: string,
limit: number | undefined = undefined,
next: string | undefined = undefined,
chain: Chain = this.chain,
): Promise<ListNFTsResponse> {
const response = await this.fetcher.get<ListNFTsResponse>(
getListNFTsByContractPath(chain, address),
{
limit,
next,
},
);
return response;
}
/**
* Fetch NFTs owned by an account.
*/
async getNFTsByAccount(
address: string,
limit: number | undefined = undefined,
next: string | undefined = undefined,
chain = this.chain,
): Promise<ListNFTsResponse> {
const response = await this.fetcher.get<ListNFTsResponse>(
getListNFTsByAccountPath(chain, address),
{
limit,
next,
},
);
return response;
}
/**
* Fetch metadata, traits, ownership information, and rarity for a single NFT.
*/
async getNFT(
address: string,
identifier: string,
chain = this.chain,
): Promise<GetNFTResponse> {
const response = await this.fetcher.get<GetNFTResponse>(
getNFTPath(chain, address, identifier),
);
return response;
}
/**
* Force refresh the metadata for an NFT.
*/
async refreshNFTMetadata(
address: string,
identifier: string,
chain: Chain = this.chain,
): Promise<Response> {
const response = await this.fetcher.post<Response>(
getRefreshMetadataPath(chain, address, identifier),
{},
);
return response;
}
/**
* Fetch smart contract information for a given chain and address.
*/
async getContract(
address: string,
chain: Chain = this.chain,
): Promise<GetContractResponse> {
const response = await this.fetcher.get<GetContractResponse>(
getContractPath(chain, address),
);
return response;
}
}