Skip to content

Commit 874f567

Browse files
committed
feat: handle multiple responses type
1 parent 17c1251 commit 874f567

File tree

15 files changed

+736
-97
lines changed

15 files changed

+736
-97
lines changed

templates/default/procedure-call.ejs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ const describeReturnType = () => {
8181
8282
const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
8383
84+
const getSuccessResponseTypes = () => {
85+
const successResponses = responseBodyInfo.responses.filter(response => response.isSuccess);
86+
87+
return config.Ts.UnionType(successResponses.map((response) => response.type)) || "any";
88+
}
89+
90+
8491
%>
8592
/**
8693
<%~ routeDocs.description %>
@@ -91,7 +98,7 @@ const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
9198

9299
*/
93100
<% if (isValidIdentifier(route.routeName.usage)) { %><%~ route.routeName.usage %><%~ route.namespace ? ': ' : ' = ' %><% } else { %>"<%~ route.routeName.usage %>"<%~ route.namespace ? ': ' : ' = ' %><% } %>(<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
94-
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
101+
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ `${getSuccessResponseTypes()}` %>, <%~ errorType %>>({
95102
path: `<%~ path %>`,
96103
method: '<%~ _.upperCase(method) %>',
97104
<%~ queryTmpl ? `query: ${queryTmpl},` : '' %>

tests/__snapshots__/extended.test.ts.snap

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8124,7 +8124,7 @@ export class Api<
81248124
* @request GET:/v2
81258125
*/
81268126
getVersionDetailsv2: (params: RequestParams = {}) =>
8127-
this.request<GetVersionDetailsv2Data, any>({
8127+
this.request<GetVersionDetailsv2Data | void, any>({
81288128
path: \`/v2\`,
81298129
method: "GET",
81308130
format: "json",
@@ -8490,7 +8490,7 @@ export class Api<
84908490
* @request GET:/v2
84918491
*/
84928492
getVersionDetailsv2: (params: RequestParams = {}) =>
8493-
this.request<GetVersionDetailsv2Data, any>({
8493+
this.request<GetVersionDetailsv2Data | void, any>({
84948494
path: \`/v2\`,
84958495
method: "GET",
84968496
format: "json",
@@ -9600,7 +9600,7 @@ export class Api<
96009600
{ job, ...query }: SignRetrieveParams,
96019601
params: RequestParams = {},
96029602
) =>
9603-
this.request<SignRetrieveData, SignRetrieveError>({
9603+
this.request<SignRetrieveData | void, SignRetrieveError>({
96049604
path: \`/scope/\${job}\`,
96059605
method: "GET",
96069606
format: "json",
@@ -9618,7 +9618,7 @@ export class Api<
96189618
{ job, ...query }: SignRetrieveHeadParams,
96199619
params: RequestParams = {},
96209620
) =>
9621-
this.request<SignRetrieveHeadData, SignRetrieveHeadError>({
9621+
this.request<SignRetrieveHeadData | void, SignRetrieveHeadError>({
96229622
path: \`/scope/\${job}\`,
96239623
method: "HEAD",
96249624
...params,
@@ -11330,7 +11330,7 @@ export class Api<
1133011330
parameters: TdeCertificate,
1133111331
params: RequestParams = {},
1133211332
) =>
11333-
this.request<ManagedInstanceTdeCertificatesCreateData, void>({
11333+
this.request<ManagedInstanceTdeCertificatesCreateData | void, void>({
1133411334
path: \`/subscriptions/\${subscriptionId}/resourceGroups/\${resourceGroupName}/providers/Microsoft.Sql/managedInstances/\${managedInstanceName}/tdeCertificates\`,
1133511335
method: "POST",
1133611336
query: query,
@@ -51037,7 +51037,7 @@ export class Api<
5103751037
params: RequestParams = {},
5103851038
) =>
5103951039
this.request<
51040-
OauthAuthorizationsGetOrCreateAuthorizationForAppData,
51040+
OauthAuthorizationsGetOrCreateAuthorizationForAppData | Authorization,
5104151041
BasicError | ValidationError
5104251042
>({
5104351043
path: \`/authorizations/clients/\${clientId}\`,
@@ -51067,7 +51067,8 @@ export class Api<
5106751067
params: RequestParams = {},
5106851068
) =>
5106951069
this.request<
51070-
OauthAuthorizationsGetOrCreateAuthorizationForAppAndFingerprintData,
51070+
| OauthAuthorizationsGetOrCreateAuthorizationForAppAndFingerprintData
51071+
| Authorization,
5107151072
ValidationError
5107251073
>({
5107351074
path: \`/authorizations/clients/\${clientId}/\${fingerprint}\`,
@@ -52857,7 +52858,7 @@ export class Api<
5285752858
data: ActivityMarkNotificationsAsReadPayload,
5285852859
params: RequestParams = {},
5285952860
) =>
52860-
this.request<ActivityMarkNotificationsAsReadData, BasicError>({
52861+
this.request<ActivityMarkNotificationsAsReadData | void, BasicError>({
5286152862
path: \`/notifications\`,
5286252863
method: "PUT",
5286352864
body: data,
@@ -53024,7 +53025,7 @@ export class Api<
5302453025
data: ActionsCreateOrUpdateOrgSecretPayload,
5302553026
params: RequestParams = {},
5302653027
) =>
53027-
this.request<ActionsCreateOrUpdateOrgSecretData, any>({
53028+
this.request<ActionsCreateOrUpdateOrgSecretData | void, any>({
5302853029
path: \`/orgs/\${org}/actions/secrets/\${secretName}\`,
5302953030
method: "PUT",
5303053031
body: data,
@@ -54145,7 +54146,7 @@ export class Api<
5414554146
params: RequestParams = {},
5414654147
) =>
5414754148
this.request<
54148-
OrgsConvertMemberToOutsideCollaboratorData,
54149+
OrgsConvertMemberToOutsideCollaboratorData | void,
5414954150
OrgsConvertMemberToOutsideCollaboratorError
5415054151
>({
5415154152
path: \`/orgs/\${org}/outside_collaborators/\${username}\`,
@@ -55127,7 +55128,7 @@ export class Api<
5512755128
}: TeamsCheckPermissionsForRepoInOrgParams,
5512855129
params: RequestParams = {},
5512955130
) =>
55130-
this.request<TeamsCheckPermissionsForRepoInOrgData, void>({
55131+
this.request<TeamsCheckPermissionsForRepoInOrgData | void, void>({
5513155132
path: \`/orgs/\${org}/teams/\${teamSlug}/repos/\${owner}/\${repo}\`,
5513255133
method: "GET",
5513355134
format: "json",
@@ -56228,7 +56229,7 @@ export class Api<
5622856229
data: ActionsCreateOrUpdateRepoSecretPayload,
5622956230
params: RequestParams = {},
5623056231
) =>
56231-
this.request<ActionsCreateOrUpdateRepoSecretData, any>({
56232+
this.request<ActionsCreateOrUpdateRepoSecretData | void, any>({
5623256233
path: \`/repos/\${owner}/\${repo}/actions/secrets/\${secretName}\`,
5623356234
method: "PUT",
5623456235
body: data,
@@ -59430,7 +59431,7 @@ export class Api<
5943059431
params: RequestParams = {},
5943159432
) =>
5943259433
this.request<
59433-
ReactionsCreateForCommitCommentData,
59434+
ReactionsCreateForCommitCommentData | Reaction,
5943459435
| {
5943559436
documentation_url: string;
5943659437
message: string;
@@ -59493,7 +59494,7 @@ export class Api<
5949359494
params: RequestParams = {},
5949459495
) =>
5949559496
this.request<
59496-
ReactionsCreateForIssueCommentData,
59497+
ReactionsCreateForIssueCommentData | Reaction,
5949759498
| {
5949859499
documentation_url: string;
5949959500
message: string;
@@ -59527,7 +59528,7 @@ export class Api<
5952759528
params: RequestParams = {},
5952859529
) =>
5952959530
this.request<
59530-
ReactionsCreateForPullRequestReviewCommentData,
59531+
ReactionsCreateForPullRequestReviewCommentData | Reaction,
5953159532
| {
5953259533
documentation_url: string;
5953359534
message: string;
@@ -59786,7 +59787,10 @@ export class Api<
5978659787
data: ReposAddCollaboratorPayload,
5978759788
params: RequestParams = {},
5978859789
) =>
59789-
this.request<ReposAddCollaboratorData, BasicError | ValidationError>({
59790+
this.request<
59791+
ReposAddCollaboratorData | void,
59792+
BasicError | ValidationError
59793+
>({
5979059794
path: \`/repos/\${owner}/\${repo}/collaborators/\${username}\`,
5979159795
method: "PUT",
5979259796
body: data,
@@ -60022,7 +60026,13 @@ export class Api<
6002260026
data: ReposCreateDeploymentPayload,
6002360027
params: RequestParams = {},
6002460028
) =>
60025-
this.request<ReposCreateDeploymentData, ReposCreateDeploymentError>({
60029+
this.request<
60030+
| ReposCreateDeploymentData
60031+
| {
60032+
message?: string;
60033+
},
60034+
ReposCreateDeploymentError
60035+
>({
6002660036
path: \`/repos/\${owner}/\${repo}/deployments\`,
6002760037
method: "POST",
6002860038
body: data,
@@ -60115,7 +60125,7 @@ export class Api<
6011560125
params: RequestParams = {},
6011660126
) =>
6011760127
this.request<
60118-
ReposCreateOrUpdateFileContentsData,
60128+
ReposCreateOrUpdateFileContentsData | FileCommit,
6011960129
BasicError | ValidationError
6012060130
>({
6012160131
path: \`/repos/\${owner}/\${repo}/contents/\${path}\`,
@@ -61644,7 +61654,7 @@ export class Api<
6164461654
{ owner, repo, ...query }: ReposListContributorsParams,
6164561655
params: RequestParams = {},
6164661656
) =>
61647-
this.request<ReposListContributorsData, BasicError>({
61657+
this.request<ReposListContributorsData | void, BasicError>({
6164861658
path: \`/repos/\${owner}/\${repo}/contributors\`,
6164961659
method: "GET",
6165061660
query: query,
@@ -63579,7 +63589,7 @@ export class Api<
6357963589
}: TeamsCheckPermissionsForRepoLegacyParams,
6358063590
params: RequestParams = {},
6358163591
) =>
63582-
this.request<TeamsCheckPermissionsForRepoLegacyData, void>({
63592+
this.request<TeamsCheckPermissionsForRepoLegacyData | void, void>({
6358363593
path: \`/teams/\${teamId}/repos/\${owner}/\${repo}\`,
6358463594
method: "GET",
6358563595
format: "json",

tests/__snapshots__/simple.test.ts.snap

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6199,7 +6199,7 @@ export class Api<
61996199
field?: string;
62006200
/** base64safe encoded public signing key */
62016201
sub?: string;
6202-
},
6202+
} | void,
62036203
Error
62046204
>({
62056205
path: \`/scope/\${job}\`,
@@ -20357,7 +20357,7 @@ export class Api<
2035720357
this.request<
2035820358
{
2035920359
message?: string;
20360-
},
20360+
} | void,
2036120361
BasicError
2036220362
>({
2036320363
path: \`/notifications\`,
@@ -24051,7 +24051,7 @@ export class Api<
2405124051
repo: string,
2405224052
params: RequestParams = {},
2405324053
) =>
24054-
this.request<TeamRepository, void>({
24054+
this.request<TeamRepository | void, void>({
2405524055
path: \`/orgs/\${org}/teams/\${teamSlug}/repos/\${owner}/\${repo}\`,
2405624056
method: "GET",
2405724057
format: "json",
@@ -27670,7 +27670,7 @@ export class Api<
2767027670
},
2767127671
params: RequestParams = {},
2767227672
) =>
27673-
this.request<RepositoryInvitation, BasicError | ValidationError>({
27673+
this.request<RepositoryInvitation | void, BasicError | ValidationError>({
2767427674
path: \`/repos/\${owner}/\${repo}/collaborators/\${username}\`,
2767527675
method: "PUT",
2767627676
body: data,
@@ -28505,7 +28505,7 @@ export class Api<
2850528505
},
2850628506
params: RequestParams = {},
2850728507
) =>
28508-
this.request<Contributor[], BasicError>({
28508+
this.request<Contributor[] | void, BasicError>({
2850928509
path: \`/repos/\${owner}/\${repo}/contributors\`,
2851028510
method: "GET",
2851128511
query: query,
@@ -28621,7 +28621,10 @@ export class Api<
2862128621
params: RequestParams = {},
2862228622
) =>
2862328623
this.request<
28624-
Deployment,
28624+
| Deployment
28625+
| {
28626+
message?: string;
28627+
},
2862528628
| {
2862628629
/** @example ""https://docs.github.com/rest/reference/repos#create-a-deployment"" */
2862728630
documentation_url?: string;
@@ -35792,7 +35795,7 @@ export class Api<
3579235795
repo: string,
3579335796
params: RequestParams = {},
3579435797
) =>
35795-
this.request<TeamRepository, void>({
35798+
this.request<TeamRepository | void, void>({
3579635799
path: \`/teams/\${teamId}/repos/\${owner}/\${repo}\`,
3579735800
method: "GET",
3579835801
format: "json",

tests/spec/axios/__snapshots__/basic.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4741,7 +4741,7 @@ export class Api<
47414741
body: MergesBody,
47424742
params: RequestParams = {},
47434743
) =>
4744-
this.request<MergesSuccessful, void | MergesConflict>({
4744+
this.request<MergesSuccessful | void, void | MergesConflict>({
47454745
path: \`/repos/\${owner}/\${repo}/merges\`,
47464746
method: "POST",
47474747
body: body,

tests/spec/axiosSingleHttpClient/__snapshots__/basic.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4745,7 +4745,7 @@ export class Api<SecurityDataType extends unknown> {
47454745
body: MergesBody,
47464746
params: RequestParams = {},
47474747
) =>
4748-
this.http.request<MergesSuccessful, void | MergesConflict>({
4748+
this.http.request<MergesSuccessful | void, void | MergesConflict>({
47494749
path: \`/repos/\${owner}/\${repo}/merges\`,
47504750
method: "POST",
47514751
body: body,

0 commit comments

Comments
 (0)