Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only return Observable types for NestJS if the option is switched on #823

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration/nestjs-metadata-grpc-js/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export interface HeroServiceClient {
}

export interface HeroServiceController {
findOneHero(request: HeroById, metadata?: Metadata): Promise<Hero> | Observable<Hero> | Hero;
findOneHero(request: HeroById, metadata?: Metadata): Promise<Hero> | Hero;

findOneVillain(request: VillainById, metadata?: Metadata): Promise<Villain> | Observable<Villain> | Villain;
findOneVillain(request: VillainById, metadata?: Metadata): Promise<Villain> | Villain;

findManyVillain(request: Observable<VillainById>, metadata?: Metadata): Observable<Villain>;
}
Expand Down
4 changes: 2 additions & 2 deletions integration/nestjs-metadata-restparameters/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export interface HeroServiceClient {
}

export interface HeroServiceController {
findOneHero(request: HeroById, metadata: Metadata, ...rest: any): Promise<Hero> | Observable<Hero> | Hero;
findOneHero(request: HeroById, metadata: Metadata, ...rest: any): Promise<Hero> | Hero;

findOneVillain(
request: VillainById,
metadata: Metadata,
...rest: any
): Promise<Villain> | Observable<Villain> | Villain;
): Promise<Villain> | Villain;

findManyVillain(request: Observable<VillainById>, metadata: Metadata, ...rest: any): Observable<Villain>;
}
Expand Down
4 changes: 2 additions & 2 deletions integration/nestjs-metadata/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export interface HeroServiceClient {
}

export interface HeroServiceController {
findOneHero(request: HeroById, metadata?: Metadata): Promise<Hero> | Observable<Hero> | Hero;
findOneHero(request: HeroById, metadata?: Metadata): Promise<Hero> | Hero;

findOneVillain(request: VillainById, metadata?: Metadata): Promise<Villain> | Observable<Villain> | Villain;
findOneVillain(request: VillainById, metadata?: Metadata): Promise<Villain> | Villain;

findManyVillain(request: Observable<VillainById>, metadata?: Metadata): Observable<Villain>;
}
Expand Down
4 changes: 2 additions & 2 deletions integration/nestjs-restparameters/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export interface HeroServiceClient {
}

export interface HeroServiceController {
findOneHero(request: HeroById, ...rest: any): Promise<Hero> | Observable<Hero> | Hero;
findOneHero(request: HeroById, ...rest: any): Promise<Hero> | Hero;

findOneVillain(request: VillainById, ...rest: any): Promise<Villain> | Observable<Villain> | Villain;
findOneVillain(request: VillainById, ...rest: any): Promise<Villain> | Villain;

findManyVillain(request: Observable<VillainById>, ...rest: any): Observable<Villain>;
}
Expand Down
2 changes: 1 addition & 1 deletion integration/nestjs-simple-restparameters/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface HeroServiceClient {
}

export interface HeroServiceController {
findCurrentUser(request: Empty, ...rest: any): Promise<User> | Observable<User> | User;
findCurrentUser(request: Empty, ...rest: any): Promise<User> | User;
}

export function HeroServiceControllerMethods() {
Expand Down
6 changes: 3 additions & 3 deletions integration/nestjs-simple-usedate/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ export interface HeroServiceClient {
export interface HeroServiceController {
addOneHero(request: Hero): void;

findOneHero(request: HeroById): Promise<Hero> | Observable<Hero> | Hero;
findOneHero(request: HeroById): Promise<Hero> | Hero;

findOneVillain(request: VillainById): Promise<Villain> | Observable<Villain> | Villain;
findOneVillain(request: VillainById): Promise<Villain> | Villain;

findManyVillain(request: Observable<VillainById>): Observable<Villain>;

findManyVillainStreamIn(request: Observable<VillainById>): Promise<Villain> | Observable<Villain> | Villain;
findManyVillainStreamIn(request: Observable<VillainById>): Promise<Villain> | Villain;

findManyVillainStreamOut(request: VillainById): Observable<Villain>;
}
Expand Down
6 changes: 3 additions & 3 deletions integration/nestjs-simple/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ export interface HeroServiceClient {
export interface HeroServiceController {
addOneHero(request: Hero): void;

findOneHero(request: HeroById): Promise<Hero> | Observable<Hero> | Hero;
findOneHero(request: HeroById): Promise<Hero> | Hero;

findOneVillain(request: VillainById): Promise<Villain> | Observable<Villain> | Villain;
findOneVillain(request: VillainById): Promise<Villain> | Villain;

findManyVillain(request: Observable<VillainById>): Observable<Villain>;

findManyVillainStreamIn(request: Observable<VillainById>): Promise<Villain> | Observable<Villain> | Villain;
findManyVillainStreamIn(request: Observable<VillainById>): Promise<Villain> | Villain;
Copy link
Owner

@stephenh stephenh Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexkb Disclaimer that I don't use NestJS, but wouldn't just Promise<Villain> make more sense here as a return type?

I agree that your PR is already better than the previous Observable<T> | Promise<T> | T, which imo seems just kind of maddening to use as a caller, b/c you'd have to constantly be checking what the return value was.

...ah, maybe this interface is meant for the server-side to implement, and we want to give flexibility to the server to return what it wants.

Are these interfaces used by both the client and the server? I don't remember...


findManyVillainStreamOut(request: VillainById): Observable<Villain>;
}
Expand Down
4 changes: 2 additions & 2 deletions src/generate-nestjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export function generateNestjsServiceController(
let returns: Code;
if (isEmptyType(methodDesc.outputType)) {
returns = code`void`;
} else if (options.returnObservable || methodDesc.serverStreaming) {
} else if (methodDesc.serverStreaming) {
returns = code`${responseObservable(ctx, methodDesc)}`;
} else {
// generate nestjs union type
returns = code`
${responsePromise(ctx, methodDesc)}
| ${responseObservable(ctx, methodDesc)}
${options.returnObservable ? "\n| " + responseObservable(ctx, methodDesc) : ''}
| ${responseType(ctx, methodDesc)}
`;
}
Expand Down
Loading