Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/graphql / dist / bootstrapper / setup / requestMiddleware.js
Size: Mime:
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
// import { isTrue } from 'exotic'
const config_1 = require("@skava/request/dist/config");
const config_2 = require("../../bootstrapper/api/config");
// Todo isTruish is not available in uxui-module
const isTruish = x => x === true || x === 'true' || /^["|']?true["|']?$/.test(x);
const storeKey = isTruish(process.env.SHOULD_USE_LOWERCASE_STORE_ID)
    ? 'storeid'
    : 'storeId';
const constantAndDefault = {
    partnerId: config_2.partnerId,
    [storeKey]: config_2.storeId,
    appid: config_2.appid,
    locale: config_2.locale,
};
const constantHeaders = {
    get skcid() {
        return config_2.toCorrelationId();
    },
};
if (isTruish(config_2.shouldIncudeCampaignId)) {
    constantAndDefault.campaignId = config_2.campaignId;
}
config_1.setConfig(Object.assign({}, constantHeaders, constantAndDefault));
config_1.config.set('constantParams', constantAndDefault);
config_1.config.set('constantHeaders', constantHeaders);
// this is how it's done in express
// endpoints.map(fromRouteToProxy)
// const route = endpoints[index]
// const proxyConfig = getProxyUrl(route, callDetails[route])
function proxyMiddleware(requestChain, eventData = {}) {
    if (eventData.type !== 'toRequest') {
        return;
    }
    const method = requestChain.get('method') || FETCHREQUEST;
    function interceptRequestWithProxy(url, params) {
        return __awaiter(this, void 0, void 0, function* () {
            // @todo doesn't cover subroutes like submitorder/return
            const afterApi = url.split('api/').pop();
            const finalUrl = url.split('/').shift();
            console.log('[uxui-apis] proxymiddleware: ', finalUrl);
            const [error, response] = yield method(url, params);
            if (error) {
                throw error;
            }
            else {
                // const { authCookies, onCookie } = createCookieHacker()
                // response.cookies =
                return [error, response];
            }
        });
    }
    requestChain.setMethod(interceptRequestWithProxy);
}
// @todo @fixme seemingly middleware will muck with request method
// RequestChain.middleware.set('proxy', proxyMiddleware)
// if (IS_FIXTURES_ENABLED) {
//   // RequestChain.middleware.set('staticData', staticFixtureMiddleware)
// }
// if (DISABLE_CACHE) {
//   // console.log('Should Be Getting Cached')
//   RequestChain.middleware.set('persistance', middlewareForLocalStorage)
// } else {
//   // console.log('Should Not Be Getting Cached!!!')
// }
//# sourceMappingURL=requestMiddleware.js.map