Skip to content

RouteParamsFactory

reference
1 min readUpdated

Kind: Class

Source: packages/core/router/route-params-factory.ts

Part of: Core

RouteParamsFactory resolves a configured route-parameter key into its corresponding typed value. It centralizes route parameter exchange logic so router consumers can safely retrieve values while handling missing parameters through a null result.

Implements: IRouteParamsFactory

Methods

MethodSignatureReturns
exchangeKeyForValue`exchangeKeyForValue(key: RouteParamtypesstring, data: string, { req, res, next }: { req: TRequest; res: TResponse; next: Function })`

Diagram

mermaid
graph LR
  A[Route definition / parameter mapping] --> B[RouteParamsFactory]
  C[Current route parameters] --> B
  B --> D[exchangeKeyForValue<TResult>()]
  D --> E[Typed parameter value]
  D --> F[null when no matching value exists]

Usage

ts
import { RouteParamsFactory } from "@your-package/core/router";

function loadUser(routeParamsFactory: RouteParamsFactory) {
  const userId = routeParamsFactory.exchangeKeyForValue<string>();

  if (userId === null) {
    throw new Error("A user route parameter is required.");
  }

  return fetch(`/api/users/${encodeURIComponent(userId)}`);
}

AI Coding Instructions

  • Treat exchangeKeyForValue() as a nullable lookup; always handle the null case before using the returned value.
  • Provide the expected result type through the generic parameter when the route value needs to be treated as a specific type.
  • Keep route-key mapping and parameter exchange logic inside RouteParamsFactory rather than duplicating lookup behavior in route handlers.
  • Ensure the route definition and the factory’s configured parameter mapping stay aligned when renaming route parameters.

How it works

RouteParamsFactory

RouteParamsFactory is a class implementing IRouteParamsFactory; its exchangeKeyForValue method maps a route-parameter type and optional data key to a value from { req, res, next }. route-params-factory.ts:4-13 The router constructs one instance and passes it to RouterExecutionContext; that context creates extraction functions which call this method with the request, response, and next callback. router-explorer.ts:78-98 router-execution-context.ts:315-326

Relationships

  • IMPORTS → RouteParamtypes

Was this page helpful?

Download as PDF
RouteParamsFactory — NestJS head-to-head