applesauce-core
Advanced tools
Comparing version 0.0.0-next-20241114194041 to 0.0.0-next-20241115160057
import { NostrEvent } from "nostr-tools"; | ||
export declare const ProfileContentSymbol: unique symbol; | ||
declare module "nostr-tools" { | ||
interface Event { | ||
[ProfileContentSymbol]?: ProfileContent | Error; | ||
} | ||
} | ||
export type ProfileContent = { | ||
@@ -24,3 +19,1 @@ name?: string; | ||
export declare function getProfileContent(event: NostrEvent): ProfileContent; | ||
export declare function getProfileContent(event: NostrEvent, quite: false): ProfileContent; | ||
export declare function getProfileContent(event: NostrEvent, quite: true): ProfileContent | Error; |
@@ -0,28 +1,16 @@ | ||
import { getOrComputeCachedValue } from "./cache.js"; | ||
export const ProfileContentSymbol = Symbol.for("profile-content"); | ||
export function getProfileContent(event, quite = false) { | ||
let cached = event[ProfileContentSymbol]; | ||
if (!cached) { | ||
try { | ||
const profile = JSON.parse(event.content); | ||
// ensure nip05 is a string | ||
if (profile.nip05 && typeof profile.nip05 !== "string") | ||
profile.nip05 = String(profile.nip05); | ||
cached = event[ProfileContentSymbol] = profile; | ||
/** Returns the parsed profile content for a kind 0 event */ | ||
export function getProfileContent(event) { | ||
return getOrComputeCachedValue(event, ProfileContentSymbol, () => { | ||
const profile = JSON.parse(event.content); | ||
// ensure nip05 is a string | ||
if (profile.nip05 && typeof profile.nip05 !== "string") | ||
profile.nip05 = String(profile.nip05); | ||
// add missing protocol to website | ||
if (profile.website?.startsWith("http") === false) { | ||
profile.website = "https://" + profile.website; | ||
} | ||
catch (e) { | ||
if (e instanceof Error) | ||
cached = event[ProfileContentSymbol] = e; | ||
} | ||
} | ||
if (cached === undefined) { | ||
throw new Error("Failed to parse profile"); | ||
} | ||
else if (cached instanceof Error) { | ||
if (!quite) | ||
throw cached; | ||
else | ||
return cached; | ||
} | ||
else | ||
return cached; | ||
return profile; | ||
}); | ||
} |
{ | ||
"name": "applesauce-core", | ||
"version": "0.0.0-next-20241114194041", | ||
"version": "0.0.0-next-20241115160057", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
118275
3003