Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const request = require('request');
const fs = require('fs');
import pkg from 'request';
import { createWriteStream } from 'fs';

function sendRequest(urlParts) {
return new Promise((resolve, reject) => {
request.get('https://serverjars.com/api/' + urlParts, (error, response, body) => {
pkg.get('https://serverjars.com/api/' + urlParts, (error, response, body) => {
if (error) {
reject({title: 'Error Occurred', message: 'Try again later!'})
} else {
Expand All @@ -27,30 +27,69 @@ function sendRequest(urlParts) {
});
}


function fetchLatest(type) {
return sendRequest(`fetchLatest/${type}`)
/**
* Fetch details on the latest jar for a type and category.
* @param {*} type
* @param {*} category
* @returns
*/
function fetchLatest(type,category) {
return sendRequest(`fetchLatest/${type}/${category}`)
}

/**
* Fetch a direct download link to a specific jar type with either the latest version or a specified one.
* @param {*} type
* @param {*} version
* @param {*} output
* @returns
*/
function downloadJar(type, version, output) {
return new Promise((resolve) => {
let file = fs.createWriteStream(output);
resolve(request.get(`https://serverjars.com/api/fetchJar/${type}/${version}`).pipe(file));
let file = createWriteStream(output);
resolve(pkg.get(`https://serverjars.com/api/fetchJar/${type}/${version}`).pipe(file));
});
}

function fetchAll(type) {
return sendRequest(`fetchAll/${type}`)
/**
* Fetch details on the all the jars for a type and category.
* @param {*} type
* @param {*} category
* @param {*} max
* @returns
*/
function fetchAll(type,category,max = 5) {
return sendRequest(`fetchAll/${type}/${category}/${max}`)
}

/**
* Fetch a list of the possible jar types.
* @returns
*/
function fetchTypes() {
return sendRequest('fetchTypes');

}

/**
* Fetch a list of the possible jar's for a specific type.
* @param {*} mainType
* @returns
*/
function fetchSubTypes(mainType) {
return sendRequest(`fetchTypes/${type}`)
return sendRequest(`fetchTypes/${mainType}`)
}

/**
* Fetch the details of a single jar.
* @param {*} type
* @param {*} category
* @param {*} version
* @returns
*/
function fetchDetails(type,category,version) {
return sendRequest(`fetchDetails/${type}/${category}/${version}`);
}


module.exports = {fetchLatest, downloadJar, fetchAll, fetchTypes, fetchSubTypes};
export default {fetchLatest, downloadJar, fetchAll, fetchTypes, fetchSubTypes, fetchDetails};
Loading