request.ts 3.1 KB

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