FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(core): Public custom service methods by daffl · Pull Request #2270 · feathersjs/feathers · GitHub

feat(core): Public custom service methods - #2270

Merged
daffl merged 3 commits into
dovefrom
custom-methods
Mar 28, 2021
Merged

feat(core): Public custom service methods#2270
daffl merged 3 commits into
dovefrom
custom-methods

Conversation

daffl commented Mar 27, 2021
edited
Loading

Copy link
Copy Markdown
Member

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 merged commit e65abfb into dove Mar 28, 2021
daffl deleted the custom-methods branch March 28, 2021 21:50

Copy link
Copy Markdown
Contributor

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 commented Mar 31, 2021

Copy link
Copy Markdown
Member Author

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.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL