requestBody()

in src/resolver/client.js [399:471]


  requestBody(ast, body, func) {
    if (body) {
      if (body.type === 'apiBody') {
        // TeaRequest _request = new TeaRequest()
        func.addBodyNode(
          new GrammerExpr(
            new GrammerVar(this.config.request, requestType),
            Symbol.assign(),
            new GrammerNewObject('$Request')
          )
        );
      }

      const stmt = ['declares', 'protocol', 'port', 'method', 'pathname', 'quert', 'headers', 'body', 'appendStmts'];
      stmt.forEach(key => {
        if (body[key] && body[key] !== 'undefined') {
          if (Array.isArray(body[key])) {
            body[key].forEach(stmtItem => {
              this.visitStmt(func, stmtItem, func.index);
            });
          } else {
            this.visitStmt(func, body[key], func.index);
          }
        }
      });

      if (body.type === 'apiBody') {
        var doActionParams = [];
        doActionParams.push(new GrammerValue('param', this.config.request, requestType));

        if (ast.runtimeBody) {
          doActionParams.push(new GrammerValue('param', this.config.runtime, runtimeType));
        }

        // response = Tea.doAction
        const doActionBehavior = new BehaviorDoAction(
          new GrammerVar(this.config.response, new TypeObject('$Response')), doActionParams
        );

        if (body.stmts) {
          body.stmts.stmts.forEach(stmt => {
            this.visitStmt(func, stmt, func.index);
          });
        }

        // _lastRequest = request_;
        func.addBodyNode(
          new GrammerExpr(
            new GrammerVar('_lastRequest', requestType, 'var'),
            Symbol.assign(),
            new GrammerVar(this.config.request, requestType)
          )
        );

        // returns
        if (ast.returns) {
          if (ast.returns.stmts.stmts.length > 0) {
            ast.returns.stmts.stmts.forEach(stmt => {
              this.visitStmt(doActionBehavior, stmt, doActionBehavior.index);
            });
          } else {
            this.findComments(doActionBehavior, ast.returns, 'between');
          }
        }
        func.addBodyNode(doActionBehavior);
      }
      if (body.tokenRange) {
        this.resolveAnnotations(this.getFrontComments(body.tokenRange[1], func.index)).forEach(c => {
          func.addBodyNode(c);
        });
      }
    }
  }