Skip to content

Commit 96ce207

Browse files
committed
fix(guards): method response basic type guards used instead of custom function for primitive types
1 parent 277ee3f commit 96ce207

File tree

16 files changed

+49
-204
lines changed

16 files changed

+49
-204
lines changed

src/parser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ function parseMethods(
148148
parameters || {},
149149
);
150150

151+
const responseTypeName = typeName(responseType.name);
152+
151153
return {
152154
hasJsonResponse: true,
153155
isSecure:
@@ -173,7 +175,10 @@ function parseMethods(
173175
(_: string, ...args: string[]): string =>
174176
`\${args.${toCamelCase(args[0])}}`,
175177
),
176-
responseTypeName: typeName(responseType.name),
178+
responseTypeName,
179+
responseGuard: BASIC_TS_TYPE_REGEX.test(responseTypeName)
180+
? `typeof res === '${responseTypeName}'`
181+
: `guards.is${responseTypeName}(res)`,
177182
isVoid: responseType.name === 'void',
178183
response: prefixImportedModels(responseType.type),
179184
// tslint:disable-next-line:max-line-length

templates/ngx-guarded-service.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export class Guarded{{&serviceName}} extends {{&serviceName}} {
3535
{{/parameters.length}}
3636
requestHttpOptions?: HttpOptions
3737
): Observable<{{&response}}> {
38-
return super.{{&methodName}}({{#parameters.length}}args, {{/parameters.length}}requestHttpOptions)
39-
.pipe(tap((res: any) => guards.is{{&responseTypeName}}(res) || console.error(`TypeGuard for response '{{&responseTypeName}}' caught inconsistency.`, res)));
38+
return super.{{&methodName}}({{#parameters.length}}args, {{/parameters.length}}requestHttpOptions){{#responseGuard}}
39+
.pipe(tap((res: any) => {{&responseGuard}} || console.error(`TypeGuard for response '{{&responseTypeName}}' caught inconsistency.`, res))){{/responseGuard}};
4040
}
4141

4242
{{/isVoid}}{{/methods}}

templates/ngx-model-guards-export.mustache

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,12 @@ import * as models from '../models';
44

55
/* pre-prepared guards for build in complex types */
66

7-
export function isany(_: any): _ is any {
8-
return true;
9-
}
10-
11-
export function isstring(arg: any): arg is string {
12-
return typeof arg === 'string';
13-
}
14-
15-
export function isnumber(arg: any): arg is number {
16-
return typeof arg === 'number';
17-
}
18-
19-
export function isboolean(arg: any): arg is boolean {
20-
return typeof arg === 'boolean';
21-
}
22-
23-
export function isobject(arg: any): arg is any {
24-
return typeof arg === 'object';
25-
}
26-
27-
export function isBlob(arg: any): arg is Blob {
7+
function _isBlob(arg: any): arg is Blob {
288
return arg != null && typeof arg.size === 'number' && typeof arg.type === 'string' && typeof arg.slice === 'function';
299
}
3010

3111
export function isFile(arg: any): arg is File {
32-
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && isBlob(arg);
12+
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && _isBlob(arg);
3313
}
3414

3515
/* generated type guards */

tests/custom/api/guarded-api-client.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class GuardedAPIClient extends APIClient {
4141
requestHttpOptions?: HttpOptions
4242
): Observable<object> {
4343
return super.getItemModels(args, requestHttpOptions)
44-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
44+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
4545
}
4646

4747
getPetsId(
@@ -82,21 +82,21 @@ export class GuardedAPIClient extends APIClient {
8282
requestHttpOptions?: HttpOptions
8383
): Observable<object> {
8484
return super.getRandomObject(requestHttpOptions)
85-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
85+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
8686
}
8787

8888
getRandomModel(
8989
requestHttpOptions?: HttpOptions
9090
): Observable<object> {
9191
return super.getRandomModel(requestHttpOptions)
92-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
92+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
9393
}
9494

9595
getRandomString(
9696
requestHttpOptions?: HttpOptions
9797
): Observable<string> {
9898
return super.getRandomString(requestHttpOptions)
99-
.pipe(tap((res: any) => guards.isstring(res) || console.error(`TypeGuard for response 'string' caught inconsistency.`, res)));
99+
.pipe(tap((res: any) => typeof res === 'string' || console.error(`TypeGuard for response 'string' caught inconsistency.`, res)));
100100
}
101101

102102
}

tests/custom/api/guards/index.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,12 @@ import * as models from '../models';
44

55
/* pre-prepared guards for build in complex types */
66

7-
export function isany(_: any): _ is any {
8-
return true;
9-
}
10-
11-
export function isstring(arg: any): arg is string {
12-
return typeof arg === 'string';
13-
}
14-
15-
export function isnumber(arg: any): arg is number {
16-
return typeof arg === 'number';
17-
}
18-
19-
export function isboolean(arg: any): arg is boolean {
20-
return typeof arg === 'boolean';
21-
}
22-
23-
export function isobject(arg: any): arg is any {
24-
return typeof arg === 'object';
25-
}
26-
27-
export function isBlob(arg: any): arg is Blob {
7+
function _isBlob(arg: any): arg is Blob {
288
return arg != null && typeof arg.size === 'number' && typeof arg.type === 'string' && typeof arg.slice === 'function';
299
}
3010

3111
export function isFile(arg: any): arg is File {
32-
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && isBlob(arg);
12+
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && _isBlob(arg);
3313
}
3414

3515
/* generated type guards */

tests/esquare/api/guarded-api-client.service.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ export class GuardedAPIClient extends APIClient {
2929
requestHttpOptions?: HttpOptions
3030
): Observable<object> {
3131
return super.auth(args, requestHttpOptions)
32-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
32+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
3333
}
3434

3535
authRef(
3636
requestHttpOptions?: HttpOptions
3737
): Observable<object> {
3838
return super.authRef(requestHttpOptions)
39-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
39+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
4040
}
4141

4242
passwordRestoreRequest(
@@ -46,7 +46,7 @@ export class GuardedAPIClient extends APIClient {
4646
requestHttpOptions?: HttpOptions
4747
): Observable<object> {
4848
return super.passwordRestoreRequest(args, requestHttpOptions)
49-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
49+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
5050
}
5151

5252
passwordRestoreEmailRequest(
@@ -56,7 +56,7 @@ export class GuardedAPIClient extends APIClient {
5656
requestHttpOptions?: HttpOptions
5757
): Observable<object> {
5858
return super.passwordRestoreEmailRequest(args, requestHttpOptions)
59-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
59+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
6060
}
6161

6262
passwordRestoreCheckRestoreGuid(
@@ -66,7 +66,7 @@ export class GuardedAPIClient extends APIClient {
6666
requestHttpOptions?: HttpOptions
6767
): Observable<object> {
6868
return super.passwordRestoreCheckRestoreGuid(args, requestHttpOptions)
69-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
69+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
7070
}
7171

7272
getAclList(
@@ -115,7 +115,7 @@ export class GuardedAPIClient extends APIClient {
115115
requestHttpOptions?: HttpOptions
116116
): Observable<object> {
117117
return super.getReportsList(args, requestHttpOptions)
118-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
118+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
119119
}
120120

121121
getReportDetails(
@@ -139,7 +139,7 @@ export class GuardedAPIClient extends APIClient {
139139
requestHttpOptions?: HttpOptions
140140
): Observable<object> {
141141
return super.getReportPreview(args, requestHttpOptions)
142-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
142+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
143143
}
144144

145145
getImportHistory(
@@ -160,7 +160,7 @@ export class GuardedAPIClient extends APIClient {
160160
requestHttpOptions?: HttpOptions
161161
): Observable<number> {
162162
return super.uploadFile(args, requestHttpOptions)
163-
.pipe(tap((res: any) => guards.isnumber(res) || console.error(`TypeGuard for response 'number' caught inconsistency.`, res)));
163+
.pipe(tap((res: any) => typeof res === 'number' || console.error(`TypeGuard for response 'number' caught inconsistency.`, res)));
164164
}
165165

166166
listTemplateColumns(
@@ -267,7 +267,7 @@ export class GuardedAPIClient extends APIClient {
267267
requestHttpOptions?: HttpOptions
268268
): Observable<object> {
269269
return super.getIssuesList(args, requestHttpOptions)
270-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
270+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
271271
}
272272

273273
getStatusesList(
@@ -282,7 +282,7 @@ export class GuardedAPIClient extends APIClient {
282282
requestHttpOptions?: HttpOptions
283283
): Observable<object> {
284284
return super.getStatusesList(args, requestHttpOptions)
285-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
285+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
286286
}
287287

288288
getUsersList(
@@ -299,7 +299,7 @@ export class GuardedAPIClient extends APIClient {
299299
requestHttpOptions?: HttpOptions
300300
): Observable<object> {
301301
return super.getUsersList(args, requestHttpOptions)
302-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
302+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
303303
}
304304

305305
createUser(
@@ -402,7 +402,7 @@ export class GuardedAPIClient extends APIClient {
402402
requestHttpOptions?: HttpOptions
403403
): Observable<object> {
404404
return super.getNotificationsList(args, requestHttpOptions)
405-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
405+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
406406
}
407407

408408
getModulesList(
@@ -430,7 +430,7 @@ export class GuardedAPIClient extends APIClient {
430430
requestHttpOptions?: HttpOptions
431431
): Observable<object> {
432432
return super.getModuleNotificationsList(args, requestHttpOptions)
433-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
433+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
434434
}
435435

436436
getNotification(
@@ -450,7 +450,7 @@ export class GuardedAPIClient extends APIClient {
450450
requestHttpOptions?: HttpOptions
451451
): Observable<number> {
452452
return super.createNotification(args, requestHttpOptions)
453-
.pipe(tap((res: any) => guards.isnumber(res) || console.error(`TypeGuard for response 'number' caught inconsistency.`, res)));
453+
.pipe(tap((res: any) => typeof res === 'number' || console.error(`TypeGuard for response 'number' caught inconsistency.`, res)));
454454
}
455455

456456
getPassVerificationPolicies(

tests/esquare/api/guards/index.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,12 @@ import * as models from '../models';
44

55
/* pre-prepared guards for build in complex types */
66

7-
export function isany(_: any): _ is any {
8-
return true;
9-
}
10-
11-
export function isstring(arg: any): arg is string {
12-
return typeof arg === 'string';
13-
}
14-
15-
export function isnumber(arg: any): arg is number {
16-
return typeof arg === 'number';
17-
}
18-
19-
export function isboolean(arg: any): arg is boolean {
20-
return typeof arg === 'boolean';
21-
}
22-
23-
export function isobject(arg: any): arg is any {
24-
return typeof arg === 'object';
25-
}
26-
27-
export function isBlob(arg: any): arg is Blob {
7+
function _isBlob(arg: any): arg is Blob {
288
return arg != null && typeof arg.size === 'number' && typeof arg.type === 'string' && typeof arg.slice === 'function';
299
}
3010

3111
export function isFile(arg: any): arg is File {
32-
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && isBlob(arg);
12+
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && _isBlob(arg);
3313
}
3414

3515
/* generated type guards */

tests/filtered-api/api/guards/index.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,12 @@ import * as models from '../models';
44

55
/* pre-prepared guards for build in complex types */
66

7-
export function isany(_: any): _ is any {
8-
return true;
9-
}
10-
11-
export function isstring(arg: any): arg is string {
12-
return typeof arg === 'string';
13-
}
14-
15-
export function isnumber(arg: any): arg is number {
16-
return typeof arg === 'number';
17-
}
18-
19-
export function isboolean(arg: any): arg is boolean {
20-
return typeof arg === 'boolean';
21-
}
22-
23-
export function isobject(arg: any): arg is any {
24-
return typeof arg === 'object';
25-
}
26-
27-
export function isBlob(arg: any): arg is Blob {
7+
function _isBlob(arg: any): arg is Blob {
288
return arg != null && typeof arg.size === 'number' && typeof arg.type === 'string' && typeof arg.slice === 'function';
299
}
3010

3111
export function isFile(arg: any): arg is File {
32-
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && isBlob(arg);
12+
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && _isBlob(arg);
3313
}
3414

3515
/* generated type guards */

tests/filtered-api/api/services/dummy-selector/guarded-dummy-selector-api-client.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class GuardedDummySelectorAPIClient extends DummySelectorAPIClient {
5050
requestHttpOptions?: HttpOptions
5151
): Observable<object> {
5252
return super.putSettings(args, requestHttpOptions)
53-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
53+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
5454
}
5555

5656
deleteSettings(
@@ -60,7 +60,7 @@ export class GuardedDummySelectorAPIClient extends DummySelectorAPIClient {
6060
requestHttpOptions?: HttpOptions
6161
): Observable<object> {
6262
return super.deleteSettings(args, requestHttpOptions)
63-
.pipe(tap((res: any) => guards.isobject(res) || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
63+
.pipe(tap((res: any) => typeof res === 'object' || console.error(`TypeGuard for response 'object' caught inconsistency.`, res)));
6464
}
6565

6666
}

tests/gcloud-firestore/api/guards/index.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,12 @@ import * as models from '../models';
44

55
/* pre-prepared guards for build in complex types */
66

7-
export function isany(_: any): _ is any {
8-
return true;
9-
}
10-
11-
export function isstring(arg: any): arg is string {
12-
return typeof arg === 'string';
13-
}
14-
15-
export function isnumber(arg: any): arg is number {
16-
return typeof arg === 'number';
17-
}
18-
19-
export function isboolean(arg: any): arg is boolean {
20-
return typeof arg === 'boolean';
21-
}
22-
23-
export function isobject(arg: any): arg is any {
24-
return typeof arg === 'object';
25-
}
26-
27-
export function isBlob(arg: any): arg is Blob {
7+
function _isBlob(arg: any): arg is Blob {
288
return arg != null && typeof arg.size === 'number' && typeof arg.type === 'string' && typeof arg.slice === 'function';
299
}
3010

3111
export function isFile(arg: any): arg is File {
32-
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && isBlob(arg);
12+
return arg != null && typeof arg.lastModified === 'number' && typeof arg.name === 'string' && _isBlob(arg);
3313
}
3414

3515
/* generated type guards */

0 commit comments

Comments
 (0)