npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-router-forward-request

v1.1.3

Published

Forward routes to a seperate API and modify the body along the way

Downloads

8

Readme

koa-router-forward-request

Forward single routes to a different API and modify the content on the way.

  • intended for use with koa-router
  • forwards a request to another route or seperate API
  • allows to modify both the request body, as well as the response body
  • takes a configuration object
  • returns a generator function to be passed router.METHOD()

Usage

All example use a POST request, but you can specify any HTTP method you want.

Basic Usage - A simple proxy example

The simplest example possible is to forward a request and specify which headers should be forwarded. While this example offers no additional benefit over existing koa proxy packages, it shows how to use the createForward method:

const app = require('koa')();
const router = require('koa-router')();
const createForward = require('koa-router-forward-request');

router.post('/foo', createForward({
  request: {
    url: 'http://other-api.com/foo',
    method: 'post',
    forwardHeaders: ['authorization'],
  }
}));
  
app
.use(router.routes())
.use(router.allowedMethods());

It will forward any POST request made to our server on the /foo route to the other-api.com. Only the authorization header is passed along, other headers are ignored.

Adding a compose method to modify the request body

Let's say you're expecting a string and want to uppercase it.

const composeRequest = body => body.toUpperCase();

router.post('/foo', createForward({
  request: {
    url: 'http://other-api.com/foo',
    method: 'post',
    forwardHeaders: ['authorization'],
    composeBody: composeRequest
  }
}));

Our body will now be uppercased before it is forwarded to the foreign API. The response is still unchanged.

Adding a compose method to modfiy the response

Now we also want to modify the response. Let's say you are expecting an array and want to uppercase each item. But what if the request fails? You don't have an array to map over then. So let's only have our compose function run on a specific status:

const composeRequest = body => body.toUpperCase();
const composeResponse = list => list.map( item => item.toUpperCase() );

router.post('/foo', createForward({
  request: {
    url: 'http://other-api.com/foo',
    method: 'post',
    forwardHeaders: ['authorization'],
    composeBody: composeRequest
  },
  response: {
    successOnStatus: 201,
    composeBody: composeResponse
  }
}));

Making additional calls

If you need more data to compose your result, you can also make additional requests from your compose function. Just create a generator function instead of a regular function:

const request = require('request-promise');

const composeResponse = function* (body) {
  const data1 = yield request('http://other-api.com/tomatoes')
  const data2 = yield request('http://other-api.com/potatoes')
  
  return doSomethingWithAllTheData(body, data1, data2);
}

router.post('/foo', createForward({
  request: {
    url: 'http://other-api.com/foo',
    method: 'post',
    forwardHeaders: ['authorization'],
  },
  response: {
    successOnStatus: 201,
    composeBody: composeResponse
  }
}));