configcat-js-ssr
Version:
ConfigCat Feature Flags for Server Side Rendered apps like NuxtJS. Official ConfigCat SDK for Server Side Rendered to easily access feature flags.
82 lines (81 loc) • 4.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpConfigFetcher = void 0;
var tslib_1 = require("tslib");
var axios_1 = require("axios");
var configcat_common_1 = require("configcat-common");
var HttpConfigFetcher = /** @class */ (function () {
function HttpConfigFetcher() {
}
HttpConfigFetcher.prototype.fetchLogic = function (options, lastEtag) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var url, headers, axiosConfig, response, err_1, statusCode_1, reasonPhrase_1, _a, code, message, _b, statusCode, reasonPhrase, eTag;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
url = options.getUrl();
if (lastEtag) {
if (typeof window !== "undefined") {
// NOTE: If we are running in browser, it's intentional that we don't specify the If-None-Match header.
// The browser automatically handles it, adding it manually would cause an unnecessary CORS OPTIONS request.
// In case the browser doesn't handle it, we are transforming the ccetag query parameter to the If-None-Match header in our CDN provider.
url += "&ccetag=" + lastEtag;
}
else {
headers = { "If-None-Match": lastEtag };
}
}
axiosConfig = {
method: "get",
timeout: options.requestTimeoutMs,
url: url,
headers: headers,
responseType: "text",
transformResponse: function (data) { return data; }
};
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, (0, axios_1.default)(axiosConfig)];
case 2:
response = _c.sent();
return [3 /*break*/, 4];
case 3:
err_1 = _c.sent();
(response = err_1.response);
if (response) {
statusCode_1 = response.status, reasonPhrase_1 = response.statusText;
return [2 /*return*/, { statusCode: statusCode_1, reasonPhrase: reasonPhrase_1 }];
}
else if (err_1.request) {
_a = err_1, code = _a.code, message = _a.message;
switch (code) {
case "ERR_CANCELED":
throw new configcat_common_1.FetchError("abort");
case "ECONNABORTED":
// Axios ambiguously use ECONNABORTED instead of ETIMEDOUT, so we need this additional check to detect timeout
// (see https://github.com/axios/axios/issues/1543#issuecomment-558166483)
if (message.indexOf("timeout") >= 0) {
throw new configcat_common_1.FetchError("timeout", options.requestTimeoutMs);
}
break;
default:
break;
}
throw new configcat_common_1.FetchError("failure", err_1);
}
throw err_1;
case 4:
_b = response, statusCode = _b.status, reasonPhrase = _b.statusText;
if (statusCode === 200) {
eTag = response.headers.etag;
return [2 /*return*/, { statusCode: statusCode, reasonPhrase: reasonPhrase, eTag: eTag, body: response.data }];
}
return [2 /*return*/, { statusCode: statusCode, reasonPhrase: reasonPhrase }];
}
});
});
};
return HttpConfigFetcher;
}());
exports.HttpConfigFetcher = HttpConfigFetcher;