Nex.Req (nex_core v0.4.3)

Copy Markdown

Standardized Request object for API handlers.

Normalizes incoming requests toward Next.js Pages Router API Route semantics while keeping Nex-specific additions available for framework internals.

Next.js Standard Fields

  • query - Path params + query string params (Next.js req.query)
  • body - Request body params (Next.js req.body)
  • headers - Request headers as a Map
  • cookies - Request cookies as a Map
  • method - HTTP method (uppercase string)

Parameter Merging Behavior

Like Next.js, req.query contains both dynamic path parameters and query string parameters. When the same key appears in both, path parameters take precedence.

For example, GET /api/users/123?id=456:

  • req.query["id"]"123" (path parameter wins)

req.body is completely independent and never merged with req.query.

Examples

# GET /api/users/[id]?page=2
def get(req) do
  user_id = req.query["id"]    # From path parameter [id]
  page = req.query["page"]     # From query string

  Nex.json(%{user_id: user_id, page: page})
end

# POST /api/users
def post(req) do
  name = req.body["name"]
  email = req.body["email"]

  Nex.json(%{message: "User created"}, status: 201)
end

Comparison with Next.js

Next.jsNexNotes
req.queryreq.querySame merged path + query semantics
req.bodyreq.bodynil when the request has no body params
req.headersreq.headersSame access pattern
req.cookiesreq.cookiesSame access pattern
req.methodreq.methodSame uppercase method string

Summary

Functions

Constructs a Nex.Req from a Plug.Conn.

Types

cookies()

@type cookies() :: %{optional(String.t()) => String.t()}

headers()

@type headers() :: %{optional(String.t()) => String.t()}

method()

@type method() :: :get | :post | :put | :patch | :delete | :head | :options

params()

@type params() :: %{optional(String.t()) => term()}

t()

@type t() :: %Nex.Req{
  body:
    params()
    | %Plug.Upload{content_type: term(), filename: term(), path: term()}
    | nil,
  cookies: cookies(),
  headers: headers(),
  method: String.t(),
  path: String.t(),
  private: map(),
  query: params()
}

Functions

from_plug_conn(conn, path_params \\ %{})

@spec from_plug_conn(Plug.Conn.t(), params()) :: t()

Constructs a Nex.Req from a Plug.Conn.

This function normalizes the Plug.Conn into a Next.js-compatible request object.