2
0

request.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Metadata } from "grpc-web";
  2. import { get as getLocale } from "./locales";
  3. import { get as getToken } from "./reducers/current-user";
  4. export const backend = (u: string) => `/api${u}`;
  5. export const GRPC_HOST: string =
  6. import.meta.env.VITE_APP_GRPC_HOST || "http://localhost:9999";
  7. export const grpc_metadata = (): Metadata => {
  8. return {
  9. authorization: `Bearer ${getToken()}`,
  10. "accept-language": getLocale() || "en-US",
  11. };
  12. };
  13. export const upload = () => {
  14. return {
  15. Authorization: `Bearer ${getToken()}`,
  16. };
  17. };
  18. export const options = (method: string): RequestInit => {
  19. return {
  20. credentials: "include",
  21. headers: {
  22. Authorization: `Bearer ${getToken()}`,
  23. "Content-Type": "application/json; charset=utf-8",
  24. },
  25. mode: "cors",
  26. method,
  27. };
  28. };
  29. export const get = async <R>(path: string): Promise<R> => {
  30. const response = await fetch(backend(path), options("GET"));
  31. const res: R = await response.json();
  32. return res;
  33. };
  34. export const delete_ = async <R>(path: string): Promise<R> => {
  35. const response = await fetch(backend(path), options("DELETE"));
  36. const res: R = await response.json();
  37. return res;
  38. };
  39. // https://github.github.io/fetch/#options
  40. export const post = async <Q, R>(path: string, body: Q): Promise<R> => {
  41. const data = options("POST");
  42. data.body = JSON.stringify(body);
  43. const response = await fetch(backend(path), data);
  44. const res: R = await response.json();
  45. return res;
  46. };
  47. export const patch = <Request, Response>(
  48. path: string,
  49. body: Request
  50. ): Promise<Response> => {
  51. const data = options("PATCH");
  52. data.body = JSON.stringify(body);
  53. return fetch(backend(path), data).then((res) => {
  54. if (res.status === 200) {
  55. return res.json();
  56. }
  57. throw res.text();
  58. });
  59. };
  60. export const put = <Request, Response>(
  61. path: string,
  62. body: Request
  63. ): Promise<Response> => {
  64. const data = options("PUT");
  65. data.body = JSON.stringify(body);
  66. return fetch(backend(path), data).then((res) =>
  67. res.status === 200
  68. ? res.json()
  69. : res.json().then((err) => {
  70. throw err;
  71. })
  72. );
  73. };
  74. export const download = (path: string, name: string) => {
  75. const data = options("GET");
  76. fetch(backend(path), data)
  77. .then((response) => response.blob())
  78. .then((blob) => {
  79. var url = window.URL.createObjectURL(blob);
  80. var a = document.createElement("a");
  81. a.href = url;
  82. a.download = name;
  83. document.body.appendChild(a); // for firefox
  84. a.click();
  85. a.remove();
  86. });
  87. };