【问题标题】:Add Request body Schema in loopback4在 loopback4 中添加请求主体架构
【发布时间】:2020-05-29 22:17:16
【问题描述】:

我是 loopback 4 的新手,我真的很难处理一些时间不是最新的文档。我成功添加了身份验证系统和登录用户的路线。我的问题出在“/explorer”网址上,我不知道如何在自定义路由的请求正文架构上添加示例值。

有我的路线:

@post('/users/login', {
    responses: {
      '200': {
        description: 'Token',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                token: {
                  type: 'string'
                }
              }
            }
          }
        }
      }
    }
  })
  async login(
    @requestBody() credentials: Credentials,
  ): Promise<{token: string}> {
    // make sure user exist,password should be valid
    const user = await this.userService.verifyCredentials(credentials);
    // console.log(user);
    const userProfile = await this.userService.convertToUserProfile(user);
    // console.log(userProfile);

    const token = await this.jwtService.generateToken(userProfile);
    return Promise.resolve({token: token})
  }

我想补充一点:

{
   "username": "string",
   "password": "string"
}

这里:

我想有一种简单的方法可以做到这一点,但我真的找不到任何相关信息。

【问题讨论】:

    标签: routes openapi strongloop loopback4


    【解决方案1】:

    仅供参考:loopback4 使用路由装饰器,它提供 OpenAPI 规范来描述端点。有关于OpenAPI decorator in loopbac4 here的详细信息。
    现在来解决上述问题。让我们创建:

    1. 用户登录模式,即 {"username":string, "password":string} 在架构定义中,您还可以添加验证规则。

      const  UserLoginSchema = {
      type: 'object',
      required: ['email', 'password'],
      properties: {
        username: {
          type: 'string',
        },
        password: {
          type: 'string',
        },
      },
      }; ```
      
    2. 现在让我们快速创建用于登录的 RequestBody c'tor。请记住,根据 OpenApi 规范,请求正文将包含描述、要求和内容。

     export const UserLoginRequestBody = {
          description: 'Required input for login',
          required: true,
          content: {
            'application/json': {schema: UserLoginSchema},
          },
        };
    
    1. 现在您可以使用请求正文了。
      async login(
        @requestBody(UserLoginRequestBody) credentials: Credentials,
      ): Promise<{token: string}> {
    ..restCode
    

    这样就搞定了。

    【讨论】:

      【解决方案2】:

      谢谢@Madaky。阅读您的答案后,我只是直接在我的路线中直接添加了不同的对象,而不是制作额外的文件(我不知道这是否是一个好习惯)但它有效。

      在“@requestBody”函数中添加了信息的最终代码:

      
        @post('/users/login', {
          responses: {
            '200': {
              description: 'Token',
              content: {
                'application/json': {
                  schema: {
                    type: 'object',
                    properties: {
                      token: {
                        type: 'string'
                      }
                    }
                  }
                }
              }
            }
          }
        })
        async login(
          //here above inside @requestBody
          @requestBody(
            {
              description: 'Required input for login',
              required: true,
              content: {
                'application/json': {
                  schema: {
                    type: 'object',
                    required: ['email', 'password'],
                    properties: {
                      username: {
                        type: 'string',
                      },
                      password: {
                        type: 'string',
                      },
                    },
                  },
                }
              },
            }
          ) credentials: Credentials,
        ): Promise<{token: string}> {
          // make sure user exist,password should be valid
          const user = await this.userService.verifyCredentials(credentials);
          // console.log(user);
          const userProfile = await this.userService.convertToUserProfile(user);
          // console.log(userProfile);
      
          const token = await this.jwtService.generateToken(userProfile);
          return Promise.resolve({token: token})
        }
      
      

      它的工作就像一个魅力,谢谢!

      【讨论】:

      • 很好听。继续。为了使工作隔离,您可以使用不同文件中的代码。更容易跟踪以供以后修订
      猜你喜欢
      • 2022-01-19
      • 2019-06-13
      • 1970-01-01
      • 2019-10-30
      • 2023-02-09
      • 2012-04-22
      • 2018-12-27
      • 1970-01-01
      • 2018-08-01
      相关资源
      最近更新 更多