typerinth
Version:
A TypeScript library for interacting with the Modrinth API.
288 lines (287 loc) • 12.3 kB
TypeScript
import MROptions from './interfaces/MROptions';
import { URL } from 'url';
import { Project, SearchResult } from './interfaces/project';
import { Range0to100 } from './types/Range';
import ModrinthStatistics from './interfaces/miscellaneous/ModrinthStatistics';
import SearchOptions from './interfaces/project/search/SearchOptions';
import { User } from './interfaces/users';
import { TagType } from './enums/TagType';
import { TagTypeMapping } from './types/TagTypeMapping';
import { FullLicense } from './interfaces/tags';
import { ProjectVersion, ProjectVersionFromHashOptions, ProjectVersionSearchOptions } from './interfaces/version';
import { GetTokenResponse } from './interfaces/auth';
import { AuthScope } from './enums/AuthScope';
import { TeamMember } from './interfaces/teams';
/**
* The main class for the Modrinth API
*
* @example
* import { Modrinth } from "typerinth";
*
* const modrinth = new Modrinth();
*
* modrinth.search("fabric").then(console.log);
*/
export default class Modrinth {
private options;
private cacheManager;
/**
* Create a new Modrinth instance
* @param options Options for the Modrinth instance
* @example
* const modrinth = new Modrinth({
* baseUrl: 'https://api.modrinth.com',
* apiVersion: 'v2',
* userAgent: 'AppName/Version',
* cache: {
* ttl: 600,
* checkperiod: 120,
* useCache: true,
* },
* });
*/
constructor(options?: MROptions);
/**
* Get the options for the Modrinth instance
* @returns The options for the Modrinth instance
*/
getOptions(): MROptions;
/**
* Get the base URL for the Modrinth API
* @returns The base URL for the Modrinth API
*/
getApiUrl(): URL;
/**
* Search for projects
* @param query The query to search for
* @param options Options for the search
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @returns The search results
* @category Projects
* @example
* const result = await modrinth.search('life', {
* limit: 3,
* index: SearchIndex.Downloads,
* facets: new SearchFacets(
* new FacetGroup(
* new Facet(FacetType.Categories, FacetOperation.EQUALS, 'forge')
* ),
* new FacetGroup(
* new Facet(FacetType.Versions, FacetOperation.EQUALS, '1.16.5'),
* new Facet(FacetType.Versions, FacetOperation.EQUALS, '1.17.1')
* )
* ),
* });
*/
search(query: string, options?: SearchOptions): Promise<SearchResult>;
/**
* Get a project by its ID or slug
* @param projectId The ID or slug of the project to get
* @returns The project with the given ID or slug
* @throws {} {@link ProjectNotFoundError} If the project with the given ID or slug does not exist
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Projects
*/
getProject(projectId: string): Promise<Project>;
/**
* Get multiple projects by their IDs or slugs
* @param projectIds The IDs or slugs of the projects to get
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Projects
*/
getProjects(projectIds: string[]): Promise<Project[]>;
/**
* Get a random selection of projects
* @param count The number of projects to get (between 0 and 100)
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Projects
*/
getRandomProjects(count: Range0to100): Promise<Project[]>;
/**
* Check if a project is valid
* @param projectId The ID or slug of the project to check
* @returns Whether the project is valid
* @category Projects
*/
checkProjectValidity(projectId: string): Promise<boolean>;
/**
* Get the versions of a project
* @param projectId The ID or slug of the project to get the versions of
* @param options Options for the search
* @throws {} {@link VersionNotFoundError} If there are no versions for the project
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Versions
*/
getProjectVersions(projectId: string, options?: ProjectVersionSearchOptions): Promise<ProjectVersion[]>;
/**
* Get a version of a project
* @param versionId The ID of the version to get
* @throws {} {@link VersionNotFoundError} If the version with the given ID does not exist
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Versions
*/
getVersion(versionId: string): Promise<ProjectVersion>;
/**
* Get multiple versions by their IDs
* @param versionIds The IDs of the versions to get
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Versions
*/
getVersions(versionIds: string[]): Promise<ProjectVersion[]>;
/**
* Get a version from a file hash
* @param fileHash The hash of the file to get the version of
* @param options Options for the search
* @returns The version with the given file hash
* @throws {} {@link VersionNotFoundError} If the version with the given file hash does not exist
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Versions
*/
getVersionFromFileHash(fileHash: string, options?: ProjectVersionFromHashOptions): Promise<ProjectVersion>;
/**
* Get a user by their ID or username
* @param userId The ID or username of the user to get
* @throws {} {@link UserNotFoundError} If the user with the given ID or username does not exist
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Users
*/
getUser(userId: string): Promise<User>;
/**
* Get multiple users by their IDs or usernames
* @param userIds The IDs or usernames of the users to get
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Users
*/
getUsers(userIds: string[]): Promise<User[]>;
/**
* Get a user's projects by their ID or username
* @param userId The ID or username of the user
* @throws {} {@link UserProjectsNotFoundError} If the user's projects do not exist
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Users
*/
getUserProjects(userId: string): Promise<Project[]>;
/**
* Get the projects followed by a user
*
* **Rquires authorization** with scope:
* > `USER_READ`
* @param userId The ID or username of the user to get the followed projects of
* @param authorization The authorization header to use (if not set, the one from the options will be used)
* @throws {} {@link UserNotFoundError} If the user with the given ID or username does not exist
* @throws {} {@link ApiError} If an error occurs
* @category Users
*/
getFollowedProjects(userId: string, authorization?: string): Promise<Project[]>;
/**
* Get the user that is authenticated with the authorization header
*
* **Rquires authorization** with scope:
* > `USER_READ`
* @param authorization The authorization header to use (if not set, the one from the options will be used)
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @returns The authenticated user
* @category Users
*/
getAuthUser(authorization?: string): Promise<User>;
/**
* Get the team members of a project
*
* @param projectId The ID or slug of the project to get the team members of
* @throws {} {@link ProjectNotFoundError} If the project with the given ID or slug does not exist
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @returns The team members of the project with the given ID or slug
* @category Teams
*/
getProjectTeamMembers(projectId: string): Promise<TeamMember[]>;
/**
* Get the team members of a team
*
* @param teamId The ID of the team to get the members of
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @returns The team members of the team with the given ID
* @category Teams
*/
getTeamMembers(teamId: string): Promise<TeamMember[]>;
/**
* Get the members of multiple teams
*
* @param teamIds The IDs of the teams to get the members of
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @returns The members of the teams with the given IDs
* @category Teams
*/
getMultipleTeamMembers(teamIds: string[]): Promise<TeamMember[][]>;
/**
* Get a tag by its type
* @param tagType The type of the tag to get
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Tags
*/
getTag<T extends TagType>(tagType: T): Promise<TagTypeMapping[T]>;
/**
* Get a license by its ID
* @param licenseId The ID of the license to get
* @throws {} {@link LicenseNotFoundError} If the license with the given ID does not exist
* @throws {} {@link ApiError} If an error occurs
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Tags
*/
getLicense(licenseId: string): Promise<FullLicense>;
/**
* Get the statistics for Modrinth
* @returns The statistics for Modrinth
* @throws {} {@link UnexpectedApiError} If an unexpected error occurs
* @category Miscellaneous
*/
getStatistics(): Promise<ModrinthStatistics>;
/**
* Get an access token from an authorization code
*
* (Rquest to `https://api.modrinth.com/_internal/oauth/token`)
*
* [Modrinth OAuth Guide](https://docs.modrinth.com/guide/oauth/)
* @param code The authorization code gotten from the authorization URL
* @param clientId The ID of the client
* @param redirectUri The uri to redirect to after getting the token (must be listed in the client's redirect URIs)
* @param authorization The authorization header to use (if not set, the one from the options will be used)
* @returns The access token and other token information
* @category Auth
*/
getToken(code: string, clientId: string, redirectUri: string, authorization?: string): Promise<GetTokenResponse>;
/**
* Generate an authorization URL to get an authorization code
*
* [Modrinth OAuth Guide](https://docs.modrinth.com/guide/oauth/)
* @param clientId The ID of the client
* @param redirectUri The uri to redirect to with the authorization code (must be listed in the client's redirect URIs)
* @param scopes The scopes to request authorization for (must be listed in the client's scopes)
* @param state An optional state parameter to include in the URL (useful for CSRF protection)
* @returns The URL to get the authorization code
* @category Auth
*
* @example
* new Modrinth().generateAuthorizationUrl(
* CLIENT_ID,
* "https://example.com/auth/callback",
* [AuthScope.UserRead, AuthScope.PayoutsRead]
* );
*/
generateAuthorizationUrl(clientId: string, redirectUri: string, scopes: AuthScope[], state?: string): string;
}