# RouteParamsFactory

**Kind:** Class

**Source:** [`packages/core/router/route-params-factory.ts`](https://github.com/nestjs/nest/blob/master/packages/core/router/route-params-factory.ts#L4)

**Part of:** [Core](subsystem-packages-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

| Method | Signature | Returns |
|---|---|---|
| `exchangeKeyForValue` | `exchangeKeyForValue(key: RouteParamtypes | string, data: string, { req, res, next }: { req: TRequest; res: TResponse; next: Function })` | `TResult | null` |

## 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](packages/core/router/route-params-factory.ts#L4-L13) 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](packages/core/router/router-explorer.ts#L78-L98) [router-execution-context.ts:315-326](packages/core/router/router-execution-context.ts#L315-L326)

## Relationships

- IMPORTS → `RouteParamtypes`
