我用如下内容定义我的 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);
}
当我想从客户端使用数据库适配器方法(即查找和/或创建)时,我执行以下操作:
var accountsAPIService = app.service('/api/accounts');
accountsAPIService.find( function(error, accounts) {
...
});
如何从客户端调用自定义方法,例如 findAdminAccounts()?
客户端只能使用普通的服务接口。我们发现对自定义方法的支持(以及它带来的从明确定义的接口到任意方法名称和参数的所有问题)并不是真正必要的,因为一切本身都可以被描述为资源(服务)。
到目前为止,其好处(例如安全性、可预测性和发送明确定义的实时事件)远远超过了概念化应用程序逻辑时所需的思维上的微小改变。
在您的示例中,您可以创建一个包装服务来获取管理员帐户,如下所示:
class AdminAccounts {
find(params) {
const accountService = this.app.service('/api/accounts');
return accountService.find({ query: { adminProfile: { $exists: true } } });
}
setup(app) {
this.app = app;
}
}
app.use('/api/adminAccounts', new AdminAccounts());
或者你可以实现一个hook将查询参数映射到更大的查询,如下所示:
app.service('/api/accounts').hooks({
before: {
find(hook) {
if(hook.params.query.admin) {
hook.params.query.adminProfile = { $exists: true };
}
}
}
});
现在允许调用类似的东西/api/accounts?admin
.
欲了解更多信息,请参阅this FAQ.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)