调用自定义API方法的羽毛

我用下面的东西来定义我的api:

class MyFeathersApi { feathersClient: any; accountsAPI: any; productsAPI: any; constructor(app) { var port: number = app.get('port'); this.accountsAPI = app.service('/api/accounts'); this.productsAPI = app.service('/api/products'); } findAdminAccounts(filter: any, cb: (err:Error, accounts:Models.IAccount[]) => void) { filter = { query: { adminProfile: { $exists: true } } } this.accountsAPI.find(filter, cb); } 

当我想要使用数据库适配器的方法,从客户端,即查找和/或创build,我做到以下几点:

 var accountsAPIService = app.service('/api/accounts'); accountsAPIService.find( function(error, accounts) { ... }); 

我如何从客户端调用自定义方法,例如findAdminAccounts()?

您只能使用客户端上的普通服务接口。 我们发现对自定义方法的支持(以及它从一个明确定义的接口到任意方法名称和参数所带来的所有问题)并不是真正必要的,因为它本身的一切都可以被描述为资源(服务)。

到目前为止,好处(如安全性,可预测性和发送明确定义的实时事件)已经远远超过了概念化应用程序逻辑所需的思维微小变化。

在你的例子中,你可以做一个包装服务,获取pipe理员帐户像这样:

 class AdminAccounts { find(params, cb) { var accountService = this.app.service('/api/accounts'); accountService.find({ query: { adminProfile: { $exists: true } } }, cb); } setup(app) { this.app = app; } } app.use('/api/adminAccounts', new AdminAccounts()); 

或者,您可以实现一个挂钩 ,将查询参数映射到更大的查询,如下所示:

 app.service('/api/accounts').before({ find(hook, next) { if(hook.params.query.admin) { hook.params.query.adminProfile = { $exists: true }; } next(); } }); 

这现在允许调用类似/api/accounts?admin

欲了解更多信息,请参阅此FAQ 。