serverless-loopback icon indicating copy to clipboard operation
serverless-loopback copied to clipboard

Loopback 4 example - convert lamdba-wrapper.js to Typescript equivalent.

Open awongCM opened this issue 2 years ago • 5 comments

Hi, I've followed the loopback 4 example for this and I really like how you have the framework to play nicely with serverless framework when following the tutorial!

The thing I'm confused about is the lambda wrapper js file.

https://github.com/botbits/serverless-loopback/blob/master/examples/lb4-express/src/lambda-wrapper.js

Could it not have been better to rewrite it in Typescript in the following:

import {ExpressServer} from '../server';

import serverless from 'serverless-http';

import {ApplicationConfig} from '@loopback/core';

let app: Function;

async function main(options: ApplicationConfig) {
  const server = new ExpressServer(options);
  app = serverless(server.app);
  await server.boot();
  await server.start();
  console.log('Server is running at http://127.0.0.1:3000');
}

exports.handler = async function handler(
  request: Object,
  ...context: Object[]
) {
  if (app === undefined) {
    // Run the application
    const config = {
      rest: {
        port: +(process.env.PORT ? process.env.PORT : 3000),
        host: process.env.HOST ? process.env.HOST : 'localhost',
        openApiSpec: {
          // useful when used with OpenAPI-to-GraphQL to locate your application
          setServersFromRequest: true,
        },
        // Use the LB4 application as a route. It should not be listening.
        listenOnStart: false,
      },
    };
    await main(config).catch(err => {
      console.error('Cannot start the application.', err);
      process.exit(1);
    });
  }
  return app(request, ...context);
};

That way, you don't need to configure tsconfig.json to include raw JS files as part of the build process and you only have Typescript files to work with, not JS/Typescript files combo..

I'm just curious what's your rationale is keeping both JS and Typescript this way.

awongCM avatar Mar 05 '23 10:03 awongCM

Hi @awongCM, happy to hear you found the tutorial useful!

To the best of my recollection, the reason for JS rather than TS was that LB4 was added as a fix and I did not spend the time to look into the TS code as I was unfamiliar with TypeScript :-)

If you feel comfortable, please go ahead and create a PR for this change.

Or if you prefer, I can do the PR with the code provided.

marcelobern avatar Mar 05 '23 17:03 marcelobern

Hi @awongCM, happy to hear you found the tutorial useful!

To the best of my recollection, the reason for JS rather than TS was that LB4 was added as a fix and I did not spend the time to look into the TS code as I was unfamiliar with TypeScript :-)

If you feel comfortable, please go ahead and create a PR for this change.

Or if you prefer, I can do the PR with the code provided.

Hi @marcelobern

Ah. That makes sense!

Thank you for the candid reply. I certainly enjoy the tutorial - without a doubt! I have been working on my own pet serverless projects on my profile, and I've been recommended by my colleague how great the loopback framework is for building enterprise-level microservices due to its strong DDD philosophy.

Thus it had me pondering how to incorporate loopback's microservices architecture into the serverless world until I googled "loopback serverless" which lead me to your GitHub repo as the first search result page.. ;)

I'm more than happy to contribute. 👍

Let me create one and get back to on the PR submitted.

awongCM avatar Mar 06 '23 10:03 awongCM

@marcelobern

I create my local PR; did the above code to replace .js counterpart file.

But when I tried to run presls-deploy, I ran into the following:

node_modules/@loopback/testlab/node_modules/@types/express-serve-static-core/index.d.ts:1183:31 - error TS1005: ';' expected.

1183      * Render the given view `name` name with `options`
                                   ~~~~

node_modules/@loopback/testlab/node_modules/@types/express-serve-static-core/index.d.ts:1183:48 - error TS1005: ';' expected.

1183      * Render the given view `name` name with `options`
                                                    ~~~~~~~

node_modules/@loopback/testlab/node_modules/@types/express-serve-static-core/index.d.ts:1199:16 - error TS1005: ';' expected.

1199      * A node `http.Server` is returned, with this
                    ~~~~

node_modules/@loopback/testlab/node_modules/@types/express-serve-static-core/index.d.ts:1200:33 - error TS1005: ';' expected.

1200      * application (which is a `Function`) as its
                                     ~~~~~~~~

node_modules/@loopback/testlab/node_modules/@types/express-serve-static-core/index.d.ts:1268:1 - error TS1160: Unterminated template literal.

1268 
     

I'm getting lot of Typescript compilation errors... 😕

awongCM avatar Mar 06 '23 11:03 awongCM

@awongCM this brings some memories.

I vaguely recall getting into some TypeScript issues that I did not know how to solve and wanted to add the lb4 sample.

So I guess these might have been it :-D

I am not sure I can help you with the TypeScript errors, but I will ask you all the same. How can I help?

marcelobern avatar Mar 09 '23 01:03 marcelobern

@awongCM this brings some memories.

I vaguely recall getting into some TypeScript issues that I did not know how to solve and wanted to add the lb4 sample.

So I guess these might have been it :-D

I am not sure I can help you with the TypeScript errors, but I will ask you all the same. How can I help?

Interesting.

Odd thing is that I never run into any problems when cloning the loopback4 examples as a separate project; followed the tutorial; convert the main lambda wrapper to Typescript.

It works fine there after.

Perhaps there's some config settings from either tsconfig.json or similar that's not liking here...

awongCM avatar Mar 15 '23 10:03 awongCM