Express Gateway:使用 Request-Transformer 策略添加参数,但在条件下

Express Gateway: add params with Request-Transformer policy, but under condition

在我的 Node 系统中,我有一个服务器公开了一些 API,可通过 Express Gateway 访问。根据使用我的 API.

的应用程序,我想在 req.body 中强制添加一些参数

为此,我使用了个性化的 scope 值。例如,我使用我的 API 拥有 App1 和 App2(及其凭据)。因此,在 eg credentials 中,我将范围 app1 添加到 App1 凭证,并将 app2 添加到 App2 凭证的范围。

gateway.config.yml 文件中我写:

   ...
      - request-transformer:   
        - condition:
          name: allOf
          conditions:
              -
                name: regexpmatch
                match: ^/user/signup?(.*)$                 
              -
                name: expression
                expression: "apiEndpoint.scopes.indexOf('app1')>=0"
        - action:
          body:
            add:
              custom_field: "'app1'"   
        - condition:
          name: allOf
          conditions:
              -
                name: regexpmatch
                match: ^/user/signup?(.*)$                 
              -
                name: expression
                expression: "apiEndpoint.scopes.indexOf('app2')>=0"
        - action:
          body:
            add:
              custom_field: "'app2'"   

但是我得到这个错误:

     error: Policy request-transformer params validation failed: 
data should have required property '.headers', 
data should have required property '.body', 
data should match some schema in anyOf
    (node:17362) UnhandledPromiseRejectionWarning: Error: POLICY_PARAMS_VALIDATION_FAILED

============

其实看了JSON的改造,action并不是request-transformer的属性。缩进错误。

您应该注意 yaml 缩进,您发布的缩进是错误的,因此 Express Gateway 试图消化的最终对象是不正确的。

我建议你使用 Yaml2Json Converter,它肯定会帮助你看到最终的结构并帮助你调试问题。

干杯!

V.