@decentrl/sdk 0.0.7 → 0.0.8

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.
package/src/types.ts CHANGED
@@ -17,26 +17,120 @@ export type EventDefinitions = Record<
17
17
  }
18
18
  >;
19
19
 
20
+ // --- Public Event Definition Types ---
21
+
22
+ export interface PublicEventDefinition<TSchema extends z.ZodType = z.ZodType> {
23
+ schema: TSchema;
24
+ tags: string[];
25
+ channelId: string;
26
+ }
27
+
28
+ export type PublicEventDefinitions = Record<
29
+ string,
30
+ {
31
+ schema: z.ZodType;
32
+ tags: string[];
33
+ channelId: string;
34
+ }
35
+ >;
36
+
37
+ // --- Channel Definition Types (external public channels you read) ---
38
+
39
+ export interface ChannelDefinition<TSchema extends z.ZodType = z.ZodType> {
40
+ schema: TSchema;
41
+ }
42
+
43
+ export type ChannelDefinitions = Record<
44
+ string,
45
+ {
46
+ schema: z.ZodType;
47
+ }
48
+ >;
49
+
50
+ // --- Public Event Meta & Envelope ---
51
+
52
+ export interface PublicEventMeta {
53
+ publisherDid: string;
54
+ timestamp: number;
55
+ eventId: string;
56
+ channelId: string;
57
+ }
58
+
59
+ export interface PublicEventEnvelope {
60
+ type: string;
61
+ data: unknown;
62
+ meta: PublicEventMeta;
63
+ tags?: string[];
64
+ }
65
+
66
+ // --- Channel Subscription/Fetch ---
67
+
68
+ export interface ChannelSubscriptionConfig {
69
+ did: string;
70
+ channelId: string;
71
+ tags?: string[];
72
+ pollIntervalMs?: number;
73
+ }
74
+
75
+ export interface ChannelFetchOptions {
76
+ did: string;
77
+ mediatorEndpoint?: string;
78
+ channelId?: string;
79
+ tags?: string[];
80
+ afterTimestamp?: number;
81
+ beforeTimestamp?: number;
82
+ pagination?: { page: number; pageSize: number };
83
+ }
84
+
85
+ // --- Reducer Key Inference ---
86
+
87
+ // Builds a typed reduce map from all three event sources:
88
+ // - Private events: key matches TEvents, data inferred from schema, meta = EventMeta
89
+ // - Public events: key is `public:${K}` where K is in TPublicEvents, data inferred, meta = PublicEventMeta
90
+ // - Channel events: key is `channel:${K}` where K is in TChannels, data inferred, meta = PublicEventMeta
91
+ type PrivateReducers<TSlice, TEvents extends EventDefinitions> = {
92
+ [K in string & keyof TEvents]?: (
93
+ state: TSlice,
94
+ data: z.infer<TEvents[K]['schema']>,
95
+ meta: EventMeta,
96
+ ) => TSlice;
97
+ };
98
+
99
+ type PublicReducers<TSlice, TPublicEvents extends PublicEventDefinitions> = {
100
+ [K in string & keyof TPublicEvents as `public:${K}`]?: (
101
+ state: TSlice,
102
+ data: z.infer<TPublicEvents[K]['schema']>,
103
+ meta: PublicEventMeta,
104
+ ) => TSlice;
105
+ };
106
+
107
+ type ChannelReducers<TSlice, TChannels extends ChannelDefinitions> = {
108
+ [K in string & keyof TChannels as `channel:${K}`]?: (
109
+ state: TSlice,
110
+ data: z.infer<TChannels[K]['schema']>,
111
+ meta: PublicEventMeta,
112
+ ) => TSlice;
113
+ };
114
+
20
115
  // --- State Slice Types ---
21
116
 
22
117
  export interface StateSliceDefinition<
23
118
  TSlice = unknown,
24
119
  TEvents extends EventDefinitions = EventDefinitions,
120
+ TPublicEvents extends PublicEventDefinitions = PublicEventDefinitions,
121
+ TChannels extends ChannelDefinitions = ChannelDefinitions,
25
122
  > {
26
123
  initial: TSlice;
27
- reduce: {
28
- [K in keyof TEvents]?: (
29
- state: TSlice,
30
- data: z.infer<TEvents[K]['schema']>,
31
- meta: EventMeta,
32
- ) => TSlice;
33
- };
124
+ reduce: PrivateReducers<TSlice, TEvents> &
125
+ PublicReducers<TSlice, TPublicEvents> &
126
+ ChannelReducers<TSlice, TChannels>;
34
127
  }
35
128
 
36
- export type StateDefinitions<TEvents extends EventDefinitions = EventDefinitions> = Record<
37
- string,
38
- StateSliceDefinition<any, TEvents>
39
- >;
129
+ export type StateDefinitions<
130
+ TEvents extends EventDefinitions = EventDefinitions,
131
+ TPublicEvents extends PublicEventDefinitions = PublicEventDefinitions,
132
+ TChannels extends ChannelDefinitions = ChannelDefinitions,
133
+ > = Record<string, StateSliceDefinition<unknown, TEvents, TPublicEvents, TChannels>>;
40
134
 
41
135
  // --- Inferred State ---
42
136
 
@@ -64,7 +158,7 @@ export interface EventEnvelope {
64
158
  // --- Client Config ---
65
159
 
66
160
  export interface DecentrlClientConfig {
67
- mediatorDid: string;
161
+ mediatorDid?: string;
68
162
  /** Enable localStorage persistence. Pass a key prefix (e.g. "decentrl-myapp"). */
69
163
  persist?: { key: string };
70
164
  /** Optional transport layer. Defaults to DirectTransport (direct HTTP to mediator). */
@@ -75,10 +169,18 @@ export interface DecentrlClientConfig {
75
169
 
76
170
  export interface DecentrlAppConfig<
77
171
  TEvents extends EventDefinitions = EventDefinitions,
78
- TState extends StateDefinitions<TEvents> = StateDefinitions<TEvents>,
172
+ TPublicEvents extends PublicEventDefinitions = PublicEventDefinitions,
173
+ TChannels extends ChannelDefinitions = ChannelDefinitions,
174
+ TState extends StateDefinitions<TEvents, TPublicEvents, TChannels> = StateDefinitions<
175
+ TEvents,
176
+ TPublicEvents,
177
+ TChannels
178
+ >,
79
179
  > {
80
180
  events: TEvents;
81
181
  state: TState;
182
+ publicEvents?: TPublicEvents;
183
+ channels?: TChannels;
82
184
  }
83
185
 
84
186
  // --- Serialized Identity ---
@@ -162,6 +264,29 @@ export interface PaginatedResult<T> {
162
264
  pagination: PaginationMeta;
163
265
  }
164
266
 
267
+ // --- Public Query Options ---
268
+
269
+ export interface PublicQueryOptions {
270
+ publisherDid: string;
271
+ mediatorEndpoint?: string;
272
+ channelId?: string;
273
+ tags?: string[];
274
+ /** Unix timestamp in seconds */
275
+ afterTimestamp?: number;
276
+ /** Unix timestamp in seconds */
277
+ beforeTimestamp?: number;
278
+ pagination?: { page: number; pageSize: number };
279
+ }
280
+
281
+ export interface PublicEventResult {
282
+ id: string;
283
+ channelId: string;
284
+ event: string;
285
+ tags: string[];
286
+ timestamp: number;
287
+ eventSignature: string;
288
+ }
289
+
165
290
  // --- Publish Options ---
166
291
 
167
292
  export interface PublishOptions {