-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@thunderstore/thunderstore-api: add two package listing fetchers
One will fetch package listings scoped on a specific community, while the other scopes for both community and namespace. Both fetchers use the same arguments to order, paginatem, and filter the package listings. Fetchers receive these arguments as a single object, for which the interface is exported from the package. I would have liked them to get the args separately for simplicity's and consistency's sake, but that would have lead to lot of boilerplate code both in thunderstore-api and in dapper-ts. Especially when more package listing fetchers are added in the future. Refs TS-1875
- Loading branch information
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/thunderstore-api/src/fetch/__tests__/communityPackageListings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { config } from "./defaultConfig"; | ||
import { fetchCommunityPackageListings } from "../communityPackageListings"; | ||
|
||
interface PartialPackage { | ||
community_identifier: string; | ||
} | ||
|
||
it("receives community scoped paginated package listing", async () => { | ||
const communityId = "riskofrain2"; | ||
const response = await fetchCommunityPackageListings(config, communityId); | ||
|
||
expect(typeof response.count).toEqual("number"); | ||
expect(Array.isArray(response.results)).toEqual(true); | ||
|
||
response.results.forEach((pkg: PartialPackage) => { | ||
expect(pkg.community_identifier.toLowerCase()).toStrictEqual(communityId); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/thunderstore-api/src/fetch/__tests__/namespacePackageListings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { config } from "./defaultConfig"; | ||
import { fetchNamespacePackageListings } from "../namespacePackageListings"; | ||
|
||
interface PartialPackage { | ||
community_identifier: string; | ||
namespace: string; | ||
} | ||
|
||
it("receives namespace scoped paginated package listing", async () => { | ||
const communityId = "riskofrain2"; | ||
const namespaceId = "testteam"; | ||
const response = await fetchNamespacePackageListings( | ||
config, | ||
communityId, | ||
namespaceId | ||
); | ||
|
||
expect(typeof response.count).toEqual("number"); | ||
expect(Array.isArray(response.results)).toEqual(true); | ||
|
||
response.results.forEach((pkg: PartialPackage) => { | ||
expect(pkg.community_identifier.toLowerCase()).toStrictEqual(communityId); | ||
expect(pkg.namespace.toLowerCase()).toStrictEqual(namespaceId); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/thunderstore-api/src/fetch/communityPackageListings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { RequestConfig } from "../index"; | ||
import { apiFetch } from "../apiFetch"; | ||
import { serializeQueryString } from "../queryString"; | ||
import { PackageListingQueryParams } from "../types"; | ||
|
||
export async function fetchCommunityPackageListings( | ||
config: RequestConfig, | ||
communityId: string, | ||
options?: PackageListingQueryParams | ||
) { | ||
const path = `api/cyberstorm/package/${communityId.toLowerCase()}/`; | ||
|
||
const queryParams = [ | ||
{ key: "ordering", value: options?.ordering, impotent: "last-updated" }, | ||
{ key: "page", value: options?.page, impotent: 1 }, | ||
{ key: "q", value: options?.q.trim() }, | ||
{ key: "included_categories", value: options?.includedCategories }, | ||
{ key: "excluded_categories", value: options?.excludedCategories }, | ||
{ key: "section", value: options?.section }, | ||
{ key: "nsfw", value: options?.nsfw, impotent: false }, | ||
{ key: "deprecated", value: options?.deprecated, impotent: false }, | ||
]; | ||
const query = serializeQueryString(queryParams); | ||
|
||
return await apiFetch(config, path, query); | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/thunderstore-api/src/fetch/namespacePackageListings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { RequestConfig } from "../index"; | ||
import { apiFetch } from "../apiFetch"; | ||
import { serializeQueryString } from "../queryString"; | ||
import { PackageListingQueryParams } from "../types"; | ||
|
||
export async function fetchNamespacePackageListings( | ||
config: RequestConfig, | ||
communityId: string, | ||
namespaceId: string, | ||
options?: PackageListingQueryParams | ||
) { | ||
const path = `api/cyberstorm/package/${communityId.toLowerCase()}/${namespaceId.toLowerCase()}/`; | ||
|
||
const queryParams = [ | ||
{ key: "ordering", value: options?.ordering, impotent: "last-updated" }, | ||
{ key: "page", value: options?.page, impotent: 1 }, | ||
{ key: "q", value: options?.q.trim() }, | ||
{ key: "included_categories", value: options?.includedCategories }, | ||
{ key: "excluded_categories", value: options?.excludedCategories }, | ||
{ key: "section", value: options?.section }, | ||
{ key: "nsfw", value: options?.nsfw, impotent: false }, | ||
{ key: "deprecated", value: options?.deprecated, impotent: false }, | ||
]; | ||
const query = serializeQueryString(queryParams); | ||
|
||
return await apiFetch(config, path, query); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Parameters for ordering, paginating, and filtering package lists. | ||
*/ | ||
export interface PackageListingQueryParams { | ||
/** Ordering for the results */ | ||
ordering: string; | ||
/** Page number for the paginated results */ | ||
page: number; | ||
/** Free text search for filtering e.g. by package name */ | ||
q: string; | ||
/** Ids of categories the package MUST belong to */ | ||
includedCategories: number[]; | ||
/** Ids of categories the package MUST NOT belong to */ | ||
excludedCategories: number[]; | ||
/** | ||
* UUID of community's section the package MUST fit. | ||
* | ||
* Sections are community-specific shorthands for filtering by | ||
* multiple included and excluded categories at once. | ||
* */ | ||
section: string; | ||
/** Should NSFW packages be included (by default they're not) */ | ||
nsfw: boolean; | ||
/** Should deprecated packages be included (by default they're not) */ | ||
deprecated: boolean; | ||
} |