request.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if (!response.ok) {
  47. throw response.status;
  48. }
  49. const res: R = await response.json();
  50. return res;
  51. };
  52. export const delete_ = async <R>(path: string): Promise<R> => {
  53. const response = await fetch(backend(path), options("DELETE"));
  54. const res: R = await response.json();
  55. return res;
  56. };
  57. export const delete_2 = async <Q, R>(path: string, body: Q): Promise<R> => {
  58. const data = options("DELETE");
  59. data.body = JSON.stringify(body);
  60. const response = await fetch(backend(path), data);
  61. const res: R = await response.json();
  62. return res;
  63. };
  64. // https://github.github.io/fetch/#options
  65. export const post = async <Q, R>(path: string, body: Q): Promise<R> => {
  66. const data = options("POST");
  67. data.body = JSON.stringify(body);
  68. const response = await fetch(backend(path), data);
  69. const res: R = await response.json();
  70. return res;
  71. };
  72. export const patch = <Request, Response>(
  73. path: string,
  74. body: Request
  75. ): Promise<Response> => {
  76. const data = options("PATCH");
  77. data.body = JSON.stringify(body);
  78. return fetch(backend(path), data).then((res) => {
  79. if (res.status === 200) {
  80. return res.json();
  81. }
  82. throw res.text();
  83. });
  84. };
  85. export const put = async <Request, Response>(
  86. path: string,
  87. body: Request
  88. ): Promise<Response> => {
  89. const data = options("PUT");
  90. data.body = JSON.stringify(body);
  91. const response = await fetch(backend(path), data);
  92. const res: Response = await response.json();
  93. return res;
  94. };
  95. export const download = (path: string, name: string) => {
  96. const data = options("GET");
  97. fetch(backend(path), data)
  98. .then((response) => response.blob())
  99. .then((blob) => {
  100. var url = window.URL.createObjectURL(blob);
  101. var a = document.createElement("a");
  102. a.href = url;
  103. a.download = name;
  104. document.body.appendChild(a); // for firefox
  105. a.click();
  106. a.remove();
  107. });
  108. };