Skip to content

Commit 14f9416

Browse files
SharkEzzGabrola
andauthored
refactor(nest): allow nestjs handlers to return a promise or a value (#492)
Co-authored-by: Youssef Gaber <1728215+Gabrola@users.noreply.github.com>
1 parent b3fde9b commit 14f9416

4 files changed

Lines changed: 12 additions & 3 deletions

File tree

.changeset/polite-kiwis-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/nest': patch
3+
---
4+
5+
Allow Nest.js handlers to return non-promise responses

libs/ts-rest/core/src/lib/type-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,5 @@ export type Not<B extends boolean> = {
176176
false: true;
177177
true: false;
178178
}[`${B}`];
179+
180+
export type Promisable<T> = Promise<T> | T;

libs/ts-rest/nest/src/lib/ts-rest-nest-handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
isAppRouteOtherResponse,
3131
parseJsonQueryObject,
3232
ServerInferResponses,
33+
Promisable,
3334
} from '@ts-rest/core';
3435
import {
3536
JsonQuerySymbol,
@@ -145,12 +146,12 @@ export const TsRestHandler = (
145146

146147
type NestHandlerImplementation<T extends AppRouter | AppRoute> =
147148
T extends AppRoute
148-
? (args: TsRestRequestShape<T>) => Promise<ServerInferResponses<T>>
149+
? (args: TsRestRequestShape<T>) => Promisable<ServerInferResponses<T>>
149150
: {
150151
[K in keyof T]: T[K] extends AppRoute
151152
? (
152153
args: TsRestRequestShape<T[K]>,
153-
) => Promise<ServerInferResponses<T[K]>>
154+
) => Promisable<ServerInferResponses<T[K]>>
154155
: never;
155156
};
156157

libs/ts-rest/nest/src/lib/ts-rest-nest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import {
33
AppRouter,
44
Without,
55
ServerInferResponses,
6+
Promisable,
67
} from '@ts-rest/core';
78
import { TsRestRequestShape } from './ts-rest-request.decorator';
89

910
type AppRouterMethodShape<T extends AppRoute> = (
1011
...args: any[]
11-
) => Promise<ServerInferResponses<T>>;
12+
) => Promisable<ServerInferResponses<T>>;
1213

1314
type AppRouterControllerShape<T extends AppRouter> = Without<
1415
{

0 commit comments

Comments
 (0)