Skip to content

Bind request data to method arguments ​

@Args describes a method's inputs in parameter order. For PUT /books/42?notify=yes, the following handler receives "42", "yes" and the parsed request body. A final optional context parameter still gives access to request-local state.

ts
@Put(":id")
@Args(Param("id"), Query("notify"), Body())
update(id: string, notify: string | undefined, body: unknown, ctx: ChoWebContext) {
  return { id, notify, body, path: ctx.url.pathname };
}

Choose a reader ​

ReaderWhole sourceNamed value
ParamDecoded path parametersString. CHO checks the name against the full path during creation
QueryQuery keys mapped to first valuesFirst value or undefined
HeaderRequest headers with lowercase namesCase-insensitive lookup or undefined
BodyParsed JSON, or undefined for empty inputObject property or undefined
FormURL-encoded fields mapped to first valuesFirst field value or undefined

Call a reader with no arguments for the whole source. Pass a string key for one field. To transform the result, pass a validator as the last argument and enable validation middleware.

Work with repeated values and bodies ​

Use ctx.url.searchParams.getAll("tag") for every query value. For multipart forms, use await ctx.req.formData(). Form parses URL-encoded text. The chosen body reader determines how CHO interprets the body, independently of the request's content type.

Multiple Body arguments can use the same cached JSON parse. Body and Form share the underlying text read and cache their own parsed representations. Choose the reader that matches the input your endpoint accepts. Calling req.json() yourself after Body() has already consumed the stream also fails.

Define a custom reader ​

ts
import { Args, Controller, Get, type ChoWebContext } from "@chojs/web";

const tags = (ctx: ChoWebContext) => ctx.url.searchParams.getAll("tag");

@Controller()
class Search {
  @Get("search")
  @Args(tags)
  search(values: string[]) { return { tags: values }; }
}

Readers may return promises. CHO evaluates them sequentially after route middleware calls through. An earlier failure prevents later reads and skips the handler. TypeScript checks the declared input types. Two parameters with the same type can still be accidentally transposed.

Released under the MIT License.