UNPKG

hypertune

Version:

[Hypertune](https://www.hypertune.com/) is the most flexible platform for feature flags, A/B testing, analytics and app configuration. Built with full end-to-end type-safety, Git-style version control and local, synchronous, in-memory flag evaluation. Opt

109 lines 4.38 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.codegenRequest = codegenRequest; exports.initRequest = initRequest; exports.hashRequest = hashRequest; exports.getEdgeRequestUrl = getEdgeRequestUrl; const shared_1 = require("../shared"); const hashDataEncoding_1 = require("../shared/helpers/hashDataEncoding"); const parseInitResponse_1 = __importDefault(require("./parseInitResponse")); function codegenRequest(_a) { return __awaiter(this, arguments, void 0, function* ({ traceId, token, branchName, body, language, edgeBaseUrl, tracedFetch, }) { const fullBody = Object.assign(Object.assign({}, body), { sdkType: "js", sdkVersion: shared_1.sdkVersion, language }); const responseString = yield edgeRequest({ traceId, token, branchName, edgeBaseUrl, tracedFetch, requestType: "codegen", body: fullBody, }); const response = JSON.parse(responseString); if (!response.files) { throw new Error(`[codegenRequest] unexpected response: ${responseString}`); } return response; }); } function initRequest(_a) { return __awaiter(this, arguments, void 0, function* ({ traceId, token, query, variables, branchName, edgeBaseUrl, tracedFetch, }) { const body = { query, variables, sdkType: "js", sdkVersion: shared_1.sdkVersion, }; const responseString = yield edgeRequest({ traceId, token, branchName, body, edgeBaseUrl, tracedFetch, requestType: "init", }); const response = (0, parseInitResponse_1.default)(responseString); if (!response.commitId) { throw new Error(`[initRequest] unexpected response: ${responseString}`); } return response; }); } function hashRequest(_a) { return __awaiter(this, arguments, void 0, function* ({ traceId, token, query, variables, branchName, edgeBaseUrl, tracedFetch, }) { const body = { query, variables, sdkType: "js", sdkVersion: shared_1.sdkVersion, }; const responseString = yield edgeRequest({ traceId, token, branchName, body, edgeBaseUrl, tracedFetch, requestType: "hash", }); const data = (0, hashDataEncoding_1.parseHashData)(responseString); if (!data) { throw new Error(`[hashRequest] unexpected response: ${responseString}`); } return data; }); } function edgeRequest(_a) { return __awaiter(this, arguments, void 0, function* ({ traceId, token, branchName, requestType, body, edgeBaseUrl, tracedFetch, }) { const url = getEdgeRequestUrl({ baseUrl: edgeBaseUrl, requestType, token, branchName, body, }); const response = yield tracedFetch(traceId, url, { method: "GET", headers: { "Cache-Control": "no-store" }, }); return response.text(); }); } function getEdgeRequestUrl({ baseUrl, requestType, token, branchName, body, }) { return `${baseUrl}/${requestType}?token=${encodeURIComponent(token)}${branchName ? `&branch=${encodeURIComponent(branchName)}` : ""}&body=${encodeURIComponent(JSON.stringify(body))}`; } //# sourceMappingURL=edge.js.map