Skip to content

Commit bc2613b

Browse files
committed
Update dapper-fake functions to match the changed dapper methods return shapes (pagination)
1 parent 37a4e74 commit bc2613b

File tree

2 files changed

+114
-26
lines changed

2 files changed

+114
-26
lines changed

packages/dapper-fake/src/fakers/community.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,28 @@ export const getFakeCommunities: GetCommunities = async (
8383
}
8484

8585
const count = communities.length;
86-
const pageCommunities = communities.splice((page - 1) * pageSize, pageSize);
86+
const currentPage = Math.max(page, 1);
87+
const startIndex = (currentPage - 1) * pageSize;
88+
const pageCommunities = communities.slice(startIndex, startIndex + pageSize);
89+
const hasNext = startIndex + pageSize < count;
90+
const buildPageUrl = (targetPage: number) => {
91+
const params = new URLSearchParams();
92+
params.set("page", String(targetPage));
93+
if (ordering) {
94+
params.set("ordering", ordering);
95+
}
96+
if (typeof search === "string" && search.trim().length > 0) {
97+
params.set("search", search);
98+
}
99+
return `https://thunderstore.io/api/cyberstorm/community/?${params.toString()}`;
100+
};
101+
const next = hasNext ? buildPageUrl(currentPage + 1) : null;
102+
const previous = currentPage > 1 ? buildPageUrl(currentPage - 1) : null;
87103

88104
return {
89105
count,
90-
hasMore: page > fullPages + 1,
106+
next,
107+
previous,
91108
results: pageCommunities,
92109
};
93110
};

packages/dapper-fake/src/fakers/package.ts

Lines changed: 95 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,93 @@ const getFakePackageListing = (
3636
// here that's done for community listing, but getting all the filters
3737
// to work properly might not be worth the effort.
3838
export const getFakePackageListings = async (
39-
type: PackageListingType
40-
// ordering = "last-updated",
41-
// page = 1,
42-
// q = "",
43-
// includedCategories = [],
44-
// excludedCategories = [],
45-
// section = "",
46-
// nsfw = false,
47-
// deprecated = false
48-
) => ({
49-
count: 200,
50-
hasMore: true,
51-
results: range(20).map(() =>
52-
getFakePackageListing(
53-
type.communityId,
54-
type.kind === "namespace" ? type.namespaceId : faker.word.sample()
55-
)
56-
),
57-
});
39+
type: PackageListingType,
40+
ordering = "last-updated",
41+
page = 1,
42+
q = "",
43+
includedCategories: string[] = [],
44+
excludedCategories: string[] = [],
45+
section = "",
46+
nsfw = false,
47+
deprecated = false
48+
) => {
49+
const pageSize = 20;
50+
const count = 200;
51+
const normalizedPage = Number.isFinite(page)
52+
? Math.max(1, Math.trunc(page))
53+
: 1;
54+
const currentPage = normalizedPage;
55+
const startIndex = (currentPage - 1) * pageSize;
56+
const endIndex = Math.min(startIndex + pageSize, count);
57+
const pageLength = Math.max(endIndex - startIndex, 0);
58+
59+
const collectionPath = (() => {
60+
switch (type.kind) {
61+
case "community":
62+
return `api/cyberstorm/listing/${type.communityId.toLowerCase()}/`;
63+
case "namespace":
64+
return `api/cyberstorm/listing/${type.communityId.toLowerCase()}/${type.namespaceId.toLowerCase()}/`;
65+
case "package-dependants":
66+
return `api/cyberstorm/listing/${type.communityId.toLowerCase()}/${type.namespaceId.toLowerCase()}/${type.packageName.toLowerCase()}/dependants/`;
67+
default:
68+
return "api/cyberstorm/listing/";
69+
}
70+
})();
71+
72+
const buildQueryString = (targetPage: number) => {
73+
const params = new URLSearchParams();
74+
params.set("page", String(targetPage));
75+
if (ordering) {
76+
params.set("ordering", ordering);
77+
}
78+
if (q) {
79+
params.set("q", q);
80+
}
81+
includedCategories?.forEach((value) => {
82+
params.append("included_categories", value);
83+
});
84+
excludedCategories?.forEach((value) => {
85+
params.append("excluded_categories", value);
86+
});
87+
if (section) {
88+
params.set("section", section);
89+
}
90+
if (nsfw) {
91+
params.set("nsfw", "true");
92+
}
93+
if (deprecated) {
94+
params.set("deprecated", "true");
95+
}
96+
return params.toString();
97+
};
98+
99+
const buildPageUrl = (targetPage: number) =>
100+
`https://thunderstore.io/${collectionPath}?${buildQueryString(targetPage)}`;
101+
102+
const hasNext = endIndex < count;
103+
const next = hasNext ? buildPageUrl(currentPage + 1) : null;
104+
const previous = currentPage > 1 ? buildPageUrl(currentPage - 1) : null;
105+
106+
const results = range(pageLength).map((index) => {
107+
const namespaceId =
108+
type.kind === "namespace" || type.kind === "package-dependants"
109+
? type.namespaceId
110+
: `${type.communityId}-namespace-${currentPage}-${index}`;
111+
const packageName =
112+
type.kind === "package-dependants"
113+
? `${type.packageName.toLowerCase()}-dependant-${currentPage}-${index}`
114+
: `${namespaceId}-package-${currentPage}-${index}`;
115+
116+
return getFakePackageListing(type.communityId, namespaceId, packageName);
117+
});
118+
119+
return {
120+
count,
121+
next,
122+
previous,
123+
results,
124+
};
125+
};
58126

59127
const getFakeDependencies = async (
60128
community: string,
@@ -274,11 +342,14 @@ export const getFakePackageVersionDependencies = async (
274342
page?: number
275343
) => {
276344
setSeed(`${namespace}-${name}-${version}`);
277-
page = page ?? 1;
345+
const normalizedPage =
346+
typeof page === "number" && Number.isFinite(page)
347+
? Math.max(1, Math.trunc(page))
348+
: 1;
278349

279350
// Split the fake data into pages of 10 items each.
280351

281-
const start = (page - 1) * 10;
352+
const start = (normalizedPage - 1) * 10;
282353
const end = start + 10;
283354
const items = fakePackageVersionDependencies.slice(start, end);
284355

@@ -287,13 +358,13 @@ export const getFakePackageVersionDependencies = async (
287358
next:
288359
end < fakePackageVersionDependencies.length
289360
? `https://thunderstore.io/api/cyberstorm/package/${namespace}/${name}/v/${version}/dependencies/?page=${
290-
page + 1
361+
normalizedPage + 1
291362
}`
292363
: null,
293364
previous:
294-
page > 1
365+
normalizedPage > 1
295366
? `https://thunderstore.io/api/cyberstorm/package/${namespace}/${name}/v/${version}/dependencies/?page=${
296-
page - 1
367+
normalizedPage - 1
297368
}`
298369
: null,
299370
results: items,

0 commit comments

Comments
 (0)