freechat/freechat-client/src/request.js
2021-02-12 19:07:14 -08:00

49 lines
1.0 KiB
JavaScript

const got = require('got')
const { Response } = require('./response.js')
const fs = require('fs')
/**
*
* @param {String} method GET POST DELETE
* @param {String} domain
* @param {Number} port
* @param {String} path
* @param {Object} Query options to pass into a query string
* @param {Buffer} Optional body that defaults to an empty string
*
* @returns Response
*/
exports.Request = async function (method, domain, port, path, params, body=null) {
try {
let url = `https://${domain}:${port}${path}`
const options = {
method: method,
searchParams: params,
responseType: 'json',
throwHttpErrors: false,
}
// NOTE: only in dev environments do we use http
if(process.env['DEV_ENV']) {
url = `http://${domain}:${port}${path}`
}
if(body) {
options.body = body
if(method == 'get') {
options.allowGetBody = true
}
}
const resp = await got(url , options)
return new Response(
resp.statusCode,
resp.body,
)
} catch(err) {
return new Response(null, null, err)
}
}