TypeError: resolver is not a function in `next-connect`

3.1k views Asked by At

I have a project using next-connect & twitter-api-v2.

I have a handler which calls 2 get routes like:

export default handler()
  .get('/twitter/generate-auth-link', generateAuthLink)
  .get('/twitter/get-verifier-token', getVerifierToken)

The handler looks like follows:

import { NextApiResponse } from 'next'
import cookieSession from 'cookie-session'
import nc from 'next-connect'
import { ironSession } from 'next-iron-session'
import { error } from 'next/dist/build/output/log'
import { NextIronRequest } from '../types/index'

const COOKIE_SECRET = process.env.COOKIE_SECRET
const SESSION_SECRET = process.env.SESSION_SECRET

const IS_PRODUCTION = process.env.NODE_ENV !== 'development'

/**
 * Create an API route handler with next-connect and all the necessary middlewares
 *
 * @example
 * ```ts
 * export default handler().get((req, res) => { ... })
 * ```
 */
function handler() {
  if (!COOKIE_SECRET || !SESSION_SECRET)
    throw new Error(
      `Please add COOKIE_SECRET & SESSION_SECRET to your .env.local file!`
    )

  return nc<NextIronRequest, NextApiResponse>({
    onError: (err, _, res) => {
      error(err)
      res.status(500).end(err.toString())
    },
  })
    .use(
      cookieSession({
        name: 'session',
        keys: [COOKIE_SECRET],
        maxAge: 24 * 60 * 60 * 1000 * 30,
        secure: IS_PRODUCTION && !process.env.INSECURE_AUTH,
        signed: IS_PRODUCTION && !process.env.INSECURE_AUTH,
      })
    )
    .use(
      ironSession({
        cookieName: 'mysite-session',
        password: SESSION_SECRET,
        // if your localhost is served on http:// then disable the secure flag
        cookieOptions: {
          secure: IS_PRODUCTION,
        },
      })
    )
}

export default handler

I think I'm doing next-connect right but why do I get an error when I try to do simple fetch in the client like:

const res = await fetch('/api/twitter/generate-auth-link');
console.log({res})

The backend gives the following error:

error - TypeError: resolver is not a function
    at Object.apiResolver (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/api-utils.js:101:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async DevServer.handleApiRequest (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:770:9)
    at async Object.fn (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:661:37)
    at async Router.execute (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/router.js:205:32)
    at async DevServer.run (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:841:29)
    at async DevServer.run (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/dev/next-dev-server.js:355:20)
    at async DevServer.handleRequest (/~/twitter-api-v2-3-legged-login-using-next-connect/node_modules/next/dist/server/next-server.js:292:20) {
  page: '/api/twitter/generate-auth-link'
}

I have made a complete repro → https://github.com/deadcoder0904/twitter-api-v2-3-legged-login-using-next-connect

All I want to do is perform steps of 3-legged Oauth using twitter-api-v2. The steps are given on https://github.com/PLhery/node-twitter-api-v2/blob/master/doc/auth.md

How do I solve this?

1

There are 1 answers

0
deadcoder0904 On BEST ANSWER

I removed index.ts file from pages/api/twitter/ & used default export in both pages/api/twitter/generate-auth-link & pages/api/twitter/get-verifier-token.

This solved it!