Skip to content

Commit 37a4e74

Browse files
committed
Refactor API methods to remove unnecessary async/await and improve return handling
Note: Some of the functions return shapes differ as of this commit, so the places that use these functions should change in future commits
1 parent 13fecd4 commit 37a4e74

File tree

9 files changed

+50
-112
lines changed

9 files changed

+50
-112
lines changed

packages/dapper-ts/src/methods/communities.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66

77
import { type DapperTsInterface } from "../index";
88

9-
export async function getCommunities(
9+
export function getCommunities(
1010
this: DapperTsInterface,
1111
page?: number,
1212
ordering?: string,
@@ -22,7 +22,7 @@ export async function getCommunities(
2222
) {
2323
supportedOrdering = ordering as CommunityListOrderingEnum;
2424
}
25-
const data = await fetchCommunityList({
25+
return fetchCommunityList({
2626
config: this.config,
2727
queryParams: [
2828
{ key: "page", value: page, impotent: 1 },
@@ -36,24 +36,13 @@ export async function getCommunities(
3636
params: {},
3737
data: {},
3838
});
39-
40-
return {
41-
count: data.count,
42-
hasMore: Boolean(data.next),
43-
results: data.results,
44-
};
4539
}
4640

47-
export async function getCommunity(
48-
this: DapperTsInterface,
49-
communityId: string
50-
) {
51-
const data = await fetchCommunity({
41+
export function getCommunity(this: DapperTsInterface, communityId: string) {
42+
return fetchCommunity({
5243
config: this.config,
5344
params: { community_id: communityId },
5445
data: {},
5546
queryParams: {},
5647
});
57-
58-
return data;
5948
}

packages/dapper-ts/src/methods/communityFilters.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ export async function getCommunityFilters(
66
this: DapperTsInterface,
77
communityId: string
88
) {
9-
const data = await fetchCommunityFilters({
9+
return fetchCommunityFilters({
1010
config: this.config,
1111
params: { community_id: communityId },
1212
data: {},
1313
queryParams: {},
1414
});
15-
16-
return data;
1715
}

packages/dapper-ts/src/methods/dynamicHTML.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ export async function getDynamicHTML(
66
this: DapperTsInterface,
77
placement: string
88
) {
9-
const data = await fetchDynamicHTML({
9+
return fetchDynamicHTML({
1010
config: this.config,
1111
params: { placement },
1212
data: {},
1313
queryParams: {},
1414
});
15-
16-
return data.dynamic_htmls;
1715
}

packages/dapper-ts/src/methods/package.ts

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { z } from "zod";
22

33
import {
4-
ApiError,
54
type PackageVersionDependenciesRequestQueryParams,
65
fetchPackageChangelog,
76
fetchPackagePermissions,
@@ -23,7 +22,7 @@ export async function getPackageChangelog(
2322
packageName: string,
2423
versionNumber?: string
2524
) {
26-
const data = await fetchPackageChangelog({
25+
return fetchPackageChangelog({
2726
config: this.config,
2827
params: {
2928
namespace_id: namespaceId,
@@ -33,8 +32,6 @@ export async function getPackageChangelog(
3332
data: {},
3433
queryParams: {},
3534
});
36-
37-
return data;
3835
}
3936

4037
export async function getPackageReadme(
@@ -43,7 +40,7 @@ export async function getPackageReadme(
4340
packageName: string,
4441
versionNumber?: string
4542
) {
46-
const data = await fetchPackageReadme({
43+
return fetchPackageReadme({
4744
config: this.config,
4845
params: {
4946
namespace_id: namespaceId,
@@ -53,8 +50,6 @@ export async function getPackageReadme(
5350
data: {},
5451
queryParams: {},
5552
});
56-
57-
return data;
5853
}
5954

6055
export async function getPackageSource(
@@ -63,7 +58,7 @@ export async function getPackageSource(
6358
packageName: string,
6459
versionNumber?: string
6560
) {
66-
const data = await fetchPackageSource({
61+
return fetchPackageSource({
6762
config: this.config,
6863
params: {
6964
namespace_id: namespaceId,
@@ -73,8 +68,6 @@ export async function getPackageSource(
7368
data: {},
7469
queryParams: {},
7570
});
76-
77-
return data;
7871
}
7972

8073
export const versionsSchema = z
@@ -92,7 +85,7 @@ export async function getPackageVersions(
9285
namespaceId: string,
9386
packageName: string
9487
) {
95-
const data = await fetchPackageVersions({
88+
return fetchPackageVersions({
9689
config: this.config,
9790
params: {
9891
namespace_id: namespaceId,
@@ -101,8 +94,6 @@ export async function getPackageVersions(
10194
data: {},
10295
queryParams: {},
10396
});
104-
105-
return data;
10697
}
10798
export async function getPackageVersionDependencies(
10899
this: DapperTsInterface,
@@ -119,7 +110,7 @@ export async function getPackageVersionDependencies(
119110
},
120111
];
121112

122-
const data = await fetchPackageVersionDependencies({
113+
return fetchPackageVersionDependencies({
123114
config: this.config,
124115
params: {
125116
namespace_id: namespaceId,
@@ -129,16 +120,14 @@ export async function getPackageVersionDependencies(
129120
data: {},
130121
queryParams: options,
131122
});
132-
133-
return data;
134123
}
135124

136125
export async function getPackageWiki(
137126
this: DapperTsInterface,
138127
namespaceId: string,
139128
packageName: string
140129
) {
141-
const data = await fetchPackageWiki({
130+
return fetchPackageWiki({
142131
config: this.config,
143132
params: {
144133
namespace_id: namespaceId,
@@ -147,21 +136,17 @@ export async function getPackageWiki(
147136
data: {},
148137
queryParams: {},
149138
});
150-
151-
return data;
152139
}
153140

154141
export async function getPackageWikiPage(this: DapperTsInterface, id: string) {
155-
const data = await fetchPackageWikiPage({
142+
return fetchPackageWikiPage({
156143
config: this.config,
157144
params: {
158145
id,
159146
},
160147
data: {},
161148
queryParams: {},
162149
});
163-
164-
return data;
165150
}
166151

167152
export async function postPackageSubmissionMetadata(
@@ -173,7 +158,7 @@ export async function postPackageSubmissionMetadata(
173158
categories?: string[],
174159
community_categories?: { [key: string]: string[] }
175160
) {
176-
const data = await postPackageSubmission({
161+
return postPackageSubmission({
177162
config: this.config,
178163
params: {},
179164
data: {
@@ -186,24 +171,20 @@ export async function postPackageSubmissionMetadata(
186171
},
187172
queryParams: {},
188173
});
189-
190-
return data;
191174
}
192175

193176
export async function getPackageSubmissionStatus(
194177
this: DapperTsInterface,
195178
submissionId: string
196179
) {
197-
const response = await fetchPackageSubmissionStatus({
180+
return fetchPackageSubmissionStatus({
198181
config: this.config,
199182
params: {
200183
submission_id: submissionId,
201184
},
202185
data: {},
203186
queryParams: {},
204187
});
205-
206-
return response;
207188
}
208189

209190
export async function getPackagePermissions(
@@ -212,24 +193,14 @@ export async function getPackagePermissions(
212193
namespaceId: string,
213194
packageName: string
214195
) {
215-
try {
216-
const response = await fetchPackagePermissions({
217-
config: this.config,
218-
params: {
219-
community_id: communityId,
220-
namespace_id: namespaceId,
221-
package_name: packageName,
222-
},
223-
data: {},
224-
queryParams: {},
225-
});
226-
return response;
227-
} catch (error) {
228-
// In case of user not being logged in or stale session
229-
if (error instanceof ApiError && error.response.status === 401) {
230-
return undefined;
231-
} else {
232-
throw error;
233-
}
234-
}
196+
return fetchPackagePermissions({
197+
config: this.config,
198+
params: {
199+
community_id: communityId,
200+
namespace_id: namespaceId,
201+
package_name: packageName,
202+
},
203+
data: {},
204+
queryParams: {},
205+
});
235206
}

packages/dapper-ts/src/methods/packageListings.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
import { type DapperTsInterface } from "../index";
1212

13-
export async function getPackageListings(
13+
export function getPackageListings(
1414
this: DapperTsInterface,
1515
type: PackageListingType,
1616
ordering?: string,
@@ -75,7 +75,7 @@ export async function getPackageListings(
7575
let data;
7676

7777
if (type.kind === "community") {
78-
data = await fetchCommunityPackageListings({
78+
data = fetchCommunityPackageListings({
7979
config: this.config,
8080
params: {
8181
community_id: type.communityId,
@@ -84,7 +84,7 @@ export async function getPackageListings(
8484
queryParams: options,
8585
});
8686
} else if (type.kind === "namespace") {
87-
data = await fetchNamespacePackageListings({
87+
data = fetchNamespacePackageListings({
8888
config: this.config,
8989
params: {
9090
community_id: type.communityId,
@@ -94,7 +94,7 @@ export async function getPackageListings(
9494
queryParams: options,
9595
});
9696
} else if (type.kind === "package-dependants") {
97-
data = await fetchPackageDependantsListings({
97+
data = fetchPackageDependantsListings({
9898
config: this.config,
9999
params: {
100100
community_id: type.communityId,
@@ -110,11 +110,7 @@ export async function getPackageListings(
110110
);
111111
}
112112

113-
return {
114-
count: data.count,
115-
hasMore: Boolean(data.next),
116-
results: data.results,
117-
};
113+
return data;
118114
}
119115

120116
export async function getPackageListingDetails(
@@ -123,7 +119,7 @@ export async function getPackageListingDetails(
123119
namespaceId: string,
124120
packageName: string
125121
) {
126-
const data = await fetchPackageListingDetails({
122+
return fetchPackageListingDetails({
127123
config: this.config,
128124
params: {
129125
community_id: communityId,
@@ -133,6 +129,4 @@ export async function getPackageListingDetails(
133129
data: {},
134130
queryParams: {},
135131
});
136-
137-
return data;
138132
}

packages/dapper-ts/src/methods/packageVersion.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function getPackageVersionDetails(
88
packageName: string,
99
packageVersion: string
1010
) {
11-
const data = await fetchPackageVersionDetails({
11+
return fetchPackageVersionDetails({
1212
config: this.config,
1313
params: {
1414
namespace_id: namespaceId,
@@ -18,6 +18,4 @@ export async function getPackageVersionDetails(
1818
data: {},
1919
queryParams: {},
2020
});
21-
22-
return data;
2321
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { fetchRatedPackages } from "@thunderstore/thunderstore-api";
1+
import {
2+
RatedPackagesResponseData,
3+
fetchRatedPackages,
4+
} from "@thunderstore/thunderstore-api";
25

36
import { type DapperTsInterface } from "../index";
47

5-
export async function getRatedPackages(this: DapperTsInterface) {
6-
const data = await fetchRatedPackages({
8+
export function getRatedPackages(
9+
this: DapperTsInterface
10+
): Promise<RatedPackagesResponseData> {
11+
return fetchRatedPackages({
712
config: this.config,
813
params: {},
914
data: {},
1015
queryParams: {},
1116
});
12-
13-
return data;
1417
}

0 commit comments

Comments
 (0)