@opi_pib/node-utility
v2.0.0
Published
Node utility
Downloads
63
Readme
Node Utility
Install
npm install @opi_pib/node-utility
Notify
Simple wrapper around chalk
Usage
Notify.success({ message: "message" });
Notify.warning({ message: "message" });
Notify.info({ message: "message" });
Notify.error({ message: "message", error: new Error("Some error") });
HttpProxyMiddleware
Proxy middleware for express server
Usage
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
const proxyOptions: ProxyOptions = {
target: "http://127.0.0.1:4010",
changeOrigin: true,
onProxyReq(proxyReq, req, res, options) {
HttpProxyMiddleware.writeParsedBody(proxyReq, req);
},
onProxyRes(proxyRes, req, res) {},
};
app.use("/", HttpProxyMiddleware.create(proxyOptions));
app.listen(4011);
HttpAdapters
HttpAdapters.replaceResponseBody()
Replace response with new value of body
Usage
processRes(
proxyRes: IncomingMessage,
req: ProxyRequest,
res: ServerResponse,
): void {
if (req._parsedUrl?.pathname.match('^/user$') && req.method === 'GET') {
HttpAdapters.replaceResponseBody(userDto, proxyRes, res);
}
}
HttpAdapters.replaceResponseBodyFromJsonFile()
Replace response with new value of body from json file
Usage
processRes(
proxyRes: IncomingMessage,
req: ProxyRequest,
res: ServerResponse,
): void {
if (req._parsedUrl?.pathname.match('^/user$') && req.method === 'GET') {
HttpAdapters.replaceResponseBodyFromJsonFile('./userDto.json', proxyRes, res);
}
}
HttpAdapters.replaceResponseBodyFromFile()
Replace response with content of file
Usage
processRes(
proxyRes: IncomingMessage,
req: ProxyRequest,
res: ServerResponse,
): void {
if (req._parsedUrl?.pathname.match('^/user$') && req.method === 'GET') {
HttpAdapters.replaceResponseBodyFromFile('./user.pdf', proxyRes, res);
}
}
HttpAdapters.toQueryString()
Convert object into queryString
Usage
const result = HttpAdapters.toQueryString({
a: "b",
c: 1,
d: undefined,
e: null,
});
expect("a=b&c=1").toEqual(result);
HttpAdapters.stringify()
Stringify value
Usage
const result = HttpAdapters.stringify({
a: "b",
c: 1,
d: undefined,
e: null,
});
expect('{"a":"b","c":1}').toEqual(result);