Serverless toolbox
npm i serverless-tools
A more node-style way to write your handler functions.
const httpless = require('serverless-tools/httpless')
exports.hello = httpless((req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.end(`Hello ${req.query.name || 'gays'}! -- Provide by ${req.provider}`)
})
{
statusCode: 200,
headers: {
'content-type': 'text/plain'
},
body: 'Hello gays! -- Provide by aws'
}
A micro-style way to write your handler functions.
const microless = require('serverless-tools/microless')
const { json, send, sendError, createError } = microless
exports.hello = microless(() => {
return 'Hello world'
})
exports.ok = microless((req, res) => {
send(res, 200, { message: 'ok' })
})
exports.auth = microless((req, res) => {
if (!auth(req)) throw createError(401, 'Must signin first')
send(res, 200, 'welcome')
})
exports.download = microless((req, res) => {
if (limited) {
const err = new Error('Bandwidth limit exceeded')
err.status = 509
err.expose = true
sendError(req, res, err)
return
}
send(res, 200, base64Content)
})
Generate a report for serverless function runtime environment.
const probe = require('serverless-tools/probe')
console.log(probe())
{
os: { },
process: {
env: { },
},
'aws-sdk': { },
}
Evaluate runtime feature and guess possible env.
const evaluate = require('serverless-tools/evaluate')
console.log(evaluate())
{
provider: 'aws',
nodeEnv: 'production'
}
Write evaluate envs into process.env
. This side effect happend immediately.
require('serverless-tools/setEnv')
console.log(process.env)
{
...
PROVIDER: 'aws',
NODE_ENV: 'production',
...
}