@fragment-dev/cli
Version:
113 lines (109 loc) • 3.32 kB
JavaScript
import {
BadRequestError,
assert_default,
codes,
getStringParametersInternal
} from "./chunk-CFMJRPDM.js";
import {
init_cjs_shims
} from "./chunk-7GH3YGSC.js";
// ../../libs/schema-validation/parameterization.ts
init_cjs_shims();
var fillParams = (obj, params = {}, options = {
allowMissing: false,
allowMissingInChildren: true
}) => {
if (typeof obj === "string") {
const tokens = obj.split(/(\{\{[^{}]+?\}\})/g);
const missingParams = /* @__PURE__ */ new Set();
const result = tokens.map((token) => {
const match = token.match(/\{\{([^{}]+?)\}\}/);
if (match) {
const param = match[1];
const paramValue = params[param];
if (paramValue === void 0 || paramValue === null) {
missingParams.add(param);
return token;
}
return paramValue;
}
return token;
}).join("");
if (!options.allowMissing) {
assert_default(!missingParams.size, BadRequestError, {
message: `Failed to fill all parameters: ${obj} -> ${result}`,
code: codes.parameterization_failed
});
}
return result;
}
if (typeof obj === "object" && obj !== null) {
if (Array.isArray(obj)) {
return obj.map((value) => fillParams(value, params, options));
}
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [
key,
fillParams(value, params, {
...options,
allowMissing: key === "children" ? options.allowMissingInChildren : options.allowMissing
})
])
);
}
return obj;
};
function getSchemaObjectParametersInternal(obj, parameters, includeChildren) {
if (typeof obj === "string") {
getStringParametersInternal(obj, parameters);
} else if (typeof obj === "object") {
for (const key in obj) {
if (key !== "templatePath" && (key !== "children" || includeChildren)) {
getSchemaObjectParametersInternal(
obj[key],
parameters,
includeChildren
);
}
}
}
}
function getSchemaObjectParameters(obj, includeChildren = false) {
const params = /* @__PURE__ */ new Set();
getSchemaObjectParametersInternal(obj, params, includeChildren);
return Array.from(params);
}
// ../../libs/path-utils/path.ts
init_cjs_shims();
var getStructuralPath = (path) => {
return path.split("/").map((part) => part.split(":")[0]).join("/");
};
var getInstanceValueByAccountPath = (path) => {
const parts = path.split("/");
const result = /* @__PURE__ */ new Map();
let workingSet = "";
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const [templateKey, templateValue] = part.split(":");
workingSet = workingSet ? `${workingSet}/${templateKey}` : templateKey;
result.set(workingSet, templateValue);
if (templateValue) {
workingSet = `${workingSet}:${templateValue}`;
}
}
return result;
};
var getSubpaths = (path) => path.split("/").reduce((acc, part) => {
const prev = acc[acc.length - 1];
const subPath = prev ? `${prev}/${part}` : part;
acc.push(subPath);
return acc;
}, []);
var getStructuralSubpaths = (path) => getSubpaths(getStructuralPath(path));
export {
fillParams,
getSchemaObjectParameters,
getStructuralPath,
getInstanceValueByAccountPath,
getStructuralSubpaths
};