Giter Club home page Giter Club logo

Comments (7)

jkelvie avatar jkelvie commented on August 21, 2024 2

Okay, that does not surprise me - we don't handle typescript files at the moment - I guess we would need to auto-translate them to the js name?

Another alternative, which you may have already discovered, is that you can pass the lambda function itself to handler. Like this:

const myFunction = function(event: any, context: any) {
  context.done(null, { custom: true });
};

const virtualAlexa = VirtualAlexa.Builder()
  .handler(myFunction)
  .interactionModelFile("models/en-US.json")
  .create();

Perhaps that is a more elegant way to use it with TS.

from virtual-alexa.

CoreyCole avatar CoreyCole commented on August 21, 2024

I was able to work around this by wrapping my handlers in a js file, i.e. in my init.js (not init.ts) file

const handler = require('./src/index').handler;
exports.handler = handler;

and in my integration test file

        alexa = new va.VirtualAlexaBuilder()
            .handler('./init.handler')
            .intentSchemaFile('./speechAssets/IntentSchema.json')
            .sampleUtterancesFile('./speechAssets/SampleUtterances.txt')
            .create();

I guess this is fine. You can close this issue if this is the behavior you want.

Also, weird little edge case I found while messing around with this. I thnk when you are getting the module in ModuleInvoked.js, the way you are getting the file name is splitting on periods (.)

    static invokeHandler(handler, event) {
        const handlerParts = handler.split(".");
        const functionName = handlerParts[handlerParts.length - 1];
        const fileName = handlerParts.slice(0, handlerParts.length - 1).join("/") + ".js";
        const fullPath = path.join(process.cwd(), fileName);
        const handlerModule = require(fullPath);
        return ModuleInvoker.invokeFunction(handlerModule[functionName], event);
    }

so I was unable to name my init file something like init.test.js as I get error:

    Cannot find module '/my-skill-folder/init/test.js' from 'ModuleInvoker.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17)
      at Function.invokeHandler (node_modules/virtual-alexa/lib/src/ModuleInvoker.js:10:31)
      at LocalSkillInteractor.invoke (node_modules/virtual-alexa/lib/src/LocalSkillInteractor.js:13:50)
      at LocalSkillInteractor.<anonymous> (node_modules/virtual-alexa/lib/src/SkillInteractor.js:77:39)

not sure if you'd want to open up another issue about fixing that one as it's probably not too important

from virtual-alexa.

jkelvie avatar jkelvie commented on August 21, 2024

I don't see here how you were initially calling it that led to the issue, so I'm not sure what issue you are working around. Please provide a sample of the code that you are having trouble with when submitting issues.

And I do see lots of other code, just not the code that caused the issue (which makes it difficult to help).

from virtual-alexa.

CoreyCole avatar CoreyCole commented on August 21, 2024

Sorry I was all over the place. In typescript jest test:

    beforeEach(async (done: jest.DoneCallback) => {
        // the below file paths are relative to where package.json is for some reason
        // maybe because it's where the test command is called...
        server = new bst.LambdaServer('./init', 10000, true);
        alexa = new va.VirtualAlexaBuilder()
            .handler('./init.handler')
            .interactionModelFile('./speechAssets/InteractionModel.json')
            .sampleUtterancesFile('./speechAssets/SampleUtterances.txt')
            .applicationID(constants.appId)
            .create();
        await server.start();
        done();
    });

Only works with a javascript init file. If I rename my init.js file toinit.ts and run my ts-jest suite I get the error I pasted above:

    Cannot find module '/my-skill-folder/init.js' from 'ModuleInvoker.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17)
      at Function.invokeHandler (node_modules/virtual-alexa/lib/src/ModuleInvoker.js:10:31)
      at LocalSkillInteractor.invoke (node_modules/virtual-alexa/lib/src/LocalSkillInteractor.js:13:50)
      at LocalSkillInteractor.<anonymous> (node_modules/virtual-alexa/lib/src/SkillInteractor.js:77:39)

The contents of my init file (typescript or javascript) are the same in both cases

const handler = require('./src/index').handler;
exports.handler = handler;

Honestly, this is fine for me. I don't mind having the javascript file as long as the rest of my tests are in typescript. It's up to you if you'd like to keep this open.

The other issue I was talking about is naming the init file init.test.js is not currently possible, but let's not worry about that.

from virtual-alexa.

jkelvie avatar jkelvie commented on August 21, 2024

Hi @CoreyCole - thanks for this info. With regard to this code:

alexa = new va.VirtualAlexaBuilder()
            .handler('./init.handler')
            .interactionModelFile('./speechAssets/InteractionModel.json')
            .sampleUtterancesFile('./speechAssets/SampleUtterances.txt')
            .applicationID(constants.appId)
            .create();

The name of the handler should index.handler, where index is the name of the file (such as index.js) and handler is the function being called.

Now, what you may have run into - we have a bug with handling "." in the filenames. This has been fixed in the latest beta version (you can give it a try by installing virtual-alexa@beta). But if you stay with the current version, do not use a period in the name, so it would just be:

.handler("index.handler")

I hope that helps!

from virtual-alexa.

CoreyCole avatar CoreyCole commented on August 21, 2024

Right, the name of my file is init.js and the above beforeEach code works. The problem is that when I change the init file name to typescript and call it init.ts is when it does not work.

Thanks for all the help by the way!

from virtual-alexa.

CoreyCole avatar CoreyCole commented on August 21, 2024

Yeah, it's definitely not a big deal. What was interesting is that the ts setup worked with BSTAlexa. The ts-jest preprocessing converts everything to js files before running, so I was assuming something weird was going on.

I had not discovered that, thanks for the help!

from virtual-alexa.

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.