Ghost/core/server/services/nft-oembed.js
Fabien O'Carroll 31e103be9d Updated NFT OEmbed provider to return metadata
refs https://github.com/TryGhost/Team/issues/1211

Instead of rendering the HTML as an embed - we will send back the
necessary data. This will allow us to keep all the knowledge of HTML
structure in the Koenig repository.
2021-11-15 10:31:35 +02:00

61 lines
1.7 KiB
JavaScript

/**
* @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
*
* @returns {Promise<import('oembed-parser').RichTypeData & Object<string, any>>}
*/
async getOEmbedData(url, externalRequest) {
const [match, transaction, asset] = url.pathname.match(OPENSEA_PATH_REGEX);
if (!match) {
return null;
}
const result = await externalRequest(`https://api.opensea.io/api/v1/asset/${transaction}/${asset}/`, {
json: true
});
return {
version: '1.0',
type: 'rich',
title: result.body.name,
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',
html: '',
width: 1000,
height: 1000,
card_type: 'nft',
image_url: result.body.image_url,
creator_name: result.body.creator.user.username,
description: result.body.description
};
}
}
module.exports = NFTOEmbedProvider;