-
Type:
Task
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
We extend the Shell API TS definitions created in MONGOSH-2031 with definitions for the shell variants of the BSON constructors. This requires properly typing the bsonPkg object in order to provide reasonable definitions to consumers of it.
When post-processing api-raw.d.ts into api-processed.d.ts, declare global functions for all the methods inside the ShellBsonBase interface since we assign those straight onto the shell's global context.
Example:
~/mongo/mongosh % cat packages/shell-api/lib/shell-bson.d.ts /// <reference types="node" /> import Help from './help'; import type { BinaryType, Document } from '@mongosh/service-provider-core'; import { bson as BSON } from '@mongosh/service-provider-core'; type LongWithoutAccidentallyExposedMethods = Omit<typeof BSON.Long, 'fromExtendedJSON'>; interface ShellBsonBase { DBRef: (namespace: string, oid: any, db?: string, fields?: Document) => typeof BSON.DBRef.prototype; bsonsize: (object: any) => number; MaxKey: () => typeof BSON.MaxKey.prototype; MinKey: () => typeof BSON.MinKey.prototype; ObjectId: (id?: string | number | typeof BSON.ObjectId.prototype | Buffer) => typeof BSON.ObjectId.prototype; Timestamp: (t?: number | typeof BSON.Long.prototype | { t: number; i: number; }, i?: number) => typeof BSON.Timestamp.prototype; Code: (c?: string | Function, s?: any) => typeof BSON.Code.prototype; NumberDecimal: (s?: string) => typeof BSON.Decimal128.prototype; NumberInt: (v?: string) => typeof BSON.Int32.prototype; NumberLong: (s?: string | number) => typeof BSON.Long.prototype; ISODate: (input?: string) => Date; BinData: (subtype: number, b64string: string) => BinaryType; HexData: (subtype: number, hexstr: string) => BinaryType; UUID: (hexstr?: string) => BinaryType; MD5: (hexstr: string) => BinaryType; Decimal128: typeof BSON.Decimal128; BSONSymbol: typeof BSON.BSONSymbol; Int32: typeof BSON.Int32; Long: LongWithoutAccidentallyExposedMethods; Binary: typeof BSON.Binary; Double: typeof BSON.Double; EJSON: typeof BSON.EJSON; BSONRegExp: typeof BSON.BSONRegExp; } type WithHelp<T> = { [prop in keyof T]: T[prop] & { help?: () => Help; }; }; export type ShellBson = WithHelp<ShellBsonBase>; export default function constructShellBson(bson: typeof BSON, printWarning: (msg: string) => void): ShellBson; export {}
Then we'll append the following code to api-processed.d.ts:
declare global { const UUID: (hexstr?: string) => BinaryType; // etc. }
Probably with some added
& { help?: () => Help }
- depends on
-
MONGOSH-2031 Create and ship TS definitions for shell-api package
-
- In Code Review
-