@metaplex-foundation/digital-asset-standard-api
Version:
Open-source specification for interacting with digital assets on Solana
193 lines • 8.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDasApiDecorator = void 0;
const errors_1 = require("./errors");
// Utility function to remove null and empty object properties
function cleanInput(obj) {
return Object.fromEntries(Object.entries(obj).filter(([_, value]) => value !== null &&
value !== undefined &&
(typeof value !== 'object' || (value && Object.keys(value).length > 0))));
}
const createDasApiDecorator = (rpc) => {
const validatePagination = (page, before, after) => {
if (typeof page === 'number' && (before || after)) {
throw new errors_1.DasApiError('Pagination Error. Please use either page or before/after, but not both.');
}
};
return {
...rpc,
getAsset: async (input) => {
const assetId = typeof input === 'object' && 'assetId' in input ? input.assetId : input;
const displayOptions = typeof input === 'object' && 'displayOptions' in input
? input.displayOptions
: {};
const asset = await rpc.call('getAsset', {
id: assetId,
options: displayOptions,
});
if (!asset)
throw new errors_1.DasApiError(`Asset not found: ${assetId}`);
return asset;
},
getAssets: async (input) => {
const assetIds = Array.isArray(input) ? input : input.assetIds;
const displayOptions = Array.isArray(input) ? {} : input.displayOptions;
const assets = await rpc.call('getAssets', {
ids: assetIds,
options: displayOptions,
});
if (!assets)
throw new errors_1.DasApiError(`No assets found: ${assetIds}`);
return assets;
},
getAssetProof: async (assetId) => {
const proof = await rpc.call('getAssetProof', { id: assetId });
if (!proof)
throw new errors_1.DasApiError(`No proof found for asset: ${assetId}`);
return proof;
},
getAssetProofs: async (assetIds) => {
const proofs = await rpc.call('getAssetProofs', { ids: assetIds });
if (!proofs)
throw new errors_1.DasApiError(`No proofs found for assets: ${assetIds}`);
return proofs;
},
getAssetsByAuthority: async (input) => {
validatePagination(input.page, input.before, input.after);
const cleanedInput = cleanInput({
authorityAddress: input.authority,
sortBy: input.sortBy,
limit: input.limit,
page: input.page,
before: input.before,
after: input.after,
displayOptions: input.displayOptions,
cursor: input.cursor,
});
const assetList = await rpc.call('getAssetsByAuthority', cleanedInput);
if (!assetList) {
throw new errors_1.DasApiError(`No assets found for authority: ${input.authority}`);
}
return assetList;
},
getAssetsByCreator: async (input) => {
validatePagination(input.page, input.before, input.after);
const cleanedInput = cleanInput({
creatorAddress: input.creator,
onlyVerified: input.onlyVerified,
sortBy: input.sortBy,
limit: input.limit,
page: input.page,
before: input.before,
after: input.after,
displayOptions: input.displayOptions,
cursor: input.cursor,
});
const assetList = await rpc.call('getAssetsByCreator', cleanedInput);
if (!assetList) {
throw new errors_1.DasApiError(`No assets found for creator: ${input.creator}`);
}
return assetList;
},
getAssetsByGroup: async (input) => {
validatePagination(input.page, input.before, input.after);
const cleanedInput = cleanInput({
groupKey: input.groupKey,
groupValue: input.groupValue,
sortBy: input.sortBy,
limit: input.limit,
page: input.page,
before: input.before,
after: input.after,
displayOptions: input.displayOptions,
cursor: input.cursor,
});
const assetList = await rpc.call('getAssetsByGroup', cleanedInput);
if (!assetList) {
throw new errors_1.DasApiError(`No assets found for group: ${input.groupKey} => ${input.groupValue}`);
}
return assetList;
},
getAssetsByOwner: async (input) => {
validatePagination(input.page, input.before, input.after);
const cleanedInput = cleanInput({
ownerAddress: input.owner,
sortBy: input.sortBy,
limit: input.limit,
page: input.page,
before: input.before,
after: input.after,
displayOptions: input.displayOptions,
cursor: input.cursor,
});
const assetList = await rpc.call('getAssetsByOwner', cleanedInput);
if (!assetList) {
throw new errors_1.DasApiError(`No assets found for owner: ${input.owner}`);
}
return assetList;
},
searchAssets: async (input) => {
validatePagination(input.page, input.before, input.after);
const cleanedInput = cleanInput({
negate: input.negate,
conditionType: input.conditionType,
interface: input.interface,
ownerAddress: input.owner,
ownerType: input.ownerType,
creatorAddress: input.creator,
creatorVerified: input.creatorVerified,
authorityAddress: input.authority,
grouping: input.grouping,
delegateAddress: input.delegate,
frozen: input.frozen,
supply: input.supply,
supplyMint: input.supplyMint,
compressed: input.compressed,
compressible: input.compressible,
royaltyTargetType: input.royaltyModel,
royaltyTarget: input.royaltyTarget,
royaltyAmount: input.royaltyAmount,
burnt: input.burnt,
sortBy: input.sortBy,
limit: input.limit,
page: input.page,
before: input.before,
after: input.after,
jsonUri: input.jsonUri,
cursor: input.cursor,
name: input.name,
displayOptions: input.displayOptions,
tokenType: input.tokenType,
});
const assetList = await rpc.call('searchAssets', cleanedInput);
if (!assetList) {
throw new errors_1.DasApiError('No assets found for the given search criteria');
}
return assetList;
},
getAssetSignatures: async (input) => {
validatePagination(input.page, input.before, input.after);
const cleanedInput = cleanInput({
id: input.assetId,
limit: input.limit,
page: input.page,
before: input.before,
after: input.after,
tree: input.tree,
leafIndex: input.leaf_index,
cursor: input.cursor,
sortDirection: input.sort_direction,
});
const signatures = await rpc.call('getAssetSignatures', cleanedInput);
if (!signatures) {
const identifier = 'assetId' in input
? `asset: ${input.assetId}`
: `tree: ${input.tree}, leaf_index: ${input.leaf_index}`;
throw new errors_1.DasApiError(`No signatures found for ${identifier}`);
}
return signatures;
},
};
};
exports.createDasApiDecorator = createDasApiDecorator;
//# sourceMappingURL=decorator.js.map