dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.71k stars 164 forks source link

How to properly wrap the http.createServer? #284

Closed vorant94 closed 9 months ago

vorant94 commented 10 months ago

Hi, there!

I see in the README it is said that your lib supports built-in nodejs server (http.createServer). However once I try to create a handler from a server, i get this typescript error:

TS2345: Argument of type
Server<typeof IncomingMessage, typeof ServerResponse>
is not assignable to parameter of type  Application

What am I missing?

logusgraphics commented 9 months ago

This works for me:

const { createServer } = require('http');
const serverless = require('serverless-http');

const server = createServer(async (req, res) => {
    res.statusCode = 200;
    res.end('Hello serverless world!'); 
});

exports.handler = serverless(server, { provider: 'aws' });
vorant94 commented 9 months ago

This works for me:

const { createServer } = require('http');
const serverless = require('serverless-http');

const server = createServer(async (req, res) => {
    res.statusCode = 200;
    res.end('Hello serverless world!'); 
});

exports.handler = serverless(server, { provider: 'aws' });

you are writing JS, but I tried to use with TS so I got type error... maybe the code is fine, only types are not reflecting it as they should🤔

vorant94 commented 9 months ago

still no response from someone of maintainers... is this repo open source just for the sake of being open source (which is totally valid by the way) or authors meant to communicate with the community and so on?

I also see tests that should cover integration with node built-in http module, so the problem is really with types only. I am happy to contribute, but i'd like to get feedback on whether it is needed or not at all... despite what is written in readme about contribution there are opened pr's from 2021...

regarding the issue itself. I see the type that handler accepts type Application = Function | Partial<FrameworkApplication>. The easiest way to fix the issue is to add Server<typeof IncomingMessage, typeof ServerResponse> to the union and the type gymnastic is done, but since you are not iterating in this union over all possible integrations, I assume you'd prefer some another approach...

dougmoscrop commented 9 months ago

I don't think another approach is needed, updating the union makes sense. I didn't write the types, it was a community contribution.