esijs
Version:
A simple module for ESI.
260 lines (249 loc) • 8.82 kB
JavaScript
const chalk = require('chalk')
const axios = require('axios')
const path = require('path')
const fs = require('fs')
const { URLSearchParams } = require('node:url')
const { projectPath, projectConfig, localConfig } = require('./constants')
const { version } = require('../../package.json')
const DEFAULT_USER_AGENT = `esiJS/${version}`
// documented in utility.js
function getSettings() {
let settings
if (checkForConfig()) {
settings = fs.readFileSync(projectConfig, 'utf8')
return JSON.parse(settings)
} else {
log(`No project config file! Attempting to revert to default configuration...`, 'WARN')
settings = fs.readFileSync(localConfig, 'utf8')
}
return JSON.parse(settings)
}
/**
* @private
* @param {string} msg - The error message.
* @param {string} code - The error code.
* @param {string} url - The endpoint url the error occured on.
*/
function buildError(msg, code, url) {
let error = new Error(msg)
url ? (error.url = url) : false
error.code = code ? code : 'NO_CODE_DEFINED'
return error
}
/**
* @private
* @param {string} message The log message.
* @param {string} type Either 'INFO', 'WARN', or 'WARNING'.
*/
function log(message, type = 'info') {
if (message) {
switch (type.toLowerCase()) {
case 'info': {
console.log(`${chalk.green(`[esiJS:${type.toUpperCase()}]:`)} ${message}`)
break
}
case 'warn':
case 'warning': {
console.log(`${chalk.yellow(`[esiJS:${type.toUpperCase()}]:`)} ${message}`)
break
}
case 'error': {
return `${chalk.red(`[esiJS:${type.toUpperCase()}]:`)} ${message}`
}
default: {
return
}
}
}
}
/**
* @private
* @param {*} args
* @param {any} args.input The input to validate.
* @param {string} args.type The type `args.input` should be.
* @param {string} args.message The error message to respond with if there's a mismatch.
* @param {array} args.options If `args.input` has set options, pass them as an array here.
* @param {boolean} args.optional Marks this input as optional so we don't throw an error
* if `args.input` is undefined.
* @returns
*/
function inputValidation({ input, type, message, options, optional = false }) {
// If is optional and input is undefined, no need to validate
if (optional && input === undefined) {
return
}
// Do not check for !input or you are making that you won't accept falsy values such as empty '' or id = 0
if (input === undefined) {
throw buildError(message, `INPUT_UNDEFINED`)
}
if (typeof input !== type) {
throw buildError(message, `INPUT_NOT_EQUAL_TO_REQUIRED_TYPE`)
}
// If options is provided, check that input is included
if (options && !options.includes(input)) {
throw buildError(message, `GIVEN_OPTION_NOT_VALID_OPTION`)
}
}
/**
* @private
* subUrl -> remaining url part specific to the function call
*
* requestType -> state the request type, defaults to GET
*
* body -> data to pass to the request body for requests of type post
*
* query -> aditional query parameters
*
* needsAuth -> flag a endpoint as authed
*/
function makeRequest({ subUrl, body, query, requestType = 'GET', needsAuth = false }) {
const { link, authToken, language, programName } = getSettings()
const urlTest = /\/(?=\/)(?