@knapsack/app
Version:
Build Design Systems with Knapsack
60 lines • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPatternUrl = createPatternUrl;
exports.getPatternInfoFromUrl = getPatternInfoFromUrl;
exports.createDemoUrl = createDemoUrl;
const utils_1 = require("@knapsack/utils");
const constants_1 = require("./constants");
/**
* Create a pattern url to view in Knapsack environment
*/
function createPatternUrl({ patternId, templateId, demoId, subPageId, customTabId, }) {
if ([templateId, subPageId, customTabId].filter(Boolean).length > 1) {
const msg = `Cannot call "createPatternUrl()" with that many params - see console`;
console.error(msg, { templateId, subPageId, customTabId });
throw new Error(msg);
}
let query;
if (templateId && demoId) {
query = {
templateId,
demoId,
};
}
else if (templateId) {
query = {
templateId,
};
}
else if (subPageId) {
query = {
subPageId,
};
}
else if (customTabId) {
query = {
customTabId,
};
}
return `${constants_1.BASE_PATHS.PATTERN}/${patternId}${query ? `?${(0, utils_1.createQueryParams)(query)}` : ''}`;
}
function getPatternInfoFromUrl(url) {
// @todo consider using `url.parse()`
if (url.startsWith(`${constants_1.BASE_PATHS.PATTERN}/`)) {
const [_, base, patternId, templateId, demoId] = url.split('/');
return {
patternId,
templateId,
demoId,
};
}
return {};
}
/**
* Create a root relative demo url to view in a standalone environment w/no Knapsack UI
* i.e. the link you see when you open a pattern in a new window
*/
function createDemoUrl(params) {
return `${constants_1.apiUrlBase}/render-demo-id?${(0, utils_1.createQueryParams)(params)}`;
}
//# sourceMappingURL=routes.js.map