templating.ts 643 B

12345678910111213141516171819202122232425
  1. import { Environment } from "~/newstore/environments"
  2. export function parseBodyEnvVariables(
  3. body: string,
  4. env: Environment["variables"]
  5. ) {
  6. return body.replace(/<<\w+>>/g, (key) => {
  7. const found = env.find((envVar) => envVar.key === key.replace(/[<>]/g, ""))
  8. return found ? found.value : key
  9. })
  10. }
  11. export function parseTemplateString(
  12. str: string,
  13. variables: Environment["variables"]
  14. ) {
  15. if (!variables || !str) {
  16. return str
  17. }
  18. const searchTerm = /<<([^>]*)>>/g // "<<myVariable>>"
  19. return decodeURI(encodeURI(str)).replace(
  20. searchTerm,
  21. (_, p1) => variables.find((x) => x.key === p1)?.value || ""
  22. )
  23. }