web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit c43f655b4f44f59608e8df332ae9650a6344d021
parent 732ec0f9c84e5d0cdfee4665eacf23a405e1e4fd
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Thu, 10 Apr 2025 17:59:59 +0000

utils-nostr: add nostr relay service

Diffstat:
Mutils-nostr/src/index.ts | 3+++
Autils-nostr/src/lib/relay.ts | 32++++++++++++++++++++++++++++++++
Autils-nostr/src/services/relay/lib.ts | 17+++++++++++++++++
Autils-nostr/src/services/relay/types.ts | 6++++++
4 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/utils-nostr/src/index.ts b/utils-nostr/src/index.ts @@ -1,11 +1,14 @@ export * from "./lib/events" export * from "./lib/keys" export * from "./lib/ndk" +export * from "./lib/relay" export * from "./lib/tags" export * from "./lib/types" export * from "./services/events/lib" export * from "./services/events/types" export * from "./services/keys/lib" export * from "./services/keys/types" +export * from "./services/relay/lib" +export * from "./services/relay/types" export * from "./types" export * from "./util" diff --git a/utils-nostr/src/lib/relay.ts b/utils-nostr/src/lib/relay.ts @@ -0,0 +1,32 @@ +export type NostrRelayInformationDocument = { + id?: string; + name?: string; + description?: string; + pubkey?: string; + contact?: string; + supported_nips?: number[]; + software?: string; + version?: string; + limitation_payment_required?: string; + limitation_restricted_writes?: boolean; +} + +export type NostrRelayInformationDocumentFields = { [K in keyof NostrRelayInformationDocument]: string; }; + +export const lib_nostr_relay_parse_information_document = (data: any): NostrRelayInformationDocument | undefined => { + const obj = JSON.parse(data); + return { + id: typeof obj.id === 'string' ? obj.id : undefined, + name: typeof obj.name === 'string' ? obj.name : undefined, + description: typeof obj.description === 'string' ? obj.description : undefined, + pubkey: typeof obj.pubkey === 'string' ? obj.pubkey : undefined, + contact: typeof obj.contact === 'string' ? obj.contact : undefined, + supported_nips: Array.isArray(obj.supported_nips) && obj.supported_nips.every((nip: any) => typeof nip === 'number') + ? obj.supported_nips + : undefined, + software: typeof obj.software === 'string' ? obj.software : undefined, + version: typeof obj.version === 'string' ? obj.version : undefined, + limitation_payment_required: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.payment_required === 'string' ? obj.limitation.payment_required : undefined, + limitation_restricted_writes: obj.limitation && typeof obj.limitation === 'object' && typeof obj.limitation.restricted_writes === 'boolean' ? obj.limitation.restricted_writes : undefined, + }; +}; diff --git a/utils-nostr/src/services/relay/lib.ts b/utils-nostr/src/services/relay/lib.ts @@ -0,0 +1,16 @@ +import { lib_nostr_relay_parse_information_document, type INostrRelayService, type NostrRelayInformationDocument, type NostrRelayInformationDocumentFields } from "$root"; + +export class NostrRelayService implements INostrRelayService { + public parse_information_document = (data: any): NostrRelayInformationDocumentFields | undefined => { + const doc = lib_nostr_relay_parse_information_document(data); + if (!doc) return; + const result: Partial<NostrRelayInformationDocumentFields> = {}; + Object.entries(doc).forEach(([key, value]) => { + if (typeof value === 'boolean') result[key as keyof NostrRelayInformationDocument] = value ? '1' : '0'; + else if (Array.isArray(value)) result[key as keyof NostrRelayInformationDocument] = value.join(', '); + else if (value === null || value === undefined) result[key as keyof NostrRelayInformationDocument] = ''; + else result[key as keyof NostrRelayInformationDocument] = String(value); + }); + return result; + }; +} +\ No newline at end of file diff --git a/utils-nostr/src/services/relay/types.ts b/utils-nostr/src/services/relay/types.ts @@ -0,0 +1,6 @@ +import { type NostrRelayInformationDocumentFields } from "$root"; + +export type INostrRelayService = { + parse_information_document: (data: any) => NostrRelayInformationDocumentFields | undefined; +}; +