@apify/utilities
Advanced tools
Comparing version 2.4.1 to 2.5.0
69
index.js
@@ -1647,3 +1647,3 @@ "use strict"; | ||
*/ | ||
static parse(payloadTemplate, allowedVariables = null, context = {}) { | ||
static parse(payloadTemplate, allowedVariables = null, context = {}, options = {}) { | ||
const type = typeof payloadTemplate; | ||
@@ -1653,3 +1653,7 @@ if (type !== "string") | ||
const template = new WebhookPayloadTemplate(payloadTemplate, allowedVariables, context); | ||
return template._parse(); | ||
const data = template._parse(); | ||
if (options.interpolateStrings) { | ||
return template._interpolate(data); | ||
} | ||
return data; | ||
} | ||
@@ -1696,2 +1700,3 @@ /** | ||
_parse() { | ||
let currentIndex = 0; | ||
while (true) { | ||
@@ -1701,11 +1706,47 @@ try { | ||
} catch (err) { | ||
const position = this._findPositionOfNextVariable(); | ||
if (position) { | ||
this._replaceVariable(position); | ||
} else { | ||
const position = this._findPositionOfNextVariable(currentIndex); | ||
if (!position) { | ||
throw new InvalidJsonError(err); | ||
} | ||
if (!position.isInsideString) { | ||
this._replaceVariable(position); | ||
} | ||
currentIndex = position.openBraceIndex + 1; | ||
} | ||
} | ||
} | ||
_interpolate(value) { | ||
if (typeof value === "string") { | ||
return this._interpolateString(value); | ||
} | ||
if (Array.isArray(value)) { | ||
return this._interpolateArray(value); | ||
} | ||
if (typeof value === "object" && value !== null) { | ||
return this._interpolateObject(value); | ||
} | ||
return value; | ||
} | ||
_interpolateString(value) { | ||
if (value.match(/^\{\{([a-zA-Z0-9.]+)\}\}$/)) { | ||
const variableName = value.substring(2, value.length - 2); | ||
this._validateVariableName(variableName); | ||
return this._getVariableValue(variableName); | ||
} | ||
return value.replace(/\{\{([a-zA-Z0-9.]+)\}\}/g, (match, variableName) => { | ||
this._validateVariableName(variableName); | ||
const variableValue = this._getVariableValue(variableName); | ||
return `${variableValue}`; | ||
}); | ||
} | ||
_interpolateObject(value) { | ||
const result = {}; | ||
Object.entries(value).forEach(([key, v]) => { | ||
result[key] = this._interpolate(v); | ||
}); | ||
return result; | ||
} | ||
_interpolateArray(value) { | ||
return value.map(this._interpolate.bind(this)); | ||
} | ||
_findPositionOfNextVariable(startIndex = 0) { | ||
@@ -1718,5 +1759,3 @@ const openBraceIndex = this.payload.indexOf("{{", startIndex); | ||
const isInsideString = this._isVariableInsideString(openBraceIndex); | ||
if (!isInsideString) | ||
return { openBraceIndex, closeBraceIndex }; | ||
return this._findPositionOfNextVariable(openBraceIndex + 1); | ||
return { isInsideString, openBraceIndex, closeBraceIndex }; | ||
} | ||
@@ -1744,3 +1783,3 @@ _isVariableInsideString(openBraceIndex) { | ||
this.replacedVariables.push({ variableName, replacement }); | ||
this.payload = this.payload.replace(`{{${variableName}}}`, replacement); | ||
this.payload = this.payload.substring(0, openBraceIndex) + replacement + this.payload.substring(closeBraceIndex + 1); | ||
} | ||
@@ -1755,6 +1794,6 @@ _validateVariableName(variableName) { | ||
} | ||
_getVariableReplacement(variableName) { | ||
_getVariableValue(variableName) { | ||
const [variable, ...properties] = variableName.split("."); | ||
const context = this.context[variable]; | ||
const replacement = properties.reduce((ctx, prop) => { | ||
const value = properties.reduce((ctx, prop) => { | ||
if (!ctx || typeof ctx !== "object") | ||
@@ -1764,4 +1803,8 @@ return null; | ||
}, context); | ||
return replacement ? JSON.stringify(replacement) : null; | ||
return value; | ||
} | ||
_getVariableReplacement(variableName) { | ||
const value = this._getVariableValue(variableName); | ||
return value ? JSON.stringify(value) : null; | ||
} | ||
}; | ||
@@ -1768,0 +1811,0 @@ __name(WebhookPayloadTemplate, "WebhookPayloadTemplate"); |
{ | ||
"name": "@apify/utilities", | ||
"version": "2.4.1", | ||
"version": "2.5.0", | ||
"description": "Tools and constants shared across Apify projects.", | ||
@@ -52,3 +52,3 @@ "main": "./index.js", | ||
}, | ||
"gitHead": "48ef6b6693d168dcd735d38035a36a25e4ee7119" | ||
"gitHead": "d570c3fbfd8bee76c9cb49d52529da20cc7bb178" | ||
} |
@@ -66,3 +66,5 @@ import { JsonVariable } from './utilities.client'; | ||
*/ | ||
static parse(payloadTemplate: string, allowedVariables?: Set<string> | null, context?: Record<string, any>): Record<string, any>; | ||
static parse(payloadTemplate: string, allowedVariables?: Set<string> | null, context?: Record<string, any>, options?: { | ||
interpolateStrings?: boolean; | ||
}): Record<string, any>; | ||
/** | ||
@@ -101,2 +103,6 @@ * Stringify an object into a webhook payload template. | ||
private _parse; | ||
private _interpolate; | ||
private _interpolateString; | ||
private _interpolateObject; | ||
private _interpolateArray; | ||
private _findPositionOfNextVariable; | ||
@@ -107,4 +113,5 @@ private _isVariableInsideString; | ||
private _validateVariableName; | ||
private _getVariableValue; | ||
private _getVariableReplacement; | ||
} | ||
export {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
313273
4235