@flatjs/mock

the mock services of @flatjs

Usage no npm install needed!

<script type="module">
  import flatjsMock from 'https://cdn.skypack.dev/@flatjs/mock';
</script>

README

@flatjs/mock

The simple server to provider quick mock data for flatjs applications

The usages

$ npm i @flatjs/mock
// Install the following devDependencies.
"@types/express": "^4.17.7",
"@types/express-mung": "^0.5.2",
"@types/http-proxy-middleware": "^0.19.3",
"@types/mockjs": "^1.0.3",

The configuration of the flatjs.mock.js as below example

baseCwd: join(process.cwd(), `packages/mock/mocks`),
apiContext: '/api',
hostname: 'dev.flatjs.com',
port: 4000,
staticMap: {
  '/static': 'static',
},
mockMap: {
  '/func': { type: 'FUNC', defs: ['func'], middlewares: {res: otherFuncMiddleware.forFuncApiSimpleResponse()] } },
  '/rest': { type: 'REST', defs: ['rest']},
},
// Tor expressjs middleware cycle
// The outer middlewares defined by user should be executed in the final phase

// e.g. Below middleware will used to simplify response for `func`
otherRestMiddleware.forFuncApiSimpleResponse();

Features

  • Both support .ts ,.js mock services.
  • Provider class MockBase as base class for all mock services
  • Built in support mockjs
  • Proiders usually module exports.

Examples

  • The first scenario, auto instance class need @Mockable() for class
@Mockable()
export default class MockProductService extends MockBase {
  @MockAlias('products')
  geProducts(_req: MockRequest, res: MockResponse): void {
    res.json({
      code: '0000',
      message: 'func product',
      data: ['https://www.flatjs.com/blog/img/flatjs_avatar.png'],
    });
  }
}
  • The second scenario, auto instance class need @Mockable() for class
@Mockable()
export class MockCatalogService extends MockBase {
  @MockAlias('catalog/list')
  getCatalogs(_req: MockRequest, res: MockResponse): void {
    res.json({
      code: '0000',
      message: 'func catalogs',
      data: ['https://www.flatjs.com/blog/img/flatjs_avatar.png'],
    });
  }
}
  • The third scenario, use module.exports = new () the class decortor @Mockable is optional
class MockUserService extends MockBase {
  @MockAlias('product/list')
  getUsers(_req: MockRequest, res: MockResponse): void {
    res.json({
      data: ['https://www.flatjs.com/blog/img/flatjs_avatar.png'],
    });
  }
}
module.exports = new MockUserService();
  • The forth scenario
export function getAds(_req: MockRequest, res: MockResponse): void {
  res.json({
    code: '0000',
    message: 'func ads',
    data: ['https://www.flatjs.com/blog/img/flatjs_avatar.png'],
  });
}

Use Http Proxy proxyMap

 ....
  proxyMap: {
    '/proxy': {
      target: 'https://fex.qa.tcshuke.com',
      pathRewrite: { '^/proxy': '' },
      router(req) {
        switch (req.query.env) {
          case 'me':
            return 'https://dev.flatjs.com:4000';
          case 'uat':
            return `https://fex.qa.tcshuke.com`;
        }
        return 'https://fex.qa.tcshuke.com';
      },
    },
  },
  // request `/proxy/mock/api/status/200?env=uat` ---> `https://fex.qa.tcshuke.com/mock/api/status/200?env=uat`

Notes

Note, it we want to using import or require with cache First we can't include this folder into mockMap of flatjs.mock.js Second we can't use importFresh(``) @example

mockMap: {
  '/func': { type: 'FUNC', defs: ['func'], middlewares: {} },
  '/rest': { type: 'REST', defs: ['rest'], middlewares: {} },
},
the `defs` config should not include any `folders` that we want to cache.