Skip to content

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

Open
@awongCM

Description

@awongCM

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions