@hv-kit/hexpress
Version:
facilitates typescript backend development with express
228 lines • 9.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getResponseError = exports.getResponseLogin = exports.getResponse = exports.cleanResponseLogin = exports.cleanResponse = exports.getHeaderRequest = exports.tokenAuth = exports.getTokenAuth = exports.setTokenAuth = void 0;
const js_cookie_1 = __importDefault(require("js-cookie"));
const hexpress_config_json_1 = __importDefault(require("../../../../hexpress.config.json"));
const error_1 = require("./error");
const other_1 = require("./other");
function setTokenAuth(val, expires = 1) {
val = (typeof (val) === 'string' &&
val.length > 0) ? val : undefined;
if (val) {
js_cookie_1.default.set(hexpress_config_json_1.default.cache_name.auth, val, { expires: expires });
}
}
exports.setTokenAuth = setTokenAuth;
function getTokenAuth() {
return js_cookie_1.default.get(hexpress_config_json_1.default.cache_name.auth);
}
exports.getTokenAuth = getTokenAuth;
exports.tokenAuth = getTokenAuth();
function getHeaderRequest(headers = {
'Content-Type': 'application/json;charset=UTF-8',
Accept: 'application/json',
}, others = {}) {
headers = (typeof headers === 'object' &&
Array.isArray(headers) === false) ? headers : {};
others = (typeof others === 'object' &&
Array.isArray(others) === false) ? others : {};
const result = Object.assign({
headers: headers,
}, others);
if (getTokenAuth()) {
result.headers.Authorization = getTokenAuth();
}
// const token = getTokenAuth();
// console.log('> scripts.http | responseToken - token :: ', token);
console.log('> scripts.http | getHeaderRequest - result:: ', result);
return result;
}
exports.getHeaderRequest = getHeaderRequest;
function cleanResponse(dataResponse, type = 'single') {
const allDatas = (typeof dataResponse === 'object'
&& Array.isArray(dataResponse) === false) ? dataResponse : {};
let result = {};
type = (['all', 'single', 'exists', 'edit', 'edit-multiple'].includes(type)) ? type : 'single';
if (type === 'all') {
const meta = (Object.keys(allDatas).includes('meta')
&& typeof allDatas.meta === 'object'
&& Array.isArray(allDatas.meta) === false) ? allDatas.meta : {
pagination: {
page: 1,
pageSize: 10,
pageCount: 1,
pageLength: 0,
total: 0,
}
};
result = {
datas: (Object.keys(allDatas).includes('datas')
&& typeof allDatas.datas === 'object'
&& Array.isArray(allDatas.datas) === true
&& Object.keys(allDatas.datas).length > 0) ? allDatas.datas : [],
pagination: (Object.keys(meta).includes('pagination')
&& typeof meta.pagination === 'object'
&& Array.isArray(meta.pagination) === false) ? meta.pagination : {},
};
}
else if (type === 'single') {
result = {
data: (Object.keys(allDatas).includes('data')
&& typeof allDatas.data === 'object'
&& Array.isArray(allDatas.data) === false
&& Object.keys(allDatas.data).length > 0
&& Object.keys(allDatas.data).includes('data')
&& typeof allDatas.data.data === 'object'
&& Array.isArray(allDatas.data.data) === false
&& Object.keys(allDatas.data.data).length > 0
&& Object.keys(allDatas.data).includes('exists')
&& typeof allDatas.data.exists === 'boolean') ? allDatas.data : {
data: {},
exists: false,
},
};
}
else if (type === 'exists') {
result = {
exists: (Object.keys(allDatas).includes('exists')
&& typeof allDatas.exists === 'boolean') ? allDatas.exists : false,
};
}
else if (type === 'edit') {
result = {
data: (Object.keys(allDatas).includes('data')
&& typeof allDatas.data === 'object'
&& Array.isArray(allDatas.data) === false
&& Object.keys(allDatas.data).length > 0) ? allDatas.data : {},
};
}
else if (type === 'edit-multiple') {
result = {
data: (Object.keys(allDatas).includes('data')
&& typeof allDatas.data === 'object'
&& Array.isArray(allDatas.data) === true
&& Object.keys(allDatas.data).length > 0) ? allDatas.data : [],
};
}
if (Object.keys(allDatas).includes('error')
&& typeof allDatas.error === 'object'
&& Array.isArray(allDatas.error) === false) {
result.error = allDatas.error;
}
result = cleanForError(result);
// console.log('> scripts.http | getResponse - meta :: ', meta);
// console.log('> scripts.http | getResponse - result.pagination :: ', result.pagination);
return result;
}
exports.cleanResponse = cleanResponse;
function cleanResponseLogin(dataResponse, type = 'single') {
const allDatas = (typeof dataResponse === 'object'
&& Array.isArray(dataResponse) === false) ? dataResponse : {};
const result = Object.assign({}, cleanResponse(dataResponse, type));
if (Object.keys(result).includes('data')) {
delete result.data;
}
if (Object.keys(result).includes('datas')) {
delete result.datas;
}
if (Object.keys(result).includes('exists')) {
delete result.exists;
}
if (Object.keys(result).includes('meta')) {
delete result.meta;
}
if (Object.keys(result).includes('pagination')) {
delete result.pagination;
}
result.jwt = (Object.keys(allDatas).includes('jwt')
&& typeof allDatas.jwt === 'string'
&& allDatas.jwt.length > 0) ? allDatas.jwt : '';
result.user = {
data: (Object.keys(allDatas).includes('user')
&& typeof allDatas.user === 'object'
&& Array.isArray(allDatas.user) === false) ? allDatas.user : {},
exists: (Object.keys(allDatas).includes('user')
&& typeof allDatas.user === 'object'
&& Array.isArray(allDatas.user) === false)
};
if (!!result.jwt) {
setTokenAuth(result.jwt);
}
console.log('> scripts.http | cleanResponseLogin - allDatas :: ', allDatas);
console.log('> scripts.http | cleanResponseLogin - result :: ', result);
// console.log('> scripts.http | cleanResponseLogin - meta :: ', meta);
// console.log('> scripts.http | cleanResponseLogin - result.pagination :: ', result.pagination);
return result;
}
exports.cleanResponseLogin = cleanResponseLogin;
function cleanForError(data) {
data = (typeof (data) === 'object' &&
Array.isArray(data) === false) ? data : {};
if (Object.keys(data).includes('error')) {
if (['0001__unknown_error', '0003__data_exists', '0004__data_doesnt_exists', '0005__fail_send_email', '0006__request_not_processed'].includes(data.error.code)) {
data.msg = {
type: 'danger',
};
}
else if (['0002__invalid_form'].includes(data.error.code)) {
data.msg = {
type: 'warning',
};
}
else if (['0007__auth_no_access', '0008__auth_no_permission'].includes(data.error.code)) {
data.msg = {
type: 'danger',
};
}
else {
data.msg = {
type: 'danger',
};
}
data.msg.message = data.error.message;
}
else {
data.msg = {
type: 'success'
};
}
return data;
}
function getResponse(response, type = 'single') {
const result = cleanResponse(response.data, type);
console.log('> scripts.http | getResponse - result :: ', result);
return result;
}
exports.getResponse = getResponse;
function getResponseLogin(response, type = 'single') {
const result = cleanResponseLogin(response.data, type);
console.log('> scripts.http | getResponseLogin - result :: ', result);
return result;
}
exports.getResponseLogin = getResponseLogin;
function getResponseError(err, lang = 'fr', type = 'single') {
console.log('> scripts.http | getResponseError - err.response :: ', err.response);
let result = (err.response) ? cleanResponse(err.response.data, type) : cleanResponse({
data: {},
}, type);
result.error = new error_1.CustomError(hexpress_config_json_1.default.errors.unknown_error.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.unknown_error.code, err.stack).res;
result = cleanForError(result);
return result;
}
exports.getResponseError = getResponseError;
exports.default = {
setTokenAuth,
getTokenAuth,
tokenAuth: exports.tokenAuth,
getHeaderRequest,
cleanResponse,
cleanResponseLogin,
cleanForError,
getResponse,
getResponseLogin,
getResponseError,
};
//# sourceMappingURL=http.js.map