Giter Site home page Giter Site logo

Comments (7)

dherault avatar dherault commented on May 28, 2024

You're right, requestTemplates as defined per const requestTemplates = endpoint.requestTemplates; is already an Object, so there is no need for parsing. I've been working on 1.0 (that should supports velocity in full) on master but I'll create a branch for it.

Allow 30 min, I'll publish a new version applying your fix. Make sure you've read the source (you've done it obviously) before using the --useTemplates option, it's very opinionated.

Thanks for the issue!

from serverless-offline.

rallu avatar rallu commented on May 28, 2024

Yeah. I noticed that after fiddling for a while. Then I found another error in second route and ended up catching whole error:

try {
  toApply = JSON.parse(jsonContent);
} catch (e) {
  toApply = {};
}

This way it at least doesn't crash all the time.

from serverless-offline.

dherault avatar dherault commented on May 28, 2024

I wanted to get rid of the JSON parsing, but do you think it is still necessary ?

from serverless-offline.

rallu avatar rallu commented on May 28, 2024

Just get rid of it. It was nice to have something automatic for simple cases.

from serverless-offline.

dherault avatar dherault commented on May 28, 2024

This is a fix, not amazing:

// Then we create the event object
const event = Object.assign({ isServerlessOffline: true }, request);

if (requestTemplates && this.evt.useTemplates) {
  // Apply request template to event
  const requestTemplate = requestTemplate[request.mime || 'application/json'];
  if (typeof requestTemplate === 'object') {
    try {
      // TODO: proces $context variables in a more complete way http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference
      // TODO: $input could also be dealt with in a more robust way http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference
      for (let key in requestTemplate) {

        if (requestTemplate[key] === '$context.httpMethod') {
          event[key] = request.method.toUpperCase();
        } else if (requestTemplate[key] === '$input.params()') {
          event[key] = request.params;
        } else {
          const reResp = reInputParam.exec(requestTemplate[key]);
          if (reResp) {
            // lookup variable replacement in params
            const paramName = reResp[1];
            event[key] = paramName in event.params ? event.params[paramName] : '';
          }
          else {
            event[key] = requestTemplate[key];
          }
        }
      }
    }
    catch (err) {
      SCli.log('Error while trying to use your templates:');
      console.log(err.stack || err);
      serverResponse.statusCode = 500;
      serverResponse.source = 'Error while trying to use your templates.';
      serverResponse.send();
      return;
    }
  }
}

What do you think ?

from serverless-offline.

dherault avatar dherault commented on May 28, 2024

I replaced if (typeof requestTemplate === 'object') { with if (requestTemplate) {

from serverless-offline.

dherault avatar dherault commented on May 28, 2024

Publised under v0.2.2

from serverless-offline.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.