typerinth
Version:
A TypeScript library for interacting with the Modrinth API.
47 lines (46 loc) • 1.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Route_1 = require("../Route");
const SearchOptions_1 = require("../../interfaces/project/search/SearchOptions");
const errors_1 = require("../../errors");
class SearchProjectRoute extends Route_1.Route {
query;
searchOptions;
facets;
index;
offset;
limit;
constructor(baseUrl, userAgent, cacheManager, query, options = {}) {
super(baseUrl, userAgent, cacheManager);
this.query = query;
this.searchOptions = {
...(0, SearchOptions_1.getDefaultSearchOptions)(),
...options,
};
this.facets = this.searchOptions.facets;
this.index = this.searchOptions.index;
this.offset = this.searchOptions.offset;
this.limit = this.searchOptions.limit;
}
getCacheKey() {
return `search:${this.query}:${this.facets.stringify()}:${this.index}:${this.offset}:${this.limit}`;
}
getUrl() {
const url = Route_1.Route.addPathSegment(this.baseUrl, `/search`);
url.searchParams.append('query', this.query);
if (this.facets.getFacetGroups().length > 0)
url.searchParams.append('facets', this.facets.stringify());
url.searchParams.append('index', this.index);
url.searchParams.append('offset', this.offset.toString());
url.searchParams.append('limit', this.limit.toString());
return url;
}
parseData(data) {
if (!data)
throw new errors_1.UnexpectedApiError('Unexpected empty response');
if (data.error)
throw new errors_1.ApiError(data.error, data.description);
return data;
}
}
exports.default = SearchProjectRoute;