Mongoose''插件'方法的头文件(通过.methods和.statics扩展)

时间:2015-08-05 09:54:21

标签: mongoose typescript passport.js definitelytyped mongoose-plugins

我正在尝试为script创建使用.plugin方法扩展我的Mongoose模型的Typescript头文件。来自Mongoose header-files的当前签名:

export class Schema {
   // ...
   plugin(plugin: (schema: Schema, options?: Object) => void, 
                   options?: Object): Schema;
   // ...
}

来自Mongoose-lib的一些实际代码:

/**
 * Registers a plugin for this schema.
 *
 * @param {Function} plugin callback
 * @param {Object} [opts]
 * @see plugins
 * @api public
 */

Schema.prototype.plugin = function (fn, opts) {
  fn(this, opts);
  return this;
};

然后是我自己的模型,扩展plugin;

import passportLocalMongoose = require('passport-local-mongoose')
// ...
var userSchema = new mongoose.Schema({
    email: String,
    password: String,
});
// ...
userSchema.plugin(passportLocalMongoose, {
    usernameField: "email",
    usernameLowerCase: true
});

来自passport-local-mongoose来源的代码段

module.exports = function(schema, options) {
   // ...   
   schema.methods.setPassword = function (password, cb) {
      // ...
   }

   schema.statics.authenticate = function() {
      // ...
   }
   // ...
}

主app.js中出现问题

   // ...

   userSchema.authenticate()                // <<< Typescript error, undefined

   //   OR

   userSchemaInstance.setPassword(pass, cb) // <<< Typescript error, undefined

问题在于.authenticate等是通过.methods.statics动态添加的......

我找不到在打字稿头文件中对此进行建模的方法。

我尝试了泛型和东西,但我不能(动态)将提供的插件方法应用回原始模型。我还尝试plugin返回泛型T extends S & P(其中S从第一个参数扩展Schema,P =插件本身)。没有运气: - (

如何解决此问题的任何建议或示例?

1 个答案:

答案 0 :(得分:3)

passport-local-mongoose.d.ts文件中声明接口:

declare module 'mongoose' {
    // methods
    export interface PassportLocalDocument extends Document {
        setPassword(pass: string, cb: (err: any) => void);
    }

    // statics
    export interface PassportLocalModel<T extends PassportLocalDocument> extends Model<T> {
        authenticate(username: string, password: string, cb: (err: any) => void);
    }

    // plugin options
    export interface PassportLocalOptions {
        usernameField?: string;
        usernameLowerCase?: boolean;
    }

    export interface PassportLocalSchema extends Schema {
        plugin(
            plugin: (schema: PassportLocalSchema, options?: PassportLocalOptions) => void,
            options?: PassportLocalOptions): Schema;
    }

    export function model<T extends PassportLocalDocument>(
        name: string,
        schema?: PassportLocalSchema,
        collection?: string,
        skipInit?: boolean): PassportLocalModel<T>;
}

declare module 'passport-local-mongoose' {
    import mongoose = require('mongoose');
    var _: (schema: mongoose.Schema, Options?: Object) => void;
    export = _;
}

app.ts

中使用它
import mongoose = require('mongoose');
import passportLocalMongoose = require('passport-local-mongoose');

interface UserDocument extends mongoose.PassportLocalDocument {
    email: string,
    password: string;
}

var userSchema = <mongoose.PassportLocalSchema>new mongoose.Schema({
    email: String,
    password: String
});

userSchema.plugin(passportLocalMongoose, {
    usernameField: 'email',
    usernameLowerCase: true
});

var User = mongoose.model<UserDocument>('User', userSchema);
User.authenticate(userName, pass, cb);

var user = new User();
user.setPassword(newPass, cb);