@nfq/react-ssr
Version:
An helper bundle for react with ssr setup
190 lines (165 loc) • 6.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ApiFactory = void 0;
var _abortController = _interopRequireDefault(require("abort-controller"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint-disable max-lines-per-function */
/* eslint-disable node/no-unsupported-features/node-builtins */
/**
* An factory function to create an Class with all needed boilerplate to communicate with the server.
*
* @param {String} apiUrl The Api url.
* @param {String} serverAdress The server adress.
* @param {String} serverPort The server port.
* @param {String} protocol The protocol the server should request data with.
* @param {String} htaccessName Htaccess name.
* @param {String} htaccessPass Htaccess password.
*
* @returns {Object} Returns an extendable api class.
*/
const ApiFactory = (apiUrl, serverAdress, serverPort, protocol, htaccessName, htaccessPass) => class Api {
/**
* Creates an instance of Api.
*
* @memberof Api
*/
constructor() {
if (typeof window === 'undefined') {
this.apiPrefix = `${protocol}://${serverAdress}${serverPort ? `:${serverPort}` : ''}${apiUrl}`; // eslint-disable-next-line node/no-new-require, new-cap
this.agent = new require('https').Agent({
rejectUnauthorized: false
});
} else {
this.apiPrefix = apiUrl;
}
}
/**
* Sends an get request.
*
* @param {String} path The api path.
* @param {Object} header An header object.
* @param {Object} [data={}] The data to send.
* @param {Function} [fetchFunc=null] Own fetch function if needed. Uses default browser fetch if not given.
*
* @returns {Object} An Fetch promise and an abort controller.
* @memberof Api
*/
getRequest(path, header, data = {}, fetchFunc = null) {
const controller = new _abortController.default();
const fetchPromise = this.abortableFetch(controller, `${this.apiPrefix}${path}?${new URLSearchParams(data).toString()}`, 'GET', header, null, fetchFunc);
return {
controller,
request: fetchPromise
};
}
/**
* Sends an post request.
*
* @param {String} path The api path.
* @param {Object} header An header object.
* @param {Object} data The data to send.
* @param {Object} [urlParams={}] The Query params.
* @param {Function} [fetchFunc=null] Own fetch function if needed. Uses default browser fetch if not given.
*
* @returns {Object} An Fetch promise and an abort controller.
* @memberof Api
*/
postRequest(path, header, data, urlParams = {}, fetchFunc = null) {
const controller = new _abortController.default();
const fetchPromise = this.abortableFetch(controller, `${this.apiPrefix}${path}?${new URLSearchParams(urlParams).toString()}`, 'POST', header, data, fetchFunc);
return {
controller,
request: fetchPromise
};
}
/**
* Sends an put request.
*
* @param {String} path The api path.
* @param {Object} header An header object.
* @param {Object} data The data to send.
* @param {Object} [urlParams={}] The Query params.
* @param {Function} [fetchFunc=null] Own fetch function if needed. Uses default browser fetch if not given.
*
* @returns {Object} An Fetch promise and an abort controller.
* @memberof Api
*/
putRequest(path, header, data, urlParams = {}, fetchFunc = null) {
const controller = new _abortController.default();
const fetchPromise = this.abortableFetch(controller, `${this.apiPrefix}${path}?${new URLSearchParams(urlParams).toString()}`, 'PUT', header, data, fetchFunc);
return {
controller,
request: fetchPromise
};
}
/**
* Sends an get request.
*
* @param {String} path The api path.
* @param {Object} header An header object.
* @param {Object} data The data to send.
* @param {Function} [fetchFunc=null] Own fetch function if needed. Uses default browser fetch if not given.
*
* @returns {Object} An Fetch promise and an abort controller.
* @memberof Api
*/
deleteRequest(path, header, data, fetchFunc = null) {
const controller = new _abortController.default();
const fetchPromise = this.abortableFetch(controller, `${this.apiPrefix}${path}?${new URLSearchParams(data).toString()}`, 'DELETE', header, null, fetchFunc);
return {
controller,
request: fetchPromise
};
}
/**
* An fetch implementation for abortable requests.
*
* @param {AbortController} controller Controller for Abort.
* @param {String} uri Uri to fetch.
* @param {String} method Method to use.
* @param {Object} [headers=null] Headers to send.
* @param {Object} [data=null] Data to send.
* @param {Function} [fetchFunc=null] Own fetch function if needed. Uses default browser fetch if not given.
*
* @returns {Promise} An Promise with resolved json.
* @memberof Api
*/
async abortableFetch(controller, uri, method, headers = null, data = null, fetchFunc = null) {
let fetcher;
const options = {
headers: {},
method,
signal: controller.signal
};
if (typeof window === 'undefined') {
if (process.env.NODE_ENV === 'development') {
options.agent = this.agent;
}
}
if (htaccessName && htaccessPass) {
const token = `${htaccessName}:${htaccessPass}`;
options.headers = {
Authorization: `Basic ${Buffer.from(token).toString('base64')}`
};
}
if (headers) {
options.headers = { ...headers,
...options.headers
};
}
if (data) {
options.body = data;
}
if (fetchFunc) {
fetcher = fetchFunc;
} else {
fetcher = fetch;
}
const res = await fetcher(uri, options);
const json = await res.json();
return json;
}
};
exports.ApiFactory = ApiFactory;