diff --git a/types/sharedb/index.d.ts b/types/sharedb/index.d.ts index 7657075f8e..2f78d1d3dc 100644 --- a/types/sharedb/index.d.ts +++ b/types/sharedb/index.d.ts @@ -125,8 +125,9 @@ declare namespace middleware { doc: DocContext; // Deprecated, use 'readSnapshots' instead. op: OpContext; query: QueryContext; - receive: ReceiveContext; readSnapshots: ReadSnapshotsContext; + receive: ReceiveContext; + reply: ReplyContext; submit: SubmitContext; } @@ -165,22 +166,27 @@ declare namespace middleware { projection: Projection | undefined; fields: ProjectionFields | undefined; channel: string; - query: any; - options: any; + query: ShareDB.JSONObject; + options: ShareDB.JSONObject; db: sharedb.DB | null; snapshotProjection: Projection | null; } - interface ReceiveContext extends BaseContext { - data: any; - } - interface ReadSnapshotsContext extends BaseContext { collection: string; snapshots: ShareDB.Snapshot[]; snapshotType: SnapshotType; } + interface ReceiveContext extends BaseContext { + data: ShareDB.JSONObject; // ClientRequest, but before any validation + } + + interface ReplyContext extends BaseContext { + request: ShareDB.ClientRequest; + reply: ShareDB.JSONObject; + } + type SnapshotType = 'current' | 'byVersion' | 'byTimestamp'; interface SubmitContext extends BaseContext, SubmitRequest { diff --git a/types/sharedb/lib/sharedb.d.ts b/types/sharedb/lib/sharedb.d.ts index 169b7e6674..01eace14c6 100644 --- a/types/sharedb/lib/sharedb.d.ts +++ b/types/sharedb/lib/sharedb.d.ts @@ -1,18 +1,25 @@ import { EventEmitter } from 'events'; +export type JSONValue = string | number | boolean | null | JSONObject | JSONArray; +export interface JSONObject { + [name: string]: JSONValue; +} +export interface JSONArray extends Array {} + export type Path = ReadonlyArray; export interface Snapshot { id: string; v: number; type: string | null; - data: any; // JSON object, could be undefined + data: JSONObject | undefined; m: SnapshotMeta | null; } export interface SnapshotMeta { ctime: number; mtime: number; - [key: string]: any; + // Users can use server middleware to add additional metadata to snapshots. + [key: string]: JSONValue; } export interface AddNumOp { p: Path; na: number; } @@ -95,3 +102,12 @@ export class Query extends EventEmitter { removeListener(event: QueryEvent, listener: (...args: any[]) => any): this; destroy(): void; } + +export type RequestAction = 'qf' | 'qs' | 'qu' | 'bf' | 'bs' | 'bu' | 'f' | 's' | 'u' | 'op' | 'nf' | 'nt'; + +export interface ClientRequest { + /** Short name of the request's action */ + a: RequestAction; + + [propertyName: string]: JSONValue; +} diff --git a/types/sharedb/sharedb-tests.ts b/types/sharedb/sharedb-tests.ts index a4e35d3507..da4e5ed948 100644 --- a/types/sharedb/sharedb-tests.ts +++ b/types/sharedb/sharedb-tests.ts @@ -101,6 +101,16 @@ backend.use('receive', (context, callback) => { ); callback(); }); +backend.use('reply', (context, callback) => { + console.log( + context.action, + context.agent, + context.backend, + context.request, + context.reply, + ); + callback(); +}); backend.use('readSnapshots', (context, callback) => { console.log( context.action,