Context
Every handler and middleware in GamanJS receives a ctx (Context) object containing all request information and utilities for accessing data.
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 Res.send({ id });});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 Res.send(body, 201);}ctx.text()
Section titled “ctx.text()”Read body as plain text:
async Webhook(ctx) { const raw = await ctx.text(); return Res.message('OK');}ctx.formData()
Section titled “ctx.formData()”Parse body as FormData (multipart or URL-encoded):
async Upload(ctx) { const form = await ctx.formData(); const name = form.get('name'); // ...}Form Shortcuts
Section titled “Form Shortcuts”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 | null
if (avatar) { avatar.name; // original file name avatar.type; // MIME type avatar.size; // size in bytes // ...}ctx.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 header value (case-insensitive):
const token = ctx.header('Authorization'); // string | nullconst contentType = ctx.header('Content-Type');ctx.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 cookies:
const session = ctx.cookies.get('session_id');Data Store
Section titled “Data Store”Middleware and handlers can share data via the context store:
ctx.set(key, value)
Section titled “ctx.set(key, value)”ctx.set('user', { id: 1, name: 'Angga' });ctx.get<T>(key)
Section titled “ctx.get<T>(key)”const user = ctx.get<{ id: number; name: string }>('user');ctx.has(key)
Section titled “ctx.has(key)”if (ctx.has('user')) { // user was set by middleware}ctx.delete(key)
Section titled “ctx.delete(key)”ctx.delete('tempData');Full Example
Section titled “Full Example”export default composeController(() => ({ 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; content: string; }>();
// Headers const token = ctx.header('Authorization');
// Data from middleware const currentUser = ctx.get('user');
return Res.send({ userId, draft, title: body.title, author: currentUser.name, }, 201); },}));