Feathers invoking a custom API method

I define my api with something like below:

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); } 

When I want to use the methods of the database adapter, from the client, i.e. find and / or create, I do the following:

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

How do I call custom methods like findAdminAccounts () from the client?

+8
javascript express feathersjs
source share
1 answer

You can only use the regular service interface on the client. We found that support for user methods (and all the problems that it brings with it, from a well-defined interface to arbitrary names and parameters) is not really needed, because everything in itself can be described as a resource (service).

The benefits (such as security, predictability, and the sending of well-defined real-time events) have so far far outweighed the small changes in thinking needed to conceptualize your application logic.

In your example, you can create a wrapper service that will receive administrator accounts as follows:

 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()); 

Alternatively, you can implement a hook that maps request parameters to larger requests, such as:

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

Now it will allow calling something like /api/accounts?admin .

See this FAQ for more information.

+8
source share

All Articles