@k-msg/template 0.19.0 → 0.20.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/README.md +53 -43
- package/dist/index.d.ts +4 -6
- package/dist/index.js +15 -15
- package/dist/index.js.map +8 -10
- package/dist/index.mjs +15 -15
- package/dist/index.mjs.map +8 -10
- package/dist/personalization/variable.replacer.d.ts +139 -0
- package/dist/runtime/template-input.d.ts +19 -0
- package/dist/{service.d.ts → runtime/template-lifecycle.service.d.ts} +5 -3
- package/dist/{services/template.service.d.ts → toolkit/in-memory-template-store.d.ts} +2 -2
- package/dist/toolkit/index.d.ts +3 -0
- package/dist/toolkit/index.js +43 -0
- package/dist/toolkit/index.js.map +88 -0
- package/dist/toolkit/index.mjs +43 -0
- package/dist/toolkit/index.mjs.map +88 -0
- package/package.json +9 -4
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Variable replacement and personalization system
|
|
3
|
+
*/
|
|
4
|
+
export type TemplateVariableMap = Record<string, unknown>;
|
|
5
|
+
export interface TemplatePersonalizerOptions {
|
|
6
|
+
variablePattern: RegExp;
|
|
7
|
+
allowUndefined: boolean;
|
|
8
|
+
undefinedReplacement: string;
|
|
9
|
+
caseSensitive: boolean;
|
|
10
|
+
enableFormatting: boolean;
|
|
11
|
+
enableConditionals: boolean;
|
|
12
|
+
enableLoops: boolean;
|
|
13
|
+
maxRecursionDepth: number;
|
|
14
|
+
}
|
|
15
|
+
export interface TemplateVariableInfo {
|
|
16
|
+
name: string;
|
|
17
|
+
value: any;
|
|
18
|
+
formatted: string;
|
|
19
|
+
type: "string" | "number" | "date" | "boolean" | "array" | "object" | "undefined";
|
|
20
|
+
position: {
|
|
21
|
+
start: number;
|
|
22
|
+
end: number;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface ReplacementResult {
|
|
26
|
+
content: string;
|
|
27
|
+
variables: TemplateVariableInfo[];
|
|
28
|
+
missingVariables: string[];
|
|
29
|
+
errors: ReplacementError[];
|
|
30
|
+
metadata: {
|
|
31
|
+
originalLength: number;
|
|
32
|
+
finalLength: number;
|
|
33
|
+
variableCount: number;
|
|
34
|
+
replacementTime: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface ReplacementError {
|
|
38
|
+
type: "missing_variable" | "format_error" | "syntax_error" | "recursion_limit";
|
|
39
|
+
message: string;
|
|
40
|
+
variable?: string;
|
|
41
|
+
position?: {
|
|
42
|
+
start: number;
|
|
43
|
+
end: number;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export interface ConditionalBlock {
|
|
47
|
+
condition: string;
|
|
48
|
+
content: string;
|
|
49
|
+
elseContent?: string;
|
|
50
|
+
}
|
|
51
|
+
export interface LoopBlock {
|
|
52
|
+
variable: string;
|
|
53
|
+
array: string;
|
|
54
|
+
content: string;
|
|
55
|
+
}
|
|
56
|
+
export declare class TemplatePersonalizer {
|
|
57
|
+
private options;
|
|
58
|
+
private defaultOptions;
|
|
59
|
+
constructor(options?: Partial<TemplatePersonalizerOptions>);
|
|
60
|
+
/**
|
|
61
|
+
* Replace variables in content
|
|
62
|
+
*/
|
|
63
|
+
replace(content: string, variables: TemplateVariableMap): ReplacementResult;
|
|
64
|
+
/**
|
|
65
|
+
* Extract variables from content without replacing
|
|
66
|
+
*/
|
|
67
|
+
extractVariables(content: string): string[];
|
|
68
|
+
/**
|
|
69
|
+
* Validate that all required variables are provided
|
|
70
|
+
*/
|
|
71
|
+
validate(content: string, variables: TemplateVariableMap): {
|
|
72
|
+
isValid: boolean;
|
|
73
|
+
missingVariables: string[];
|
|
74
|
+
errors: ReplacementError[];
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Preview replacement result without actually replacing
|
|
78
|
+
*/
|
|
79
|
+
preview(content: string, variables: TemplateVariableMap): {
|
|
80
|
+
originalContent: string;
|
|
81
|
+
previewContent: string;
|
|
82
|
+
variableHighlights: Array<{
|
|
83
|
+
variable: string;
|
|
84
|
+
value: string;
|
|
85
|
+
positions: Array<{
|
|
86
|
+
start: number;
|
|
87
|
+
end: number;
|
|
88
|
+
}>;
|
|
89
|
+
}>;
|
|
90
|
+
};
|
|
91
|
+
private replaceSimpleVariables;
|
|
92
|
+
private replaceRecursive;
|
|
93
|
+
private processConditionals;
|
|
94
|
+
private processLoops;
|
|
95
|
+
private parseVariableName;
|
|
96
|
+
private getVariableValue;
|
|
97
|
+
private formatValue;
|
|
98
|
+
private formatDate;
|
|
99
|
+
private getValueType;
|
|
100
|
+
private hasVariables;
|
|
101
|
+
private extractConditionals;
|
|
102
|
+
private extractLoops;
|
|
103
|
+
private extractVariablesFromExpression;
|
|
104
|
+
private evaluateCondition;
|
|
105
|
+
private buildConditionalPattern;
|
|
106
|
+
private buildLoopPattern;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Default instance with Korean-optimized settings
|
|
110
|
+
*/
|
|
111
|
+
export declare const defaultTemplatePersonalizer: TemplatePersonalizer;
|
|
112
|
+
/**
|
|
113
|
+
* Utility functions
|
|
114
|
+
*/
|
|
115
|
+
export declare const TemplateVariableUtils: {
|
|
116
|
+
/**
|
|
117
|
+
* Extract all variables from content
|
|
118
|
+
*/
|
|
119
|
+
extractVariables: (content: string) => string[];
|
|
120
|
+
/**
|
|
121
|
+
* Replace variables in content
|
|
122
|
+
*/
|
|
123
|
+
replace: (content: string, variables: TemplateVariableMap) => string;
|
|
124
|
+
/**
|
|
125
|
+
* Validate content has all required variables
|
|
126
|
+
*/
|
|
127
|
+
validate: (content: string, variables: TemplateVariableMap) => boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Create personalized content for multiple recipients
|
|
130
|
+
*/
|
|
131
|
+
personalize: (content: string, recipients: Array<{
|
|
132
|
+
phoneNumber: string;
|
|
133
|
+
variables: TemplateVariableMap;
|
|
134
|
+
}>) => Array<{
|
|
135
|
+
phoneNumber: string;
|
|
136
|
+
content: string;
|
|
137
|
+
errors?: string[];
|
|
138
|
+
}>;
|
|
139
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { KMsgError, type Result } from "@k-msg/core";
|
|
2
|
+
import type { TemplateButton } from "../types/template.types";
|
|
3
|
+
type TemplatePayload = {
|
|
4
|
+
name?: unknown;
|
|
5
|
+
content?: unknown;
|
|
6
|
+
buttons?: unknown;
|
|
7
|
+
};
|
|
8
|
+
export type ValidateTemplatePayloadOptions = {
|
|
9
|
+
requireName?: boolean;
|
|
10
|
+
requireContent?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type NormalizedTemplatePayload = {
|
|
13
|
+
name?: string;
|
|
14
|
+
content?: string;
|
|
15
|
+
buttons?: TemplateButton[];
|
|
16
|
+
};
|
|
17
|
+
export declare function parseTemplateButtons(value: unknown): Result<TemplateButton[] | undefined, KMsgError>;
|
|
18
|
+
export declare function validateTemplatePayload(payload: TemplatePayload, options?: ValidateTemplatePayloadOptions): Result<NormalizedTemplatePayload, KMsgError>;
|
|
19
|
+
export {};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { KMsgError, type Result, type Template, type TemplateContext, type TemplateCreateInput, type TemplateProvider, type TemplateUpdateInput } from "@k-msg/core";
|
|
2
|
-
export declare class
|
|
1
|
+
import { KMsgError, type Result, type Template, type TemplateContext, type TemplateCreateInput, type TemplateInspectionProvider, type TemplateProvider, type TemplateUpdateInput } from "@k-msg/core";
|
|
2
|
+
export declare class TemplateLifecycleService {
|
|
3
3
|
private readonly provider;
|
|
4
|
-
|
|
4
|
+
private readonly inspectionProvider?;
|
|
5
|
+
constructor(provider: TemplateProvider, inspectionProvider?: TemplateInspectionProvider);
|
|
5
6
|
create(input: TemplateCreateInput, ctx?: TemplateContext): Promise<Result<Template, KMsgError>>;
|
|
6
7
|
update(code: string, patch: TemplateUpdateInput, ctx?: TemplateContext): Promise<Result<Template, KMsgError>>;
|
|
7
8
|
delete(code: string, ctx?: TemplateContext): Promise<Result<void, KMsgError>>;
|
|
@@ -11,4 +12,5 @@ export declare class TemplateService {
|
|
|
11
12
|
page?: number;
|
|
12
13
|
limit?: number;
|
|
13
14
|
}, ctx?: TemplateContext): Promise<Result<Template[], KMsgError>>;
|
|
15
|
+
requestInspection(code: string, ctx?: TemplateContext): Promise<Result<void, KMsgError>>;
|
|
14
16
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { AlimTalkTemplate } from "../types/template.types";
|
|
2
|
-
export declare class
|
|
2
|
+
export declare class InMemoryTemplateStore {
|
|
3
3
|
private templates;
|
|
4
4
|
createTemplate(template: Omit<AlimTalkTemplate, "id" | "metadata">): Promise<AlimTalkTemplate>;
|
|
5
5
|
getTemplate(templateId: string): Promise<AlimTalkTemplate | null>;
|
|
6
6
|
updateTemplate(templateId: string, updates: Partial<AlimTalkTemplate>): Promise<AlimTalkTemplate>;
|
|
7
7
|
deleteTemplate(templateId: string): Promise<void>;
|
|
8
|
-
renderTemplate(templateId: string, variables: Record<string,
|
|
8
|
+
renderTemplate(templateId: string, variables: Record<string, string | number | Date>): Promise<string>;
|
|
9
9
|
private generateTemplateId;
|
|
10
10
|
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { TemplateBuilder, TemplateBuilders } from "../builder/template.builder";
|
|
2
|
+
export { type TemplateHistory, TemplateRegistry, type TemplateRegistryOptions, type TemplateSearchFilters, type TemplateSearchOptions, type TemplateSearchResult, type TemplateUsageStats, type TemplateVersion, } from "../registry/template.registry";
|
|
3
|
+
export { InMemoryTemplateStore } from "./in-memory-template-store";
|