@kokimoki/app 1.8.3 → 1.10.0

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.
@@ -1,5 +1,6 @@
1
- import TypedEventEmitter from 'typed-emitter';
1
+ import TypedEmitter from 'typed-emitter';
2
2
  import * as Y from 'yjs';
3
+ import { ZodType } from 'zod';
3
4
 
4
5
  interface Paginated<T> {
5
6
  items: T[];
@@ -24,216 +25,61 @@ interface Upload {
24
25
  tags: string[];
25
26
  }
26
27
 
27
- declare namespace KokimokiSchema {
28
- abstract class Generic<T> {
29
- abstract get defaultValue(): T;
30
- abstract set defaultValue(value: T);
31
- }
32
- class Number extends Generic<number> {
33
- defaultValue: number;
34
- constructor(defaultValue?: number);
35
- }
36
- function number(defaultValue?: number): Number;
37
- class String extends Generic<string> {
38
- defaultValue: string;
39
- constructor(defaultValue?: string);
40
- }
41
- function string(defaultValue?: string): String;
42
- class Boolean extends Generic<boolean> {
43
- defaultValue: boolean;
44
- constructor(defaultValue?: boolean);
45
- }
46
- function boolean(defaultValue?: boolean): Boolean;
47
- class Struct<Data extends Record<string, Generic<unknown>>> extends Generic<{
48
- [key in keyof Data]: Data[key]["defaultValue"];
49
- }> {
50
- fields: Data;
51
- constructor(fields: Data);
52
- get defaultValue(): {
53
- [key in keyof Data]: Data[key]["defaultValue"];
54
- };
55
- set defaultValue(value: {
56
- [key in keyof Data]: Data[key]["defaultValue"];
57
- });
58
- }
59
- function struct<Data extends Record<string, Generic<unknown>>>(schema: Data): Struct<Data>;
60
- class Dict<T extends Generic<unknown>> {
61
- schema: T;
62
- defaultValue: {
63
- [key: string]: T["defaultValue"];
64
- };
65
- constructor(schema: T, defaultValue?: {
66
- [key: string]: T["defaultValue"];
67
- });
68
- }
69
- function dict<T extends Generic<unknown>>(schema: T, defaultValue?: {
70
- [key: string]: T["defaultValue"];
71
- }): Dict<T>;
72
- class List<T extends Generic<unknown>> extends Generic<T["defaultValue"][]> {
73
- schema: T;
74
- defaultValue: T["defaultValue"][];
75
- constructor(schema: T, defaultValue?: T["defaultValue"][]);
76
- }
77
- function list<T extends Generic<unknown>>(schema: T, defaultValue?: T["defaultValue"][]): List<T>;
78
- /**
79
- * Nullable
80
- */
81
- class Nullable<T extends Generic<unknown>> extends Generic<T["defaultValue"] | null> {
82
- schema: T;
83
- defaultValue: T["defaultValue"] | null;
84
- constructor(schema: T, defaultValue: T["defaultValue"] | null);
85
- }
86
- function nullable<T extends Generic<unknown>>(schema: T, defaultValue?: T["defaultValue"] | null): Nullable<T>;
87
- class StringEnum<T extends readonly string[]> extends Generic<string> {
88
- readonly options: T;
89
- defaultValue: T[number];
90
- constructor(options: T, defaultValue: T[number]);
91
- }
92
- function stringEnum<T extends readonly string[]>(options: T, defaultValue: T[number]): StringEnum<T>;
93
- class StringConst<T extends string> extends Generic<string> {
94
- readonly defaultValue: T;
95
- constructor(defaultValue: T);
96
- }
97
- function stringConst<T extends string>(defaultValue: T): StringConst<T>;
98
- class AnyOf<T extends readonly Generic<unknown>[]> extends Generic<T[number]["defaultValue"]> {
99
- readonly schemas: T;
100
- defaultValue: T[number]["defaultValue"];
101
- constructor(schemas: T, defaultValue: T[number]["defaultValue"]);
102
- }
103
- function anyOf<T extends readonly Generic<unknown>[]>(schemas: T, defaultValue: T[number]["defaultValue"]): AnyOf<T>;
104
- class Optional<T extends Generic<unknown>> extends Generic<T["defaultValue"] | undefined> {
105
- schema: T;
106
- defaultValue: T["defaultValue"] | undefined;
107
- constructor(schema: T, defaultValue?: T["defaultValue"] | undefined);
108
- }
109
- function optional<T extends Generic<unknown>>(schema: T, defaultValue?: T["defaultValue"] | undefined): Optional<T>;
110
- }
111
-
112
28
  declare enum RoomSubscriptionMode {
113
29
  Read = "r",
114
30
  Write = "w",
115
31
  ReadWrite = "b"
116
32
  }
117
33
 
118
- declare class KokimokiStore<T extends KokimokiSchema.Generic<unknown>, SubscribeT = T["defaultValue"]> {
34
+ declare class KokimokiStore<T> {
119
35
  readonly roomName: string;
120
36
  readonly mode: RoomSubscriptionMode;
121
37
  readonly doc: Y.Doc;
122
- readonly proxy: T["defaultValue"];
123
- readonly root: T["defaultValue"];
124
- readonly defaultValue: T["defaultValue"];
38
+ readonly proxy: T;
39
+ readonly root: T;
40
+ readonly defaultValue: T;
125
41
  readonly docRoot: Y.Map<unknown>;
126
- constructor(roomName: string, schema: T, mode?: RoomSubscriptionMode);
127
- get(): T["defaultValue"];
128
- subscribe(set: (value: SubscribeT) => void): () => void;
42
+ constructor(roomName: string, schema: ZodType<T>, mode?: RoomSubscriptionMode);
43
+ get(): T;
44
+ subscribe(set: (value: T) => void): () => void;
129
45
  onJoin(client: KokimokiClient): Promise<void>;
130
46
  onBeforeLeave(client: KokimokiClient): Promise<void>;
131
47
  onLeave(client: KokimokiClient): Promise<void>;
132
48
  }
133
49
 
134
- declare class KokimokiQueue<Req extends KokimokiSchema.Generic<unknown>> extends KokimokiStore<KokimokiSchema.Dict<Req>> {
135
- readonly payloadSchema: Req;
136
- private _emitter;
137
- readonly on: <E extends "messages">(event: E, listener: {
138
- messages: (messages: {
139
- id: string;
140
- payload: Req["defaultValue"];
141
- }[]) => void;
142
- }[E]) => TypedEventEmitter<{
143
- messages: (messages: {
144
- id: string;
145
- payload: Req["defaultValue"];
146
- }[]) => void;
147
- }>;
148
- readonly off: <E extends "messages">(event: E, listener: {
149
- messages: (messages: {
150
- id: string;
151
- payload: Req["defaultValue"];
152
- }[]) => void;
153
- }[E]) => TypedEventEmitter<{
154
- messages: (messages: {
155
- id: string;
156
- payload: Req["defaultValue"];
157
- }[]) => void;
158
- }>;
159
- constructor(roomName: string, payloadSchema: Req, mode: RoomSubscriptionMode);
160
- }
161
-
162
- declare class KokimokiTransaction {
163
- private kmClient;
164
- private _clones;
165
- private _updates;
166
- private _consumeMessagesInRooms;
167
- private _queueMessageCounter;
168
- constructor(kmClient: KokimokiClient<any>);
169
- private _parseTarget;
170
- private _parsePath;
171
- private _getClone;
172
- get<T>(target: T): T;
173
- set<T>(target: T, value: T): void;
174
- delete<T>(target: T): void;
175
- push<T>(target: T[], value: T): void;
176
- queueMessage<T>(queue: KokimokiQueue<KokimokiSchema.Generic<T>>, payload: T): string;
177
- consumeMessage<T>(queue: KokimokiQueue<KokimokiSchema.Generic<T>>, messageId: string): void;
178
- getUpdates(): Promise<{
179
- updates: {
180
- roomName: string;
181
- update: Uint8Array;
182
- }[];
183
- consumedMessages: Set<string>;
184
- }>;
185
- }
186
-
187
- declare class KokimokiAwareness<Data extends KokimokiSchema.Generic<unknown>> extends KokimokiStore<KokimokiSchema.Dict<KokimokiSchema.Struct<{
188
- clientId: KokimokiSchema.String;
189
- lastPing: KokimokiSchema.Number;
190
- data: Data;
191
- }>>> {
192
- readonly dataSchema: Data;
50
+ declare class KokimokiAwareness<DataT> extends KokimokiStore<{
51
+ [connectionId: string]: {
52
+ clientId: string;
53
+ lastPing: number;
54
+ data: DataT;
55
+ };
56
+ }> {
57
+ readonly dataSchema: ZodType<DataT>;
193
58
  private _data;
194
59
  private _pingInterval;
195
60
  private _kmClients;
196
- constructor(roomName: string, dataSchema: Data, _data: Data["defaultValue"], mode?: RoomSubscriptionMode, pingTimeout?: number);
61
+ constructor(roomName: string, dataSchema: ZodType<DataT>, _data: DataT, mode?: RoomSubscriptionMode, pingTimeout?: number);
197
62
  onJoin(client: KokimokiClient<any>): Promise<void>;
198
63
  onBeforeLeave(client: KokimokiClient<any>): Promise<void>;
199
64
  onLeave(client: KokimokiClient<any>): Promise<void>;
200
65
  getClients(): {
201
- [clientId: string]: Data["defaultValue"];
66
+ [clientId: string]: DataT;
202
67
  };
203
- setData(data: Data["defaultValue"]): Promise<void>;
68
+ setData(data: DataT): Promise<void>;
204
69
  }
205
70
 
206
- declare class KokimokiReqRes<Req extends KokimokiSchema.Generic<unknown>, Res extends KokimokiSchema.Generic<unknown>> {
207
- private kmClient;
208
- readonly serviceName: string;
209
- readonly reqSchema: Req;
210
- readonly resSchema: Res;
211
- private handleRequest;
212
- private _reqQueueSchema;
213
- private _resQueueSchema;
214
- private _reqQueues;
215
- private _resQueues;
216
- private _reqPromises;
217
- private _getReqQueue;
218
- private _getResQueue;
219
- constructor(kmClient: KokimokiClient, serviceName: string, reqSchema: Req, resSchema: Res, handleRequest: (payload: Req["defaultValue"]) => Promise<Res["defaultValue"]>);
220
- private handleRequests;
221
- private handleResponses;
222
- send(toClientId: string, payload: Req["defaultValue"], timeout?: number): Promise<Res["defaultValue"]>;
223
- }
224
-
225
- declare class KokimokiLocalStore<Data extends KokimokiSchema.Generic<unknown>> extends KokimokiStore<Data> {
71
+ declare class KokimokiLocalStore<Data> extends KokimokiStore<Data> {
226
72
  private readonly localRoomName;
227
73
  private _stateKey?;
228
74
  private get stateKey();
229
- constructor(localRoomName: string, schema: Data);
75
+ constructor(localRoomName: string, schema: ZodType<Data>);
230
76
  getInitialUpdate(appId: string, clientId: string): {
231
77
  roomHash: number;
232
78
  initialUpdate: Uint8Array | undefined;
233
79
  };
234
80
  }
235
81
 
236
- declare const KokimokiClient_base: new () => TypedEventEmitter<KokimokiClientEvents>;
82
+ declare const KokimokiClient_base: new () => TypedEmitter<KokimokiClientEvents>;
237
83
  declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
238
84
  readonly host: string;
239
85
  readonly appId: string;
@@ -298,17 +144,17 @@ declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
298
144
  exposeScriptingContext(context: any): Promise<void>;
299
145
  private sendSubscribeReq;
300
146
  private sendUnsubscribeReq;
301
- join<T extends KokimokiSchema.Generic<unknown>>(store: KokimokiStore<T>): Promise<void>;
302
- leave<T extends KokimokiSchema.Generic<unknown>>(store: KokimokiStore<T>): Promise<void>;
303
- transact<ReturnT = void>(handler: (t: KokimokiTransaction) => ReturnT | Promise<ReturnT>): Promise<ReturnT>;
147
+ join<T>(store: KokimokiStore<T>): Promise<void>;
148
+ leave<T>(store: KokimokiStore<T>): Promise<void>;
149
+ transact<T extends unknown[], ReturnT = void>(stores: {
150
+ [K in keyof T]: KokimokiStore<T[K]>;
151
+ }, handler: (proxies: T) => ReturnT | Promise<ReturnT>): Promise<ReturnT>;
304
152
  close(): Promise<void>;
305
- getRoomHash<T extends KokimokiSchema.Generic<unknown>>(store: KokimokiStore<T>): number;
153
+ getRoomHash<T extends ZodType>(store: KokimokiStore<T>): number;
306
154
  /** Initializers */
307
- store<T extends KokimokiSchema.Generic<unknown>>(name: string, schema: T, autoJoin?: boolean): KokimokiStore<T, T["defaultValue"]>;
308
- localStore<T extends KokimokiSchema.Generic<unknown>>(name: string, schema: T): KokimokiLocalStore<T>;
309
- queue<T extends KokimokiSchema.Generic<unknown>>(name: string, schema: T, mode: RoomSubscriptionMode, autoJoin?: boolean): KokimokiQueue<T>;
310
- awareness<T extends KokimokiSchema.Generic<unknown>>(name: string, dataSchema: T, initialData?: T["defaultValue"], autoJoin?: boolean): KokimokiAwareness<T>;
311
- reqRes<Req extends KokimokiSchema.Generic<unknown>, Res extends KokimokiSchema.Generic<unknown>>(serviceName: string, reqSchema: Req, resSchema: Res, handleRequest: (payload: Req["defaultValue"]) => Promise<Res["defaultValue"]>): KokimokiReqRes<Req, Res>;
155
+ store<T extends ZodType>(name: string, schema: T, autoJoin?: boolean): KokimokiStore<T>;
156
+ localStore<T>(name: string, schema: ZodType<T>): KokimokiLocalStore<T>;
157
+ awareness<T>(name: string, dataSchema: ZodType<T>, initialData?: T, autoJoin?: boolean): KokimokiAwareness<T>;
312
158
  /**
313
159
  * Add a new entry to a leaderboard
314
160
  * @param leaderboardName
@@ -353,6 +199,12 @@ declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
353
199
  score: number;
354
200
  metadata: MetadataT;
355
201
  }>;
202
+ /**
203
+ * Send app data via webhook
204
+ */
205
+ sendWebhook<T>(event: string, data: T): Promise<{
206
+ jobId: string;
207
+ }>;
356
208
  }
357
209
 
358
210
  declare class RoomSubscription {
@@ -369,4 +221,4 @@ declare class RoomSubscription {
369
221
  close(): void;
370
222
  }
371
223
 
372
- export { KokimokiAwareness, KokimokiClient, type KokimokiClientEvents, KokimokiQueue, KokimokiSchema, KokimokiStore, type Paginated, RoomSubscription, RoomSubscriptionMode, type Upload };
224
+ export { KokimokiAwareness, KokimokiClient, type KokimokiClientEvents, KokimokiStore, type Paginated, RoomSubscription, RoomSubscriptionMode, type Upload };