API Reference

JopiRequest

Documentation for the JopiRequest class.

JopiRequest

The JopiRequest class encapsulates an incoming HTTP request in JopiJS. It provides a rich set of utilities for extracting data, managing authentication, interacting with the cache, and generating responses.

Instances of JopiRequest are passed to all route handlers (onGET, onPOST, etc.) and page components.

Data Extraction

req_getData(options)

Aggregates data from all sources: URL path parameters, Query String, and the Request Body (JSON, Form Data, etc.).

  • options.ignoreUrl: If true, ignores URL parameters.
  • options.dataSchema: Optional schema to validate the returned data.

req_getBodyData(options)

Parses and returns only the request body data. Automatically handles JSON, URL-Encoded, and Multipart Form Data.

req_urlInfos

Returns the parsed URL object for the current request.

req_urlParts

Contains the dynamic parts of the URL path derived from the route definition (e.g., { id: "123" } for route /post/[id]).

req_urlSearchParams

Returns the URL search parameters as a plain object.

req_callerIP

Returns information about the caller's IP address.

Body Helpers

  • req_bodyAsJson(): Parses the body as JSON.
  • req_bodyAsText(): Reads the body as text.
  • req_bodyAsFormData(): Parses the body as FormData.

Response Helpers

JopiRequest provides helper methods to create standard responses easily.

  • res_jsonResponse(json, statusCode): Returns a JSON response.
  • res_htmlResponse(html, statusCode): Returns an HTML response.
  • res_textResponse(text, statusCode): Returns a plain text response.
  • res_redirect(url, permanent): Creates a redirect response.
  • res_returnResultMessage(isOk, message): Returns a standardized JSON result object { isOk, message }.

Error Responses

  • res_returnError404_NotFound(): Triggers the standard 404 handler.
  • res_returnError500_ServerError(error): Triggers the standard 500 handler.
  • res_returnError401_Unauthorized(error): Triggers the standard 401 handler.

Cache Control

cache_getFromCache()

Manually retrieves the cache entry for the current URL.

cache_addToCache(response)

Manually adds a response to the cache for the current request.

cache_removeFromCache(url?)

Invalidates the cache entry for a specific URL or the current one.

cache_ignoreDefaultBehaviors()

Disables the default automatic caching behavior for the request.

cache_ignoreCacheRead() / cache_ignoreCacheWrite()

Forces the request to bypass reading from or writing to the cache.

Proxying

  • proxy_fetchServer(headers, method, url, body): Performs a fetch request using the website's load balancer logic.
  • proxy_directProxyToServer(): Proxies the current request directly to the backend server defined in the load balancer.