Git Product home page Git Product logo

ryuu-proxy's Introduction

ryuu-proxy

Simple middleware to add to a local development server while developing Domo Apps. The middleware will intercept any calls to /data/v1 or /domo/v1, proxy an authenticated request to the Domo App service, and pipe the response back so that you can develop your Domo App locally and still get request data from Domo.

Installation

npm install @domoinc/ryuu-proxy --save-dev

Usage

This library leverages the last login session from your Domo CLI. If that session is no longer active or doesn't exist then the proxy won't work. Be sure that you've logged in before you start working:

$ domo login

This library comes with a simple wrapper for Express/Connect middleware.

const express = require('express');
const { Proxy } = require('@domoinc/ryuu-proxy');

const app = express();

const manifest = require('./path/to/app/manifest.json');
const proxy = new Proxy(manifest);

app.use(proxy.express());

Build Your Own

For other frameworks, the library exposes the necessary functions to create a stream to pipe back to your server. You'll need to handle this stream as your server would expect. The only thing that stream() expects is a standard Node Request which most server frameworks extend in some way.

// koa
app.use(async (ctx, next) => {
  await proxy
    .stream(ctx.req)
    .then(data => ctx.body = ctx.req.pipe(data))
    .catch(next);
});
// express
app.use((req, res, next) => {
  proxy
    .stream(req)
    .then(stream => stream.pipe(res))
    .catch(() => next());
});
// node http
const server = http.createServer((req, res) => {
  if (req.url === '/') {
    loadHomePage(res);
  } else {
    proxy
      .stream(req)
      .then(stream => stream.pipe(res));
  }
});

Error Handling

Ignoring the errors will cause the proxy to fail silently and the proxy request will return a 404 error. If you'd like a little more detail on the errors you can expose them in the response:

// koa
app.use(async (ctx, next) => {
  await proxy
    .stream(ctx.req)
    .then(data => ctx.body = ctx.req.pipe(data))
    .catch((err) => {
      if (err.name === 'DomoException') {
        ctx.status = err.status || err.statusCode || 500;
        ctx.body = err;
      } else {
        next();
      }
    });
});
// express / connect
app.use((req, res, next) => {
  proxy
    .stream(req)
    .then(stream => stream.pipe(res))
    .catch(err => {
      if (err.name === 'DomoException') {
        res.status(err.status || err.statusCode || 500).json(err);
      } else {
        next();
      }
    });
});
// http
const server = http.createServer((req, res) => {
  if (req.url === '/') {
    loadHomePage();
  } else {
    proxy
      .stream(req)
      .then(stream => stream.pipe(res))
      .catch(err => {
        if (err.name === 'DomoException') {
          res.writeHead(err.status || err.statusCode || 500);
          res.end(JSON.stringify(err));
        }
      });
  }
});

ryuu-proxy's People

Contributors

bmbarker90 avatar walexnelson avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.