faker-proto
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

faker-proto

npm version npm license

Generate mock data from protobuf files.

Prepare Protobuf Files Repository

You should put your protobuf file under a git repository.

demo.proto is one of the protobuf files.

syntax = "proto3";
package coderge.demo;
 
service ReadmeService {
  rpc CopyText(CopyTextReq) returns (CopyTextResp);
}
 
message CopyTextReq {}
message CopyTextResp {}

Usage

Put a .fakerpbrc file at root dir of the project which would like to use mock data.

{
  "repository": "/path/to/your/git/repository",
  "branch": "master"
}

Create a mock server.

import { createServer } from 'faker-proto';
 
createServer({
  /*
    The param req is a Restify Request entity
    http://restify.com/docs/request-api/#request
  */
  getConfigHandler: req => {
    /*
      according to your api route
      should return packageName, serviceName and method according to the request entity
      eg. API Route Config /:package_name/:service_name/:method_name
          Request Path /coderge.demo/ReadmeService/CopyText
    */
    return {
      packageName: req.params['package_name'], // 'coderge.demo', 
      serviceName: req.params['service_name'], // 'ReadmeService',
      methodName: req.params['method_name'] // 'CopyText'
    };
  },
  /*
    The param data is result of mock.js
    https://github.com/nuysoft/Mock
  */
  responseHandler: (res, data) => {
    /*
      Can customize a response
    */
    // res.json(data);
    // res.send(JSON.stringify(data))
    res.json({ msg: 'ok', ret: 0, data });
  },
  /*
    The param data is result of mock.js
    https://github.com/nuysoft/Mock
  */
  /**
   * Hack mock rules of template
   * @param key protobuf message key
   * @param type protobuf message key type (eg. string/int32/bool/...)
   * @param random mock.js random entity
   */
  hackMockTpl: (key, type, random) => {
    key = key.toLowerCase();
    const keyTypeHas = (k: string, t: string) =>
      type === t && key.indexOf(k) > -1;
    if (keyTypeHas('name', 'string')) return '@name';
    else if (keyTypeHas('icon', 'string')) return () => random.image(100);
    return '';
  }
}).then(server => server.start());

Readme

Keywords

Package Sidebar

Install

npm i faker-proto

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

65.9 kB

Total Files

14

Last publish

Collaborators

  • coderge