Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Typescript? #110

Closed
xtheshumanator opened this issue May 15, 2019 · 5 comments
Closed

Support for Typescript? #110

xtheshumanator opened this issue May 15, 2019 · 5 comments
Labels

Comments

@xtheshumanator
Copy link

xtheshumanator commented May 15, 2019

Hi, do you have a sample typescript which uses lambda-api?
I'm getting a bunch of errors and just want to make sure I'm not missing anything.

The errors I am getting are like this:

node_modules/lambda-api/index.d.ts:152:3 - error TS7010: 'getLink', which lacks return-type annotation, implicitly has an 'any' return type.

152   getLink(s3Path: string, expires?: number, callback?: (err, data) => void);
      ~~~~~~~

Thanks

@stawecki
Copy link

stawecki commented Jun 3, 2019

Try using the updated type declarations from maxmellen's PR: https://github.com/maxmellen/lambda-api/tree/strict-ts

My sample handler.ts looks like this (note that it's not ideal, because i'm still using require and assigning it the API type, but it works. Can't seem to use import to retrieve createAPI in one go.).

import { API } from "lambda-api";
const api = require("lambda-api")() as API;

api.get("/status", async (req, res) => {
  return { status: 'ok' }
});

exports.run = async (event: any, context: any) => {
  return await api.run(event, context)
}

Any other ideas appreciated.

@stawecki
Copy link

stawecki commented Jun 3, 2019

Just to elaborate the import issue, the suggested import:

import API from 'lambda-api'
const api = API({});

Transpiles into:

const lambda_api_1 = require("lambda-api");
const api = lambda_api_1.default({});

Which results in: TypeError: lambda_api_1.default is not a function
I believe it's the correct behaviour for export default createAPI; in index.d.ts, but it doesn't match the module.exports = opts => new API(opts) in index.js. The fix is to add the following line to index.js:

module.exports.default = opts => new API(opts)

I didn't start a PR for this, because it may not be the ideal solution. I also may be initialising the API wrong.

@jsepia
Copy link

jsepia commented Jun 6, 2019

This worked for me:

handler.ts

import API from 'lambda-api'
import usersApi from './api/users'
import { APIGatewayProxyHandler, APIGatewayEvent, APIGatewayProxyCallback, Context } from 'aws-lambda';

const api = API({ version: 'v0.1.0', base: 'v0' })

api.get('/users', usersApi)

const handler: APIGatewayProxyHandler = (event: APIGatewayEvent, context: Context, callback: APIGatewayProxyCallback) => {
  api.run(event, context, callback)
}

export { handler }

@stawecki
Copy link

stawecki commented Jun 6, 2019

@jsepia what are your compilerOptions in tsconfig.json, if I may know?

@stawecki
Copy link

Looking at ts-lambda-api, looks like you need "esModuleInterop": true in tsconfig.json's compilerOptions, unless you're using Babel.

I've tested this myself now and it works.

@jeremydaly can we add this information to the README?

I also think it might be worth adding the module.exports.default mapping as esModuleInterop is not enabled by default in the TS compiler and it shouldn't affect Babel or ES6+ users.

stawecki pushed a commit to stawecki/lambda-api that referenced this issue Mar 10, 2020
jeremydaly added a commit that referenced this issue Apr 27, 2020
Add default export to match TypeScript definition #110
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants