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

Module resolution problems with TypeScript #8

Closed
Dombo opened this issue Nov 25, 2018 · 5 comments · Fixed by #81
Closed

Module resolution problems with TypeScript #8

Dombo opened this issue Nov 25, 2018 · 5 comments · Fixed by #81
Labels

Comments

@Dombo
Copy link

Dombo commented Nov 25, 2018

I'm unable to import this when transpiling/compiling with TypeScript.
If I copy the functionality from the module directly into my handler function, it works as expected.

I've tried various different module resolution & target module systems, as well as aliasing and directly addressing. Surely I've just misconfigured something?

I have a PoC/sample config below.

// handler.ts
/**
* @param {import('probot').Application} app
* @param {import('probot').Context} context
*/

const { createProbot } = require('probot')
const { resolve } = require('probot/lib/resolver')
const { findPrivateKey } = require('probot/lib/private-key')
// const { template } = require('./views/probot')

let probot

const loadProbot = appFn => {
  probot = probot || createProbot({
    id: process.env.APP_ID,
    secret: process.env.WEBHOOK_SECRET,
    cert: findPrivateKey()
  })

  if (typeof appFn === 'string') {
    appFn = resolve(appFn)
  }

  probot.load(appFn)

  return probot
}

const serverless = appFn => {
  return async (event, context) => {
    // 🤖 A friendly homepage if there isn't a payload
    if (event.httpMethod === 'GET' && event.path === '/probot') {
      const res = {
        statusCode: 200,
        headers: {
          'Content-Type': 'text/html'
        },
        body: 'replaced template with string'
      }
      return context.done(null, res)
    }

    // Otherwise let's listen handle the payload
    probot = probot || loadProbot(appFn)

    // Ends function immediately after callback
    context.callbackWaitsForEmptyEventLoop = false

    // Determine incoming webhook event type
    const e = event.headers['x-github-event'] || event.headers['X-GitHub-Event']

    // Convert the payload to an Object if API Gateway stringifies it
    event.body = (typeof event.body === 'string') ? JSON.parse(event.body) : event.body

    // Do the thing
    console.log(`Received event ${e}${event.body.action ? ('.' + event.body.action) : ''}`)
    if (event) {
      try {
        await probot.receive({
          name: e,
          payload: event.body
        })
        const res = {
          statusCode: 200,
          body: JSON.stringify({
            message: `Received ${e}.${event.body.action}`
          })
        }
        return context.done(null, res)
      } catch (err) {
        console.error(err)
        return context.done(null, {
          statusCode: 500,
          body: JSON.stringify(err)
        })
      }
    } else {
      console.error({ event, context })
      context.done(null, 'unknown error')
    }
    return context.done(null, {
      statusCode: 200,
      body: 'Nothing to do.'
    })
  }
}

// const { serverless } = require('@probot/serverless-lambda')
// import { serverless } from '@probot/serverless-lambda/index'
// import * as serverless from '@probot/serverless-lambda';
// import { serverless } from 'serverless-lambda'

const pro = (app) => {
  app.log('Yay, the app was loaded!')

  app.on('issues.opened', async context => {
    const issueComment = context.issue({ body: 'Thanks for opening this issue!' })
    return context.github.issues.createComment(issueComment)
  })
}

module.exports.pro = serverless(pro)
// tsconfig.json
{
  "compilerOptions": {
    "allowJs": false,
    "lib": ["es2015", "es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es6",
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": false,
    "pretty": true,
    "strict": true,
    "sourceMap": true,
    "outDir": "./lib",
    "skipLibCheck": true,
    "noImplicitAny": false, // Disabled
    "esModuleInterop": true,
    "declaration": true,
    "resolveJsonModule": true
  },
  "include": [
    "src/**/*"
  ],
  "compileOnSave": false
}
@vineethsoma
Copy link

I am having a similar issue as well. Were you ever able to figure out a solution for this?

@ptdel
Copy link

ptdel commented May 12, 2020

how do people typically use this extension with typescript? Documentation is unclear.

Could not find a declaration file for module '@probot/serverless-lambda'. '***/node_modules/@probot/serverless-lambda/index.js' implicitly has an 'any' type.
  Try `npm install @types/probot__serverless-lambda` if it exists or add a new declaration (.d.ts) file containing `declare module '@probot/serverless-lambda';`ts(7016)

I'm also unable to properly import this.

@awarecan
Copy link

awarecan commented Aug 19, 2020

Set "noImplicitAny": false in the tsconfig.json works for me. The default tsconfig.json created by https://github.com/probot/create-probot-app set "strict": true which means noImplicitAny is true as well.

GitHub
🤖📦 Create a new probot app. Contribute to probot/create-probot-app development by creating an account on GitHub.

@gr2m
Copy link
Contributor

gr2m commented Feb 9, 2021

happy to add better TypeScript definitions via #59

The new version will export the same as the probot module, plus it adds { createLambdaFunction } which has the same type as { createNodeMiddleware } from the probot module.

Happy to accept a pull request if anyone would like to add types for that

@github-actions
Copy link

github-actions bot commented Nov 3, 2021

🎉 This issue has been resolved in version 2.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants