@hostwebhook/template-engine 1.1.1 → 1.2.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/dist/index.d.mts +30 -1
- package/dist/index.d.ts +30 -1
- package/dist/index.js +24 -0
- package/dist/index.mjs +22 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -55,6 +55,35 @@ interface CodeResult {
|
|
|
55
55
|
*/
|
|
56
56
|
declare function renderTemplate(template: string, ctx: TemplateContext, opts?: TemplateOptions): TemplateResult | CodeResult;
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Resolve a single input value — literal string or {{expression}}.
|
|
60
|
+
*
|
|
61
|
+
* If the value is wrapped in `{{...}}`, it's evaluated as a template expression
|
|
62
|
+
* (supports paths, pipes, $() cross-node references, JS when allowJs).
|
|
63
|
+
* Otherwise, the value is returned as-is (literal).
|
|
64
|
+
*
|
|
65
|
+
* Preserves types: `{{payload.count}}` returns the original number, not a string.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* resolveInput("active", payload) // → "active"
|
|
69
|
+
* resolveInput("{{payload.status}}", payload) // → "delivered"
|
|
70
|
+
* resolveInput("{{payload.amount}}", payload) // → 1500 (number)
|
|
71
|
+
* resolveInput("{{payload.date | formatDate}}", payload) // → "2024-06-15"
|
|
72
|
+
* resolveInput('{{$("Filter").count}}', payload, { allowJs: true, workspacePayloads }) // → 42
|
|
73
|
+
* resolveInput(undefined, payload) // → undefined
|
|
74
|
+
*/
|
|
75
|
+
declare function resolveInput(value: string | undefined | null, payload: unknown, opts?: Pick<TemplateOptions, 'allowJs' | 'workspacePayloads'>): unknown;
|
|
76
|
+
/**
|
|
77
|
+
* Normalize a field path — strips `{{`, `}}`, and `payload.` prefix.
|
|
78
|
+
*
|
|
79
|
+
* Accepts any of these formats and returns the plain dot-notation path:
|
|
80
|
+
* - `"{{payload.customer.name}}"` → `"customer.name"`
|
|
81
|
+
* - `"payload.customer.name"` → `"customer.name"`
|
|
82
|
+
* - `"customer.name"` → `"customer.name"`
|
|
83
|
+
* - `"{{customer.name}}"` → `"customer.name"`
|
|
84
|
+
*/
|
|
85
|
+
declare function resolveFieldPath(field: string): string;
|
|
86
|
+
|
|
58
87
|
/**
|
|
59
88
|
* Resolve a dot-notation path against a TemplateContext.
|
|
60
89
|
* Supports bracket notation for array indexing: `payload.items[0].name`
|
|
@@ -87,4 +116,4 @@ declare function applyPipe(pipe: string, val: unknown, arg?: string, ctx?: Templ
|
|
|
87
116
|
*/
|
|
88
117
|
declare function evaluate(expr: string, ctx: TemplateContext, opts?: TemplateOptions): string;
|
|
89
118
|
|
|
90
|
-
export { type CodeResult, type TemplateContext, type TemplateMode, type TemplateOptions, type TemplateResult, applyPipe, evaluate, renderTemplate, resolvePath, tryNullCoalesce, tryTernary, valueToString };
|
|
119
|
+
export { type CodeResult, type TemplateContext, type TemplateMode, type TemplateOptions, type TemplateResult, applyPipe, evaluate, renderTemplate, resolveFieldPath, resolveInput, resolvePath, tryNullCoalesce, tryTernary, valueToString };
|
package/dist/index.d.ts
CHANGED
|
@@ -55,6 +55,35 @@ interface CodeResult {
|
|
|
55
55
|
*/
|
|
56
56
|
declare function renderTemplate(template: string, ctx: TemplateContext, opts?: TemplateOptions): TemplateResult | CodeResult;
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Resolve a single input value — literal string or {{expression}}.
|
|
60
|
+
*
|
|
61
|
+
* If the value is wrapped in `{{...}}`, it's evaluated as a template expression
|
|
62
|
+
* (supports paths, pipes, $() cross-node references, JS when allowJs).
|
|
63
|
+
* Otherwise, the value is returned as-is (literal).
|
|
64
|
+
*
|
|
65
|
+
* Preserves types: `{{payload.count}}` returns the original number, not a string.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* resolveInput("active", payload) // → "active"
|
|
69
|
+
* resolveInput("{{payload.status}}", payload) // → "delivered"
|
|
70
|
+
* resolveInput("{{payload.amount}}", payload) // → 1500 (number)
|
|
71
|
+
* resolveInput("{{payload.date | formatDate}}", payload) // → "2024-06-15"
|
|
72
|
+
* resolveInput('{{$("Filter").count}}', payload, { allowJs: true, workspacePayloads }) // → 42
|
|
73
|
+
* resolveInput(undefined, payload) // → undefined
|
|
74
|
+
*/
|
|
75
|
+
declare function resolveInput(value: string | undefined | null, payload: unknown, opts?: Pick<TemplateOptions, 'allowJs' | 'workspacePayloads'>): unknown;
|
|
76
|
+
/**
|
|
77
|
+
* Normalize a field path — strips `{{`, `}}`, and `payload.` prefix.
|
|
78
|
+
*
|
|
79
|
+
* Accepts any of these formats and returns the plain dot-notation path:
|
|
80
|
+
* - `"{{payload.customer.name}}"` → `"customer.name"`
|
|
81
|
+
* - `"payload.customer.name"` → `"customer.name"`
|
|
82
|
+
* - `"customer.name"` → `"customer.name"`
|
|
83
|
+
* - `"{{customer.name}}"` → `"customer.name"`
|
|
84
|
+
*/
|
|
85
|
+
declare function resolveFieldPath(field: string): string;
|
|
86
|
+
|
|
58
87
|
/**
|
|
59
88
|
* Resolve a dot-notation path against a TemplateContext.
|
|
60
89
|
* Supports bracket notation for array indexing: `payload.items[0].name`
|
|
@@ -87,4 +116,4 @@ declare function applyPipe(pipe: string, val: unknown, arg?: string, ctx?: Templ
|
|
|
87
116
|
*/
|
|
88
117
|
declare function evaluate(expr: string, ctx: TemplateContext, opts?: TemplateOptions): string;
|
|
89
118
|
|
|
90
|
-
export { type CodeResult, type TemplateContext, type TemplateMode, type TemplateOptions, type TemplateResult, applyPipe, evaluate, renderTemplate, resolvePath, tryNullCoalesce, tryTernary, valueToString };
|
|
119
|
+
export { type CodeResult, type TemplateContext, type TemplateMode, type TemplateOptions, type TemplateResult, applyPipe, evaluate, renderTemplate, resolveFieldPath, resolveInput, resolvePath, tryNullCoalesce, tryTernary, valueToString };
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,8 @@ __export(index_exports, {
|
|
|
33
33
|
applyPipe: () => applyPipe,
|
|
34
34
|
evaluate: () => evaluate,
|
|
35
35
|
renderTemplate: () => renderTemplate,
|
|
36
|
+
resolveFieldPath: () => resolveFieldPath,
|
|
37
|
+
resolveInput: () => resolveInput,
|
|
36
38
|
resolvePath: () => resolvePath,
|
|
37
39
|
tryNullCoalesce: () => tryNullCoalesce,
|
|
38
40
|
tryTernary: () => tryTernary,
|
|
@@ -533,11 +535,33 @@ function renderTemplate(template, ctx, opts) {
|
|
|
533
535
|
return resolveTextTemplate(template, ctx, opts);
|
|
534
536
|
}
|
|
535
537
|
}
|
|
538
|
+
|
|
539
|
+
// src/resolve-input.ts
|
|
540
|
+
function resolveInput(value, payload, opts) {
|
|
541
|
+
if (value == null || value === "") return value;
|
|
542
|
+
const str = String(value);
|
|
543
|
+
const match = str.match(/^\{\{([\s\S]+)\}\}$/);
|
|
544
|
+
if (!match) return value;
|
|
545
|
+
const expr = match[1].trim();
|
|
546
|
+
const ctx = { payload, headers: {}, meta: {} };
|
|
547
|
+
const resolved = resolvePath(expr, ctx);
|
|
548
|
+
if (resolved !== void 0) return resolved;
|
|
549
|
+
const evalOpts = opts ? { allowJs: opts.allowJs, workspacePayloads: opts.workspacePayloads } : void 0;
|
|
550
|
+
const result = evaluate(expr, ctx, evalOpts);
|
|
551
|
+
if (result !== "") return result;
|
|
552
|
+
return value;
|
|
553
|
+
}
|
|
554
|
+
function resolveFieldPath(field) {
|
|
555
|
+
if (!field) return "";
|
|
556
|
+
return field.replace(/^\{\{\s*/, "").replace(/\s*\}\}$/, "").replace(/^payload\./, "").trim();
|
|
557
|
+
}
|
|
536
558
|
// Annotate the CommonJS export names for ESM import in node:
|
|
537
559
|
0 && (module.exports = {
|
|
538
560
|
applyPipe,
|
|
539
561
|
evaluate,
|
|
540
562
|
renderTemplate,
|
|
563
|
+
resolveFieldPath,
|
|
564
|
+
resolveInput,
|
|
541
565
|
resolvePath,
|
|
542
566
|
tryNullCoalesce,
|
|
543
567
|
tryTernary,
|
package/dist/index.mjs
CHANGED
|
@@ -491,10 +491,32 @@ function renderTemplate(template, ctx, opts) {
|
|
|
491
491
|
return resolveTextTemplate(template, ctx, opts);
|
|
492
492
|
}
|
|
493
493
|
}
|
|
494
|
+
|
|
495
|
+
// src/resolve-input.ts
|
|
496
|
+
function resolveInput(value, payload, opts) {
|
|
497
|
+
if (value == null || value === "") return value;
|
|
498
|
+
const str = String(value);
|
|
499
|
+
const match = str.match(/^\{\{([\s\S]+)\}\}$/);
|
|
500
|
+
if (!match) return value;
|
|
501
|
+
const expr = match[1].trim();
|
|
502
|
+
const ctx = { payload, headers: {}, meta: {} };
|
|
503
|
+
const resolved = resolvePath(expr, ctx);
|
|
504
|
+
if (resolved !== void 0) return resolved;
|
|
505
|
+
const evalOpts = opts ? { allowJs: opts.allowJs, workspacePayloads: opts.workspacePayloads } : void 0;
|
|
506
|
+
const result = evaluate(expr, ctx, evalOpts);
|
|
507
|
+
if (result !== "") return result;
|
|
508
|
+
return value;
|
|
509
|
+
}
|
|
510
|
+
function resolveFieldPath(field) {
|
|
511
|
+
if (!field) return "";
|
|
512
|
+
return field.replace(/^\{\{\s*/, "").replace(/\s*\}\}$/, "").replace(/^payload\./, "").trim();
|
|
513
|
+
}
|
|
494
514
|
export {
|
|
495
515
|
applyPipe,
|
|
496
516
|
evaluate,
|
|
497
517
|
renderTemplate,
|
|
518
|
+
resolveFieldPath,
|
|
519
|
+
resolveInput,
|
|
498
520
|
resolvePath,
|
|
499
521
|
tryNullCoalesce,
|
|
500
522
|
tryTernary,
|