Skip to content

Latest commit

 

History

History
130 lines (100 loc) · 2.88 KB

README.md

File metadata and controls

130 lines (100 loc) · 2.88 KB

express-body-parser-xml

The pkg is a extension relative to the body-parser library. you can use it to parser xml data and convert into JavaScript Object, mounting on req.body.

The config options is highly opened:

  • you can use your read to extract raw body from request stream if you think the default is not satisfied with your case.
  • you can custom parser xml, just to set the parser into options, passing in middleware.

Installation

$ npm install express-body-parser-xml

Usage

for js

const { XMLParseMiddleware } = require('express-body-parser-xml');
app.use(XMLParseMiddleware());

for ts

import { XMLParseMiddleware } from 'express-body-parser-xml';
app.use(XMLParseMiddleware());

Options

  • types
    • an array of types to match incoming request content-type.
  • limit
    • only parser content-length less than limit body.
  • charset
    • ascii
    • utf8
    • utf-8
    • utf16le
    • ucs2
    • ucs-2
    • base64
    • base64url
    • latin1
    • binary
    • hex
  • parser
    • parser text to js object, the default is xml2js.
  • defaultParserOpts
    • parserOpts for xml2js parser
  • read
    • extract data from request stream.

Default Options

{
  types: ['*/xml', '+xml'],
  limit: '1mb',
  charset: 'utf8',
  parser: defaultParser,
  defaultParserOpts: { async: true, explicitArray: false },
  read: defaultRead,
}

Custom Parser type

type IParser = (xmlRaw: string) => Promise<Record<string, any>>;

Custom Read

It will be considered to be right, when only the readRes.status === 200.

interface IReadRes {
  status: number;
  msg?: string;
  rawBody: string;
}
type IRead = (req: Request, opts: IXmlOptions) => Promise<IReadRes>;

Example

for js

const express = require('express');
const { XMLParseMiddleware } = require('express-body-parser-xml');
const app = express();
app.use(XMLParseMiddleware({ limit: '2mb' }));
app.post('/test', async (req, res) => {
  res.end(JSON.stringify(req.body));
});

for ts

import * as express from 'express';
import { XMLParseMiddleware } from 'express-body-parser-xml';
const app = express();
app.use(
  XMLParseMiddleware({
    types: ['text/xml'],
    limit: '2mb',
  }),
);
app.post('/test', async (req: express.Request, res: express.Response) => {
  res.end(JSON.stringify(req.body));
});

Motivation

The chance to build this pkg is that i failed to find one pkg to use easily that parser xml as middleware, when i develop my wechat account service.

After refer to relevant packages, such as body-parser-xml, body-parser, xml2js, type-is and so on. i write this pkg by ts.

In brief, thanks a lot, owing to the above pkgs, this pkg can born.

License

MIT