Skip to content

ShokupanContext

Defined in: src/context.ts:108

Shokupan Request Context

The context object passed to all middleware and route handlers. Provides access to request data, response helpers, and typed state management.

app.get('/hello', (ctx) => {
return ctx.json({ message: 'Hello' });
});
interface AppState {
userId: string;
requestId: string;
}
const app = new Shokupan<AppState>();
app.use((ctx, next) => {
ctx.state.requestId = crypto.randomUUID(); // ✓ Type-safe
return next();
});
app.get('/users/:userId/posts/:postId', (ctx) => {
// ctx.params is automatically typed as { userId: string; postId: string }
const { userId, postId } = ctx.params;
return ctx.json({ userId, postId });
});
interface RequestState {
userId: string;
permissions: string[];
}
const app = new Shokupan<RequestState>();
app.get('/admin/users/:userId', (ctx) => {
// Both typed!
const { userId } = ctx.params; // ✓ From path
const { permissions } = ctx.state; // ✓ From state
if (!permissions.includes('admin')) {
return ctx.json({ error: 'Forbidden' }, 403);
}
return ctx.json({ userId });
});

State extends Record<string, any> = Record<string, any>

The shape of ctx.state for type-safe state access across middleware.

Params extends Record<string, string> = Record<string, string>

The shape of ctx.params based on the route path pattern.

new ShokupanContext<State, Params>(request, server?, state?, app?, signal?, enableMiddlewareTracking?): ShokupanContext<State, Params>

Defined in: src/context.ts:146

any

Server

State

Shokupan

AbortSignal

boolean = false

ShokupanContext<State, Params>

optional _bodyParseError: Error

Defined in: src/context.ts:126


optional _debug: DebugCollector

Defined in: src/context.ts:117


optional _finalResponse: Response

Defined in: src/context.ts:118


optional _rawBody: string | ArrayBuffer | Uint8Array<ArrayBufferLike>

Defined in: src/context.ts:119


_routeMatched: boolean = false

Defined in: src/context.ts:128


readonly optional app: Shokupan

Defined in: src/context.ts:150


handlerStack: HandlerStackItem[] = []

Defined in: src/context.ts:114


params: Params

Defined in: src/context.ts:112


readonly request: any

Defined in: src/context.ts:147


readonly response: ShokupanResponse

Defined in: src/context.ts:116


readonly optional server: Server

Defined in: src/context.ts:148


session: SessionData & object

Defined in: src/plugins/middleware/session.ts:294

id: string

destroy(callback): void

(err) => void

void

regenerate(callback): void

(err) => void

void

reload(callback): void

(err) => void

void

save(callback): void

(err) => void

void

touch(): void

void


sessionID: string

Defined in: src/plugins/middleware/session.ts:295


sessionStore: Store

Defined in: src/plugins/middleware/session.ts:296


readonly optional signal: AbortSignal

Defined in: src/context.ts:151


state: State

Defined in: src/context.ts:113

get headers(): any

Defined in: src/context.ts:299

Request headers

any


get host(): string

Defined in: src/context.ts:273

Request host (e.g. “localhost:3000”)

string


get hostname(): string

Defined in: src/context.ts:266

Request hostname (e.g. “localhost”)

string


get ip(): SocketAddress

Defined in: src/context.ts:261

Client IP address

SocketAddress


get method(): any

Defined in: src/context.ts:188

HTTP method

any


get origin(): string

Defined in: src/context.ts:292

Request origin (e.g. “http://localhost:3000”)

string


get path(): any

Defined in: src/context.ts:192

Request path

any


get protocol(): string

Defined in: src/context.ts:280

Request protocol (e.g. “http:”, “https:“)

string


get query(): Record<string, any>

Defined in: src/context.ts:227

Request query params

Record<string, any>


get req(): any

Defined in: src/context.ts:184

Base request

any


get res(): ShokupanResponse

Defined in: src/context.ts:310

Base response object

ShokupanResponse


get secure(): boolean

Defined in: src/context.ts:287

Whether request is secure (https)

boolean


get url(): URL

Defined in: src/context.ts:172

URL

body<T>(): Promise<T>

Defined in: src/context.ts:406

Read request body with caching to avoid double parsing. The body is only parsed once and cached for subsequent reads.

T = any

Promise<T>


file(path, fileOptions?, responseOptions?): Promise<Response>

Defined in: src/context.ts:663

Respond with a file

string

BlobPropertyBag

ResponseInit

Promise<Response>


get(name): any

Defined in: src/context.ts:305

Get a request header

string

Header name

any


html(html, status?, headers?): Response

Defined in: src/context.ts:612

Respond with HTML content

string

number

HeadersInit

Response


json(data, status?, headers?): Response

Defined in: src/context.ts:551

Respond with a JSON object

any

number

HeadersInit

Response


jsx(element, args?, status?, headers?): Promise<Response>

Defined in: src/context.ts:695

Render a JSX element

any

JSX Element

unknown

number

HTTP Status

HeadersInit

HTTP Headers

Promise<Response>


parseBody(): Promise<void>

Defined in: src/context.ts:463

Pre-parse the request body before handler execution. This improves performance and enables Node.js compatibility for large payloads. Errors are deferred until the body is actually accessed in the handler.

Promise<void>


redirect(url, status): Response

Defined in: src/context.ts:633

Respond with a redirect

string

number = 302

Response


send(body?, options?): Response

Defined in: src/context.ts:530

Send a response

BodyInit

Response body

ResponseInit

Response options

Response

Response


set(key, value): ShokupanContext<State, Params>

Defined in: src/context.ts:317

Helper to set a header on the response

string

Header key

string

Header value

ShokupanContext<State, Params>


setCookie(name, value, options): ShokupanContext<State, Params>

Defined in: src/context.ts:328

Set a cookie

string

Cookie name

string

Cookie value

CookieOptions = {}

Cookie options

ShokupanContext<State, Params>


setRenderer(renderer): void

Defined in: src/context.ts:142

JSXRenderer

void


status(status): Response

Defined in: src/context.ts:649

Respond with a status code DOES NOT CHAIN!

number

Response


text(data, status?, headers?): Response

Defined in: src/context.ts:582

Respond with a text string

string

number

HeadersInit

Response