中文 English

How to add MD5 verification and project dependencies in Swagger-JS

Published: 2021-02-06
swagger md5 js

swagger-bundle packaging

First enter the swagger-js directory

npm install
npm run build

How to modify

Add code to the src/execute.js file:

Add code before the export function buildRequest({ method class mergeInQueryOrForm(req) statement:

package.json needs to add support for cross-platform compilation

Screenshot Screenshot

import md5 from 'md5';
  //calcSn for server
  var req = request
  console.log(req.body);
  if (req.body) {
    let bodyObj = JSON.parse(req.body);
    bodyObj['header']['token'] = req['headers']['token'];
    bodyObj['header']['snTime'] = (new Date()).valueOf();

    let allData = Object.assign(JSON.parse(JSON.stringify(bodyObj['body'])), {
      token: bodyObj['header']['token'],
      snTime: bodyObj['header']['snTime']
    });
    bodyObj['header']['sn'] = calcSn(allData);
    req.body = JSON.stringify(bodyObj);
    req.headers['X-Sn-Verify'] = md5(req.body);
  }
  console.log(req.body);

  function calcSn(allData) {
    let queryString = "";
    Object.keys(allData)
      .sort()
      .forEach(function (key, i) {
        let value = allData[key];
        queryString += key + "=" + value + "&";
      });
    if (queryString.length > 0) {
      queryString = queryString.substring(0, queryString.length - 1)
    }
    return md5(queryString);
  }

package.json需要增加跨平台编译的支持