Repository URL to install this package:
|
Version:
7.1.2 ▾
|
import { Request } from 'express'
import { EMPTY_OBJ } from 'exotic'
import { ExpressRequest } from '../typings'
// import { stringify } from 'chain-able-boost'
const version = process.env.VERSION
/**
* @example GET/url/@@/{params, body}
*/
// eslint-disable-next-line
// tslint:disable cyclomatic-complexity
function fromRequestToHash(request: ExpressRequest, namespace: string = '') {
const req: ExpressRequest = (request as any).req || request
// statusCode
const {
hash,
cookie,
originalUrl,
_parsedOriginalUrl,
_parsedUrl,
method,
referer,
baseUrl,
} = req
const query = req.query || req.params || req.body || EMPTY_OBJ
// const interestedin = {
// query,
// hash,
// cookie,
// originalUrl,
// _parsedOriginalUrl,
// _parsedUrl,
// method,
// referer,
// baseUrl,
// }
// fromRequestToLog(req)
if (hash) {
return hash
}
// GET/url
let computed = ''
if (req.url) {
computed += req.url._raw || req.url
}
if (!computed && req.originalUrl) {
computed += originalUrl
}
if (!computed && req._parsedOriginalUrl) {
computed += req._parsedOriginalUrl.href
}
if (!computed && req._parsedUrl) {
computed += req._parsedUrl.url
}
if (!computed && req.referer) {
computed += req.referer
}
// if (!computed && req.req) {
// return fromRequestToHash(req.req, namespace)
// }
computed = '' + version + namespace + method + computed
// can cache per user
// computed += cookie || ''
// separate
computed += '/@@/'
Object.keys(query).forEach(
key => (computed += key + JSON.stringify(query[key]))
)
// could also md5 / fliphash
if (computed.length > 100) {
computed = computed.slice(0, 100)
}
// const computed = stringify({ [method + originalUrl || url._raw]: query })
return computed
}
export { fromRequestToHash }
export default fromRequestToHash