utils.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { stringOrDate } from "react-big-calendar";
  2. export function ApiFetch(url: string, method = "GET", data: any = null): Promise<Response> {
  3. const apiHost = process.env.REACT_APP_API_HOST;
  4. let body: string = "{}";
  5. if (data) {
  6. body = JSON.stringify(data);
  7. }
  8. return new Promise((resolve, reject) => {
  9. let apiUrl = apiHost + url;
  10. console.log("api", apiUrl);
  11. fetch(apiUrl, {
  12. method: method,
  13. })
  14. .then((response) => response.json())
  15. .then((response) => {
  16. resolve(response);
  17. })
  18. .catch((error) => {
  19. reject(error);
  20. });
  21. });
  22. }
  23. export function ApiGetText(url: stringOrDate): Promise<String> {
  24. const apiHost = "http://127.0.0.1:8000/api/";
  25. return new Promise((resolve, reject) => {
  26. let apiUrl = apiHost + url;
  27. console.log("api", apiUrl);
  28. fetch(apiUrl)
  29. .then((response) => response.text())
  30. .then((response) => {
  31. resolve(response);
  32. })
  33. .catch((error) => {
  34. reject(error);
  35. });
  36. });
  37. }
  38. export function PaliToEn(pali: string): string {
  39. let output: string = pali.toLowerCase();
  40. output = output.replaceAll(" ", "_");
  41. output = output.replaceAll("-", "_");
  42. output = output.replaceAll("ā", "a");
  43. output = output.replaceAll("ī", "i");
  44. output = output.replaceAll("ū", "u");
  45. output = output.replaceAll("ḍ", "d");
  46. output = output.replaceAll("ṭ", "t");
  47. output = output.replaceAll("ḷ", "l");
  48. return output;
  49. }