template.d.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { MessageContent } from "../messages/index.js";
  2. import type { InputValues } from "../utils/types/index.js";
  3. /**
  4. * Type that specifies the format of a template.
  5. */
  6. export type TemplateFormat = "f-string" | "mustache";
  7. /**
  8. * Type that represents a node in a parsed format string. It can be either
  9. * a literal text or a variable name.
  10. */
  11. export type ParsedTemplateNode = {
  12. type: "literal";
  13. text: string;
  14. } | {
  15. type: "variable";
  16. name: string;
  17. };
  18. /**
  19. * Alias for `ParsedTemplateNode` since it is the same for
  20. * both f-string and mustache templates.
  21. */
  22. export type ParsedFStringNode = ParsedTemplateNode;
  23. export declare const parseFString: (template: string) => ParsedTemplateNode[];
  24. export declare const parseMustache: (template: string) => ParsedTemplateNode[];
  25. export declare const interpolateFString: (template: string, values: InputValues) => string;
  26. export declare const interpolateMustache: (template: string, values: InputValues) => string;
  27. /**
  28. * Type that represents a function that takes a template string and a set
  29. * of input values, and returns a string where all variables in the
  30. * template have been replaced with their corresponding values.
  31. */
  32. type Interpolator = (template: string, values: InputValues) => string;
  33. /**
  34. * Type that represents a function that takes a template string and
  35. * returns an array of `ParsedTemplateNode`.
  36. */
  37. type Parser = (template: string) => ParsedTemplateNode[];
  38. export declare const DEFAULT_FORMATTER_MAPPING: Record<TemplateFormat, Interpolator>;
  39. export declare const DEFAULT_PARSER_MAPPING: Record<TemplateFormat, Parser>;
  40. export declare const renderTemplate: (template: string, templateFormat: TemplateFormat, inputValues: InputValues) => string;
  41. export declare const parseTemplate: (template: string, templateFormat: TemplateFormat) => ParsedTemplateNode[];
  42. export declare const checkValidTemplate: (template: MessageContent, templateFormat: TemplateFormat, inputVariables: string[]) => void;
  43. export {};