pmu-tech / stub-server

Stub server for REST APIs
MIT License
12 stars 2 forks source link

Parse request body #13

Closed dsumac closed 3 years ago

dsumac commented 4 years ago

Purpose

In some of cases we would like parse the request body to make some logic. For example, if body contains some attribute/value I would like return stub A and if it contains another attribute/value I would like return B.

Actual behavior

With this configuration:

    '/test': {
      get: req => {
        console.log('req query', req.query);
        console.log('req body', req.body);
        return `${stubsPath}/test_200_OK.js`;
      }
    }

We have this result:

req query { myQueryParam: 'myValue' }
req body undefined

Desired behaviour

req query { myQueryParam: 'myValue' }
req body { bodyAttr: 'firstAttr' } 

We should consider to use body-parser:

tkrotoff commented 4 years ago

No problem to do that, I'm using it and it works fine:

import express from 'express';
import { stubServer } from '@pmu-tech/stub-server';

// ...

const app = express();
app.use(express.json());
stubServer(configPath, app);

// ...

or

// webpack.config.ts

import express from 'express';
import { stubServer } from '@pmu-tech/stub-server';

// ...

    devServer: {
      before: app => {
        app.use(express.json());
        const configPath = path.resolve(__dirname, 'stubs', 'config');
        stubServer(configPath, app);
      }
    },

// ...

Should it be enabled by stubServer? Not sure.

dsumac commented 3 years ago

Yes, finally I think it's not stub-server's responsability to do that