@powerhousedao/reactor-api 1.3.4 → 1.4.1

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/CHANGELOG.md CHANGED
@@ -1,20 +1,9 @@
1
- ## 1.3.4 (2024-11-11)
2
-
3
- ### 🚀 Features
4
-
5
- - **codegen:** regenerate lockfile ([31c2d0c5](https://github.com/powerhouse-inc/powerhouse/commit/31c2d0c5))
6
- - **ph-cli:** update cli to format by default ([7418e777](https://github.com/powerhouse-inc/powerhouse/commit/7418e777))
7
- - **codegen:** remove format generated from action ([5d7e1c48](https://github.com/powerhouse-inc/powerhouse/commit/5d7e1c48))
8
- - **codegen:** use prettier api to format typescript from gql ([8896d86e](https://github.com/powerhouse-inc/powerhouse/commit/8896d86e))
1
+ ## 1.4.1 (2024-11-11)
9
2
 
10
3
  ### 🧱 Updated Dependencies
11
4
 
12
- - Updated document-model-libs to 1.110.0
13
- - Updated document-drive to 1.3.4
14
-
15
- ### ❤️ Thank You
16
-
17
- - ryanwolhuter @ryanwolhuter
5
+ - Updated document-model-libs to 1.111.0
6
+ - Updated document-drive to 1.4.1
18
7
 
19
8
  ## 1.2.0 (2024-10-29)
20
9
 
package/dist/index.d.ts CHANGED
@@ -1,14 +1,289 @@
1
- import * as document_drive from 'document-drive';
2
- import { BaseDocumentDriveServer, Listener, InternalTransmitterUpdate } from 'document-drive';
1
+ import { Trigger, PullResponderTriggerData, DocumentDriveDocument, ListenerFilter, DocumentDriveLocalState, ListenerCallInfo, DocumentDriveAction, DocumentDriveState } from 'document-model-libs/document-drive';
2
+ import { Document, OperationScope, Operation, DocumentModel, Action, ActionContext, BaseAction, State, CreateChildDocumentInput, ReducerOptions, Signal } from 'document-model/document';
3
+ import { Unsubscribe } from 'nanoevents';
3
4
  import express, { Express, IRouter } from 'express';
4
5
  import * as graphql from 'graphql';
5
6
  import { GraphQLResolverMap } from '@apollo/subgraph/dist/schema-helper';
6
7
 
8
+ type StrandUpdateSource = {
9
+ type: "local";
10
+ } | {
11
+ type: "trigger";
12
+ trigger: Trigger;
13
+ };
14
+ interface ITransmitter {
15
+ transmit?(strands: StrandUpdate[], source: StrandUpdateSource): Promise<ListenerRevision[]>;
16
+ disconnect?(): Promise<void>;
17
+ }
18
+ type PullResponderTrigger = Omit<Trigger, "data" | "type"> & {
19
+ data: PullResponderTriggerData;
20
+ type: "PullResponder";
21
+ };
22
+
23
+ interface IReceiver {
24
+ transmit: (strands: InternalTransmitterUpdate[]) => Promise<void>;
25
+ disconnect: () => Promise<void>;
26
+ }
27
+ type InternalTransmitterUpdate<T extends Document = Document, S extends OperationScope = OperationScope> = {
28
+ driveId: string;
29
+ documentId: string;
30
+ scope: S;
31
+ branch: string;
32
+ operations: OperationUpdate[];
33
+ state: T["state"][S];
34
+ };
35
+ interface IInternalTransmitter extends ITransmitter {
36
+ setReceiver(receiver: IReceiver): void;
37
+ }
38
+
39
+ declare class DocumentModelNotFoundError extends Error {
40
+ id: string;
41
+ constructor(id: string, cause?: unknown);
42
+ }
43
+ declare class OperationError extends Error {
44
+ status: ErrorStatus;
45
+ operation: Operation | undefined;
46
+ constructor(status: ErrorStatus, operation?: Operation, message?: string, cause?: unknown);
47
+ }
48
+ declare class SynchronizationUnitNotFoundError extends Error {
49
+ syncUnitId: string;
50
+ constructor(message: string, syncUnitId: string);
51
+ }
52
+
53
+ type DriveInfo = {
54
+ id: string;
55
+ name: string;
56
+ slug: string;
57
+ icon?: string;
58
+ };
59
+
60
+ declare abstract class ReadDriveError extends Error {
61
+ }
62
+ declare class ReadDriveNotFoundError extends ReadDriveError {
63
+ constructor(driveId: string);
64
+ }
65
+ declare class ReadDriveSlugNotFoundError extends ReadDriveError {
66
+ constructor(slug: string);
67
+ }
68
+ declare class ReadDocumentNotFoundError extends ReadDriveError {
69
+ constructor(drive: string, id: string);
70
+ }
71
+
72
+ type InferDocumentState<D extends Document> = D extends Document<infer S> ? S : never;
73
+ type InferDocumentOperation<D extends Document> = D extends Document<unknown, infer A> ? A : never;
74
+ type InferDocumentLocalState<D extends Document> = D extends Document<unknown, Action, infer L> ? L : never;
75
+ type ReadDrivesListener = (drives: ReadDrive[], operation: "add" | "delete") => void;
76
+ type ReadDrivesListenerUnsubscribe = () => void;
77
+ interface IReadModeDriveServer extends IReadModeDriveService {
78
+ migrateReadDrive(id: string, options: RemoteDriveOptions): Promise<DocumentDriveDocument | ReadDriveNotFoundError>;
79
+ onReadDrivesUpdate(listener: ReadDrivesListener): Promise<ReadDrivesListenerUnsubscribe>;
80
+ }
81
+ type ReadDriveOptions = {
82
+ expectedDriveInfo?: DriveInfo;
83
+ filter?: ListenerFilter;
84
+ };
85
+ type ReadDriveContext = {
86
+ url: string;
87
+ } & ReadDriveOptions;
88
+ type ReadDrive = DocumentDriveDocument & {
89
+ readContext: ReadDriveContext;
90
+ };
91
+ interface IReadModeDriveService {
92
+ addReadDrive(url: string, options?: ReadDriveOptions): Promise<void>;
93
+ getReadDrives(): Promise<string[]>;
94
+ getReadDriveBySlug(slug: string): Promise<ReadDrive | ReadDriveSlugNotFoundError>;
95
+ getReadDrive(id: string): Promise<ReadDrive | ReadDriveNotFoundError>;
96
+ getReadDriveContext(id: string): Promise<ReadDriveContext | ReadDriveNotFoundError>;
97
+ fetchDrive(id: string): Promise<ReadDrive | ReadDriveNotFoundError>;
98
+ fetchDocument<D extends Document>(driveId: string, documentId: string, documentType: DocumentModel<InferDocumentState<D>, InferDocumentOperation<D>, InferDocumentLocalState<D>>["documentModel"]["id"]): Promise<Document<InferDocumentState<D>, InferDocumentOperation<D>, InferDocumentLocalState<D>> | DocumentModelNotFoundError | ReadDriveNotFoundError | ReadDocumentNotFoundError>;
99
+ deleteReadDrive(id: string): Promise<ReadDriveNotFoundError | undefined>;
100
+ }
101
+
102
+ interface IDefaultDrivesManager {
103
+ initializeDefaultRemoteDrives(): Promise<void>;
104
+ getDefaultRemoteDrives(): Map<string, DefaultRemoteDriveInfo>;
105
+ setDefaultDriveAccessLevel(url: string, level: RemoteDriveAccessLevel): Promise<void>;
106
+ setAllDefaultDrivesAccessLevel(level: RemoteDriveAccessLevel): Promise<void>;
107
+ }
108
+
109
+ type DriveInput = State<Omit<DocumentDriveState, "__typename" | "id" | "nodes"> & {
110
+ id?: string;
111
+ }, DocumentDriveLocalState>;
112
+ type RemoteDriveAccessLevel = "READ" | "WRITE";
113
+ type RemoteDriveOptions = DocumentDriveLocalState & {
114
+ pullFilter?: ListenerFilter;
115
+ pullInterval?: number;
116
+ expectedDriveInfo?: DriveInfo;
117
+ accessLevel?: RemoteDriveAccessLevel;
118
+ };
119
+ type CreateDocumentInput = CreateChildDocumentInput;
120
+ type SignalResult = {
121
+ signal: Signal;
122
+ result: unknown;
123
+ };
124
+ type IOperationResult<T extends Document = Document> = {
125
+ status: UpdateStatus;
126
+ error?: OperationError;
127
+ operations: Operation[];
128
+ document: T | undefined;
129
+ signals: SignalResult[];
130
+ };
131
+ type SynchronizationUnit = {
132
+ syncId: string;
133
+ driveId: string;
134
+ documentId: string;
135
+ documentType: string;
136
+ scope: string;
137
+ branch: string;
138
+ lastUpdated: string;
139
+ revision: number;
140
+ };
141
+ type SynchronizationUnitQuery = Omit<SynchronizationUnit, "revision" | "lastUpdated">;
142
+ type Listener = {
143
+ driveId: string;
144
+ listenerId: string;
145
+ label?: string;
146
+ block: boolean;
147
+ system: boolean;
148
+ filter: ListenerFilter;
149
+ callInfo?: ListenerCallInfo;
150
+ };
151
+ type ListenerRevision = {
152
+ driveId: string;
153
+ documentId: string;
154
+ scope: string;
155
+ branch: string;
156
+ status: UpdateStatus;
157
+ revision: number;
158
+ error?: string;
159
+ };
160
+ type UpdateStatus = "SUCCESS" | "CONFLICT" | "MISSING" | "ERROR";
161
+ type ErrorStatus = Exclude<UpdateStatus, "SUCCESS">;
162
+ type OperationUpdate = {
163
+ timestamp: string;
164
+ index: number;
165
+ skip: number;
166
+ type: string;
167
+ input: object;
168
+ hash: string;
169
+ context?: ActionContext;
170
+ id?: string;
171
+ };
172
+ type StrandUpdate = {
173
+ driveId: string;
174
+ documentId: string;
175
+ scope: OperationScope;
176
+ branch: string;
177
+ operations: OperationUpdate[];
178
+ };
179
+ type SyncStatus = "INITIAL_SYNC" | "SYNCING" | UpdateStatus;
180
+ type PullSyncStatus = SyncStatus;
181
+ type PushSyncStatus = SyncStatus;
182
+ type SyncUnitStatusObject = {
183
+ push?: PushSyncStatus;
184
+ pull?: PullSyncStatus;
185
+ };
186
+ type AddRemoteDriveStatus = "SUCCESS" | "ERROR" | "PENDING" | "ADDING" | "ALREADY_ADDED";
187
+ interface DriveEvents {
188
+ syncStatus: (driveId: string, status: SyncStatus, error?: Error, syncUnitStatus?: SyncUnitStatusObject) => void;
189
+ defaultRemoteDrive: (status: AddRemoteDriveStatus, defaultDrives: Map<string, DefaultRemoteDriveInfo>, driveInput: DefaultRemoteDriveInput, driveId?: string, driveName?: string, error?: Error) => void;
190
+ strandUpdate: (update: StrandUpdate) => void;
191
+ clientStrandsError: (driveId: string, trigger: Trigger, status: number, errorMessage: string) => void;
192
+ documentModels: (documentModels: DocumentModel[]) => void;
193
+ driveAdded: (drive: DocumentDriveDocument) => void;
194
+ driveDeleted: (driveId: string) => void;
195
+ }
196
+ type PartialRecord<K extends keyof any, T> = {
197
+ [P in K]?: T;
198
+ };
199
+ type RevisionsFilter = PartialRecord<OperationScope, number>;
200
+ type GetDocumentOptions = ReducerOptions & {
201
+ revisions?: RevisionsFilter;
202
+ checkHashes?: boolean;
203
+ };
204
+ type AddOperationOptions = {
205
+ forceSync?: boolean;
206
+ source: StrandUpdateSource;
207
+ };
208
+ type DefaultRemoteDriveInput = {
209
+ url: string;
210
+ options: RemoteDriveOptions;
211
+ };
212
+ type DefaultRemoteDriveInfo = DefaultRemoteDriveInput & {
213
+ status: AddRemoteDriveStatus;
214
+ metadata?: DriveInfo;
215
+ };
216
+ type GetStrandsOptions = {
217
+ limit?: number;
218
+ since?: string;
219
+ fromRevision?: number;
220
+ };
221
+ declare abstract class AbstractDocumentDriveServer {
222
+ /** Public methods **/
223
+ abstract initialize(): Promise<Error[] | null>;
224
+ abstract setDocumentModels(models: DocumentModel[]): void;
225
+ abstract getDrives(): Promise<string[]>;
226
+ abstract addDrive(drive: DriveInput): Promise<DocumentDriveDocument>;
227
+ abstract addRemoteDrive(url: string, options: RemoteDriveOptions): Promise<DocumentDriveDocument>;
228
+ abstract deleteDrive(id: string): Promise<void>;
229
+ abstract getDrive(id: string, options?: GetDocumentOptions): Promise<DocumentDriveDocument>;
230
+ abstract getDriveBySlug(slug: string): Promise<DocumentDriveDocument>;
231
+ abstract getDocuments(drive: string): Promise<string[]>;
232
+ abstract getDocument(drive: string, id: string, options?: GetDocumentOptions): Promise<Document>;
233
+ abstract addOperation(drive: string, id: string, operation: Operation, options?: AddOperationOptions): Promise<IOperationResult>;
234
+ abstract addOperations(drive: string, id: string, operations: Operation[], options?: AddOperationOptions): Promise<IOperationResult>;
235
+ abstract queueOperation(drive: string, id: string, operation: Operation, options?: AddOperationOptions): Promise<IOperationResult>;
236
+ abstract queueOperations(drive: string, id: string, operations: Operation[], options?: AddOperationOptions): Promise<IOperationResult>;
237
+ abstract queueAction(drive: string, id: string, action: Action, options?: AddOperationOptions): Promise<IOperationResult>;
238
+ abstract queueActions(drive: string, id: string, actions: Action[], options?: AddOperationOptions): Promise<IOperationResult>;
239
+ abstract addDriveOperation(drive: string, operation: Operation<DocumentDriveAction | BaseAction>, options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
240
+ abstract addDriveOperations(drive: string, operations: Operation<DocumentDriveAction | BaseAction>[], options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
241
+ abstract queueDriveOperation(drive: string, operation: Operation<DocumentDriveAction | BaseAction>, options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
242
+ abstract queueDriveOperations(drive: string, operations: Operation<DocumentDriveAction | BaseAction>[], options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
243
+ abstract queueDriveAction(drive: string, action: DocumentDriveAction | BaseAction, options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
244
+ abstract queueDriveActions(drive: string, actions: Array<DocumentDriveAction | BaseAction>, options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
245
+ abstract addAction(drive: string, id: string, action: Action, options?: AddOperationOptions): Promise<IOperationResult>;
246
+ abstract addActions(drive: string, id: string, actions: Action[], options?: AddOperationOptions): Promise<IOperationResult>;
247
+ abstract addDriveAction(drive: string, action: DocumentDriveAction | BaseAction, options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
248
+ abstract addDriveActions(drive: string, actions: (DocumentDriveAction | BaseAction)[], options?: AddOperationOptions): Promise<IOperationResult<DocumentDriveDocument>>;
249
+ abstract getSyncStatus(syncUnitId: string): SyncStatus | SynchronizationUnitNotFoundError;
250
+ abstract addInternalListener(driveId: string, receiver: IReceiver, options: {
251
+ listenerId: string;
252
+ label: string;
253
+ block: boolean;
254
+ filter: ListenerFilter;
255
+ }): Promise<IInternalTransmitter>;
256
+ /** Synchronization methods */
257
+ abstract getSynchronizationUnits(driveId: string, documentId?: string[], scope?: string[], branch?: string[], documentType?: string[], loadedDrive?: DocumentDriveDocument): Promise<SynchronizationUnit[]>;
258
+ abstract getSynchronizationUnit(driveId: string, syncId: string, loadedDrive?: DocumentDriveDocument): Promise<SynchronizationUnit | undefined>;
259
+ abstract getSynchronizationUnitsIds(driveId: string, documentId?: string[], scope?: string[], branch?: string[], documentType?: string[]): Promise<SynchronizationUnitQuery[]>;
260
+ abstract getOperationData(driveId: string, syncId: string, filter: GetStrandsOptions, loadedDrive?: DocumentDriveDocument): Promise<OperationUpdate[]>;
261
+ /** Internal methods **/
262
+ protected abstract createDocument(drive: string, document: CreateDocumentInput): Promise<Document>;
263
+ protected abstract deleteDocument(drive: string, id: string): Promise<void>;
264
+ protected abstract getDocumentModel(documentType: string): DocumentModel;
265
+ abstract getDocumentModels(): DocumentModel[];
266
+ /** Event methods **/
267
+ protected abstract emit<K extends keyof DriveEvents>(event: K, ...args: Parameters<DriveEvents[K]>): void;
268
+ abstract on<K extends keyof DriveEvents>(event: K, cb: DriveEvents[K]): Unsubscribe;
269
+ abstract getTransmitter(driveId: string, listenerId: string): Promise<ITransmitter | undefined>;
270
+ abstract clearStorage(): Promise<void>;
271
+ abstract registerPullResponderTrigger(id: string, url: string, options: Pick<RemoteDriveOptions, "pullFilter" | "pullInterval">): Promise<PullResponderTrigger>;
272
+ }
273
+ type PublicKeys<T> = {
274
+ [K in keyof T]: T extends {
275
+ [P in K]: T[K];
276
+ } ? K : never;
277
+ }[keyof T];
278
+ type PublicPart<T> = Pick<T, PublicKeys<T>>;
279
+ type IBaseDocumentDriveServer = PublicPart<AbstractDocumentDriveServer>;
280
+ type IDocumentDriveServer = IBaseDocumentDriveServer & IDefaultDrivesManager & IReadModeDriveServer;
281
+
7
282
  type Options = {
8
283
  express?: Express;
9
284
  port?: number;
10
285
  };
11
- declare function startAPI(reactor: BaseDocumentDriveServer, options: Options): Promise<void>;
286
+ declare function startAPI(reactor: IDocumentDriveServer, options: Options): Promise<void>;
12
287
 
13
288
  type InternalListenerModule = {
14
289
  name: string;
@@ -19,26 +294,26 @@ declare class InternalListenerManager {
19
294
  #private;
20
295
  private driveServer;
21
296
  private modules;
22
- constructor(driveServer: BaseDocumentDriveServer);
297
+ constructor(driveServer: IDocumentDriveServer);
23
298
  init(): Promise<void>;
24
299
  registerInternalListener(module: InternalListenerModule): Promise<void>;
25
300
  }
26
301
 
27
302
  declare const SUBGRAPH_REGISTRY: {
28
303
  name: string;
29
- getSchema: (driveServer: document_drive.BaseDocumentDriveServer) => graphql.GraphQLSchema;
304
+ getSchema: (driveServer: IDocumentDriveServer) => graphql.GraphQLSchema;
30
305
  }[];
31
306
 
32
307
  declare let reactorRouter: IRouter;
33
- declare const getListenerManager: (driveServer: BaseDocumentDriveServer) => Promise<InternalListenerManager>;
34
- declare const updateRouter: (driveServer: BaseDocumentDriveServer) => Promise<void>;
35
- declare const initReactorRouter: (path: string, app: express.Express, driveServer: BaseDocumentDriveServer) => Promise<void>;
308
+ declare const getListenerManager: (driveServer: IDocumentDriveServer) => Promise<InternalListenerManager>;
309
+ declare const updateRouter: (driveServer: IDocumentDriveServer) => Promise<void>;
310
+ declare const initReactorRouter: (path: string, app: express.Express, driveServer: IDocumentDriveServer) => Promise<void>;
36
311
  declare const addSubgraph: (subgraph: (typeof SUBGRAPH_REGISTRY)[number]) => Promise<void>;
37
312
  declare const registerInternalListener: (module: InternalListenerModule) => Promise<void>;
38
313
  declare const getAdditionalContextFields: () => {};
39
314
  declare const setAdditionalContextFields: (fields: Record<string, any>) => void;
40
315
 
41
- declare const createSchema: (documentDriveServer: BaseDocumentDriveServer, resolvers: GraphQLResolverMap, typeDefs: string) => graphql.GraphQLSchema;
42
- declare const getDocumentModelTypeDefs: (documentDriveServer: BaseDocumentDriveServer, typeDefs: string) => graphql.DocumentNode;
316
+ declare const createSchema: (documentDriveServer: IDocumentDriveServer, resolvers: GraphQLResolverMap, typeDefs: string) => graphql.GraphQLSchema;
317
+ declare const getDocumentModelTypeDefs: (documentDriveServer: IDocumentDriveServer, typeDefs: string) => graphql.DocumentNode;
43
318
 
44
319
  export { addSubgraph, createSchema, getAdditionalContextFields, getDocumentModelTypeDefs, getListenerManager, initReactorRouter, reactorRouter, registerInternalListener, setAdditionalContextFields, startAPI, updateRouter };