Giter Site home page Giter Site logo

Comments (6)

fruffin avatar fruffin commented on May 24, 2024

I'm not quite sure what format velocity.js is meant to output, but to me it looks like it's meant to be json strings and it's just not happening.

I managed to get it to work by modifying the file velocityjs/src/compile/compile.js

This is the new version of the "format" function which seems to work with serverless-offline, but the fix may be in completely the wrong place and I may have completely misunderstood how velocity is meant to work:

    format: function(value) {
      if (utils.isArray(value)) {
        return "[" + "\"" + value.map(this.format.bind(this)).join("\", \"") + "\"" + "]";
      }
      if (utils.isObject(value)) {
        if (value.toString.toString().indexOf('[native code]') === -1) {
          return value;
        }
        var kvJoin = function(k) { return "\"" + k + "\":\"" + this.format(value[k]) + "\""; }.bind(this);
        return "{" + Object.keys(value).map(kvJoin).join(", ") + "}";
      }
      return value;
    }

The changes are just the addition of a few double quotes and the replacement of "=" with ":"
If I get this right, all this could just be replaced with a call to "stringify", but again, I may have completely misunderstood what this is meant to do.

Let me know what you think

Thanks

from serverless-offline.

dherault avatar dherault commented on May 24, 2024

Hi !
First of all I'm sorry I took so long (new job).
Using Serverless-Offline, your query string is rendered as "{first=12, other=34}". What about APIG ? I think it does the same! "$input.params().querystring" and "$input.params().header" are not meant to return JS object, neither JSON, they return Velocity-flavored string.

Nevertheless, if you stop any difference between APIG and this plugin, I'll fix it ASAP.
I'll be more available from now on.

from serverless-offline.

marcoslhc avatar marcoslhc commented on May 24, 2024

IDK if this is about the same issue... I have this from the directly from serverless-starter project:

{
  "apiRequestTemplate": {
    "application/json": {
      "pathId": "$input.params('id')"
    }
  }
}

it fails before getting to the handlers:

Serverless: GET /dev/single/?id=12 (λ: single)
[debug] requestId: 6225277074179414
[debug] contentType: application/json
[debug] requestTemplate: { httpMethod: '$context.httpMethod',
  body: '$input.json(\'$\')',
  queryParams: '$input.params().querystring',
  headerParams: '$input.params().header',
  headerParamNames: '$input.params().header.keySet()',
  contentTypeValue: '$input.params().header.get(\'Content-Type\')',
  pathId: '$input.params(\'id\')' }
[debug] payload: null
[debug] Invalidating cache...
[debug] Loading handler... (/path/to/my/serverless-starter/restApi/single/handler)
[debug] _____ REQUEST TEMPLATE PROCESSING _____
[debug] Processing key: httpMethod - value: $context.httpMethod
[debug] Velocity rendered: GET
[debug] Processing key: body - value: $input.json('$')
[debug] Calling JSONPath: $
[debug] JSONPath resolved: {}
[debug] Velocity rendered: {}
[debug] Processing key: queryParams - value: $input.params().querystring
Serverless: Error while parsing template "application/json" for single
[ 'TypeError: Cannot read property \'toString\' of undefined',
  'at Velocity.utils.mixin.format (/path/to/my/serverless-starter/node_modules/velocityjs/src/compile/compile.js:116:27)',
  'at Velocity.<anonymous> (/path/to/my/serverless-starter/node_modules/velocityjs/src/compile/compile.js:80:25)',
  'at Array.forEach (native)',
  'at Object.utils.(anonymous function) [as forEach] (/path/to/my/serverless-starter/node_modules/velocityjs/src/utils.js:9:25)',
  'at Velocity.utils.mixin._render (/path/to/my/serverless-starter/node_modules/velocityjs/src/compile/compile.js:71:13)',
  'at Velocity.utils.mixin.render (/path/to/my/serverless-starter/node_modules/velocityjs/src/compile/compile.js:37:22)',
  'at renderVelocityString (/path/to/my/serverless-starter/node_modules/serverless-offline/src/renderVelocityTemplateObject.js:58:80)',
  'at renderVelocityTemplateObject (/path/to/my/serverless-starter/node_modules/serverless-offline/src/renderVelocityTemplateObject.js:30:52)',
  'at server.route.handler.err (/path/to/my/serverless-starter/node_modules/serverless-offline/src/index.js:335:27)',
  'at Object.internals.handler (/path/to/my/serverless-starter/node_modules/hapi/lib/handler.js:96:36)',
  'at request._protect.run (/path/to/my/serverless-starter/node_modules/hapi/lib/handler.js:30:23)',
  'at internals.Protect.run (/path/to/my/serverless-starter/node_modules/hapi/lib/protect.js:64:5)',
  'at exports.execute (/path/to/my/serverless-starter/node_modules/hapi/lib/handler.js:24:22)',
  'at each (/path/to/my/serverless-starter/node_modules/hapi/lib/request.js:383:16)',
  'at iterate (/path/to/my/serverless-starter/node_modules/hapi/node_modules/items/lib/index.js:36:13)',
  'at done (/path/to/my/serverless-starter/node_modules/hapi/node_modules/items/lib/index.js:28:25)',
  'at internals.Auth._authenticate (/path/to/my/serverless-starter/node_modules/hapi/lib/auth.js:210:16)',
  'at internals.Auth.authenticate (/path/to/my/serverless-starter/node_modules/hapi/lib/auth.js:202:17)',
  'at each (/path/to/my/serverless-starter/node_modules/hapi/lib/request.js:383:16)',
  'at iterate (/path/to/my/serverless-starter/node_modules/hapi/node_modules/items/lib/index.js:36:13)',
  'at done (/path/to/my/serverless-starter/node_modules/hapi/node_modules/items/lib/index.js:28:25)',
  'at internals.state (/path/to/my/serverless-starter/node_modules/hapi/lib/route.js:350:16)',
  'at each (/path/to/my/serverless-starter/node_modules/hapi/lib/request.js:383:16)',
  'at iterate (/path/to/my/serverless-starter/node_modules/hapi/node_modules/items/lib/index.js:36:13)',
  'at Object.exports.serial (/path/to/my/serverless-starter/node_modules/hapi/node_modules/items/lib/index.js:39:9)',
  'at internals.Request._lifecycle (/path/to/my/serverless-starter/node_modules/hapi/lib/request.js:386:11)',
  'at internals.Request._execute (/path/to/my/serverless-starter/node_modules/hapi/lib/request.js:301:21)',
  'at Domain.request._protect.enter (/path/to/my/serverless-starter/node_modules/hapi/lib/connection.js:261:25)',
  'at Domain.run (domain.js:221:14)',
  'at internals.Protect.enter (/path/to/my/serverless-starter/node_modules/hapi/lib/protect.js:80:17)',
  'at Server.<anonymous> (/path/to/my/serverless-starter/node_modules/hapi/lib/connection.js:259:30)',
  'at emitTwo (events.js:106:13)',
  'at Server.emit (events.js:191:7)',
  'at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)',
  'at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)' ]
Serverless: Replying error in handler

I tried to do this and didn't get the error:

{
  "apiRequestTemplate": {
    "application/json": "{pathId=$input.params('id')}"
  }
}

I didn't got the transformed map in the function though 😞

from serverless-offline.

marcoslhc avatar marcoslhc commented on May 24, 2024

This error happens in Node 6.0. Rolling back to Node 5.0 fixed my issue. I guess is something with the Object.toString check in Velocity broke with the new version of Node.

from serverless-offline.

dherault avatar dherault commented on May 24, 2024

Yep, I'll look into it :) PR are welcome because I got a new job u_u

from serverless-offline.

dherault avatar dherault commented on May 24, 2024

This was a major bug due to an upgraded dependency (hapijs). Thanks guys for the issue. Please check the new version 2.4.0 for the fix :)

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.