You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are a number of configuration options that you can use to customize the behavior of `ts-rest` in your Nest application.
4
+
5
+
## Configuration Options
6
+
7
+
### jsonQuery
8
+
9
+
#### Default value: `false`
10
+
11
+
By default, all query parameters are encoded as strings, however, you can use the `jsonQuery` option to encode query parameters as typed JSON values.
12
+
13
+
### validateResponses
14
+
15
+
#### Default value: `false`
16
+
17
+
You can enable response parsing and validation for defined response status codes, if there are corresponding response Zod schema defined in the contract.
18
+
This is useful for ensuring absolute safety that your controller is returning the correct response types as well as stripping any extra properties.
19
+
20
+
If validation fails a `ResponseValidationError` will be thrown causing a 500 response to be returned.
21
+
You can catch this error and handle it as you wish by using a [NestJS exception filter](https://docs.nestjs.com/exception-filters).
22
+
23
+
### Request Validation
24
+
25
+
By default, `ts-rest` validates all request components - body, headers and query parameters.
26
+
In case of validation errors, the server responds with a `ZodError` object in the response body and a status code of 400.
27
+
You can disable the validation of these components if you wish to perform the validation manually or handle the error differently.
Configuring the different configuration options for each controller might get a bit cumbersome.
64
+
To make it easier, you can set global options that will be used by all controllers in your Nest module, or globally across your application if you specify `isGlobal`.
Copy file name to clipboardExpand all lines: apps/docs/docs/nest/legacy.mdx
+8-29Lines changed: 8 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,13 +14,17 @@ import { InstallTabs } from '@site/src/components/InstallTabs';
14
14
}}
15
15
/>
16
16
17
+
:::info
18
+
17
19
As part of the [v3.26.0](https://github.com/ts-rest/ts-rest) we released the new `SingleHandler` and `MultiHandler` approaches to implementing your API contracts.
18
20
19
21
We don't have any immediate plans to deprecate the old approach, but we do recommend using the new approach as it is more flexible and allows for more control over your API implementation.
20
22
21
23
If we do make a v4 release, we will likely deprecate the old approach.
22
24
23
-
::: ## Installation
25
+
:::
26
+
27
+
## Installation
24
28
25
29
<InstallTabspackageName="@ts-rest/nest" />
26
30
@@ -71,19 +75,10 @@ As Typescript cannot infer class method parameter types from an implemented inte
71
75
72
76
## Configuration
73
77
74
-
To configure certain ts-rest options you can use the `@TsRest` decorator on either your Controller or use the existing `@Api` decorator.
78
+
To configure `ts-rest` options you can use the `@TsRest` decorator on either your controller or use the existing `@TsRest` decorator on your method.
79
+
Controller options will be applied to all routes in the controller and will override any global options, and method options will override the controller options for that specific route.
75
80
76
-
### JSON Query Parameters
77
-
78
-
To handle JSON query parameters, pass the `jsonQuery` option to the `@TsRest` decorator on your entire controller.
You can also use the method decorator to override or configure options for a specific route.
81
+
Check out the [configuration](/docs/nest/configuration/) section for more details on the different configuration options, or on how to configure options globally.
If validation fails a `ResponseValidationError` will be thrown causing a 500 response to be returned.
114
-
You can catch this error and handle it as you wish by using a [NestJS exception filter](https://docs.nestjs.com/exception-filters).
115
-
116
96
## Explicit Response Type Safety
117
97
118
98
In cases where you do not want to implement `NestControllerInterface`.
119
99
Say, if you were to implement a contract's non-nested routes across multiple controllers, or use different class method names than the ones defined in your contract, you can still ensure type safety of the responses by using the `NestResponseShapes` type.
Copy file name to clipboardExpand all lines: apps/docs/docs/nest/nest.mdx
+5-115Lines changed: 5 additions & 115 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,125 +167,18 @@ This can be somewhat mitigated if you share the error code schema between multip
167
167
168
168
## Configuration
169
169
170
-
To configure certain ts-rest options you can use the `@TsRest` decorator on either your Controller or use the existing `@Api` decorator.
170
+
To configure `ts-rest` options you can use the `@TsRest` decorator on either your controller or use the existing `@TsRestHandler` decorator on your method.
171
+
Controller options will be applied to all routes in the controller and will override any global options, and method options will override the controller options for that specific route.
171
172
172
-
### JSON Query Parameters
173
-
174
-
To handle JSON query parameters, pass the `jsonQuery` option to the `@TsRest` decorator on your entire controller.
175
-
176
-
```typescript
177
-
import { TsRest } from'@ts-rest/nest';
178
-
179
-
@Controller()
180
-
@TsRest({ jsonQuery: true })
181
-
exportclassMyController {}
182
-
```
183
-
184
-
You can also use the method decorator to override or configure options for a specific route.
173
+
Check out the [configuration](/docs/nest/configuration/) section for more details on the different configuration options, or on how to configure options globally.
0 commit comments