Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Public custom service methods #2270

Merged
merged 3 commits into from
Mar 28, 2021
Merged

feat(core): Public custom service methods #2270

merged 3 commits into from
Mar 28, 2021

Conversation

daffl
Copy link
Member

@daffl daffl commented Mar 27, 2021

This pull request implements tests and client side functionality for custom service methods outlined in and closes #1976.

On the server a custom service method is added like this:

class MyMessageService {
  async create (data, params) {}

  async findOne (data, params) {}

  async resendNotification (data, params) {}
}

app.use('/messages', new MyMessageService(), {
  methods: [ 'create', 'findOne', 'resendNotification' ]
});

It can be used via an HTTP and socket API directly as described in #1976.

On the Feathers client it is used like this:

const feathers = require('@feathersjs/feathers');
const socketio = require('@feathersjs/socketio-client');
const io = require('socket.io-client');

const socket = io('http://api.feathersjs.com');
const app = feathers();

// Set up Socket.io client with the socket
app.configure(socketio(socket));

// Since there is no introspection, custom methods need to be initialized on a service
app.service('messages').methods('customMethod', 'findOne');

const message = await app.service('messages').findOne({ userId: 1 });

TypeScript has a CustomMethod and client interface:

import { feathers, CustomMethod } from '@feathersjs/feathers';
import socketio, { SocketClient } from '@feathersjs/socketio-client';
import io from 'socket.io-client';

type ServiceTypes = {
  messages: SocketClient & CustomMethod<'customMethod'|'findOne'>
}

const socket = io('http://api.feathersjs.com');
const app = feathers<ServiceTypes>();

// Set up Socket.io client with the socket
app.configure(socketio(socket));

// Since there is no introspection, custom methods need to be initialized on a service
app.service('messages').methods('customMethod', 'findOne');

const message = await app.service('messages').findOne({ userId: 1 });

@daffl daffl merged commit e65abfb into dove Mar 28, 2021
@daffl daffl deleted the custom-methods branch March 28, 2021 21:50
@bertho-zero
Copy link
Collaborator

Great ! The only thing that bothers me is that the arguments are not free.

The signature of a method must be [data, params] and I currently have custom methods that expect [id, params] or [id, data, params].

Maybe we can add flexibly if we give an array of argument to body? Which could be empty and to which we would concatenate params from the url.

@daffl
Copy link
Member Author

daffl commented Mar 31, 2021

Oh, I forgot to mention, that I added a middleware that should still allow to register any other kinds of custom methods:

const { createContext } = require('@feathersjs/feathers');
const { serviceMiddleware } = require('@feathersjs/express');

app.use('/myservice', myservice);

// With custom method handler
app.get('/myservice/:__feathersId/my-method', serviceMiddleware(async ({ req, res, options }) => {
  // options.id - __feathersId
  // options.params - Service params
  // options.data - body

  const { params, id } = options;

  return app.service('my-service').myCustomMethod(id, params);
}));

// With different params, using the body and returning the context (when using it with hooks)
app.post('/myservice/:myId/myMethod', serviceMiddleware(async ({ req, res, options }) => {
  const { myId } = req.params;
  const { params, data } = options;
  const context = createContext(service, method);

  return app.service('my-service').myCustomMethod(myId, data, params, context);
}));

It's not as declarative but I'm hoping it to be more flexible and explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom methods the Feathers way
2 participants