nostr
package: vastly simplify the API #412
No reviewers
Labels
No Label
1000k
100k
10k
200k
20k
500k
50k
5k
75k
backend
blocked:design
bug
dependencies
documentation
duplicate
enhancement
good first issue
help wanted
invalid
P1
P2
P3
question
scope:intl
scope:nip
scope:query_tracing
scope:ux
wontfix
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: Kieran/snort#412
Loading…
Reference in New Issue
No description provided.
Delete Branch "nostr-package-use-methods-for-events"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I've decided to simplify the API.
RawEvent
andEvent
concepts. I had an important realization that theRawEvent
andEvent
both carry the same data:Event
represents a particular interpretation of that data. So instead of changing the fields ofEvent
based onkind
, the fields (i.e. the data) are always the same: those ofRawEvent
. Instead, depending on theEvent
'skind
field, the event will have additional methods which allow the data to be interpreted in different ways. E.g. whenkind === EventKind.SetMetadata
, the event will have a method calledgetUserMetadata
which parses thecontent
as user metadata JSON. Similar forEventKind.DirectMessage
, there will be a methodgetMessage
which decrypts the message, etc. The TS type system is powerful enough to elegantly express this.SignedEvent
andEvent
, there is now a generic typeUnsigned<Event>
which just represents the event with thesig
,id
, andpubkey
fields optionally omitted. Similar to the point above, the goal of this design is to help with unifying the event data. There's asignEvent
method which fills in the missing fields. This also fits perfectly with NIP-07.Altogether I think this is way better than what I had before. I believe this is very close to what the final API will look like and hopefully there won't be any more big API refactors in the future, just implementing functionality from all remaining NIPs.
I was happy enough with the strong types, but maybe it was a bit overkill
One thing that we should try to accomodate is allowing people to do unsupported NIP's dont want a lib that needs to be constantly updated
One place this is obviously a problem in its current form is the REQ filters, those have a bunch of other values which are not implemented in this lib
This still has strong types! The only difference is that instead of having different fields on the types, the fields are always the same (those of
RawEvent
), while the methods change. (E.g. there's agetUserMetadata()
method on a set metadata event, whenkind == EventKind.SetMetadata
, while that method doesn't exist onkind == EventKind.DirectMessage
. The direct message hasasync getMessage()
andgetRecipient()
methods instead. And everything is still strongly typed due to how type narrowing works.)Noted.
Noted, I'm going to look for a way to improve that.
Deploying with Cloudflare Pages
589243c
View logs