diff --git a/types/sharedb/index.d.ts b/types/sharedb/index.d.ts index edf7464412ee7b..640f5dc5f187ad 100644 --- a/types/sharedb/index.d.ts +++ b/types/sharedb/index.d.ts @@ -3,7 +3,7 @@ import { EventEmitter } from "events"; import { Duplex } from "stream"; import Agent = require("./lib/agent"); -import { Connection } from "./lib/client"; +import { Connection, Snapshot } from "./lib/client"; import * as ShareDB from "./lib/sharedb"; interface PubSubOptions { @@ -56,6 +56,27 @@ declare class sharedb extends EventEmitter { * or session info. */ listen(stream: Duplex, request?: any): Agent; + fetch( + agent: Agent, + index: string, + id: string, + callback: (error: Error | null, snapshot?: Snapshot) => void, + ): void; + fetch( + agent: Agent, + index: string, + id: string, + options: ShareDB.BackendFetchOptions | null, + callback: (error: Error | null, snapshot?: Snapshot) => void, + ): void; + submit( + agent: Agent, + index: string, + id: string, + op: SubmitRequest["op"], + options: ShareDB.BackendSubmitOptions | null, + callback: (error: Error | null, ops: any[], request?: SubmitRequest) => void, + ): void; close(callback?: BasicCallback): void; /** * Registers a server middleware function. diff --git a/types/sharedb/lib/sharedb.d.ts b/types/sharedb/lib/sharedb.d.ts index fc4e290ca33e83..f1506cb49283fa 100644 --- a/types/sharedb/lib/sharedb.d.ts +++ b/types/sharedb/lib/sharedb.d.ts @@ -104,9 +104,9 @@ export interface RawOp { d: DocumentID; } -export type CreateOp = RawOp & { create: { type: string; data: any }; del: undefined; op: undefined }; -export type DeleteOp = RawOp & { del: boolean; create: undefined; op: undefined }; -export type EditOp = RawOp & { op: any[]; create: undefined; del: undefined }; +export type CreateOp = Partial & { create: { type: string; data: any } }; +export type DeleteOp = Partial & { del: boolean }; +export type EditOp = Partial & { op: any[] }; export type OTType = "ot-text" | "ot-json0" | "ot-json1" | "ot-text-tp2" | "rich-text"; @@ -154,6 +154,12 @@ export interface ShareDBSourceOptions { // interface ShareDBCreateOptions extends ShareDBSourceOptions {} // interface ShareDBDelOptions extends ShareDBSourceOptions {} // interface ShareDBSubmitOpOptions extends ShareDBSourceOptions {} +export interface BackendFetchOptions { + snapshotOptions?: Record; +} +export interface BackendSubmitOptions { + [key: string]: unknown; +} export type Callback = (err: Error) => any; diff --git a/types/sharedb/sharedb-tests.ts b/types/sharedb/sharedb-tests.ts index a741d9e9bf6e25..0a42123fb18ab6 100644 --- a/types/sharedb/sharedb-tests.ts +++ b/types/sharedb/sharedb-tests.ts @@ -92,6 +92,23 @@ console.log(backend.extraDbs); backend.addProjection("notes_minimal", "notes", { title: true, creator: true, lastUpdateTime: true }); const readonlyProjection = backend.projections["notes_minimal"]; console.log(readonlyProjection.target, readonlyProjection.fields); +backend.submit({} as Agent, "notes_minimal", "doc1", { create: { data: {}, type: "json uri type" } }, { + customField: true, + anotherOption: { nested: "value" }, +}, (error, ops, request) => { + if (error) { + console.error(error.message); + } + console.log(ops, request && request.collection); +}); +backend.fetch({} as Agent, "notes_minimal", "doc1", { + snapshotOptions: { foo: "bar" }, +}, (error, snapshot) => { + if (error) { + console.error(error.message); + } + console.log(snapshot && snapshot.data); +}); // backend.projections is used by sharedb internally, so they shouldn't be messed with. // Test that marking as readonly in API prevents external modification. // @ts-expect-error @@ -135,9 +152,9 @@ for (const action of submitRelatedActions) { request.snapshot, request.ops, request.channels, - request.op.op, - request.op.create, - request.op.del, + (request.op as ShareDB.EditOp).op, + (request.op as ShareDB.CreateOp).create, + (request.op as ShareDB.DeleteOp).del, request.extra.source, ); callback();