Context
Every handler and middleware in GamanJS receives a ctx (Context) object containing all request information and utilities for accessing data and sending responses.
Core Properties
Section titled “Core Properties”ctx.path
Section titled “ctx.path”URL pathname (without query string):
// Request: GET /users/123?active=truectx.path; // '/users/123'ctx.url
Section titled “ctx.url”Full URL instance:
ctx.url.origin; // 'http://localhost:3431'ctx.url.pathname; // '/users/123'ctx.url.search; // '?active=true'ctx.request
Section titled “ctx.request”Raw request information:
ctx.request.id; // Unique request ID (auto-generated)ctx.request.method; // 'GET', 'POST', etcctx.request.url; // Full URL as stringctx.request.pathname; // Pathnamectx.request.body(); // Promise<Buffer> — raw bodyRoute Parameters
Section titled “Route Parameters”ctx.param(name)
Section titled “ctx.param(name)”Get a single route parameter:
// Route: /users/:idr.get('/users/:id', (ctx) => { const id = ctx.param('id'); // '123' return ctx.send({ id }).ok();});ctx.params
Section titled “ctx.params”All route parameters as an object:
// Route: /posts/:postId/comments/:commentIdctx.params; // { postId: '1', commentId: '42' }Query Parameters
Section titled “Query Parameters”ctx.query
Section titled “ctx.query”Access query parameters directly:
// Request: GET /search?q=gaman&page=2ctx.query.q; // 'gaman'ctx.query.page; // '2'For parameters with multiple values:
// Request: GET /filter?tag=js&tag=tsctx.query.tag; // ['js', 'ts']Request Body
Section titled “Request Body”ctx.json<T>()
Section titled “ctx.json<T>()”Parse body as JSON:
async Create(ctx) { const body = await ctx.json<{ name: string; email: string }>(); // body.name, body.email return ctx.send(body).created();}ctx.text()
Section titled “ctx.text()”Read body as plain text:
async Webhook(ctx) { const raw = await ctx.text(); return ctx.send({ message: 'OK' }).ok();}Form Data & Files
Section titled “Form Data & Files”ctx.formData()
Section titled “ctx.formData()”Parse body as FormData (supports multipart/form-data and application/x-www-form-urlencoded).
ctx.input(name)
Section titled “ctx.input(name)”Get a single string value from form data:
const email = await ctx.input('email'); // string | nullctx.inputs(name)
Section titled “ctx.inputs(name)”Get multiple string values from form data:
const tags = await ctx.inputs('tags'); // string[]ctx.file(name)
Section titled “ctx.file(name)”Get a single file from form data:
const avatar = await ctx.file('avatar'); // GFile | nullctx.files(name)
Section titled “ctx.files(name)”Get multiple files from form data:
const images = await ctx.files('images'); // GFile[]Headers
Section titled “Headers”ctx.header(key)
Section titled “ctx.header(key)”Get a specific request header value (case-insensitive):
const token = ctx.header('Authorization'); // string | nullctx.headers
Section titled “ctx.headers”GamanHeader instance for reading and manipulating headers:
// Readctx.headers.get('Content-Type');
// Set response headerctx.headers.set('X-Custom-Header', 'value');Cookies
Section titled “Cookies”ctx.cookies
Section titled “ctx.cookies”Bun’s CookieMap instance for accessing and setting cookies:
// Readconst session = ctx.cookies.get('session_id');
// Setctx.cookies.set('session_id', 'abc-123', { httpOnly: true });Data Store
Section titled “Data Store”Middleware and handlers can share data via the context store:
ctx.set(key, value) / ctx.get<T>(key)
Section titled “ctx.set(key, value) / ctx.get<T>(key)”ctx.set('user', { id: 1, name: 'Angga' });const user = ctx.get('user');Response
Section titled “Response”ctx.send(data?, status?)
Section titled “ctx.send(data?, status?)”The primary way to send responses. Returns a GamanResponseBuilder.
return ctx.send({ success: true }).ok();Full Example
Section titled “Full Example”export default (userService) => ({ async CreatePost(ctx) { // Route params const userId = ctx.param('userId');
// Query const draft = ctx.query.draft === 'true';
// Body const body = await ctx.json<{ title: string }>();
// Headers const token = ctx.header('Authorization');
// Set response header ctx.headers.set('X-Processed-By', 'GamanJS');
// Data from middleware const currentUser = ctx.get('user');
return ctx.send({ userId, draft, title: body.title, author: currentUser.name, }).created(); },});