@magic/core
Version:
@magic core. generate static pages and serverless lambdas. ~5kb client boilerplate.
63 lines (47 loc) • 1.43 kB
JavaScript
import is from '@magic/types'
export const apiHandler = async (req, res, { lambdas, rawUrl }) => {
if (rawUrl === '/api/' || rawUrl === '/api') {
const code = 200
const lambdaEntries = Object.fromEntries(
Object.entries(lambdas).map(([k, v]) => [k, typeof v === 'function' ? true : v]),
)
const body = JSON.stringify(lambdaEntries, null, 2)
res.writeHead(code, { 'Content-Type': 'text/plain' })
res.end(body)
return
}
const [module, action] = rawUrl
.replace('/api/', '')
.split('/')
.filter(a => a)
let lambda = lambdas[module]
if (is.objectNative(lambda) && action) {
lambda = lambda[action]
}
if (is.function(lambda)) {
req.body = []
req.on('data', chunk => {
if (is.string(chunk)) {
chunk = Buffer.from(chunk)
}
req.body.push(chunk)
})
req.on('end', async (...args) => {
req.body = Buffer.concat(req.body).toString()
if (req.headers['content-type'] === 'application/json') {
try {
req.body = JSON.parse(req.body)
} catch (e) {
log.error('E_JSON_PARSE', e)
req.body = e
}
}
const result = await lambda(req, res, ...args)
const { code = 500, body = 'Internal Server Error', type = 'text/plain' } = result
res.writeHead(code, { 'Content-Type': type })
res.end(body)
})
return true
}
return false
}