@apify/utilities 2.4.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.mjs CHANGED
@@ -1560,12 +1560,16 @@ var WebhookPayloadTemplate = class {
1560
1560
  * Parse also validates the template structure, so it can be used
1561
1561
  * to check validity of the template JSON and usage of allowedVariables.
1562
1562
  */
1563
- static parse(payloadTemplate, allowedVariables = null, context = {}) {
1563
+ static parse(payloadTemplate, allowedVariables = null, context = {}, options = {}) {
1564
1564
  const type = typeof payloadTemplate;
1565
1565
  if (type !== "string")
1566
1566
  throw new Error(`Cannot parse a ${type} payload template.`);
1567
1567
  const template = new WebhookPayloadTemplate(payloadTemplate, allowedVariables, context);
1568
- return template._parse();
1568
+ const data = template._parse();
1569
+ if (options.interpolateStrings) {
1570
+ return template._interpolate(data);
1571
+ }
1572
+ return data;
1569
1573
  }
1570
1574
  /**
1571
1575
  * Stringify an object into a webhook payload template.
@@ -1607,19 +1611,56 @@ var WebhookPayloadTemplate = class {
1607
1611
  return new JsonVariable(variableName);
1608
1612
  }
1609
1613
  _parse() {
1614
+ let currentIndex = 0;
1610
1615
  while (true) {
1611
1616
  try {
1612
1617
  return JSON.parse(this.payload);
1613
1618
  } catch (err) {
1614
- const position = this._findPositionOfNextVariable();
1615
- if (position) {
1616
- this._replaceVariable(position);
1617
- } else {
1619
+ const position = this._findPositionOfNextVariable(currentIndex);
1620
+ if (!position) {
1618
1621
  throw new InvalidJsonError(err);
1619
1622
  }
1623
+ if (!position.isInsideString) {
1624
+ this._replaceVariable(position);
1625
+ }
1626
+ currentIndex = position.openBraceIndex + 1;
1620
1627
  }
1621
1628
  }
1622
1629
  }
1630
+ _interpolate(value) {
1631
+ if (typeof value === "string") {
1632
+ return this._interpolateString(value);
1633
+ }
1634
+ if (Array.isArray(value)) {
1635
+ return this._interpolateArray(value);
1636
+ }
1637
+ if (typeof value === "object" && value !== null) {
1638
+ return this._interpolateObject(value);
1639
+ }
1640
+ return value;
1641
+ }
1642
+ _interpolateString(value) {
1643
+ if (value.match(/^\{\{([a-zA-Z0-9.]+)\}\}$/)) {
1644
+ const variableName = value.substring(2, value.length - 2);
1645
+ this._validateVariableName(variableName);
1646
+ return this._getVariableValue(variableName);
1647
+ }
1648
+ return value.replace(/\{\{([a-zA-Z0-9.]+)\}\}/g, (match, variableName) => {
1649
+ this._validateVariableName(variableName);
1650
+ const variableValue = this._getVariableValue(variableName);
1651
+ return `${variableValue}`;
1652
+ });
1653
+ }
1654
+ _interpolateObject(value) {
1655
+ const result = {};
1656
+ Object.entries(value).forEach(([key, v]) => {
1657
+ result[key] = this._interpolate(v);
1658
+ });
1659
+ return result;
1660
+ }
1661
+ _interpolateArray(value) {
1662
+ return value.map(this._interpolate.bind(this));
1663
+ }
1623
1664
  _findPositionOfNextVariable(startIndex = 0) {
1624
1665
  const openBraceIndex = this.payload.indexOf("{{", startIndex);
1625
1666
  const closeBraceIndex = this.payload.indexOf("}}", openBraceIndex) + 1;
@@ -1627,9 +1668,7 @@ var WebhookPayloadTemplate = class {
1627
1668
  if (!someVariableMaybeExists)
1628
1669
  return null;
1629
1670
  const isInsideString = this._isVariableInsideString(openBraceIndex);
1630
- if (!isInsideString)
1631
- return { openBraceIndex, closeBraceIndex };
1632
- return this._findPositionOfNextVariable(openBraceIndex + 1);
1671
+ return { isInsideString, openBraceIndex, closeBraceIndex };
1633
1672
  }
1634
1673
  _isVariableInsideString(openBraceIndex) {
1635
1674
  const unescapedQuoteCount = this._countUnescapedDoubleQuotesUpToIndex(openBraceIndex);
@@ -1652,7 +1691,7 @@ var WebhookPayloadTemplate = class {
1652
1691
  this._validateVariableName(variableName);
1653
1692
  const replacement = this._getVariableReplacement(variableName);
1654
1693
  this.replacedVariables.push({ variableName, replacement });
1655
- this.payload = this.payload.replace(`{{${variableName}}}`, replacement);
1694
+ this.payload = this.payload.substring(0, openBraceIndex) + replacement + this.payload.substring(closeBraceIndex + 1);
1656
1695
  }
1657
1696
  _validateVariableName(variableName) {
1658
1697
  if (this.allowedVariables === null)
@@ -1662,15 +1701,19 @@ var WebhookPayloadTemplate = class {
1662
1701
  if (!isVariableValid)
1663
1702
  throw new InvalidVariableError(variableName);
1664
1703
  }
1665
- _getVariableReplacement(variableName) {
1704
+ _getVariableValue(variableName) {
1666
1705
  const [variable, ...properties] = variableName.split(".");
1667
1706
  const context = this.context[variable];
1668
- const replacement = properties.reduce((ctx, prop) => {
1707
+ const value = properties.reduce((ctx, prop) => {
1669
1708
  if (!ctx || typeof ctx !== "object")
1670
1709
  return null;
1671
1710
  return ctx[prop];
1672
1711
  }, context);
1673
- return replacement ? JSON.stringify(replacement) : null;
1712
+ return value;
1713
+ }
1714
+ _getVariableReplacement(variableName) {
1715
+ const value = this._getVariableValue(variableName);
1716
+ return value ? JSON.stringify(value) : null;
1674
1717
  }
1675
1718
  };
1676
1719
  __name(WebhookPayloadTemplate, "WebhookPayloadTemplate");
@@ -1717,6 +1760,28 @@ function privateDecrypt({
1717
1760
  return Buffer.concat([decipher.update(encryptedDataBuffer), decipher.final()]).toString("utf-8");
1718
1761
  }
1719
1762
  __name(privateDecrypt, "privateDecrypt");
1763
+
1764
+ // src/url_params_utils.ts
1765
+ function encodeInput(input) {
1766
+ const data = JSON.stringify(input);
1767
+ const buffer = Buffer.from(data, "utf8");
1768
+ return buffer.toString("base64url");
1769
+ }
1770
+ __name(encodeInput, "encodeInput");
1771
+ function decodeInput(urlHash) {
1772
+ const buffer = Buffer.from(urlHash, "base64url");
1773
+ const decoded = buffer.toString("utf8");
1774
+ return JSON.parse(decoded);
1775
+ }
1776
+ __name(decodeInput, "decodeInput");
1777
+ function separateImports(code) {
1778
+ const lines = code.split("\n");
1779
+ return {
1780
+ code: lines.filter((line) => !line.trim().startsWith("import")).join("\n"),
1781
+ imports: lines.filter((line) => line.trim().startsWith("import")).join("\n")
1782
+ };
1783
+ }
1784
+ __name(separateImports, "separateImports");
1720
1785
  export {
1721
1786
  CHECK_TYPES,
1722
1787
  HealthChecker,
@@ -1734,8 +1799,10 @@ export {
1734
1799
  configureLogger,
1735
1800
  cryptoRandomObjectId,
1736
1801
  dateToString,
1802
+ decodeInput,
1737
1803
  delayPromise,
1738
1804
  deterministicUniqueId,
1805
+ encodeInput,
1739
1806
  escapeForBson,
1740
1807
  escapePropertyName,
1741
1808
  escapeRegExp,
@@ -1761,6 +1828,7 @@ export {
1761
1828
  readStreamToString,
1762
1829
  removeFromArray,
1763
1830
  retryWithExpBackoff,
1831
+ separateImports,
1764
1832
  sequentializePromises,
1765
1833
  splitFullName,
1766
1834
  timeoutPromise,