Skip to content

Latest commit

 

History

History
136 lines (100 loc) · 2.94 KB

README.md

File metadata and controls

136 lines (100 loc) · 2.94 KB

Serverless Toolboxs

Serverless License NPM Build Status Coverage Status Greenkeeper badge

Serverless toolbox

Install

npm i serverless-tools

Examples

httpless

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'
}

microless

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)
})

probe

Generate a report for serverless function runtime environment.

const probe = require('serverless-tools/probe')

console.log(probe())

{
    os: { },
    process: {
        env: { },
    },
    'aws-sdk': { },
}

evaluate

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',
    ...
}