From 6b847d3197214f57b53ddd1beb019bd3d03c6478 Mon Sep 17 00:00:00 2001 From: mernxl Date: Fri, 3 Aug 2018 17:12:08 +0100 Subject: [PATCH 1/2] FixType(mongoose): fix issues around undefined(optional) options for `plugin` --- types/mongoose/index.d.ts | 6 ++++-- types/mongoose/mongoose-tests.ts | 7 ++++++- types/mongoose/v3/index.d.ts | 3 +++ types/mongoose/v4/index.d.ts | 6 ++++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 1beed12c69..d37aff854a 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -151,7 +151,8 @@ declare module "mongoose" { * @param fn plugin callback * @param opts optional options */ - export function plugin(fn: Function, opts?: T): typeof mongoose; + export function plugin(fn: Function): typeof mongoose; + export function plugin(fn: Function, opts: T): typeof mongoose; /** Sets mongoose options */ export function set(key: string, value: any): void; @@ -655,7 +656,8 @@ declare module "mongoose" { * Registers a plugin for this schema. * @param plugin callback */ - plugin(plugin: (schema: Schema, options?: T) => void, opts?: T): this; + plugin(plugin: (schema: Schema) => void): this; + plugin(plugin: (schema: Schema, options: T) => void, opts: T): this; /** * Defines a post hook for the document diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index cadc7d9a83..eaee271448 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -593,6 +593,11 @@ export default function(schema: mongoose.Schema) { } // plugins +function MyPlugin(schema: mongoose.Schema, opts?: string) { +} +mongoose.Schema({}) + .plugin(MyPlugin) + interface PluginOption { modelName: string; timestamp: string; @@ -602,7 +607,7 @@ function logger(modelName: string, timestamp: string) { // call special logger with options } -function AwesomeLoggerPlugin(schema: mongoose.Schema, options?: PluginOption) { +function AwesomeLoggerPlugin(schema: mongoose.Schema, options: PluginOption) { if (options) { schema.pre('save', function (next: Function) { logger(options.modelName, options.timestamp) diff --git a/types/mongoose/v3/index.d.ts b/types/mongoose/v3/index.d.ts index b38a20179c..2c8232f21d 100644 --- a/types/mongoose/v3/index.d.ts +++ b/types/mongoose/v3/index.d.ts @@ -14,6 +14,7 @@ declare module "mongoose" { function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; function modelNames(): string[]; + function plugin(plugin: (schema: Schema) => void): Mongoose function plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; function get(key: string): any; @@ -33,6 +34,7 @@ declare module "mongoose" { get(key: string): any; model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; modelNames(): string[]; + plugin(plugin: (schema: Schema) => void): Mongoose; plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; set(key: string, value: any): void; @@ -273,6 +275,7 @@ declare module "mongoose" { path(path: string): any; path(path: string, constructor: any): Schema; pathType(path: string): string; + plugin(plugin: (schema: Schema) => void): Schema; plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Schema; pre(method: string, fn: HookSyncCallback, errorCb?: HookErrorCallback): Schema; diff --git a/types/mongoose/v4/index.d.ts b/types/mongoose/v4/index.d.ts index 6dfd75308a..ee3cd4c045 100644 --- a/types/mongoose/v4/index.d.ts +++ b/types/mongoose/v4/index.d.ts @@ -173,7 +173,8 @@ declare module "mongoose" { * @param fn plugin callback * @param opts optional options */ - export function plugin(fn: Function, opts?: T): typeof mongoose; + export function plugin(fn: Function): typeof mongoose; + export function plugin(fn: Function, opts: T): typeof mongoose; /** Sets mongoose options */ export function set(key: string, value: any): void; @@ -678,7 +679,8 @@ declare module "mongoose" { * Registers a plugin for this schema. * @param plugin callback */ - plugin(plugin: (schema: Schema, options?: T) => void, opts?: T): this; + plugin(plugin: (schema: Schema) => void): this; + plugin(plugin: (schema: Schema, options: T) => void, opts: T): this; /** * Defines a post hook for the document From 7eb3b5f4edc6443c8b4dd9b1bdd83704ffc8bb1d Mon Sep 17 00:00:00 2001 From: mernxl Date: Sat, 4 Aug 2018 14:25:16 +0100 Subject: [PATCH 2/2] FixType(mongoose): fix minor syntax issues and make options nonNullable in v3 Check ensure by linting --- types/mongoose/mongoose-tests.ts | 14 +++++++------- types/mongoose/v3/index.d.ts | 8 ++++---- types/mongoose/v4/mongoose-tests.ts | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index eaee271448..2fe1e1b12e 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -293,7 +293,7 @@ schema.method('name', cb).method({ }); schema.path('a', mongoose.Schema.Types.Buffer).path('a'); schema.pathType('m1').toLowerCase(); -schema.plugin(function (schema, opts) { +schema.plugin(function (schema: mongoose.Schema, opts?: any) { schema.get('path'); if (opts) { opts.hasOwnProperty(''); @@ -564,25 +564,25 @@ new mongoose.Schema({ } }); -(new mongoose.Schema({})).plugin(function (schema: any, options: any) { +(new mongoose.Schema({})).plugin(function (schema: mongoose.Schema, options: any) { schema.add({ lastMod: Date }) schema.pre('save', function (next: Function) { - this.lastMod = new Date + (this as any).lastMod = new Date next() }) if (options && options['index']) { schema.path('lastMod').index(options['index']) } -}, { index: true }).plugin(function (schema: any, options: any) { +}, { index: true }).plugin(function (schema: mongoose.Schema, options: any) { schema.add({ lastMod: Date }) schema.pre('save', function (next: Function) { - this.lastMod = new Date + (this as any).lastMod = new Date next() }) if (options && options['index']) { schema.path('lastMod').index(options['index']) } -}); +}, {index: true}); new mongoose.Schema({foo: String}, {strict: 'throw'}); @@ -595,7 +595,7 @@ export default function(schema: mongoose.Schema) { // plugins function MyPlugin(schema: mongoose.Schema, opts?: string) { } -mongoose.Schema({}) +new mongoose.Schema({}) .plugin(MyPlugin) interface PluginOption { diff --git a/types/mongoose/v3/index.d.ts b/types/mongoose/v3/index.d.ts index 2c8232f21d..40e254deda 100644 --- a/types/mongoose/v3/index.d.ts +++ b/types/mongoose/v3/index.d.ts @@ -14,8 +14,8 @@ declare module "mongoose" { function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; function modelNames(): string[]; - function plugin(plugin: (schema: Schema) => void): Mongoose - function plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; + function plugin(plugin: (schema: Schema) => void): Mongoose; + function plugin(plugin: (schema: Schema, options: T) => void, options: T): Mongoose; function get(key: string): any; function set(key: string, value: any): void; @@ -35,7 +35,7 @@ declare module "mongoose" { model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): Model; modelNames(): string[]; plugin(plugin: (schema: Schema) => void): Mongoose; - plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Mongoose; + plugin(plugin: (schema: Schema, options: T) => void, options: T): Mongoose; set(key: string, value: any): void; mongo: any; @@ -276,7 +276,7 @@ declare module "mongoose" { path(path: string, constructor: any): Schema; pathType(path: string): string; plugin(plugin: (schema: Schema) => void): Schema; - plugin(plugin: (schema: Schema, options?: T) => void, options?: T): Schema; + plugin(plugin: (schema: Schema, options: T) => void, options: T): Schema; pre(method: string, fn: HookSyncCallback, errorCb?: HookErrorCallback): Schema; pre(method: string, isAsync: boolean, fn: HookAsyncCallback, errorCb?: HookErrorCallback): Schema; diff --git a/types/mongoose/v4/mongoose-tests.ts b/types/mongoose/v4/mongoose-tests.ts index ddf96848b9..b2d2a834fd 100644 --- a/types/mongoose/v4/mongoose-tests.ts +++ b/types/mongoose/v4/mongoose-tests.ts @@ -262,7 +262,7 @@ schema.method('name', cb).method({ }); schema.path('a', mongoose.Schema.Types.Buffer).path('a'); schema.pathType('m1').toLowerCase(); -schema.plugin(function (schema, opts) { +schema.plugin(function (schema: mongoose.Schema, opts?: any) { schema.get('path'); if (opts) { opts.hasOwnProperty(''); @@ -424,7 +424,7 @@ new mongoose.Schema({ if (options && options['index']) { schema.path('lastMod').index(options['index']) } -}); +}, {index: true}); // plugins interface PluginOption {