Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including nest-ts-code-gen with all npm packages installed. Try it out:

var nestTsCodeGen = require("nest-ts-code-gen")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

nest-ts-code-gen v1.0.2

a code generator to generate nest template files and typescript definition files

what it is

nest-ts-gen is a code generator to generate back-end nest template code(controller, service, module) and front-end ts type, see in the demo demo/

requirement

  1. contract/: write back-end type and controller mapping configuration in the contract folder
  2. setup codeGen in your code: setup mapping, output folder and output file name
  3. mkdir all your output folder, make sure it exist

notification

  1. nest-ts-gen will replace controller output file and module output file, and as you know nest.service is not a template file, so the service file will be keep if exist, and only replace service method and related type :)

mapping config

write the key _config in the mapping config like this:

export default {
  _config: {
    // specify Controller promised common response type, it should be a generic type, so response type will be generated as CommonRes or CommonRes<SomeDto>
    promiseGenericRes: "CommonRes"
  },
  // controller, service, entity => we name it as model name
  model1: {
      // method name
      getStr: {
          // request dto type, you should define it in your contract folder first
          req: 'GetStrRequest'
          // response dto type, define before you write
          res: 'GetStrResponse'
      }
  },
  model2: {
      _config: {
          // stop to generate controller file
          disableController: true,
          // stop to generate entity (in service imports and module imports)
          disableEntity: true
      }
      getStr: {
          // if you don't write req and res, default req: any and res: _config.promiseGenericRes will be generated
      }
  }

and see props in the file src/type/global.ts

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free