diff --git a/README.md b/README.md index 8911af8..ff03300 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ import "websocket-polyfill"; ### Basics ```ts -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; const nHoursAgo = (hrs: number): number => Math.floor((Date.now() - hrs * 60 * 60 * 1000) / 1000); @@ -50,7 +50,7 @@ const relayUrls = [/* relay URLs */]; const postIter = fetcher.allEventsIterator( relayUrls, /* filter (kinds, authors, ids, tags) */ - { kinds: [ eventKind.text ] }, + { kinds: [ 1 ] }, /* time range filter (since, until) */ { since: nHoursAgo(24) }, /* fetch options (optional) */ @@ -64,7 +64,7 @@ for await (const ev of postIter) { const allPosts = await fetcher.fetchAllEvents( relayUrls, /* filter */ - { kinds: [ eventKind.text ] }, + { kinds: [ 1 ] }, /* time range filter */ { since: nHoursAgo(24) }, /* fetch options (optional) */ @@ -75,7 +75,7 @@ const allPosts = await fetcher.fetchAllEvents( ### Various Fetch Methods ```ts -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; const fetcher = NostrFetcher.init(); const relayUrls = [/* relay URLs */]; @@ -88,7 +88,7 @@ const relayUrls = [/* relay URLs */]; const latestPosts: NostrEvent[] = await fetcher.fetchLatestEvents( relayUrls, /* filter */ - { kinds: [ eventKind.text ] }, + { kinds: [ 1 ] }, /* number of events to fetch */ 100, ); @@ -100,7 +100,7 @@ const latestPosts: NostrEvent[] = await fetcher.fetchLatestEvents( const lastMetadata: NostrEvent | undefined = await fetcher.fetchLastEvent( relayUrls, /* filter */ - { kinds: [ eventKind.metadata ], authors: [ "deadbeef..." ] }, + { kinds: [ 0 ], authors: [ "deadbeef..." ] }, ); // fetches latest 10 text posts from each author in `authors` @@ -113,7 +113,7 @@ const postsPerAuthor = fetcher.fetchLatestEventsPerAuthor( relayUrls, }, /* filter */ - { kinds: [ eventKind.text ] }, + { kinds: [ 1 ] }, /* number of events to fetch for each author */ 10, ); @@ -134,7 +134,7 @@ const metadataPerAuthor = fetcher.fetchLastEventPerAuthor( relayUrls, } /* filter */ - { kinds: [ eventKind.metadata ] }, + { kinds: [ 0 ] }, ); for await (const { author, event } of metadataPerAuthor ) { console.log(`${author}: ${event?.content ?? "not found"}`); @@ -152,7 +152,7 @@ npm install @nostr-fetch/adapter-nostr-tools Then, wrap your relay pool instance with the adapter and pass it to the initializer `NostrFetcher.withCustomPool()`. ```ts -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import { simplePoolAdapter } from "@nostr-fetch/adapter-nostr-tools"; import { SimplePool } from "nostr-tools"; @@ -177,7 +177,7 @@ const fetcher = NostrFetcher.withCustomPool(simplePoolAdapter(pool)); ### Cancelling by AbortController ```ts -import { eventKind, NostrFecher } from "nostr-fetch" +import { NostrFecher } from "nostr-fetch" const fetcher = NostrFetcher.init(); const relayUrls = [/* relay URLs */]; diff --git a/packages/examples/src/abort.ts b/packages/examples/src/abort.ts index 438c157..0d5e05c 100644 --- a/packages/examples/src/abort.ts +++ b/packages/examples/src/abort.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays, nHoursAgo } from "./utils"; @@ -10,7 +10,7 @@ const main = async () => { const evIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(24), diff --git a/packages/examples/src/allEventsIter.ts b/packages/examples/src/allEventsIter.ts index fa40713..48960a4 100644 --- a/packages/examples/src/allEventsIter.ts +++ b/packages/examples/src/allEventsIter.ts @@ -1,4 +1,4 @@ -import { NostrFetcher, eventKind } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays, nHoursAgo } from "./utils"; @@ -11,7 +11,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/backpressure.ts b/packages/examples/src/backpressure.ts index 09dc411..142a15b 100644 --- a/packages/examples/src/backpressure.ts +++ b/packages/examples/src/backpressure.ts @@ -1,5 +1,5 @@ import { setTimeout as delay } from "node:timers/promises"; -import { NostrFetcher, eventKind } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays, nHoursAgo } from "./utils"; @@ -12,7 +12,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(3), diff --git a/packages/examples/src/fetchAll.ts b/packages/examples/src/fetchAll.ts index 78f2e93..d2dd1d0 100644 --- a/packages/examples/src/fetchAll.ts +++ b/packages/examples/src/fetchAll.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays, nHoursAgo } from "./utils"; @@ -10,7 +10,7 @@ const main = async () => { const events = await fetcher.fetchAllEvents( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/fetchLast.ts b/packages/examples/src/fetchLast.ts index 5708127..bbf4f7c 100644 --- a/packages/examples/src/fetchLast.ts +++ b/packages/examples/src/fetchLast.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays } from "./utils"; @@ -8,7 +8,7 @@ const main = async () => { // fetch the last metadata event (kind 0) and contact list event (kind 3) published by the pubkey from the relays const [lastMetadata, lastContacts] = await Promise.all( - [eventKind.metadata, eventKind.contacts].map((kind) => + [0, 3].map((kind) => fetcher.fetchLastEvent(defaultRelays, { kinds: [kind], authors: ["d1d1747115d16751a97c239f46ec1703292c3b7e9988b9ebdd4ec4705b15ed44"], diff --git a/packages/examples/src/fetchLastPerAuthor.ts b/packages/examples/src/fetchLastPerAuthor.ts index 8e9a6ca..1b85adc 100644 --- a/packages/examples/src/fetchLastPerAuthor.ts +++ b/packages/examples/src/fetchLastPerAuthor.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays } from "./utils"; @@ -17,7 +17,7 @@ const main = async () => { // get pubkeys of followees from the latest kind 3 event const lastFollowEvent = await fetcher.fetchLastEvent(defaultRelays, { - kinds: [eventKind.contacts], + kinds: [3], authors: [pubkey], }); if (lastFollowEvent === undefined) { @@ -32,7 +32,7 @@ const main = async () => { const profilePerAuthor = fetcher.fetchLastEventPerAuthor( { authors: followees, relayUrls: defaultRelays }, { - kinds: [eventKind.metadata], + kinds: [0], }, ); diff --git a/packages/examples/src/fetchLatest.ts b/packages/examples/src/fetchLatest.ts index 7301d69..a52e3c9 100644 --- a/packages/examples/src/fetchLatest.ts +++ b/packages/examples/src/fetchLatest.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays } from "./utils"; @@ -10,7 +10,7 @@ const main = async () => { const latestPosts = await fetcher.fetchLatestEvents( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, 100, // { skipVerification: true } diff --git a/packages/examples/src/fetchLatestAsOf.ts b/packages/examples/src/fetchLatestAsOf.ts index b8e551f..40be63f 100644 --- a/packages/examples/src/fetchLatestAsOf.ts +++ b/packages/examples/src/fetchLatestAsOf.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays } from "./utils"; @@ -10,7 +10,7 @@ const main = async () => { const latestPosts = await fetcher.fetchLatestEvents( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, 100, { asOf: Math.floor(new Date("2023-08-31T12:00:00Z").getTime() / 1000) }, diff --git a/packages/examples/src/fetchLatestPerAuthor.ts b/packages/examples/src/fetchLatestPerAuthor.ts index 741cfa1..e0759ea 100644 --- a/packages/examples/src/fetchLatestPerAuthor.ts +++ b/packages/examples/src/fetchLatestPerAuthor.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays } from "./utils"; @@ -14,7 +14,7 @@ const main = async () => { // get pubkeys of followees from the latest kind 3 event const lastFollowEvent = await fetcher.fetchLastEvent(defaultRelays, { - kinds: [eventKind.contacts], + kinds: [3], authors: [pubkey], }); if (lastFollowEvent === undefined) { @@ -28,7 +28,7 @@ const main = async () => { // get latest 10 posts for each followee const latestPostsPerFollowee = fetcher.fetchLatestEventsPerAuthor( { authors: followees, relayUrls: defaultRelays }, - { kinds: [eventKind.text] }, + { kinds: [1] }, 10, ); diff --git a/packages/examples/src/interop/ndk.ts b/packages/examples/src/interop/ndk.ts index 3d279cd..eb45c23 100644 --- a/packages/examples/src/interop/ndk.ts +++ b/packages/examples/src/interop/ndk.ts @@ -1,6 +1,6 @@ import NDK from "@nostr-dev-kit/ndk"; import { ndkAdapter } from "@nostr-fetch/adapter-ndk"; -import { NostrFetcher, eventKind, normalizeRelayUrlSet } from "nostr-fetch"; +import { NostrFetcher, normalizeRelayUrlSet } from "nostr-fetch"; import { defaultRelays, nHoursAgo } from "../utils"; import "websocket-polyfill"; @@ -21,7 +21,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/interop/relayPoolTs.ts b/packages/examples/src/interop/relayPoolTs.ts index 638c0a5..a97f80d 100644 --- a/packages/examples/src/interop/relayPoolTs.ts +++ b/packages/examples/src/interop/relayPoolTs.ts @@ -1,5 +1,5 @@ import { relayPoolAdapter } from "@nostr-fetch/adapter-nostr-relaypool"; -import { NostrFetcher, eventKind } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import { RelayPool } from "nostr-relaypool"; import { defaultRelays, nHoursAgo } from "../utils"; @@ -13,7 +13,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/interop/rxNostr.ts b/packages/examples/src/interop/rxNostr.ts index 7d4c5cf..f46a8f8 100644 --- a/packages/examples/src/interop/rxNostr.ts +++ b/packages/examples/src/interop/rxNostr.ts @@ -1,5 +1,5 @@ import { rxNostrAdapter } from "@nostr-fetch/adapter-rx-nostr"; -import { NostrFetcher, eventKind } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import { createRxNostr } from "rx-nostr"; import "websocket-polyfill"; @@ -14,7 +14,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/interop/simplePool.ts b/packages/examples/src/interop/simplePool.ts index 5266892..e71afbf 100644 --- a/packages/examples/src/interop/simplePool.ts +++ b/packages/examples/src/interop/simplePool.ts @@ -1,5 +1,5 @@ import { simplePoolAdapter } from "@nostr-fetch/adapter-nostr-tools"; -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import { SimplePool } from "nostr-tools"; import "websocket-polyfill"; @@ -14,7 +14,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/interop/simplePoolV2.ts b/packages/examples/src/interop/simplePoolV2.ts index d7d4589..0db680b 100644 --- a/packages/examples/src/interop/simplePoolV2.ts +++ b/packages/examples/src/interop/simplePoolV2.ts @@ -1,5 +1,5 @@ import { simplePoolAdapter } from "@nostr-fetch/adapter-nostr-tools-v2"; -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import { SimplePool, useWebSocketImplementation } from "nostr-tools-v2"; import ws from "ws"; @@ -15,7 +15,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/outboxModel.ts b/packages/examples/src/outboxModel.ts index f4a705e..a83b124 100644 --- a/packages/examples/src/outboxModel.ts +++ b/packages/examples/src/outboxModel.ts @@ -1,5 +1,5 @@ import { setTimeout } from "node:timers/promises"; -import { type NostrEvent, NostrFetcher, eventKind } from "nostr-fetch"; +import { type NostrEvent, NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays, getWriteRelaysFromEvent } from "./utils"; @@ -15,7 +15,7 @@ const fetcher = NostrFetcher.init({}); // fetch pubkeys of followees from the latest kind 3 event const fetchFollowees = async (pubkey: string): Promise => { const ev = await fetcher.fetchLastEvent(defaultRelays, { - kinds: [eventKind.contacts], + kinds: [3], authors: [pubkey], }); if (ev === undefined) { @@ -29,7 +29,7 @@ const fetchWriteRelaysPerAuthors = async (authors: string[]): Promise(); @@ -56,7 +56,7 @@ const main = async () => { const lastPostsPerFollowee = fetcher.fetchLastEventPerAuthor( writeRelaysPerFollowees, { - kinds: [eventKind.text], + kinds: [1], }, { statsListener: (stats) => console.error(stats) }, ); diff --git a/packages/examples/src/search.ts b/packages/examples/src/search.ts index 99598df..2d64576 100644 --- a/packages/examples/src/search.ts +++ b/packages/examples/src/search.ts @@ -1,4 +1,4 @@ -import { NostrFetcher, eventKind } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { nHoursAgo } from "./utils"; @@ -18,7 +18,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( searchRelays, { - kinds: [eventKind.text], + kinds: [1], search: searchQuery, }, { diff --git a/packages/examples/src/seenOn.ts b/packages/examples/src/seenOn.ts index 981bc27..f3af89c 100644 --- a/packages/examples/src/seenOn.ts +++ b/packages/examples/src/seenOn.ts @@ -1,4 +1,4 @@ -import { eventKind, NostrFetcher } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays } from "./utils"; @@ -10,7 +10,7 @@ const main = async () => { const latestPosts = await fetcher.fetchLatestEvents( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, 100, { withSeenOn: false }, diff --git a/packages/examples/src/statsListener.ts b/packages/examples/src/statsListener.ts index 91942f0..735e33c 100644 --- a/packages/examples/src/statsListener.ts +++ b/packages/examples/src/statsListener.ts @@ -1,4 +1,4 @@ -import { NostrFetcher, eventKind } from "nostr-fetch"; +import { NostrFetcher } from "nostr-fetch"; import "websocket-polyfill"; import { defaultRelays, nHoursAgo } from "./utils"; @@ -11,7 +11,7 @@ const main = async () => { const eventsIter = fetcher.allEventsIterator( defaultRelays, { - kinds: [eventKind.text], + kinds: [1], }, { since: nHoursAgo(1), diff --git a/packages/examples/src/utils.ts b/packages/examples/src/utils.ts index 84c2619..76719c9 100644 --- a/packages/examples/src/utils.ts +++ b/packages/examples/src/utils.ts @@ -1,4 +1,4 @@ -import { type NostrEvent, eventKind } from "nostr-fetch"; +import type { NostrEvent } from "nostr-fetch"; export const nHoursAgo = (hrs: number): number => Math.floor((Date.now() - hrs * 60 * 60 * 1000) / 1000); @@ -13,7 +13,7 @@ export const defaultRelays = [ export const getWriteRelaysFromEvent = (ev: NostrEvent): string[] => { switch (ev.kind) { - case eventKind.contacts: { + case 3: { let parsedContent: unknown; try { parsedContent = JSON.parse(ev.content); @@ -26,7 +26,7 @@ export const getWriteRelaysFromEvent = (ev: NostrEvent): string[] => { return es.filter(([, usage]) => usage.write ?? false).map(([relay]) => relay); } - case eventKind.relayList: { + case 10002: { return ev.tags .filter((t) => { if (t.length < 2 || t[0] !== "r") { diff --git a/packages/kernel/src/nostr.ts b/packages/kernel/src/nostr.ts index 9e3e303..7ec0a56 100644 --- a/packages/kernel/src/nostr.ts +++ b/packages/kernel/src/nostr.ts @@ -14,6 +14,8 @@ export type NostrEvent = { /** * Standardized Nostr event kinds. * cf. https://github.com/nostr-protocol/nips#event-kinds + * + * @deprecated use literal kind numbers instead. */ export const eventKind = { metadata: 0,