1 min read

NestJS Proxy

NestJS ProxyModule helps to define and configure proxied routes to deal with Poq Platform services.

Supported version of NestJS:

  • @nestjs/common >= 8.x.x
  • @nestjs/core >= 8.x.x
  • @nestjs/platform-express >= 8.x.x

Installation

npm install @poq/nestjs-proxy

Usage

import { Module, OnModuleInit } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { HttpVerb, ProxyModule } from '@poq/nestjs-proxy';
@Module({
imports: [
ProxyModule.forRoot({
target: 'http://postman-echo.com',
routes: [
{
// Original path is used.
// Defines proxy for all methods: post/put/delete/options/get ...
path: '/proxy_path_one'
},
{
// Defines proxy for all methods: post/put/delete/options/get ...
path: '/proxy_path_two',
target: '/custom_target'
},
{
path: '/proxy_path_two',
target: '/custom_target',
verb: [HttpVerb.Get, HttpVerb.Post]
}
]
})
],
controllers: [],
providers: []
})
export class AppModule implements OnModuleInit {}

Example of the configuration for platform endpoints.

import { Module, OnModuleInit } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { HttpVerb, ProxyModule, ProxyService } from '@poq/nestjs-proxy';
@Module({
imports: [
ProxyModule.forRoot({
target: 'https://platform.poq.io',
routes: [
{ verb: HttpVerb.Get, path: '/appstories/apps/:appid/home' },
{ verb: HttpVerb.Get, path: '/banners/:appid' },
{ verb: [HttpVerb.Get, HttpVerb.Post], path: '/cart' },
{ verb: HttpVerb.Post, path: '/cart/items' },
{ verb: HttpVerb.Post, path: '/checkout/complete' },
{ verb: HttpVerb.Get, path: '/contentblocks/:appid/:categoryid' },
{ verb: HttpVerb.Get, path: '/lookbooks/:appid/:lookbookid' },
{ verb: HttpVerb.Get, path: '/shop' },
{ verb: HttpVerb.Get, path: '/pages/:appid/:pageid' },
{ verb: HttpVerb.Get, path: '/products' },
{ verb: HttpVerb.Get, path: '/search' },
{ verb: HttpVerb.Get, path: '/search/predictive' },
{ verb: HttpVerb.Get, path: '/settings/config/:appid/:settingid' },
{ verb: HttpVerb.Get, path: '/splash/ios/:appid:/:v' },
{ verb: HttpVerb.Get, path: '/stores/:appid' },
{ verb: HttpVerb.Get, path: '/stores/:appid/:storeid' },
{
verb: [HttpVerb.Get, HttpVerb.Post, HttpVerb.Delete],
path: '/wishlist'
},
{ verb: HttpVerb.Post, path: '/wishlist/items' },
{ verb: HttpVerb.Delete, path: '/wishlist/items/:listingid' }
]
})
],
controllers: [],
providers: []
})
export class AppModule implements OnModuleInit {}

Caveats

Some applications based on Express adapter have to use body parser middleware to transform raw body binary data into javascript object. For request methods that can contain body parameter proxy will fail when body is not in expected format. To fix this problem fixRequestBody helper is introduced.

import { Module, OnModuleInit } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { HttpVerb, ProxyModule, fixRequestBody } from '@poq/nestjs-proxy';
@Module({
imports: [
ProxyModule.forRoot({
target: 'http://postman-echo.com',
events: {
/**
* This line helps to convert body into string representation from object.
*/
onProxyReq: fixRequestBody
},
routes: [
{
path: '/proxy_path_two',
target: '/custom_target',
verb: [HttpVerb.Get, HttpVerb.Post]
}
]
})
],
controllers: [],
providers: []
})
export class AppModule implements OnModuleInit {}