Skip to content

Commit 324a95a

Browse files
committed
feat(method-params): union types determined for enum like union strings and numbers
example GitHub `orderBy: string` is now `'id' | 'title' | 'subtitle' | ...`
1 parent 7290a41 commit 324a95a

28 files changed

+160
-153
lines changed

src/parser.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -577,18 +577,24 @@ function transformParameters(
577577
(paramRef && 'type' in paramRef && paramRef.type) ||
578578
'';
579579
const isArray = /^array$/i.test(type);
580-
const typescriptType = toTypescriptType(
581-
isArray
582-
? determineArrayType(param as Schema)
583-
: !ref ||
584-
(paramRef &&
585-
'type' in paramRef &&
586-
!paramRef.enum &&
587-
paramRef.type &&
588-
BASIC_TS_TYPE_REGEX.test(paramRef.type))
589-
? type
590-
: derefName,
591-
);
580+
const typescriptType =
581+
'enum' in param
582+
? (param.type === 'string'
583+
? (param.enum || []).map(str => `'${str}'`)
584+
: param.enum || []
585+
).join(' | ')
586+
: toTypescriptType(
587+
isArray
588+
? determineArrayType(param as Schema)
589+
: !ref ||
590+
(paramRef &&
591+
'type' in paramRef &&
592+
!paramRef.enum &&
593+
paramRef.type &&
594+
BASIC_TS_TYPE_REGEX.test(paramRef.type))
595+
? type
596+
: derefName,
597+
);
592598

593599
return {
594600
...param,
@@ -601,7 +607,8 @@ function transformParameters(
601607
' ',
602608
),
603609
camelCaseName: toCamelCase(name),
604-
importType: prefixImportedModels(typescriptType),
610+
importType:
611+
'enum' in param ? typescriptType : prefixImportedModels(typescriptType),
605612
isArray,
606613
isRequired:
607614
(param as Parameter).isRequired ||

templates/ngx-guarded-service.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class Guarded{{&serviceName}} extends {{&serviceName}} {
2929
{{#parameters.length}}
3030
args: {
3131
{{#parameters}}
32-
{{&camelCaseName}}{{^isRequired}}?{{/isRequired}}: {{importType}}{{#isArray}}[]{{/isArray}},{{#description}} //{{^isRequired}} (optional){{/isRequired}} {{&.}}{{/description}}
32+
{{&camelCaseName}}{{^isRequired}}?{{/isRequired}}: {{&importType}}{{#isArray}}[]{{/isArray}},{{#description}} //{{^isRequired}} (optional){{/isRequired}} {{&.}}{{/description}}
3333
{{/parameters}}
3434
},
3535
{{/parameters.length}}

templates/ngx-service-interface.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface {{&interfaceName}} {
1515
{{#parameters.length}}
1616
args: {
1717
{{#parameters}}
18-
{{&camelCaseName}}{{^isRequired}}?{{/isRequired}}: {{importType}}{{#isArray}}[]{{/isArray}},{{#description}} //{{^isRequired}} (optional){{/isRequired}} {{&.}}{{/description}}
18+
{{&camelCaseName}}{{^isRequired}}?{{/isRequired}}: {{&importType}}{{#isArray}}[]{{/isArray}},{{#description}} //{{^isRequired}} (optional){{/isRequired}} {{&.}}{{/description}}
1919
{{/parameters}}
2020
},
2121
{{/parameters.length}}

templates/ngx-service.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class {{&serviceName}} implements {{&interfaceName}} {
5353
{{#parameters.length}}
5454
args: {
5555
{{#parameters}}
56-
{{&camelCaseName}}{{^isRequired}}?{{/isRequired}}: {{importType}}{{#isArray}}[]{{/isArray}},{{#description}} //{{^isRequired}} (optional){{/isRequired}} {{&.}}{{/description}}
56+
{{&camelCaseName}}{{^isRequired}}?{{/isRequired}}: {{&importType}}{{#isArray}}[]{{/isArray}},{{#description}} //{{^isRequired}} (optional){{/isRequired}} {{&.}}{{/description}}
5757
{{/parameters}}
5858
},
5959
{{/parameters.length}}

tests/esquare/api/api-client.interface.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export interface APIClientInterface {
108108
status?: models.Status, // (optional) - 1 Pending - 2 InProgress - 3 Complete
109109
pageSize: number,
110110
page: number, // page number
111-
orderBy: string, // id | title | subtitle | criticality | status | issues | deadline
111+
orderBy: 'id' | 'title' | 'subtitle' | 'criticality' | 'status' | 'issues' | 'deadline', // id | title | subtitle | criticality | status | issues | deadline
112112
order?: models.Order, // (optional) - asc - desc
113113
},
114114
requestHttpOptions?: HttpOptions
@@ -314,7 +314,7 @@ export interface APIClientInterface {
314314
status?: models.IssueStatus, // (optional) - 1 Pending - 2 Resolved
315315
pageSize: number,
316316
page: number, // page number
317-
orderBy: string, // name | school | dueDate | alert
317+
orderBy: 'name' | 'school' | 'dueDate' | 'alert', // name | school | dueDate | alert
318318
order?: models.Order, // (optional) - asc - desc
319319
},
320320
requestHttpOptions?: HttpOptions
@@ -331,7 +331,7 @@ export interface APIClientInterface {
331331
status?: models.ImportStatus, // (optional) - 1 Live - 2 PastDeadline
332332
pageSize: number,
333333
page: number, // page number
334-
orderBy: string, // name | issues | dueDate | progress
334+
orderBy: 'name' | 'issues' | 'dueDate' | 'progress', // name | issues | dueDate | progress
335335
order?: models.Order, // (optional) - asc - desc
336336
},
337337
requestHttpOptions?: HttpOptions
@@ -346,7 +346,7 @@ export interface APIClientInterface {
346346
status?: models.ImportStatus, // (optional) - 1 Live - 2 PastDeadline
347347
pageSize: number,
348348
page: number, // page number
349-
orderBy: string, // name | issues | dueDate | progress
349+
orderBy: 'name' | 'issues' | 'dueDate' | 'progress', // name | issues | dueDate | progress
350350
order?: models.Order, // (optional) - asc - desc
351351
assignedToRole?: number, // (optional) role id | [Screenshot from design](http://prntscr.com/ib9yal)
352352
unassignedFromRole?: number, // (optional) role id | [Screenshot from design](http://prntscr.com/ib9z16)
@@ -489,7 +489,7 @@ export interface APIClientInterface {
489489
args: {
490490
pageSize: number,
491491
page: number, // page number
492-
orderBy: string, // name | description | priority | date
492+
orderBy: 'name' | 'description' | 'priority' | 'date', // name | description | priority | date
493493
order?: models.Order, // (optional) - asc - desc
494494
},
495495
requestHttpOptions?: HttpOptions
@@ -524,7 +524,7 @@ export interface APIClientInterface {
524524
moduleId: number,
525525
pageSize: number,
526526
page: number, // page number
527-
orderBy: string, // name | description | priority | date
527+
orderBy: 'name' | 'description' | 'priority' | 'date', // name | description | priority | date
528528
order?: models.Order, // (optional) - asc - desc
529529
},
530530
requestHttpOptions?: HttpOptions

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export class APIClient implements APIClientInterface {
227227
status?: models.Status, // (optional) - 1 Pending - 2 InProgress - 3 Complete
228228
pageSize: number,
229229
page: number, // page number
230-
orderBy: string, // id | title | subtitle | criticality | status | issues | deadline
230+
orderBy: 'id' | 'title' | 'subtitle' | 'criticality' | 'status' | 'issues' | 'deadline', // id | title | subtitle | criticality | status | issues | deadline
231231
order?: models.Order, // (optional) - asc - desc
232232
},
233233
requestHttpOptions?: HttpOptions
@@ -604,7 +604,7 @@ export class APIClient implements APIClientInterface {
604604
status?: models.IssueStatus, // (optional) - 1 Pending - 2 Resolved
605605
pageSize: number,
606606
page: number, // page number
607-
orderBy: string, // name | school | dueDate | alert
607+
orderBy: 'name' | 'school' | 'dueDate' | 'alert', // name | school | dueDate | alert
608608
order?: models.Order, // (optional) - asc - desc
609609
},
610610
requestHttpOptions?: HttpOptions
@@ -647,7 +647,7 @@ export class APIClient implements APIClientInterface {
647647
status?: models.ImportStatus, // (optional) - 1 Live - 2 PastDeadline
648648
pageSize: number,
649649
page: number, // page number
650-
orderBy: string, // name | issues | dueDate | progress
650+
orderBy: 'name' | 'issues' | 'dueDate' | 'progress', // name | issues | dueDate | progress
651651
order?: models.Order, // (optional) - asc - desc
652652
},
653653
requestHttpOptions?: HttpOptions
@@ -688,7 +688,7 @@ export class APIClient implements APIClientInterface {
688688
status?: models.ImportStatus, // (optional) - 1 Live - 2 PastDeadline
689689
pageSize: number,
690690
page: number, // page number
691-
orderBy: string, // name | issues | dueDate | progress
691+
orderBy: 'name' | 'issues' | 'dueDate' | 'progress', // name | issues | dueDate | progress
692692
order?: models.Order, // (optional) - asc - desc
693693
assignedToRole?: number, // (optional) role id | [Screenshot from design](http://prntscr.com/ib9yal)
694694
unassignedFromRole?: number, // (optional) role id | [Screenshot from design](http://prntscr.com/ib9z16)
@@ -967,7 +967,7 @@ export class APIClient implements APIClientInterface {
967967
args: {
968968
pageSize: number,
969969
page: number, // page number
970-
orderBy: string, // name | description | priority | date
970+
orderBy: 'name' | 'description' | 'priority' | 'date', // name | description | priority | date
971971
order?: models.Order, // (optional) - asc - desc
972972
},
973973
requestHttpOptions?: HttpOptions
@@ -1038,7 +1038,7 @@ export class APIClient implements APIClientInterface {
10381038
moduleId: number,
10391039
pageSize: number,
10401040
page: number, // page number
1041-
orderBy: string, // name | description | priority | date
1041+
orderBy: 'name' | 'description' | 'priority' | 'date', // name | description | priority | date
10421042
order?: models.Order, // (optional) - asc - desc
10431043
},
10441044
requestHttpOptions?: HttpOptions

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class GuardedAPIClient extends APIClient {
109109
status?: models.Status, // (optional) - 1 Pending - 2 InProgress - 3 Complete
110110
pageSize: number,
111111
page: number, // page number
112-
orderBy: string, // id | title | subtitle | criticality | status | issues | deadline
112+
orderBy: 'id' | 'title' | 'subtitle' | 'criticality' | 'status' | 'issues' | 'deadline', // id | title | subtitle | criticality | status | issues | deadline
113113
order?: models.Order, // (optional) - asc - desc
114114
},
115115
requestHttpOptions?: HttpOptions
@@ -261,7 +261,7 @@ export class GuardedAPIClient extends APIClient {
261261
status?: models.IssueStatus, // (optional) - 1 Pending - 2 Resolved
262262
pageSize: number,
263263
page: number, // page number
264-
orderBy: string, // name | school | dueDate | alert
264+
orderBy: 'name' | 'school' | 'dueDate' | 'alert', // name | school | dueDate | alert
265265
order?: models.Order, // (optional) - asc - desc
266266
},
267267
requestHttpOptions?: HttpOptions
@@ -276,7 +276,7 @@ export class GuardedAPIClient extends APIClient {
276276
status?: models.ImportStatus, // (optional) - 1 Live - 2 PastDeadline
277277
pageSize: number,
278278
page: number, // page number
279-
orderBy: string, // name | issues | dueDate | progress
279+
orderBy: 'name' | 'issues' | 'dueDate' | 'progress', // name | issues | dueDate | progress
280280
order?: models.Order, // (optional) - asc - desc
281281
},
282282
requestHttpOptions?: HttpOptions
@@ -291,7 +291,7 @@ export class GuardedAPIClient extends APIClient {
291291
status?: models.ImportStatus, // (optional) - 1 Live - 2 PastDeadline
292292
pageSize: number,
293293
page: number, // page number
294-
orderBy: string, // name | issues | dueDate | progress
294+
orderBy: 'name' | 'issues' | 'dueDate' | 'progress', // name | issues | dueDate | progress
295295
order?: models.Order, // (optional) - asc - desc
296296
assignedToRole?: number, // (optional) role id | [Screenshot from design](http://prntscr.com/ib9yal)
297297
unassignedFromRole?: number, // (optional) role id | [Screenshot from design](http://prntscr.com/ib9z16)
@@ -396,7 +396,7 @@ export class GuardedAPIClient extends APIClient {
396396
args: {
397397
pageSize: number,
398398
page: number, // page number
399-
orderBy: string, // name | description | priority | date
399+
orderBy: 'name' | 'description' | 'priority' | 'date', // name | description | priority | date
400400
order?: models.Order, // (optional) - asc - desc
401401
},
402402
requestHttpOptions?: HttpOptions
@@ -424,7 +424,7 @@ export class GuardedAPIClient extends APIClient {
424424
moduleId: number,
425425
pageSize: number,
426426
page: number, // page number
427-
orderBy: string, // name | description | priority | date
427+
orderBy: 'name' | 'description' | 'priority' | 'date', // name | description | priority | date
428428
order?: models.Order, // (optional) - asc - desc
429429
},
430430
requestHttpOptions?: HttpOptions

tests/github/api/services/issues/guarded-issues-api-client.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ export class GuardedIssuesAPIClient extends IssuesAPIClient {
2424

2525
getIssues(
2626
args: {
27-
filter: string, // Issues assigned to you / created by you / mentioning you / you're subscribed to updates for / All issues the authenticated user can see
28-
state: string,
27+
filter: 'assigned' | 'created' | 'mentioned' | 'subscribed' | 'all', // Issues assigned to you / created by you / mentioning you / you're subscribed to updates for / All issues the authenticated user can see
28+
state: 'open' | 'closed',
2929
labels: string, // String list of comma separated Label names. Example - bug,ui,@high.
30-
sort: string,
31-
direction: string,
30+
sort: 'created' | 'updated' | 'comments',
31+
direction: 'asc' | 'desc',
3232
since?: string, // (optional) Optional string of a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. Only issues updated at or after this time are returned.
3333
xGitHubMediaType?: string, // (optional) You can check the current version of media type in responses.
3434
accept?: string, // (optional) Is used to set specified media type.

tests/github/api/services/issues/issues-api-client.interface.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export interface IssuesAPIClientInterface {
1414
*/
1515
getIssues(
1616
args: {
17-
filter: string, // Issues assigned to you / created by you / mentioning you / you're subscribed to updates for / All issues the authenticated user can see
18-
state: string,
17+
filter: 'assigned' | 'created' | 'mentioned' | 'subscribed' | 'all', // Issues assigned to you / created by you / mentioning you / you're subscribed to updates for / All issues the authenticated user can see
18+
state: 'open' | 'closed',
1919
labels: string, // String list of comma separated Label names. Example - bug,ui,@high.
20-
sort: string,
21-
direction: string,
20+
sort: 'created' | 'updated' | 'comments',
21+
direction: 'asc' | 'desc',
2222
since?: string, // (optional) Optional string of a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. Only issues updated at or after this time are returned.
2323
xGitHubMediaType?: string, // (optional) You can check the current version of media type in responses.
2424
accept?: string, // (optional) Is used to set specified media type.

tests/github/api/services/issues/issues-api-client.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ export class IssuesAPIClient implements IssuesAPIClientInterface {
5050
*/
5151
getIssues(
5252
args: {
53-
filter: string, // Issues assigned to you / created by you / mentioning you / you're subscribed to updates for / All issues the authenticated user can see
54-
state: string,
53+
filter: 'assigned' | 'created' | 'mentioned' | 'subscribed' | 'all', // Issues assigned to you / created by you / mentioning you / you're subscribed to updates for / All issues the authenticated user can see
54+
state: 'open' | 'closed',
5555
labels: string, // String list of comma separated Label names. Example - bug,ui,@high.
56-
sort: string,
57-
direction: string,
56+
sort: 'created' | 'updated' | 'comments',
57+
direction: 'asc' | 'desc',
5858
since?: string, // (optional) Optional string of a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. Only issues updated at or after this time are returned.
5959
xGitHubMediaType?: string, // (optional) You can check the current version of media type in responses.
6060
accept?: string, // (optional) Is used to set specified media type.

0 commit comments

Comments
 (0)