---
url: /nestjs-odata/api/generated/functions/rewriteCanonicalKeyUrl.md
---
# Function: rewriteCanonicalKeyUrl()

> **rewriteCanonicalKeyUrl**(`url`, `serviceRoot`, `isEntitySet`): `string` | `undefined`

Defined in: [utils/canonical-key-url.ts:15](https://github.com/cberd1509/nestjs-odata/blob/a42974292d62fe5da337a603d3b22d57c08cbf68/packages/core/src/utils/canonical-key-url.ts#L15)

Map the canonical URL of an entity, `{serviceRoot}/{EntitySet}({key})` (OData-URL 4.01
§4.3.1), to the key-as-segment form `{serviceRoot}/{EntitySet}/{key}` that the OData route
decorators (`@ODataGetByKey`, `@ODataPatch`, `@ODataPut`, `@ODataDelete`) are registered on.

Nest joins a controller path and a method path with `/`, so a route cannot be declared as
`Products(:key)`; `ODataModule` rewrites the request URL before routing instead. The key text
is kept as sent (still percent-encoded), so `@Param('key')` receives the same value for both
forms: `1`, `'abc'`, `orderId=1,itemId=2`. Path segments after the key and the query string
are kept.

Only the segment right after the service root is rewritten, and only when its name is an
entity set (`isEntitySet`). Returns `undefined` when the URL is not such a canonical URL.

## Parameters

### url

`string`

### serviceRoot

`string`

### isEntitySet

(`name`) => `boolean`

## Returns

`string` | `undefined`
