UNPKG

@self.id/3box-legacy

Version:
72 lines (71 loc) 2.57 kB
import crossFetch from 'cross-fetch'; export async function loadLegacy3BoxProfile(address, fetchFunc = crossFetch) { try { const res = await fetchFunc(`https://ipfs.3box.io/profile?address=${address}`); return res.ok ? await res.json() : null; } catch (err) { return null; } } // Validation for BasicProfile const lengthIndex = { name: 150, description: 420, location: 140, website: 240, emoji: 2, employer: 140, school: 140 }; const isStrAndLen = (obj, key)=>{ if (!lengthIndex[key]) return false; return typeof obj[key] === 'string' && obj[key].length <= lengthIndex[key]; }; export const transformProfile = (profile)=>{ const transform = {}; let image, background; if (isStrAndLen(profile, 'name')) transform.name = profile.name; if (isStrAndLen(profile, 'description')) transform.description = profile.description; if (isStrAndLen(profile, 'location')) transform.homeLocation = profile.location; if (isStrAndLen(profile, 'website')) transform.url = profile.website; if (isStrAndLen(profile, 'emoji')) transform.emoji = profile.emoji; if (isStrAndLen(profile, 'employer')) transform.affiliations = [ profile.employer ]; if (isStrAndLen(profile, 'school')) { transform.affiliations = (transform.affiliations || []).concat([ profile.school ]); } // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (Array.isArray(profile.image)) image = profile.image[0]?.contentUrl['/']; if (image != null && typeof image === 'string') { transform.image = { original: { src: `ipfs://${image}`, mimeType: 'application/octet-stream', width: 170, height: 170 } }; } if (Array.isArray(profile.coverPhoto)) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access background = profile.coverPhoto[0]?.contentUrl['/']; } if (background != null && typeof background === 'string') { transform.background = { original: { src: `ipfs://${background}`, mimeType: 'application/octet-stream', width: 1000, height: 175 } }; } return transform; }; export async function getLegacy3BoxProfileAsBasicProfile(address, fetchFunc) { const profile = await loadLegacy3BoxProfile(address, fetchFunc); return profile ? transformProfile(profile) : null; }