4.2.8 • Published 1 year ago

fusion-plugin-rpc v4.2.8

Weekly downloads
672
License
MIT
Repository
github
Last release
1 year ago

fusion-plugin-rpc

Build status

Fetch data on the server and client with an RPC style interface.

If you're using React/Redux, you should use fusion-plugin-rpc-redux-react instead of this package.


Installation

yarn add fusion-plugin-rpc

Example

// src/main.js
import React from 'react';
import App, {createPlugin} from 'fusion-core';
import RPC, {RPCToken, RPCHandlersToken} from 'fusion-plugin-rpc';
import UniversalEvents, {UniversalEventsToken} from 'fusion-plugin-universal-events';
import {FetchToken} from 'fusion-tokens';
import fetch from 'unfetch';

// Define your rpc methods server side
const handlers = __NODE__ && {
  getUser: async (args, ctx) => {
    return {some: 'data' + args};
  },
};

export default () => {
  const app = new App(<div></div>);
  // ...
  app.register(RPCToken, RPC);
  app.register(UniversalEventsToken, UniversalEvents);
  __NODE__
    ? app.register(RPCHandlersToken, handlers);
    : app.register(FetchToken, fetch);

  app.middleware(
    { RPCFactory: RPCToken },
    ({RPCFactory}) => (ctx, next) => {
      RPCFactory(ctx).request('getUser', 1).then(console.log) // {some: 'data1'}
    }
  );
  // ...
  return app;
}

API

Dependency registration

import {RPCHandlersToken} from 'fusion-plugin-rpc';
import UniversalEvents, {UniversalEventsToken} from 'fusion-plugin-universal-events';
import {FetchToken} from 'fusion-tokens';

app.register(UniversalEventsToken, UniversalEvents);
__NODE__
  ? app.register(RPCHandlersToken, handlers);
  : app.register(FetchToken, fetch);
}
Required dependencies
NameTypeDescription
UniversalEventsTokenUniversalEventsAn event emitter plugin, such as the one provided by fusion-plugin-universal-events.
RPCHandlersTokenObject<(...args: any) => Promise>A map of server-side RPC method implementations. Server-only.
FetchToken(url: string, options: Object) => PromiseA fetch implementation. Browser-only.
Factory
const instance = RPC.from(ctx);

Instance methods

  • instance.request(method: string, args: any) - make an rpc call to the method handler with args.

If on the server, this will directly call the method handler with (args, ctx).

If on the browser, this will POST to /api/${method} endpoint with JSON serialized args as the request body. The server will then deserialize the args and call the rpc handler. The response will be serialized and send back to the browser.

Testing

The package also exports a mock rpc plugin which can be useful for testing. For example:

import {mock as MockRPC} from 'fusion-plugin-rpc';

app.register(RPCToken, mock);
4.2.8

1 year ago

4.2.5

1 year ago

4.2.7

1 year ago

4.2.6

1 year ago

4.2.3

1 year ago

4.2.2

1 year ago

4.2.4

1 year ago

4.2.1

1 year ago

4.2.0

1 year ago

4.1.3

1 year ago

4.1.2

2 years ago

4.0.9

2 years ago

4.1.0

2 years ago

4.1.1

2 years ago

4.0.7

2 years ago

4.0.6

2 years ago

4.0.8

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.1

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.0

2 years ago

3.8.0

3 years ago

3.7.5

3 years ago

3.7.4

3 years ago

3.7.3

3 years ago

3.7.2

3 years ago

3.7.1

3 years ago

3.7.0

3 years ago

3.6.2

3 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.5.3

4 years ago

3.5.2

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.0

4 years ago

3.3.4

4 years ago

3.3.3

4 years ago

3.3.2

4 years ago

3.3.1

5 years ago

3.3.0

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.4.1

5 years ago

2.4.1-0

5 years ago

2.4.0

5 years ago

2.4.0-0

5 years ago

2.3.1

5 years ago

2.3.1-0

5 years ago

2.3.0

5 years ago

2.3.0-0

5 years ago

2.2.0

5 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.3-1

6 years ago

1.0.3-0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.4

6 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago