Skip to content

Generates TypeScript definition files(d.ts) for Egg

License

Notifications You must be signed in to change notification settings

lusyn/egg-ts-helper

 
 

Repository files navigation

egg-ts-helper

NPM version Build Status Appveyor status Coverage Status

A simple tool for generates typescript definition files(d.ts) for egg application. Injecting controller,proxy,service and extend to egg by Declaration Merging

Install

npm i egg-ts-helper -g

or

yarn global add egg-ts-helper

QuickStart

Open your egg application, executing the command

$ ets

-w flag can auto recreated d.ts while file changed

$ ets -w

Usage

$ ets -h

  Usage: ets [commands] [options]

  Options:

    -v, --version           output the version number
    -w, --watch             Watching files, d.ts would recreated while file changed
    -c, --cwd [path]        Egg application base dir (default: process.cwd)
    -C, --config [path]     Configuration file, The argument can be a file path to a valid JSON/JS configuration file.(default: {cwd}/tshelper.js
    -f, --framework [name]  Egg framework(default: egg)
    -s, --silent            Running without output
    -i, --ignore [dirs]     Ignore watchDirs, your can ignore multiple dirs with comma like: -i controller,service
    -e, --enabled [dirs]    Enable watchDirs, your can enable multiple dirs with comma like: -e proxy,other
    -E, --extra [json]      Extra config, the value should be json string
    -h, --help              output usage information

  Commands:

    clean                   Clean js file while it has the same name ts file

Options

name type default description
cwd string process.cwd egg application base dir
framework string egg egg framework
typings string {cwd}/typings typings dir
caseStyle string lower egg case style(lower,upper,camel)
watch boolean false watch file change or not
watchOptions object undefined chokidar options
execAtInit boolean false execute d.ts generation while instance was created
configFile string {cwd}/tshelper.js configure file path
watchDirs object generator configuration

egg-ts-helper would watching app/extend,app/controller,app/service, app/config, app/middleware, app/model by default. The dts would recreated when the files under these folders was changed.

you can disabled some folders by -i flag.

$ ets -i extend,controller

or configure in the config file

// {cwd}/tshelper.js

module.exports = {
  watchDirs: {
    extend: false,
    controller: false,
  }
}

or configure in package.json

// {cwd}/package.json

{
  "egg": {
    "framework": "egg",
    "tsHelper": {
      "watchDirs": {
        "extend": false
      }
    }
  }
}

Register

You can require register to start egg-ts-helper before starting egg application with egg-bin.

$ egg-bin dev -r egg-ts-helper/register

debugging

$ egg-bin debug -r egg-ts-helper/register

Demo

see https://github.com/whxaxes/egg-boilerplate-d-ts

It works in these directories : app/controller, app/service, app/proxy, app/extend, app/config, app/model, app/middleware.

Controller

(service, proxy are the same)

ts

// app/controller/home.ts
import { Controller } from 'egg';

export default class HomeController extends Controller {
  public async index() {
    this.ctx.body = 'ok';
  }
}

typings

// app/typings/app/controller/index.d.ts
import Home from '../../../app/controller/home';

declare module 'egg' {
  interface IController {
    home: Home;
  }
}

Extend

ts

// app/extend/context.ts
export default {
  doSomething() {
    console.info('do something');
  }
};

typings

// app/typings/app/controller/index.d.ts
import ExtendObject from '../../../app/extend/context';

declare module 'egg' {
  interface Context {
    doSomething: typeof ExtendObject.doSomething;
  }
}

Config

ts

// config/config.default.ts
export default function() {
  return {
    keys: '123456'
  }
}

typings

// app/typings/config/index.d.ts
import { EggAppConfig } from 'egg';
import ExportConfigDefault from '../../config/config.default';
type ConfigDefault = ReturnType<typeof ExportConfigDefault>;
type NewEggAppConfig = EggAppConfig & ConfigDefault;

declare module 'egg' {
  interface Application {
    config: NewEggAppConfig;
  }

  interface Controller {
    config: NewEggAppConfig;
  }

  interface Service {
    config: NewEggAppConfig;
  }
}

Middleware

ts

// app/middlware/uuid.ts

export default function() {
  return async (context, next) => {
    await next();
  };
}

typings

// typings/middleware/index.d.ts

import Uuid from '../../../app/middleware/uuid';

declare module 'larva' {
  interface IMiddleware {
    uuid: typeof Uuid;
  }
}

Plugin

ts

// config/plugin.ts
export default {
  cors: {
    enable: true,
    package: 'egg-cors',
  },
  static: {
    enable: true,
    package: 'egg-static',
  }
}

typings

// typings/config/plugin.d.ts

// it's only import the package was exist under the node_modules
import 'egg-cors';
import 'egg-static';

Model

Only support egg-sequelize now.

ts

// app/model/User.ts

import { Application } from 'egg';

export default (app: Application) => {
  const { STRING } = app.Sequelize;
  const User = app.model.define('user', {
    name: STRING(30),
  });

  return User;
};

typings

// typings/app/model/index.d.ts

import User from '../../../app/model/User';

declare module 'sequelize' {
  interface Sequelize {
    User: ReturnType<typeof User>;
  }
}

About

Generates TypeScript definition files(d.ts) for Egg

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.1%
  • JavaScript 3.9%