@mc-resource/cli
Version:
a Resource Manager for Minecraft
142 lines (141 loc) • 6.2 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Facets = exports.FacetOperation = exports.FacetType = void 0;
exports.searchProjects = searchProjects;
var FacetType;
(function (FacetType) {
FacetType["project_type"] = "projectType";
FacetType["categories"] = "categories";
FacetType["versions"] = "versions";
FacetType["client_side"] = "clientSide";
FacetType["server_side"] = "serverSide";
FacetType["open_source"] = "openSource";
FacetType["title"] = "title";
FacetType["author"] = "author";
FacetType["follows"] = "follows";
FacetType["project_id"] = "projectId";
FacetType["license"] = "license";
FacetType["downloads"] = "downloads";
FacetType["color"] = "color";
FacetType["created_timestamp"] = "createdTimestamp";
FacetType["modified_timestamp"] = "modifiedTimestamp";
})(FacetType || (exports.FacetType = FacetType = {}));
var FacetOperation;
(function (FacetOperation) {
FacetOperation[FacetOperation["="] = 0] = "=";
FacetOperation[FacetOperation["!="] = 1] = "!=";
FacetOperation[FacetOperation[">="] = 2] = ">=";
FacetOperation[FacetOperation[">"] = 3] = ">";
FacetOperation[FacetOperation["<="] = 4] = "<=";
FacetOperation[FacetOperation["<"] = 5] = "<";
})(FacetOperation || (exports.FacetOperation = FacetOperation = {}));
exports.Facets = {
[FacetType.project_type]: () => new SingleFacetBuilder(FacetType.project_type),
[FacetType.categories]: () => new SingleFacetBuilder(FacetType.categories),
[FacetType.versions]: () => new SingleFacetBuilder(FacetType.versions),
[FacetType.client_side]: () => new SingleFacetBuilder(FacetType.client_side),
[FacetType.server_side]: () => new SingleFacetBuilder(FacetType.server_side),
[FacetType.open_source]: () => new SingleFacetBuilder(FacetType.open_source),
[FacetType.title]: () => new SingleFacetBuilder(FacetType.title),
[FacetType.author]: () => new SingleFacetBuilder(FacetType.author),
[FacetType.follows]: () => new SingleFacetBuilder(FacetType.follows),
[FacetType.project_id]: () => new SingleFacetBuilder(FacetType.project_id),
[FacetType.license]: () => new SingleFacetBuilder(FacetType.license),
[FacetType.downloads]: () => new SingleFacetBuilder(FacetType.downloads),
[FacetType.color]: () => new SingleFacetBuilder(FacetType.color),
[FacetType.created_timestamp]: () => new SingleFacetBuilder(FacetType.created_timestamp),
[FacetType.modified_timestamp]: () => new SingleFacetBuilder(FacetType.modified_timestamp),
};
class SingleFacetBuilder {
constructor(facetType) {
this.alternatives = [];
this.facetType = facetType;
}
equals(value) {
this.facetOperation = FacetOperation['='];
this.facetValue = value;
this._commitFacet();
return this;
}
notEquals(value) {
this.facetOperation = FacetOperation['!='];
this.facetValue = value;
this._commitFacet();
return this;
}
greaterThanOrEqualsTo(value) {
this.facetOperation = FacetOperation['>='];
this.facetValue = value;
this._commitFacet();
return this;
}
greaterThan(value) {
this.facetOperation = FacetOperation['>'];
this.facetValue = value;
this._commitFacet();
return this;
}
lessThanOrEqualsTo(value) {
this.facetOperation = FacetOperation['<='];
this.facetValue = value;
this._commitFacet();
return this;
}
lessThan(value) {
this.facetOperation = FacetOperation['<'];
this.facetValue = value;
this._commitFacet();
return this;
}
or(...facets) {
facets.forEach((facet) => {
if (facet instanceof SingleFacetBuilder) {
this.alternatives.push(...facet.alternatives);
}
else {
const newFacet = new SingleFacetBuilder(this.facetType);
newFacet.facetOperation = this.facetOperation;
newFacet.facetValue = facet;
newFacet._commitFacet();
this.alternatives.push(...newFacet.alternatives);
}
});
return this;
}
_commitFacet() {
if (this.facetOperation === undefined || this.facetValue === undefined)
throw new Error('Cannot commit facet without operation and value.');
this.alternatives.push(`${this.facetType} ${FacetOperation[this.facetOperation]} ${this.facetValue}`);
return this.alternatives;
}
build() {
return this.alternatives;
}
}
function searchProjects(options) {
return __awaiter(this, void 0, void 0, function* () {
const params = new URLSearchParams();
if (options.facets) {
params.set('facets', JSON.stringify(Array.isArray(options.facets)
? options.facets
: [options.facets].map((v) => v.build())));
}
options.query && params.set('query', options.query);
options.limit && params.set('limit', options.limit.toString());
options.offset && params.set('offset', options.offset.toString());
options.sort && params.set('sort', options.sort);
const resp = yield this._request('GET', 'search', {
query: params,
});
return Object.assign(Object.assign({}, resp), { getFirst: () => { var _a; return (_a = resp.hits[0]) !== null && _a !== void 0 ? _a : null; } });
});
}