Skip to content

Common ​

Shared utilities for the Mayari ecosystem. This package contains the foundational building blocks (exceptions and response helpers) that work independently of Mayari core.

Installation ​

bash
lpm add mayari/common
lpm i

Modules ​

Exception ​

A set of constructor functions for standard HTTP exceptions. Each returns a { statusCode, message } payload that Mayari's error handling pipeline understands.

lua
local common = require("common")

local Exception = common.exception

-- Generic
Exception.HttpException(418, "I'm a teapot")

-- 4xx
Exception.BadRequest()
Exception.Unauthorized("Token expired")
Exception.Forbidden()
Exception.NotFound("User not found")
Exception.Conflict()
Exception.TooManyRequests()

-- 5xx
Exception.InternalServerError()
Exception.BadGateway()
Exception.ServiceUnavailable()

All message arguments are optional and fall back to the standard HTTP reason phrase.


Response ​

Helper functions for constructing ResponsePayload values. Each function sets a sensible default Content-Type and status code (200) while allowing full overrides through an optional config.

lua
local common = require("common")

local Response = common.response

-- Plain text
Response.send("Hello, world!")

-- JSON (body is encoded automatically)
Response.json({ ok = true, name = "John Doe" })

-- HTML, CSS, JS
Response.html("<h1>Hello</h1>")
Response.css("body { margin: 0; }")
Response.js("console.log('hi')")

-- With config overrides
Response.json({ error = "Not found" }, { status = 404 })
Response.send("Created", {
    status = 201,
    headers = { ["X-Request-Id"] = id }
})