typerinth
Version:
A TypeScript library for interacting with the Modrinth API.
67 lines (66 loc) • 2.52 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Route_1 = require("../Route");
const SearchOptions_1 = require("../../interfaces/project/search/SearchOptions");
const SearchFacets_1 = __importDefault(require("../../util/facets/SearchFacets"));
const errors_1 = require("../../errors");
class SearchProjectRoute extends Route_1.Route {
query;
searchOptions;
facets;
filters;
sort;
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.filters = this.searchOptions.filters;
this.sort = this.searchOptions.sort;
this.offset = this.searchOptions.offset;
this.limit = this.searchOptions.limit;
}
getCacheKey() {
return `search:${this.query}:${this.getStringifiedFacets()}:${this.sort}:${this.offset}:${this.limit}`;
}
getUrl() {
const url = Route_1.Route.addPathSegment(this.baseUrl, `/search`);
url.searchParams.append('query', this.query);
if (this.getSearchFacets()) {
url.searchParams.append('facets', this.getSearchFacets().stringify());
}
url.searchParams.append('index', this.sort);
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;
}
getSearchFacets() {
const hasValidFacets = this.facets && this.facets.getFacetGroups().length > 0;
const hasValidFilters = this.filters !== undefined;
if (hasValidFacets)
return this.facets;
if (hasValidFilters)
return SearchFacets_1.default.fromFilters(this.filters);
return null;
}
getStringifiedFacets() {
const facets = this.getSearchFacets();
return facets ? facets.stringify() : '[]';
}
}
exports.default = SearchProjectRoute;