applesauce-core 5.2.0 → 6.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/casts/cast.d.ts +31 -0
  2. package/dist/casts/cast.js +67 -0
  3. package/dist/casts/index.d.ts +3 -0
  4. package/dist/casts/index.js +3 -0
  5. package/dist/casts/pubkey.d.ts +27 -0
  6. package/dist/casts/pubkey.js +49 -0
  7. package/dist/casts/user.d.ts +19 -0
  8. package/dist/casts/user.js +25 -0
  9. package/dist/factories/delete.d.ts +18 -0
  10. package/dist/factories/delete.js +23 -0
  11. package/dist/factories/event.d.ts +61 -0
  12. package/dist/factories/event.js +164 -0
  13. package/dist/factories/index.d.ts +5 -0
  14. package/dist/factories/index.js +5 -0
  15. package/dist/factories/mailboxes.d.ts +33 -0
  16. package/dist/factories/mailboxes.js +57 -0
  17. package/dist/factories/profile.d.ts +46 -0
  18. package/dist/factories/profile.js +80 -0
  19. package/dist/factories/types.d.ts +56 -0
  20. package/dist/helpers/event.d.ts +11 -2
  21. package/dist/helpers/event.js +2 -1
  22. package/dist/helpers/mailboxes.d.ts +5 -1
  23. package/dist/helpers/mailboxes.js +5 -0
  24. package/dist/helpers/pipeline.d.ts +14 -1
  25. package/dist/helpers/pipeline.js +17 -2
  26. package/dist/helpers/pointers.js +2 -2
  27. package/dist/helpers/profile.d.ts +2 -2
  28. package/dist/helpers/regexp.d.ts +2 -0
  29. package/dist/helpers/regexp.js +8 -2
  30. package/dist/helpers/relays.d.ts +3 -1
  31. package/dist/helpers/relays.js +8 -10
  32. package/dist/helpers/url.d.ts +1 -4
  33. package/dist/helpers/url.js +1 -4
  34. package/dist/index.d.ts +3 -1
  35. package/dist/index.js +4 -1
  36. package/dist/observable/catch-error-inline.d.ts +3 -0
  37. package/dist/observable/catch-error-inline.js +5 -0
  38. package/dist/observable/chainable.d.ts +36 -0
  39. package/dist/observable/chainable.js +72 -0
  40. package/dist/observable/combine-latest-by-index.d.ts +10 -0
  41. package/dist/observable/combine-latest-by-index.js +121 -0
  42. package/dist/observable/combine-latest-by-key.d.ts +10 -0
  43. package/dist/observable/combine-latest-by-key.js +136 -0
  44. package/dist/observable/combine-latest-by-value.d.ts +10 -0
  45. package/dist/observable/combine-latest-by-value.js +117 -0
  46. package/dist/observable/combine-latest-by.d.ts +16 -0
  47. package/dist/observable/combine-latest-by.js +12 -0
  48. package/dist/observable/index.d.ts +10 -4
  49. package/dist/observable/index.js +10 -4
  50. package/dist/observable/timeout-with-ignore.d.ts +24 -0
  51. package/dist/observable/timeout-with-ignore.js +33 -0
  52. package/dist/operations/client.d.ts +1 -1
  53. package/dist/operations/client.js +2 -2
  54. package/dist/operations/content.d.ts +5 -2
  55. package/dist/operations/content.js +9 -7
  56. package/dist/operations/delete.d.ts +1 -1
  57. package/dist/operations/encrypted-content.d.ts +9 -3
  58. package/dist/operations/encrypted-content.js +9 -3
  59. package/dist/operations/event.d.ts +6 -4
  60. package/dist/operations/event.js +20 -12
  61. package/dist/operations/hidden-content.d.ts +8 -3
  62. package/dist/operations/hidden-content.js +11 -6
  63. package/dist/operations/mailboxes.d.ts +5 -1
  64. package/dist/operations/mailboxes.js +76 -0
  65. package/dist/operations/profile.d.ts +1 -1
  66. package/dist/operations/tag/common.d.ts +22 -7
  67. package/dist/operations/tag/common.js +33 -18
  68. package/dist/operations/tag/relay.d.ts +1 -1
  69. package/dist/operations/tags.d.ts +10 -4
  70. package/dist/operations/tags.js +19 -13
  71. package/package.json +20 -5
  72. package/dist/event-factory/event-factory.d.ts +0 -57
  73. package/dist/event-factory/event-factory.js +0 -94
  74. package/dist/event-factory/index.d.ts +0 -3
  75. package/dist/event-factory/index.js +0 -3
  76. package/dist/event-factory/methods.d.ts +0 -17
  77. package/dist/event-factory/methods.js +0 -44
  78. package/dist/event-factory/types.d.ts +0 -78
  79. /package/dist/{event-factory → factories}/types.js +0 -0
@@ -1,94 +0,0 @@
1
- import { sign, stamp } from "../operations/event.js";
2
- import { modifyTags } from "../operations/tags.js";
3
- import { buildEvent, modifyEvent } from "./methods.js";
4
- /**
5
- * Base class that provides event creation functionality.
6
- * This class can be extended by other packages to add additional helpful event creation methods.
7
- *
8
- * @example
9
- * ```ts
10
- * // In another package (e.g., applesauce-common)
11
- * import { EventFactory } from "applesauce-core/event-factory";
12
- * import { NoteBlueprint, ReactionBlueprint } from "applesauce-common/blueprints";
13
- *
14
- * // Add methods to the prototype
15
- * EventFactory.prototype.note = function(content, options) {
16
- * return this.create(NoteBlueprint, content, options);
17
- * };
18
- *
19
- * EventFactory.prototype.reaction = function(event, emoji) {
20
- * return this.create(ReactionBlueprint, event, emoji);
21
- * };
22
- *
23
- * // Extend the type via module augmentation
24
- * declare module "applesauce-core/event-factory" {
25
- * interface EventFactory {
26
- * note(content: string, options?: NoteBlueprintOptions): Promise<EventTemplate>;
27
- * reaction(event: NostrEvent, emoji?: string): Promise<EventTemplate>;
28
- * }
29
- * }
30
- * ```
31
- */
32
- export class EventFactory {
33
- context;
34
- constructor(context = {}) {
35
- this.context = context;
36
- }
37
- /** Build an event template with operations */
38
- async build(template, ...operations) {
39
- return await buildEvent(template, this.context, ...operations);
40
- }
41
- async create(blueprint, ...args) {
42
- // Context, blueprint(context)
43
- if (arguments.length === 1) {
44
- return (await blueprint(this.context));
45
- }
46
- // Context, blueprintConstructor(...args)(context), ...args
47
- else {
48
- const constructor = blueprint;
49
- return await constructor(...args)(this.context);
50
- }
51
- }
52
- /** Modify an existing event with operations and updated the created_at */
53
- async modify(draft, ...operations) {
54
- return await modifyEvent(draft, this.context, ...operations);
55
- }
56
- /** Modify a lists public and hidden tags and updated the created_at */
57
- async modifyTags(event, tagOperations, eventOperations) {
58
- let eventOperationsArr = [];
59
- // normalize event operation arg
60
- if (eventOperations === undefined)
61
- eventOperationsArr = [];
62
- else if (typeof eventOperations === "function")
63
- eventOperationsArr = [eventOperations];
64
- else if (Array.isArray(eventOperations))
65
- eventOperationsArr = eventOperations.filter((e) => !!e);
66
- // modify event
67
- return await this.modify(event, modifyTags(tagOperations), ...eventOperationsArr);
68
- }
69
- /** Attaches the signers pubkey to an event template */
70
- async stamp(draft) {
71
- return await stamp()(draft, this.context);
72
- }
73
- /** Signs a event template with the signer */
74
- async sign(draft) {
75
- return await sign()(draft, this.context);
76
- }
77
- // Helpers
78
- /** Sets the signer in the context */
79
- setSigner(signer) {
80
- this.context.signer = signer;
81
- }
82
- /** clears the signer in the context */
83
- clearSigner() {
84
- this.context.signer = undefined;
85
- }
86
- /** sets the client in the context */
87
- setClient(client) {
88
- this.context.client = client;
89
- }
90
- /** clears the client in the context */
91
- clearClient() {
92
- this.context.client = undefined;
93
- }
94
- }
@@ -1,3 +0,0 @@
1
- export * from "./types.js";
2
- export * from "./event-factory.js";
3
- export * from "./methods.js";
@@ -1,3 +0,0 @@
1
- export * from "./types.js";
2
- export * from "./event-factory.js";
3
- export * from "./methods.js";
@@ -1,17 +0,0 @@
1
- import { EventTemplate, NostrEvent, UnsignedEvent } from "../helpers/event.js";
2
- import { EventBlueprint, EventFactoryContext, EventOperation } from "./types.js";
3
- export type EventFactoryTemplate = {
4
- kind: number;
5
- content?: string;
6
- tags?: string[][];
7
- created_at?: number;
8
- };
9
- /** Creates an event using a template, context, and a set of operations */
10
- export declare function buildEvent(template: EventFactoryTemplate, context: EventFactoryContext, ...operations: (EventOperation | undefined)[]): Promise<EventTemplate>;
11
- /** Creates a blueprint function with operations */
12
- export declare function blueprint(kind: number, ...operations: (EventOperation | undefined)[]): EventBlueprint;
13
- /** Creates an event from a context and a blueprint */
14
- export declare function createEvent<T extends EventTemplate | UnsignedEvent | NostrEvent>(context: EventFactoryContext, blueprint: EventBlueprint<T>): Promise<T>;
15
- export declare function createEvent<T extends EventTemplate | UnsignedEvent | NostrEvent, Args extends Array<any>>(context: EventFactoryContext, blueprintConstructor: (...args: Args) => EventBlueprint<T>, ...args: Args): Promise<T>;
16
- /** Modifies an event using a context and a set of operations */
17
- export declare function modifyEvent(event: EventTemplate | UnsignedEvent | NostrEvent, context: EventFactoryContext, ...operations: (EventOperation | undefined)[]): Promise<EventTemplate>;
@@ -1,44 +0,0 @@
1
- import { EncryptedContentSymbol } from "../helpers/encrypted-content.js";
2
- import { isEvent } from "../helpers/event.js";
3
- import { eventPipe } from "../helpers/pipeline.js";
4
- import { unixNow } from "../helpers/time.js";
5
- import { setClient } from "../operations/client.js";
6
- import { includeReplaceableIdentifier, stripSignature, stripStamp, stripSymbols, updateCreatedAt, } from "../operations/event.js";
7
- /** Wraps a set of operations with common event operations */
8
- function wrapCommon(...operations) {
9
- return eventPipe(
10
- // Remove all symbols from the event except for the encrypted content symbol
11
- stripSymbols([EncryptedContentSymbol]),
12
- // Ensure all addressable events have "d" tags
13
- includeReplaceableIdentifier(),
14
- // Apply operations
15
- ...operations,
16
- // Include client tag if its set in the context
17
- (draft, ctx) => (ctx.client ? setClient(ctx.client.name, ctx.client.address)(draft, ctx) : draft));
18
- }
19
- /** Creates an event using a template, context, and a set of operations */
20
- export async function buildEvent(template, context, ...operations) {
21
- return await wrapCommon(stripSignature(), stripStamp(), ...operations)({ created_at: unixNow(), tags: [], content: "", ...template }, context);
22
- }
23
- /** Creates a blueprint function with operations */
24
- export function blueprint(kind, ...operations) {
25
- return async (context) => await buildEvent({ kind }, context, ...operations);
26
- }
27
- export async function createEvent(context, blueprint, ...args) {
28
- // Context, blueprint(context)
29
- if (arguments.length === 2) {
30
- return (await blueprint(context));
31
- }
32
- // Context, blueprintConstructor(...args)(context), ...args
33
- else {
34
- const constructor = blueprint;
35
- return await constructor(...args)(context);
36
- }
37
- }
38
- /** Modifies an event using a context and a set of operations */
39
- export async function modifyEvent(event, context, ...operations) {
40
- // NOTE: Unwrapping evnet object in order to handle cast events from applesauce-common
41
- if ("event" in event && isEvent(event.event))
42
- event = event.event;
43
- return await wrapCommon(stripSignature(), stripStamp(), updateCreatedAt(), ...operations)(event, context);
44
- }
@@ -1,78 +0,0 @@
1
- import { EventTemplate, NostrEvent, UnsignedEvent } from "../helpers/event.js";
2
- import { AddressPointer } from "../helpers/pointers.js";
3
- import { ModifyTagsOptions } from "../operations/tags.js";
4
- import { EventFactoryTemplate } from "./methods.js";
5
- /** Nostr event signer */
6
- export interface EventSigner {
7
- getPublicKey: () => Promise<string> | string;
8
- signEvent: (draft: EventTemplate | UnsignedEvent) => Promise<NostrEvent> | NostrEvent;
9
- nip04?: {
10
- encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
11
- decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
12
- };
13
- nip44?: {
14
- encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
15
- decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
16
- };
17
- }
18
- /** A context with optional methods for getting relay hints */
19
- export interface RelayHintContext {
20
- getEventRelayHint?: (event: string) => string | undefined | Promise<string> | Promise<undefined>;
21
- getPubkeyRelayHint?: (pubkey: string) => string | undefined | Promise<string> | Promise<undefined>;
22
- }
23
- /** A context with an optional signer */
24
- export interface EventSignerContext {
25
- signer?: EventSigner;
26
- }
27
- export interface EventFactoryClient {
28
- name: string;
29
- address?: Omit<AddressPointer, "kind" | "relays">;
30
- }
31
- /** A context with an optional NIP-89 app pointer */
32
- export interface ClientPointerContext {
33
- client?: EventFactoryClient;
34
- }
35
- /** NIP-30 emoji type. this should be moved out of the core package to common */
36
- export type Emoji = {
37
- /** The emoji shortcode (without the ::) */
38
- shortcode: string;
39
- /** The URL to the emoji image */
40
- url: string;
41
- /** The NIP-01 "a" tag address of the emoji pack this emoji belongs to */
42
- address?: AddressPointer;
43
- };
44
- export interface EmojiContext {
45
- /** An array of custom emojis that will be used for text notes */
46
- emojis?: Emoji[];
47
- }
48
- /** All options that can be passed when building an event */
49
- export interface EventFactoryContext extends ClientPointerContext, EventSignerContext, RelayHintContext, EmojiContext {
50
- }
51
- /** A single operation that modifies an events public or hidden tags array */
52
- export type Operation<I extends unknown = unknown, R extends unknown = unknown> = (value: I, context: EventFactoryContext) => R | Promise<R>;
53
- /** A single operation that modifies an events public or hidden tags array */
54
- export type TagOperation = Operation<string[][], string[][]>;
55
- /** A single operation that modifies an event */
56
- export type EventOperation<I extends EventTemplate | UnsignedEvent | NostrEvent = EventTemplate, R extends EventTemplate | UnsignedEvent | NostrEvent = EventTemplate> = Operation<I, R>;
57
- /** A method that creates a new event based on a set of operations */
58
- export type EventBlueprint<T extends EventTemplate | UnsignedEvent | NostrEvent = EventTemplate> = (context: EventFactoryContext) => Promise<T>;
59
- /**
60
- * Core helpful event creation interface.
61
- * Contains only methods that use blueprints from the core package.
62
- * Other packages (like applesauce-common) can extend this interface via module augmentation.
63
- */
64
- export interface IEventFactory {
65
- /** Build an event template with operations */
66
- build(template: EventFactoryTemplate, ...operations: (EventOperation | undefined)[]): Promise<EventTemplate>;
67
- /** Create an event from a blueprint */
68
- create<T extends EventTemplate | UnsignedEvent | NostrEvent>(blueprint: EventBlueprint<T>): Promise<T>;
69
- create<T extends EventTemplate | UnsignedEvent | NostrEvent, Args extends Array<any>>(blueprint: (...args: Args) => EventBlueprint<T>, ...args: Args): Promise<T>;
70
- /** Modify an existing event with operations and updated the created_at */
71
- modify(draft: EventTemplate | UnsignedEvent | NostrEvent, ...operations: (EventOperation | undefined)[]): Promise<EventTemplate>;
72
- /** Modify a lists public and hidden tags and updated the created_at */
73
- modifyTags(event: EventTemplate | UnsignedEvent | NostrEvent, tagOperations?: ModifyTagsOptions, eventOperations?: EventOperation | (EventOperation | undefined)[]): Promise<EventTemplate>;
74
- /** Attaches the signers pubkey to an event template */
75
- stamp(draft: EventTemplate | UnsignedEvent): Promise<UnsignedEvent>;
76
- /** Signs a event template with the signer */
77
- sign(draft: EventTemplate | UnsignedEvent): Promise<NostrEvent>;
78
- }
File without changes