@mc-resource/cli
Version:
a Resource Manager for Minecraft
64 lines (63 loc) • 3.15 kB
JavaScript
;
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.ModrinthAPI = void 0;
const utils_1 = require("./utils");
const searchProjects_1 = require("./routes/projects/searchProjects");
const getProject_1 = require("./routes/projects/getProject");
const getProjectVersions_1 = require("./routes/versions/getProjectVersions");
class ModrinthAPI {
constructor(options) {
this.searchProjects = searchProjects_1.searchProjects;
this.getProject = getProject_1.getProject;
this.getProjectVersions = getProjectVersions_1.getProjectVersions;
this.baseURL = (options === null || options === void 0 ? void 0 : options.apiURL) || 'https://api.modrinth.com/v2/';
if (typeof options.userAgent === 'string') {
this.userAgent = options.userAgent;
}
else {
this.userAgent = (0, utils_1.generateUserAgent)(options.userAgent);
}
}
_request(method, endpoint, opts) {
return __awaiter(this, void 0, void 0, function* () {
const queryStr = (opts === null || opts === void 0 ? void 0 : opts.query) ? '?' + opts.query.toString() : '';
const url = new URL(endpoint + queryStr, (opts === null || opts === void 0 ? void 0 : opts.base) || this.baseURL);
try {
const res = yield fetch(url.toString(), {
method,
body: opts === null || opts === void 0 ? void 0 : opts.body,
headers: {
'User-Agent': this.userAgent,
Accept: 'application/json',
},
});
if (!res.ok) {
throw new Error(`Modrinth API Error: ${res.status} ${res.statusText}`.red);
}
const data = yield res.json();
return data;
}
catch (err) {
console.error(`Failed to fetch ${url.toString()}`.red);
if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
console.error('Connection timed out while accessing Modrinth API.'.red);
}
else if (err.code === 'EAI_AGAIN') {
console.error('DNS lookup failed (EAI_AGAIN). Are you online? DNS may be misconfigured.'
.red);
}
throw err;
}
});
}
}
exports.ModrinthAPI = ModrinthAPI;