diff --git a/types/gun/gun-tests.ts b/types/gun/gun-tests.ts index 2f65781bac..d196fba695 100644 --- a/types/gun/gun-tests.ts +++ b/types/gun/gun-tests.ts @@ -10,7 +10,7 @@ import 'gun/lib/later.js'; import 'gun/lib/unset.js'; import 'gun/lib/time.js'; -Gun('http://yourdomain.com/gun'); +GunServer('http://yourdomain.com/gun'); Gun(['http://server1.com/gun', 'http://server2.com/gun']); Gun({ s3: { @@ -30,22 +30,34 @@ interface AppState { str: string; bool: boolean; obj: { - arr2: Array<{ foo: number; bar: string; }> - } + arr2: Array<{ foo: number; bar: string }>; + }; }; - chatRoom: Array<{ by: string; message: string; }>; + chatRoom: Array<{ by: string; message: string }>; } const app = new Gun(); -app.get('object').get('bool').put(true); -app.get('object').get('num').put(1); -app.get('object').get('obj').get('arr2').set({ foo: 1, bar: '2' }); +app.get('object') + .get('bool') + .put(true); +app.get('object') + .get('num') + .put(1); +app.get('object') + .get('obj') + .get('arr2') + .set({ foo: 1, bar: '2' }); -app.get('object').on((data) => { +app.get('object') + .get('bool') + // $ExpectError + .put(1); + +app.get('object').on(data => { data.bool; }); app.get('object').off(); -app.get('object').once((data) => { +app.get('object').once(data => { if (data) data.bool; }); async function name() { @@ -53,4 +65,8 @@ async function name() { data.put.bool; } app.get('chatRoom').time!({ by: 'A', message: 'Hello' }); -app.get('chatRoom').time!((msg) => { msg.by; }, 20); +app.get('chatRoom').time!(msg => { + msg.by; +}, 20); +// $ExpectError +app.get('object').time!({ a: 1 }); diff --git a/types/gun/gun.d.ts b/types/gun/gun.d.ts new file mode 100644 index 0000000000..841dfa34a9 --- /dev/null +++ b/types/gun/gun.d.ts @@ -0,0 +1,3 @@ + +declare const cons: typeof import('gun') +export = cons diff --git a/types/gun/index.d.ts b/types/gun/index.d.ts index 0c9063b4ed..51d02b68d5 100644 --- a/types/gun/index.d.ts +++ b/types/gun/index.d.ts @@ -2,23 +2,17 @@ // Project: https://github.com/amark/gun#readme // Definitions by: Jack Works // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.9 -declare module 'gun' { - const Gun: Gun.Constructor - export = Gun -} - -declare module 'gun/gun' { - const cons: typeof import('gun') - export = cons -} +declare const cons: Gun.Constructor; +export = cons; declare namespace Gun { - type ArrayOf = T extends Array ? U : never + type ArrayOf = T extends Array ? U : never; /** Gun does not accept Array value, so we need extract to make types correct */ - type AllowArray = ArrayOf extends never ? T : ArrayOf - type DisallowArray = ArrayOf extends never ? T : never - type ArrayAsRecord = ArrayOf extends never ? DataType : Record + type AllowArray = ArrayOf extends never ? T : ArrayOf; + type DisallowArray = ArrayOf extends never ? T : never; + type ArrayAsRecord = ArrayOf extends never ? DataType : Record; /** * options['module name'] allows you to pass options to a 3rd party module. * Their project README will likely list the exposed options @@ -26,31 +20,31 @@ declare namespace Gun { */ type ConstructorOptions = Partial<{ /** Undocumented but mentioned. Write data to a JSON. */ - file: string + file: string; /** Undocumented but mentioned. Create a websocket server */ - web: any + web: any; /** Undocumented but mentioned. Amazon S3 */ s3: { - key: any - secret: any - bucket: any - } + key: any; + secret: any; + bucket: any; + }; /** the URLs are properties, and the value is an empty object. */ - peers: Record + peers: Record; /** default: true, creates and persists local (nodejs) data using Radisk. */ - radisk: boolean + radisk: boolean; /** default: true, persists local (browser) data to localStorage. */ - localStorage: boolean + localStorage: boolean; /** uuid allows you to override the default 24 random alphanumeric soul generator with your own function. */ - uuid(): string + uuid(): string; /** * allows you to pass options to a 3rd party module. Their project README will likely list the exposed options * @see https://github.com/amark/gun/wiki/Modules */ - [key: string]: any - }> - type Saveable = Partial | string | number | boolean | null | ChainReference - type AckCallback = (ack: { err: Error; ok: any } | { err: undefined; ok: string }) => void + [key: string]: any; + }>; + type Saveable = Partial | string | number | boolean | null | ChainReference; + type AckCallback = (ack: { err: Error; ok: any } | { err: undefined; ok: string }) => void; interface ChainReference { /** * Save data into gun, syncing it with your connected peers. @@ -65,7 +59,7 @@ declare namespace Gun { * * @param callback invoked on each acknowledgment */ - put(data: DisallowArray, callback?: AckCallback): ChainReference + put(data: DisallowArray, callback?: AckCallback): ChainReference; /** * Where to read data from. * @param key The key is the ID or property name of the data that you saved from earlier @@ -89,7 +83,7 @@ declare namespace Gun { /** the key, ID, or property name of the data. */ paramB: Record<'off' | 'to' | 'next' | 'the' | 'on' | 'as' | 'back' | 'rid' | 'id', any> ) => void - ): ChainReference + ): ChainReference; /** * Change the configuration of the gun database instance. * @param options The options argument is the same object you pass to the constructor. @@ -97,7 +91,7 @@ declare namespace Gun { * The options's properties replace those in the instance's configuration but options.peers are **added** to peers known to the gun instance. * @returns No mention in the document, behavior as `ChainReference` */ - opt(options: ConstructorOptions): ChainReference + opt(options: ConstructorOptions): ChainReference; /** * Move up to the parent context on the chain. * @@ -106,7 +100,7 @@ declare namespace Gun { * `-1` or `Infinity` will take you to the root. * @returns Impossible to determinate final type. You must cast it by yourself. */ - back(amount?: number): ChainReference + back(amount?: number): ChainReference; // Main API /** @@ -123,7 +117,7 @@ declare namespace Gun { on( callback: (data: ArrayAsRecord, key: ReferenceKey) => void, option?: { change: boolean } | boolean - ): ChainReference + ): ChainReference; /** * Get the current data without subscribing to updates. Or `undefined` if it cannot be found. * @returns In the document, it said the return value may change in the future. Don't rely on it. @@ -131,7 +125,7 @@ declare namespace Gun { once( callback?: (data: (ArrayAsRecord) | undefined, key: ReferenceKey) => void, option?: { wait: number } - ): ChainReference + ): ChainReference; /** * **.set does not means 'set data', it means a Mathematical Set** * @@ -148,7 +142,7 @@ declare namespace Gun { : never : never, callback?: AckCallback - ): ChainReference> + ): ChainReference>; /** * Map iterates over each property and item on a node, passing it down the chain, * behaving like a forEach on your data. @@ -156,13 +150,13 @@ declare namespace Gun { */ map( callback?: (value: ArrayOf, key: DataType) => ArrayOf | undefined - ): ChainReference, ReferenceKey> + ): ChainReference, ReferenceKey>; /** * Undocumented, but extremely useful and mentioned in the document * * Remove **all** listener on this node. */ - off(): void + off(): void; // Extended API /** @@ -175,14 +169,14 @@ declare namespace Gun { * **Warning**: Not included by default! You must include it yourself via `require('gun/lib/path.js')` or * ``! */ - path?(path: string | string[]): ChainReference + path?(path: string | string[]): ChainReference; /** * Handle cases where data can't be found. * * **Warning**: Not included by default! You must include it yourself via `require('gun/lib/not.js')` or * ``! */ - not?(callback: (key: ReferenceKey) => void): ChainReference + not?(callback: (key: ReferenceKey) => void): ChainReference; /** * Open behaves very similarly to gun.on, except it gives you the **full depth of a document** on every update. * It also works with graphs, tables, or other data structures. Think of it as opening up a live connection to a document. @@ -190,23 +184,23 @@ declare namespace Gun { * **Warning**: Not included by default! You must include it yourself via `require('gun/lib/open.js')` or * ``! */ - open?(callback: (data: ArrayAsRecord) => void): ChainReference + open?(callback: (data: ArrayAsRecord) => void): ChainReference; /** * Loads the full object once. It is the same as `open` but with the behavior of `once`. * * **Warning**: Not included by default! You must include it yourself via `require('gun/lib/load.js')` or * ``! */ - load?(callback: (data: ArrayAsRecord) => void): ChainReference + load?(callback: (data: ArrayAsRecord) => void): ChainReference; /** * Returns a promise for you to use. * * **Warning**: Not included by default! You must include it yourself via `require('gun/lib/then.js')` or * ``! */ - then?, TResult2 = never>( - onfulfilled?: ((value: TResult1) => TResult1 | PromiseLike) | undefined | null - ): Promise + then?>( + onfulfilled?: (value: TResult1) => TResult1 | PromiseLike + ): Promise; /** * Returns a promise for you to use. * @@ -214,11 +208,10 @@ declare namespace Gun { * ``! */ promise?< - TResult1 = { put: ArrayAsRecord; key: ReferenceKey; gun: ChainReference }, - TResult2 = never + TResult1 = { put: ArrayAsRecord; key: ReferenceKey; gun: ChainReference } >( - onfulfilled?: ((value: TResult1) => TResult1 | PromiseLike) | undefined | null - ): Promise + onfulfilled?: (value: TResult1) => TResult1 | PromiseLike + ): Promise; /** * bye lets you change data after that browser peer disconnects. * This is useful for games and status messages, @@ -228,8 +221,8 @@ declare namespace Gun { * ``! */ bye?(): { - put(data: DisallowArray>): void - } + put(data: DisallowArray>): void; + }; /** * Say you save some data, but want to do something with it later, like expire it or refresh it. * Well, then `later` is for you! You could use this to easily implement a TTL or similar behavior. @@ -244,14 +237,14 @@ declare namespace Gun { key: ReferenceKey ) => void, seconds: number - ): ChainReference + ): ChainReference; /** * After you save some data in an unordered list, you may need to remove it. * * **Warning**: Not included by default! You must include it yourself via `require('gun/lib/unset.js')` or * ``! */ - unset?(data: ArrayOf): ChainReference + unset?(data: ArrayOf): ChainReference; /** * Subscribes to all future events that occur on the Timegraph and retrieve a specified number of old events * @@ -260,59 +253,43 @@ declare namespace Gun { time?( callback: (data: ArrayOf, key: ReferenceKey, time: number) => void, alsoReceiveNOldEvents?: number - ): ChainReference + ): ChainReference; /** Pushes data to a Timegraph with it's time set to Gun.state()'s time */ - time?(data: ArrayOf): void + time?(data: ArrayOf): void; } interface GunSEA { // There is no the only content in the api document. user: { - create(alias: string, passphrase: string, callback: (...args: any[]) => void): any - } + create(alias: string, passphrase: string, callback: (...args: any[]) => void): any; + }; } interface Constructor { /** * @description * no parameters creates a local datastore using the default persistence layer, either localStorage or Radisk. - */ - (): ChainReference & GunSEA - new (): ChainReference & GunSEA - - /** - * @param url + * @param options * passing a URL creates the above local datastore that also tries to sync with the URL. * * or you can pass in an array of URLs to sync with multiple peers. */ - (url: string | string[]): ChainReference & GunSEA - new (url: string | string[]): ChainReference & GunSEA - (option: ConstructorOptions): ChainReference & GunSEA - new (option: ConstructorOptions): ChainReference & GunSEA + (options?: string | string[] | ConstructorOptions): ChainReference & GunSEA; + new (options?: string | string[] | ConstructorOptions): ChainReference & GunSEA; node: { /** Returns true if data is a gun node, otherwise false. */ - is(anything: any): anything is ChainReference - /** Returns data's gun ID (instead of manually grabbing its metadata i.e. data["_"]["#"], which is faster but could change in the future) + is(anything: any): anything is ChainReference; + /** + * Returns data's gun ID (instead of manually grabbing its metadata i.e. data["_"]["#"], which is faster but could change in the future) * - * Returns undefined if data is not correct gun data. */ - soul(data: ChainReference): string + * Returns undefined if data is not correct gun data. + */ + soul(data: ChainReference): string; /** Returns a "gun-ified" variant of the json input by injecting a new gun ID into the metadata field. */ - ify(json: any): any - } + ify(json: any): any; + }; /** @see https://gun.eco/docs/SEA */ - SEA: any + SEA: any; } } - -// Following modules does not export anything, but extends the gun prototype -declare module 'gun/lib/path.js' -declare module 'gun/lib/not.js' -declare module 'gun/lib/open.js' -declare module 'gun/lib/load.js' -declare module 'gun/lib/then.js' -declare module 'gun/lib/bye.js' -declare module 'gun/lib/later.js' -declare module 'gun/lib/unset.js' -declare module 'gun/lib/time.js' -declare const Gun: typeof import('gun') +declare const Gun: Gun.Constructor; diff --git a/types/gun/tsconfig.json b/types/gun/tsconfig.json index 7b7490d14c..10fe933ba8 100644 --- a/types/gun/tsconfig.json +++ b/types/gun/tsconfig.json @@ -7,6 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/gun/tslint.json b/types/gun/tslint.json index 3db14f85ea..982ecfcd14 100644 --- a/types/gun/tslint.json +++ b/types/gun/tslint.json @@ -1 +1 @@ -{ "extends": "dtslint/dt.json" } +{ "extends": "dtslint/dt.json", "rules": { "no-unnecessary-generics": false } }