我有一个使用next-connect
&的项目twitter-api-v2
。
我有一个handler
调用 2get
条路线,例如:
export default handler()
.get('/twitter/generate-auth-link', generateAuthLink)
.get('/twitter/get-verifier-token', getVerifierToken)
handler
看起来如下:
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
我认为我做得next-connect
对,但是当我尝试在客户端执行简单操作时为什么会出现错误,fetch
例如:
const res = await fetch('/api/twitter/generate-auth-link');
console.log({res})
后端给出以下错误:
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'
}
我做了一个完整的复制→ https://github.com/deadcoder0904/twitter-api-v2-3-legged-login-using-next-connect
我想要做的就是使用twitter-api-v2
. 步骤在https://github.com/PLhery/node-twitter-api-v2/blob/master/doc/auth.md上给出
我该如何解决这个问题?