request.ts 2.9 KB

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