Skip to content

Commit b3abf3f

Browse files
committed
feat: add support observable for nest plugin
1 parent 16024b8 commit b3abf3f

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class UserService {
118118
options of the circuit breaker. You can use global options by setting the `circuitBreakerOption` property in the module.
119119
`options` is from [opossum](https://www.npmjs.com/package/opossum) library.
120120

121-
- `@Observable()`: Marks the method as an observable method. If this decorator is not specified, the method will return
121+
- `@ObservableResponse()`: Marks the method as an observable method. If this decorator is not specified, the method will return
122122
a promise.
123123

124124
## Auto generate `@ResponseBody()` from return type of exchange method

lib/fixtures/fake-service-code.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const returnStringServiceCode = `
1+
export const returnStringServiceCode = /* language=typescript */ `
22
import { HttpInterface, GetExchange } from '@r2don/nest-http-interface';
33
44
@HttpInterface()
@@ -11,7 +11,7 @@ class TextService {
1111
}
1212
}`;
1313

14-
export const hasResponseBodyServiceCode = `
14+
export const hasResponseBodyServiceCode = /* language=typescript */ `
1515
import { HttpInterface, PostExchange, ResponseBody } from '@r2don/nest-http-interface';
1616
import { User } from './user.entity';
1717
@@ -24,7 +24,7 @@ class UserService {
2424
}
2525
}`;
2626

27-
export const notPromiseServiceCode = `
27+
export const notPromiseServiceCode = /* language=typescript */ `
2828
import { HttpInterface, GetExchange, ResponseBody } from '@r2don/nest-http-interface';
2929
3030
@HttpInterface()
@@ -35,8 +35,9 @@ class UserService {
3535
}
3636
}`;
3737

38-
export const needResponseBodyServiceCode = `
39-
import { HttpInterface, GraphQLExchange } from '@r2don/nest-http-interface';
38+
export const needResponseBodyServiceCode = /* language=typescript */ `
39+
import { HttpInterface, GraphQLExchange, ObservableResponse } from '@r2don/nest-http-interface';
40+
import { Observable } from "rxjs";
4041
4142
class ResponseClass {}
4243
@@ -46,9 +47,15 @@ class UserService {
4647
async getUser(): Promise<ResponseClass> {
4748
throw new Error('not implemented');
4849
}
50+
51+
@GraphQLExchange()
52+
@ObservableResponse()
53+
getUserObservable(): Observable<ResponseClass> {
54+
throw new Error('not implemented');
55+
}
4956
}`;
5057

51-
export const arrayResponseBodyServiceCode = `
58+
export const arrayResponseBodyServiceCode = /* language=typescript */ `
5259
import { HttpInterface, GetExchange } from '@r2don/nest-http-interface';
5360
5461
class ResponseClass {}

lib/plugin/visitors/__snapshots__/http-interface.visitor.spec.ts.snap

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ let UserService = class UserService {
4646
async getUser() {
4747
throw new Error('not implemented');
4848
}
49+
getUserObservable() {
50+
throw new Error('not implemented');
51+
}
4952
};
5053
__decorate([
5154
(0, nest_http_interface_1.GraphQLExchange)()
5255
], UserService.prototype, \\"getUser\\", null);
56+
__decorate([
57+
(0, nest_http_interface_1.GraphQLExchange)(),
58+
(0, nest_http_interface_1.ObservableResponse)()
59+
], UserService.prototype, \\"getUserObservable\\", null);
5360
UserService = __decorate([
5461
(0, nest_http_interface_1.HttpInterface)()
5562
], UserService);
@@ -76,7 +83,7 @@ UserService = __decorate([
7683
"
7784
`;
7885

79-
exports[`HttpInterfaceVisitor > should ignore if return type if not a promise 1`] = `
86+
exports[`HttpInterfaceVisitor > should ignore if return type if not a promise or observable 1`] = `
8087
"\\"use strict\\";
8188
Object.defineProperty(exports, \\"__esModule\\", { value: true });
8289
const nest_http_interface_1 = require(\\"@r2don/nest-http-interface\\");
@@ -124,11 +131,19 @@ let UserService = class UserService {
124131
async getUser() {
125132
throw new Error('not implemented');
126133
}
134+
getUserObservable() {
135+
throw new Error('not implemented');
136+
}
127137
};
128138
__decorate([
129139
(0, nest_http_interface_1.GraphQLExchange)(),
130140
r2don_http_module_1.ResponseBody(ResponseClass)
131141
], UserService.prototype, \\"getUser\\", null);
142+
__decorate([
143+
(0, nest_http_interface_1.GraphQLExchange)(),
144+
(0, nest_http_interface_1.ObservableResponse)(),
145+
r2don_http_module_1.ResponseBody(ResponseClass)
146+
], UserService.prototype, \\"getUserObservable\\", null);
132147
UserService = __decorate([
133148
(0, nest_http_interface_1.HttpInterface)()
134149
], UserService);

lib/plugin/visitors/http-interface.visitor.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('HttpInterfaceVisitor', () => {
6969
expect(result.outputText).toMatchSnapshot();
7070
});
7171

72-
test('should ignore if return type if not a promise ', () => {
72+
test('should ignore if return type if not a promise or observable', () => {
7373
// given
7474
const filename = 'text.service.ts';
7575
const fakeProgram = ts.createProgram([filename], compilerOptions);

lib/plugin/visitors/http-interface.visitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ export class HttpInterfaceVisitor {
180180
node.type == null ||
181181
!ts.isTypeReferenceNode(node.type) ||
182182
!ts.isIdentifier(node.type.typeName) ||
183-
node.type.typeName.text !== 'Promise'
183+
(node.type.typeName.text !== 'Promise' &&
184+
node.type.typeName.text !== 'Observable')
184185
) {
185186
return undefined;
186187
}

0 commit comments

Comments
 (0)