Skip to main content

Context Property Reference

This page lists the complete property definitions of the context object for reference when developing middleware.

Top-Level Properties

PropertyDescription
context.requestHTTP request parameters (URL, method, headers, body, etc.)
context.response[Readonly] HTTP response proxy object, supports multiple reads of the response body
context.resRaw Fetch Response object
context.optionsCustom options set via .option()
context.output[Writeonly] Overrides the final resolved value of await request.get(...), only effective in default parsing mode
context.dataRequest-level shared data, automatically cleaned up after the request completes
context.globalGlobal shared data, not destroyed when the request ends
context.orchestration[Readonly] See Advanced - Middleware Orchestration
context.locationId[Readonly] Location identifier of the request code (file path + line number)

context.request

Contains all parameters needed to send an HTTP request:

PropertyDescription
context.request.urlRequest URL object
context.request.__url__Readonly Complete request URL with route parameters merged
context.request.methodHTTP method ('get', 'post', 'put', 'patch', 'delete', 'head', 'options')
context.request.headersRequest headers (Headers object)
context.request.bodyRequest body
context.request.pathParametersRoute parameters object
context.request.abort()Abort the current request
context.request.credentialsFetch API credentials option
context.request.modeFetch API mode option
context.request.cacheFetch API cache option
context.request.redirectFetch API redirect option
context.request.referrerFetch API referrer option
context.request.referrerPolicyFetch API referrerPolicy option
context.request.integrityFetch API integrity option
context.request.keepaliveFetch API keepalive option

Route Parameters

Use context.request.pathParameters and context.request.__url__ to handle route parameters:

import { request } from "keq"

request.use(async (context, next) => {
  console.log("Raw URL:", context.request.url.href)
  console.log("Route params:", context.request.pathParameters)
  console.log("Actual URL:", context.request.__url__.href)

  await next()
})

await request.get("/users/{id}").params("id", "123")
// Raw URL: /users/{id}
// Route params: { id: "123" }
// Actual URL: /users/123

context.response

context.response is a proxy of the raw Response object, solving the limitation that Response methods (like .json(), .text()) can only be called once, allowing multiple middleware to safely read the response body.

context.res

context.res is the raw Fetch Response object. In most cases you should use context.response — only use context.res when you need access to the raw Response object.

context.output

[Writeonly] Allows middleware to override the final resolved value of await request.get(...).

By default, Keq automatically parses the response body based on the response's Content-Type (intelligent mode). After setting context.output, automatic parsing is skipped and the caller receives this value directly.

Only effective when resolveWith is not set or is 'intelligent'. If the caller used .resolveWith('json') or other specified modes, context.output will be ignored.

context.options

Custom options set via the .option() method.

Built-in options and their defaults:

OptionDefaultDescription
context.options.fetchAPIglobal.fetchFetch API implementation used to send requests
context.options.resolveWith"intelligent"Response body parsing method
context.options.retry.timesundefinedRetry count
context.options.retry.delayundefinedRetry delay (ms)
context.options.retry.onundefinedCustom retry condition function
context.options.timeoutundefinedTimeout configuration object ({ millisecond: number })
context.options.flowControlundefinedFlow control mode

context.data

Request-level shared data object for sharing data between multiple middleware within the same request. Automatically destroyed after the request completes.

context.global

Global shared data that is not destroyed when the request ends. Requires special attention to memory management.

import { KeqMiddleware } from "keq"

const key = Symbol('myMiddleware')

const middleware: KeqMiddleware = async (context, next) => {
  context.global[key] = { startTime: Date.now() }

  try {
    await next()
  } finally {
    delete context.global[key]
  }
}

context.orchestration

Middleware orchestrator providing middleware execution state and fork capabilities. See Advanced - Middleware Orchestration.