-
Notifications
You must be signed in to change notification settings - Fork 50
ProxyIntegration/Typescript: Not returning ProxyIntegrationResult #58
Description
Hi
Updated aws-lambda-router (from 0.6.2) on our project using Typescript, and noticed strange issue.
Aws-lambda-router prevents returning non-string responses from proxyIntegration actions here: https://github.com/spring-media/aws-lambda-router/blob/master/lib/proxyIntegration.ts#L22
So our old code, which returns plain JS objects, does not work anymore, as it fails Typescript checks.
However, if I JSON.stringify my response object, before returning it from action, aws-lambda-router wraps it with another JSON.stringify call here: https://github.com/spring-media/aws-lambda-router/blob/master/lib/proxyIntegration.ts#L61 (because of condition in line 57 does not handle plain string responses).
If I do the old way, and return plain JS object from my action (and cast it to string to make typescript happy) everything seems to work as it should.
So, is something wrong with proxyIntegration Action types? Or should I be returning instances of ProxyIntegrationResult from my actions nowadays?
EDIT:
Our old code (for aws-lambda-router 0.6.2) looked like this:
import * as router from 'aws-lambda-router';
export const handler = router.handler({
proxyIntegration: {
cors: false,
routes: [
{
path: '/foobar',
method: 'GET',
action: () => ({ isFoobar: true }),
},
],
},
});With new aws-lambda-router (0.9.1), this fails with Typescript (4.1.3) error:
foo.ts:11:23 - error TS2322: Type '{ isFoobar: boolean; }' is not assignable to type 'string | Promise<string> | ProxyIntegrationResult | Promise<ProxyIntegrationResult>'.
Type '{ isFoobar: boolean; }' is missing the following properties from type 'Promise<ProxyIntegrationResult>': then, catch, [Symbol.toStringTag], finally
11 action: () => ({ isFoobar: true }),
~~~~~~~~~~~~~~~~~~~
../../node_modules/aws-lambda-router/lib/proxyIntegration.d.ts:20:13
20 action: (request: ProxyIntegrationEvent<unknown>, context: APIGatewayEventRequestContext) => ProxyIntegrationResult | Promise<ProxyIntegrationResult> | string | Promise<string>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The expected type comes from the return type of this signature.
Found 1 error.
If I change it to like this:
import * as router from 'aws-lambda-router';
export const handler = router.handler({
proxyIntegration: {
cors: false,
routes: [
{
path: '/foobar',
method: 'GET',
action: () => {
return JSON.stringify({ isFoobar: true });
},
},
],
},
});It passes typescript compilation, but calls JSON.stringify second time for that string, adding second set of quotes.
I can make it work like this:
import * as router from 'aws-lambda-router';
export const handler = router.handler({
proxyIntegration: {
cors: false,
routes: [
{
path: '/foobar',
method: 'GET',
action: () => {
return { isFoobar: true } as unknown as string;
},
},
],
},
});But that kind of kills the point of using typescript.