使用NodeJS RestAPI的VSCode智能感知和代码导航

时间:2019-01-23 02:20:05

标签: node.js rest visual-studio-code intellisense

正如标题所述-我无法弄清楚如何使代码导航和Intellisense在有点复杂的代码结构中工作。我完全愿意投入必要的文档工作来完成此工作(例如添加了@typedefs等)

我有此代码结构-

{
  "@timestamp": "2019-01-22T17:15:18.431798-0500",
  "beat": {
    "hostname": "PC-TEST",
    "version": "1.0.0",
    "name": "PC-TEST"
  },
  "PrinterDevice": {
    "DeviceName": "Impresora-Test",
    "ErrorState": {
      "code": 4,
      "message": "No Paper"
    }
  }
}

(灵感自https://auth0.com/blog/developing-well-organized-apis-with-nodejs-joi-and-mongo/

所以它是如何工作的-有一个用于引用模块和库的中央库。

servicelocator.js-维护所有已注册服务的地图。

/project
    /serviceA
        - server.js
        - node_modules
        + /src
            + /configs
                - dependencyInjector.js
                ...
            + /controllers
            + /models
            + /internalservices
            + /lib
                - servicelocator.js
            /mmiddlewares
            ...

Servicelocator用于依赖注入 示例-DI.js

ServiceLocator.prototype.register = function (dependencyName, constructor) {
  if (typeof constructor !== 'function') {
    throw new Error(dependencyName + ': Dependency constructor is not a function');
  }

  if (!dependencyName) {
    throw new Error('Invalid depdendency name provided');
  }

  this.dependencyMap[dependencyName] = constructor;
};

ServiceLocator.prototype.get = function (dependencyName) {
  if (this.dependencyMap[dependencyName] === undefined) {
    throw new Error(dependencyName + ': Attempting to retrieve unknown dependency');
  }

  if (typeof this.dependencyMap[dependencyName] !== 'function') {
    throw new Error(dependencyName + ': Dependency constructor is not a function');
  }

  if (this.dependencyCache[dependencyName] === undefined) {
    const dependencyConstructor = this.dependencyMap[dependencyName];
    const dependency = dependencyConstructor(this);
    if (dependency) {
      this.dependencyCache[dependencyName] = dependency;
    }
  }

  return this.dependencyCache[dependencyName];
};

然后,控制器的代码是

之类的标准类
serviceLocator.register('mycustomservice', () => {
    return require('../lib/mycustomservice');
});
...
...
...

serviceLocator.register('abcController', (serviceLocator) => {
    const mycustomservice = serviceLocator.get('mycustomservice');

    const abcController = require('../controllers/abcController');
    return new abcController(mycustomservice)
})

这就是我的挑战所在。我如何在这里使用自动完成功能? (mycustomservice.js是module.exports =)

我的约束:

  1. 我不能使用TypeScript /咖啡等。
  2. 结构和流量是固定的(无法更改)
  3. 如果有出路(如果没有的话),我也希望使用VSCode。

非常感谢!


[更新] 在JSDoc样式注释中使用Typedefs

class abcController 
{
    constructor(mycustomservice)
    {
        this.customService = mycustomservice;
        ....
        ....
    }
}

这对我来说很合理。试图弄清楚如何使其自动化或创建一个中央文件(尚未真正熟悉JSDocs或Typescript...。)

0 个答案:

没有答案