Skip to content
Register and handle HTTP routes in a fusion app.
Branch: master
Clone or download
Latest commit a8cfef7 Feb 9, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
flow-typed
src
.codecov.yml
.eslintrc.js
.flowconfig
.gitignore
.npmrc
LICENSE
README.md
package.json
renovate.json
yarn.lock

README.md

fusion-plugin-http-router

Register and handle HTTP routes in a fusion app.


Table of contents


Installation

yarn add fusion-plugin-http-router

Setup

// src/main.js
import React from 'react';
import App from 'fusion-core';
import HTTPRouter, {
  HTTPRouterToken,
  HTTPHandlersToken,
  ResponseError,
} from 'fusion-plugin-http-router';

// Define your http routes and methods server side
const handlers = __NODE__ && {
  '/api/user/:id': {
    GET: async ({id}, ctx) => {
      return {some: 'data' + id};
    },
    PUT: async ({id, ...args}, ctx) => {
      return {some: 'data' + id};
    },
    DELETE: async (args, ctx) => {
      // Error Handling Example
      try {
        deleteUser();
      } catch (e) {
        const error = new ResponseError('Failed to delete user');
        error.code = 'DELETEUSER';
        error.meta = {
          custom: 'metadata',
        };
        throw error;
      }
    },
  },
  '/api/book': {...}
};

export default () => {
  const app = new App(<div />);

  app.register(HTTPRouterToken, HTTPRouter);
  __NODE__ && app.register(HTTPHandlersToken, handlers)

  return app;
};

API

Registration API

HTTPRouter
import HTTPRouter from 'fusion-plugin-http-router';

The HTTPRouter plugin. Registers HTTP routes and handlers.

HTTPRouterToken
import {HTTPRouterToken} from 'fusion-plugin-http-router';

The canonical token for the HTTPRouter plugin. Typically, it should be registered with the HTTPRouter plugin.

Dependencies

HTTPHandlersToken
import {HTTPHandlersToken} from 'fusion-plugin-http-router';

Configures what HTTP Router handlers exist. Required. Server-only.

Types
type HTTPHandlers = Object<Object<string, () => any>>

You can register a value of type HTTPHandlers or a Plugin that provides a value of type HTTPHandlers.


Error Handling

Use the ResponseError error subclass for sending error responses. If this error class is not used, a generic message will be sent to the client.

import {ResponseError} from 'fusion-plugin-http-router';

function testHandler() {
  try {
    doThing();
  } catch (e) {
    const error = new ResponseError('Failed to do thing');
    error.code = 'DOTHING';
    error.meta = {
      custom: 'metadata',
    };
    throw error;
  }
}
You can’t perform that action at this time.