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 1 year 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