Repository URL to install this package:
|
Version:
0.0.6 ▾
|
import { oneRouter } from '@skava/router';
import { config } from 'src/bootstrap/api/config';
import { logger } from '../log';
function initOneUrl() {
const protocol = process.env.HTTPS ? 'https' : 'http';
const host = process.env.HOST || '@@EMPTY_HOST';
global.oneUrl = {
protocol,
host,
pathname: '/',
search: {},
hash: '',
full: `${protocol}://${host}/`,
userAgent: '',
};
oneRouter.urlFromExpress = global.oneUrl;
}
// avoid Error: could not find pathname
initOneUrl();
function routingMiddleware(req, res, next) {
const hostess = req.get('host') || req.hostname;
const { headers, protocol, originalUrl, pathname, hash, search = {}, params = {}, query = {}, } = req;
const searchParams = { ...search, ...params, ...query };
// @todo @fixme - one spreads array or string
if (searchParams['0']) {
delete searchParams['0'];
}
/* eslint prefer-template: "error" */
function fromScatterBrainedPiecesIntoNormalStandardAddress(argsWouldGoHere) {
req.oneUrl = {
protocol,
host: hostess,
pathname: pathname || originalUrl,
hash,
search: searchParams,
// not in rr
full: `${protocol}://${hostess}${originalUrl}`,
userAgent: headers['user-agent'],
};
if (req.oneUrl.pathname && req.oneUrl.pathname.includes('_webpack') === false) {
global.oneUrl = req.oneUrl;
}
logger.debug('[oneRouter] EXPRESSURL', req.oneUrl);
}
// const requestCookie = req.Cookie || req.cookie || req.headers.cookie || req.headers.Cookie
// console.log(requestCookie)
fromScatterBrainedPiecesIntoNormalStandardAddress();
next();
}
// to avoid it being replaced at build, we make this dynamic
// @example: (process.env.API_URL) will be replaced, but not when split up
const env = process.env;
/**
* @todo 1. this won't work on the very first request to the server, but every request after will
* - we should persist it to JSON (as in graphql)
* - we should do an initial request using puppeteer post deploy (_only if we do not have the ENV value_)
* 2. the conditionals here that check whether it is localhost or remote
* - I need food, it's been ~11 hours straight.
* - remember to check this on `/` and on a route like `/category/fashionmenlongsleeves`
* 3. write a test for this
*/
function miniRoutingMiddleware(req, res, next) {
const API_GRAPHQL_URL = process.env.API_GRAPHQL_URL || 'http://localhost:4000';
logger.info('twoUrl => API_URL', API_GRAPHQL_URL);
config.set('API_URL', API_GRAPHQL_URL);
next();
}
export { miniRoutingMiddleware };
export { routingMiddleware };
//# sourceMappingURL=routing.js.map