2021-11-10 17:15:01 +03:00
|
|
|
/**
|
|
|
|
* @typedef {import('./oembed').ICustomProvider} ICustomProvider
|
|
|
|
* @typedef {import('./oembed').IExternalRequest} IExternalRequest
|
|
|
|
*/
|
|
|
|
|
|
|
|
const OPENSEA_PATH_REGEX = /^\/assets\/(0x[a-f0-9]+)\/(\d+)/;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @implements ICustomProvider
|
|
|
|
*/
|
|
|
|
class NFTOEmbedProvider {
|
|
|
|
/**
|
|
|
|
* @param {object} dependencies
|
|
|
|
*/
|
|
|
|
constructor(dependencies) {
|
|
|
|
this.dependencies = dependencies;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {URL} url
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async canSupportRequest(url) {
|
|
|
|
return url.host === 'opensea.io' && OPENSEA_PATH_REGEX.test(url.pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {URL} url
|
|
|
|
* @param {IExternalRequest} externalRequest
|
|
|
|
*
|
2021-11-16 22:28:36 +03:00
|
|
|
* @returns {Promise<object>}
|
2021-11-10 17:15:01 +03:00
|
|
|
*/
|
|
|
|
async getOEmbedData(url, externalRequest) {
|
|
|
|
const [match, transaction, asset] = url.pathname.match(OPENSEA_PATH_REGEX);
|
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-11-30 11:32:38 +03:00
|
|
|
const headers = {};
|
|
|
|
if (this.dependencies.config.apiKey) {
|
|
|
|
headers['X-API-KEY'] = this.dependencies.config.apiKey;
|
|
|
|
}
|
2021-11-16 22:28:36 +03:00
|
|
|
const result = await externalRequest(`https://api.opensea.io/api/v1/asset/${transaction}/${asset}/?format=json`, {
|
2021-11-30 11:32:38 +03:00
|
|
|
json: true,
|
|
|
|
headers
|
2021-11-10 17:15:01 +03:00
|
|
|
});
|
|
|
|
return {
|
|
|
|
version: '1.0',
|
2021-11-16 22:28:36 +03:00
|
|
|
type: 'nft',
|
2021-12-03 22:16:22 +03:00
|
|
|
title: result.body.name ? result.body.name : `#${result.body.token_id}`,
|
2021-11-10 17:15:01 +03:00
|
|
|
author_name: result.body.creator.user.username,
|
|
|
|
author_url: `https://opensea.io/${result.body.creator.user.username}`,
|
|
|
|
provider_name: 'OpenSea',
|
|
|
|
provider_url: 'https://opensea.io',
|
2021-11-15 11:14:45 +03:00
|
|
|
image_url: result.body.image_url,
|
|
|
|
creator_name: result.body.creator.user.username,
|
2021-11-17 16:05:34 +03:00
|
|
|
description: result.body.description,
|
|
|
|
collection_name: result.body.collection.name
|
2021-11-10 17:15:01 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NFTOEmbedProvider;
|