Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/dapper-fake/src/fakers/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,28 @@ export const getFakeCommunities: GetCommunities = async (
}

const count = communities.length;
const pageCommunities = communities.splice((page - 1) * pageSize, pageSize);
const currentPage = Math.max(page, 1);
const startIndex = (currentPage - 1) * pageSize;
const pageCommunities = communities.slice(startIndex, startIndex + pageSize);
const hasNext = startIndex + pageSize < count;
const buildPageUrl = (targetPage: number) => {
const params = new URLSearchParams();
params.set("page", String(targetPage));
if (ordering) {
params.set("ordering", ordering);
}
if (typeof search === "string" && search.trim().length > 0) {
params.set("search", search);
}
return `https://thunderstore.io/api/cyberstorm/community/?${params.toString()}`;
};
const next = hasNext ? buildPageUrl(currentPage + 1) : null;
const previous = currentPage > 1 ? buildPageUrl(currentPage - 1) : null;

return {
count,
hasMore: page > fullPages + 1,
next,
previous,
results: pageCommunities,
};
};
Expand Down
119 changes: 95 additions & 24 deletions packages/dapper-fake/src/fakers/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,93 @@ const getFakePackageListing = (
// here that's done for community listing, but getting all the filters
// to work properly might not be worth the effort.
export const getFakePackageListings = async (
type: PackageListingType
// ordering = "last-updated",
// page = 1,
// q = "",
// includedCategories = [],
// excludedCategories = [],
// section = "",
// nsfw = false,
// deprecated = false
) => ({
count: 200,
hasMore: true,
results: range(20).map(() =>
getFakePackageListing(
type.communityId,
type.kind === "namespace" ? type.namespaceId : faker.word.sample()
)
),
});
type: PackageListingType,
ordering = "last-updated",
page = 1,
q = "",
includedCategories: string[] = [],
excludedCategories: string[] = [],
section = "",
nsfw = false,
deprecated = false
) => {
const pageSize = 20;
const count = 200;
const normalizedPage = Number.isFinite(page)
? Math.max(1, Math.trunc(page))
: 1;
const currentPage = normalizedPage;
const startIndex = (currentPage - 1) * pageSize;
const endIndex = Math.min(startIndex + pageSize, count);
const pageLength = Math.max(endIndex - startIndex, 0);

const collectionPath = (() => {
switch (type.kind) {
case "community":
return `api/cyberstorm/listing/${type.communityId.toLowerCase()}/`;
case "namespace":
return `api/cyberstorm/listing/${type.communityId.toLowerCase()}/${type.namespaceId.toLowerCase()}/`;
case "package-dependants":
return `api/cyberstorm/listing/${type.communityId.toLowerCase()}/${type.namespaceId.toLowerCase()}/${type.packageName.toLowerCase()}/dependants/`;
default:
return "api/cyberstorm/listing/";
}
})();

const buildQueryString = (targetPage: number) => {
const params = new URLSearchParams();
params.set("page", String(targetPage));
if (ordering) {
params.set("ordering", ordering);
}
if (q) {
params.set("q", q);
}
includedCategories?.forEach((value) => {
params.append("included_categories", value);
});
excludedCategories?.forEach((value) => {
params.append("excluded_categories", value);
});
if (section) {
params.set("section", section);
}
if (nsfw) {
params.set("nsfw", "true");
}
if (deprecated) {
params.set("deprecated", "true");
}
return params.toString();
};

const buildPageUrl = (targetPage: number) =>
`https://thunderstore.io/${collectionPath}?${buildQueryString(targetPage)}`;

const hasNext = endIndex < count;
const next = hasNext ? buildPageUrl(currentPage + 1) : null;
const previous = currentPage > 1 ? buildPageUrl(currentPage - 1) : null;

const results = range(pageLength).map((index) => {
const namespaceId =
type.kind === "namespace" || type.kind === "package-dependants"
? type.namespaceId
: `${type.communityId}-namespace-${currentPage}-${index}`;
const packageName =
type.kind === "package-dependants"
? `${type.packageName.toLowerCase()}-dependant-${currentPage}-${index}`
: `${namespaceId}-package-${currentPage}-${index}`;

return getFakePackageListing(type.communityId, namespaceId, packageName);
});

return {
count,
next,
previous,
results,
};
};

const getFakeDependencies = async (
community: string,
Expand Down Expand Up @@ -274,11 +342,14 @@ export const getFakePackageVersionDependencies = async (
page?: number
) => {
setSeed(`${namespace}-${name}-${version}`);
page = page ?? 1;
const normalizedPage =
typeof page === "number" && Number.isFinite(page)
? Math.max(1, Math.trunc(page))
: 1;

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

const start = (page - 1) * 10;
const start = (normalizedPage - 1) * 10;
const end = start + 10;
const items = fakePackageVersionDependencies.slice(start, end);

Expand All @@ -287,13 +358,13 @@ export const getFakePackageVersionDependencies = async (
next:
end < fakePackageVersionDependencies.length
? `https://thunderstore.io/api/cyberstorm/package/${namespace}/${name}/v/${version}/dependencies/?page=${
page + 1
normalizedPage + 1
}`
: null,
previous:
page > 1
normalizedPage > 1
? `https://thunderstore.io/api/cyberstorm/package/${namespace}/${name}/v/${version}/dependencies/?page=${
page - 1
normalizedPage - 1
}`
: null,
results: items,
Expand Down
19 changes: 4 additions & 15 deletions packages/dapper-ts/src/methods/communities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

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

export async function getCommunities(
export function getCommunities(
this: DapperTsInterface,
page?: number,
ordering?: string,
Expand All @@ -22,7 +22,7 @@ export async function getCommunities(
) {
supportedOrdering = ordering as CommunityListOrderingEnum;
}
const data = await fetchCommunityList({
return fetchCommunityList({
config: this.config,
queryParams: [
{ key: "page", value: page, impotent: 1 },
Expand All @@ -36,24 +36,13 @@ export async function getCommunities(
params: {},
data: {},
});

return {
count: data.count,
hasMore: Boolean(data.next),
results: data.results,
};
}

export async function getCommunity(
this: DapperTsInterface,
communityId: string
) {
const data = await fetchCommunity({
export function getCommunity(this: DapperTsInterface, communityId: string) {
return fetchCommunity({
config: this.config,
params: { community_id: communityId },
data: {},
queryParams: {},
});

return data;
}
4 changes: 1 addition & 3 deletions packages/dapper-ts/src/methods/communityFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ export async function getCommunityFilters(
this: DapperTsInterface,
communityId: string
) {
const data = await fetchCommunityFilters({
return fetchCommunityFilters({
config: this.config,
params: { community_id: communityId },
data: {},
queryParams: {},
});

return data;
}
4 changes: 1 addition & 3 deletions packages/dapper-ts/src/methods/dynamicHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ export async function getDynamicHTML(
this: DapperTsInterface,
placement: string
) {
const data = await fetchDynamicHTML({
return fetchDynamicHTML({
config: this.config,
params: { placement },
data: {},
queryParams: {},
});

return data.dynamic_htmls;
}
Loading
Loading