【问题标题】:Hiding _all_ remote methods in loopback model在环回模型中隐藏_all_远程方法
【发布时间】:2017-06-13 09:22:48
【问题描述】:

我正在使用环回 3。 我的模型的 js (survey.js) 中有这行代码:

let enabledRemoteMethods = []

Survey.sharedClass.methods().forEach(function(method) {
  console.log(method.name, method.isStatic)
  let matchingEnabledRemoteMethod = _.find(enabledRemoteMethods, {name: method.name});

  if (!matchingEnabledRemoteMethod) {
    Survey.disableRemoteMethodByName(method.name, method.isStatic);
  }
});

它工作....几乎。我仍然可以在资源管理器中看到“PATCH /surveys/{id}”的 REST 端点。我的期望是:资源管理器中不应列出任何 REST 端点。

然后我查看了那个操作对应的URL,是:

http://localhost:3000/explorer/#!/Survey/Survey_prototype_patchAttributes

根据文档,这意味着 patchAttributes 是一个静态方法。

然后我与控制台中的输出进行了交叉检查......它说:pathAttributes not 是静态的。

不一致!

我什至尝试过添加这一行:

Survey.disableRemoteMethodByName("patchAttributes", true);

还有

Survey.disableRemoteMethodByName("patchAttributes", false);

运气不好。

有人可以确认这是否是环回 3 中的错误(我不知道环回 2,尚未检查)?如果这是一个错误,我就不必花时间在它上面,只需等到它得到修复即可。但是,如果它不是错误,有人可以指出我的代码中缺少什么吗?

谢谢!


更新:弄清楚如何

有了这条线,我就可以摆脱它了:

Survey.disableRemoteMethodByName("prototype.patchAttributes", true);

第二个参数似乎无关紧要(您也可以设置为 false)。不知道为什么(我想它应该只接受真实的)。

这是我目前的解决方案:

let disabledPrototypesRemoteMethods = ['patchAttributes']
let enabledRemoteMethods = [
  "create", "findById", "replaceById", "deleteById",
  "replaceOrCreateQuestion"
]
Survey.sharedClass.methods().forEach(function(method) {
  if (enabledRemoteMethods.indexOf(method.name) == -1) {
    Survey.disableRemoteMethodByName(method.name);
  }

  if (disabledPrototypesRemoteMethods.indexOf(method.name) > -1) {
    Survey.disableRemoteMethodByName("prototype." + method.name);
  }
});

不过,一个小细节:这件事仍然出现(我想它为 replaceById 操作的正常 PUT 提供了 POST 替代方法......,但我不想要它;我想强制用户使用我的 API仅与 PUT 一起使用):

http://localhost:3000/explorer/#!/Survey/Survey_replaceById_post_surveys_id_replace

我尝试添加这一行:

Survey.disableRemoteMethodByName("replaceById_post_surveys_id_replace");

运气不好。

无论如何...希望这对其他人有用;环回文档有点粗略。


【问题讨论】:

    标签: node.js rest loopback


    【解决方案1】:

    您也可以通过查看stringName 属性来获取原型方法。这样您就可以将原型包含在您的列表中。

    stringName 在值中包含 sharedClass 名称,因此您需要将其解析出来。

    module.exports = BusinessProfileContacted => {
      const enabledRemoteMethods = ["create", "findById", "replaceById", "deleteById", "replaceOrCreateQuestion", "prototype.replaceAttributes"];
    
      Survey.sharedClass.methods().forEach(method => {
        const methodName = method.stringName.replace(/.*?(?=\.)/, '').substr(1);
        if (enabledRemoteMethods.indexOf(methodName) === -1) {
          Survey.disableRemoteMethodByName(methodName);
        }
      });
    };
    

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 1970-01-01
      • 2010-11-09
      • 2018-05-04
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多