Night
现在的代码是这样子的。
import cloud from '@lafjs/cloud'
import { Route } from './routes'
export async function main(ctx: FunctionContext) {
const { route } = ctx.query
const routes = cloud.shared.get('routes')
const router: Route = routes.get(route)
if (!router) {
return {
code: 404,
error: 'Route not found.'
}
}
const { ip } = await cloud.invoke('get-real-ip', {
headers: { ...ctx.headers },
request: { ...ctx.request }
})
let user = ctx.user || {}
if (user.uid) {
const res = await cloud.database().collection('users').where({ _id: user.uid }).getOne()
user = { ...user, ...res.data, password: '' }
}
try {
const { data, error } = await cloud.invoke(router.func, {
body: { ...ctx.body },
query: { ...ctx.query },
files: ctx.files,
user: { ...(user || {}), ip },
})
return {
code: error ? 10001 : 10000,
error,
data
}
} catch (e) {
console.error(e)
return {
code: 500,
error: e.message
}
}
}
如果没有那个路由表的,直接 invoke
的话,就会报主贴里面的错。