-
-
Notifications
You must be signed in to change notification settings - Fork 797
Description
Custom methods are one of the most commonly requested features for Feathers. As the FAQ points out, it is possible to handle 90% of all use cases with the normal CRUD service interface and hooks for workflows. It does however still leave some edge cases that would be good if a service could expose additional methods externally. One reason for this limitation was that it is difficult to secure arbitrary methods properly. The new https://github.com/feathersjs/hooks general purpose hooks make this much easier.
This is why starting at v5, in addition to the existing service methods, it will be possible to register your own custom methods. A custom method has a name and fixed parameters of (data, params) where data is the payload and params is the usual service method parameters (including things like provider, authenticated user, query etc.). Method information can be passed as options to app.use (app.use(path, service, options)). This will also allow to more easily disable existing service methods (or their events) externally.
Example
class MyMessageService {
async create (data, params) {}
async findOne (data, params) {}
async resendNotification (data, params) {}
}
app.use('/messages', new MyMessageService(), {
methods: [ 'create', 'findOne', 'resendNotification' ],
events: [ 'my-custom-event' ]
})The available options are:
methods: A list of all publicly exposed methods. Will default to the standard service methods. When passed those will have to be added explicitly.events: The custom service events (previous inservice.events)
Calling via transports
- Via REST a custom method can be called using the POST method and the
X-Service-Methodheader.datawill be the body payload. - Via websockets the method will be available as
socket.emit(methodName, 'messages', { username: 'dave' }, query) - Feathers does not have an introspection mechanism, so when using the Feathers client, custom methods will have to be defined again explicitly on the client
- In GraphQL the method will be exposed as a mutation under a unique name