@azure/search-documents
Version:
Azure client library to use AI Search for node.js and browser.
198 lines (197 loc) • 6.2 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var urlTemplate_exports = {};
__export(urlTemplate_exports, {
expandUrlTemplate: () => expandUrlTemplate
});
module.exports = __toCommonJS(urlTemplate_exports);
function encodeComponent(val, reserved, op) {
return (reserved ?? op === "+") || op === "#" ? encodeReservedComponent(val) : encodeRFC3986URIComponent(val);
}
function encodeReservedComponent(str) {
return str.split(/(%[0-9A-Fa-f]{2})/g).map((part) => !/%[0-9A-Fa-f]/.test(part) ? encodeURI(part) : part).join("");
}
function encodeRFC3986URIComponent(str) {
return encodeURIComponent(str).replace(
/[!'()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
);
}
function isDefined(val) {
return val !== void 0 && val !== null;
}
function getNamedAndIfEmpty(op) {
return [!!op && [";", "?", "&"].includes(op), !!op && ["?", "&"].includes(op) ? "=" : ""];
}
function getFirstOrSep(op, isFirst = false) {
if (isFirst) {
return !op || op === "+" ? "" : op;
} else if (!op || op === "+" || op === "#") {
return ",";
} else if (op === "?") {
return "&";
} else {
return op;
}
}
function getExpandedValue(option) {
let isFirst = option.isFirst;
const { op, varName, varValue: value, reserved } = option;
const vals = [];
const [named, ifEmpty] = getNamedAndIfEmpty(op);
if (Array.isArray(value)) {
for (const val of value.filter(isDefined)) {
vals.push(`${getFirstOrSep(op, isFirst)}`);
if (named && varName) {
vals.push(`${encodeURIComponent(varName)}`);
if (val === "") {
vals.push(ifEmpty);
} else {
vals.push("=");
}
}
vals.push(encodeComponent(val, reserved, op));
isFirst = false;
}
} else if (typeof value === "object") {
for (const key of Object.keys(value)) {
const val = value[key];
if (!isDefined(val)) {
continue;
}
vals.push(`${getFirstOrSep(op, isFirst)}`);
if (key) {
vals.push(`${encodeURIComponent(key)}`);
if (named && val === "") {
vals.push(ifEmpty);
} else {
vals.push("=");
}
}
vals.push(encodeComponent(val, reserved, op));
isFirst = false;
}
}
return vals.join("");
}
function getNonExpandedValue(option) {
const { op, varName, varValue: value, isFirst, reserved } = option;
const vals = [];
const first = getFirstOrSep(op, isFirst);
const [named, ifEmpty] = getNamedAndIfEmpty(op);
if (named && varName) {
vals.push(encodeComponent(varName, reserved, op));
if (value === "") {
if (!ifEmpty) {
vals.push(ifEmpty);
}
return !vals.join("") ? void 0 : `${first}${vals.join("")}`;
}
vals.push("=");
}
const items = [];
if (Array.isArray(value)) {
for (const val of value.filter(isDefined)) {
items.push(encodeComponent(val, reserved, op));
}
} else if (typeof value === "object") {
for (const key of Object.keys(value)) {
if (!isDefined(value[key])) {
continue;
}
items.push(encodeRFC3986URIComponent(key));
items.push(encodeComponent(value[key], reserved, op));
}
}
vals.push(items.join(","));
return !vals.join(",") ? void 0 : `${first}${vals.join("")}`;
}
function getVarValue(option) {
const { op, varName, modifier, isFirst, reserved, varValue: value } = option;
if (!isDefined(value)) {
return void 0;
} else if (["string", "number", "boolean"].includes(typeof value)) {
let val = value.toString();
const [named, ifEmpty] = getNamedAndIfEmpty(op);
const vals = [getFirstOrSep(op, isFirst)];
if (named && varName) {
vals.push(varName);
if (val === "") {
vals.push(ifEmpty);
} else {
vals.push("=");
}
}
if (modifier && modifier !== "*") {
val = val.substring(0, parseInt(modifier, 10));
}
vals.push(encodeComponent(val, reserved, op));
return vals.join("");
} else if (modifier === "*") {
return getExpandedValue(option);
} else {
return getNonExpandedValue(option);
}
}
function expandUrlTemplate(template, context, option) {
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
if (!expr) {
return encodeReservedComponent(text);
}
let op;
if (["+", "#", ".", "/", ";", "?", "&"].includes(expr[0])) {
op = expr[0];
expr = expr.slice(1);
}
const varList = expr.split(/,/g);
const innerResult = [];
for (const varSpec of varList) {
const varMatch = /([^:*]*)(?::(\d+)|(\*))?/.exec(varSpec);
if (!varMatch || !varMatch[1]) {
continue;
}
const varValue = getVarValue({
isFirst: innerResult.length === 0,
op,
varValue: context[varMatch[1]],
varName: varMatch[1],
modifier: varMatch[2] || varMatch[3],
reserved: option?.allowReserved
});
if (varValue) {
innerResult.push(varValue);
}
}
return innerResult.join("");
});
return normalizeUnreserved(result);
}
function normalizeUnreserved(uri) {
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
const char = String.fromCharCode(parseInt(hex, 16));
if (/[.~-]/.test(char)) {
return char;
}
return match;
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
expandUrlTemplate
});
//# sourceMappingURL=urlTemplate.js.map