@cellularjs/express-proxy
v0.21.2
Published
CellularJS's built-in extension for ExpressJS
Downloads
11
Readme
@cellularjs/express-proxy🐘
1. Example
Example 1: create a proxy for JSON api.
// $share/express-proxy/index.ts
import { expressProxy, InputTransform, OutputTransform } from '@cellularjs/express-proxy';
const inputTransform: InputTransform = (req, proxyTo) => {
return new IRQ(
{ to: proxyTo },
{ ...req.query, ...req.params, ...req.body },
);
}
const outputTransform: OutputTransform = (expressCtx, cellularCtx) => {
const { res } = expressCtx;
const { irs } = cellularCtx;
res
.status(irs.header.status)
.json(irs.body);
}
export const proxyTo = expressProxy(
{ inputTransform, outputTransform },
localTransporter,
);
// iam/$gateway/http/index.ts
import { Router } from 'express';
import { proxyTo } from '$share/express-proxy';
export const iamRouter = Router();
iamRouter.post('/groups/add-user-to-group', proxyTo('IAM:AddUserToGroup'));
Example 2: override base proxy config for individual endpoint.
// iam/$gateway/http/index.ts
import { Router } from 'express';
import { proxyTo } from '$share/express-proxy';
export const iamRouter = Router();
iamRouter.get('/users', proxyTo('IAM:ListUsers', {
outputTransform: (expressCtx, cellularCtx) => {
const { res } = expressCtx;
const { irs } = cellularCtx;
res.send(`<pre>${JSON.stringify(irs.body)}</pre>`);
},
}));