request.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = (p: string) =>
  16. `${process.env.REACT_APP_API_BASE}/api${p}`;
  17. export const API_HOST = `${process.env.REACT_APP_API_BASE}`;
  18. export const GRPC_HOST: string =
  19. process.env.REACT_APP_GRPC_HOST || "http://127.0.0.1:9999";
  20. export const grpc_metadata = (): Metadata => {
  21. return {
  22. authorization: `Bearer ${getToken()}`,
  23. "accept-language": getLocale(),
  24. };
  25. };
  26. export const upload = () => {
  27. return {
  28. Authorization: `Bearer ${getToken()}`,
  29. };
  30. };
  31. export const options = (method: string): RequestInit => {
  32. return {
  33. credentials: "include",
  34. headers: {
  35. Authorization: `Bearer ${getToken()}`,
  36. "Content-Type": "application/json; charset=utf-8",
  37. },
  38. mode: "cors",
  39. method,
  40. };
  41. };
  42. export const get = async <R>(path: string): Promise<R> => {
  43. const response = await fetch(backend(path), options("GET"));
  44. if (!response.ok) {
  45. throw response.status;
  46. }
  47. const res: R = await response.json();
  48. return res;
  49. };
  50. export const delete_ = async <R>(path: string): Promise<R> => {
  51. const response = await fetch(backend(path), options("DELETE"));
  52. const res: R = await response.json();
  53. return res;
  54. };
  55. export const delete_2 = async <Q, R>(path: string, body: Q): Promise<R> => {
  56. const data = options("DELETE");
  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. // https://github.github.io/fetch/#options
  63. export const post = async <Q, R>(path: string, body: Q): Promise<R> => {
  64. const data = options("POST");
  65. data.body = JSON.stringify(body);
  66. const response = await fetch(backend(path), data);
  67. const res: R = await response.json();
  68. return res;
  69. };
  70. export const patch = <Request, Response>(
  71. path: string,
  72. body: Request
  73. ): Promise<Response> => {
  74. const data = options("PATCH");
  75. data.body = JSON.stringify(body);
  76. return fetch(backend(path), data).then((res) => {
  77. if (res.status === 200) {
  78. return res.json();
  79. }
  80. throw res.text();
  81. });
  82. };
  83. export const put = async <Request, Response>(
  84. path: string,
  85. body: Request
  86. ): Promise<Response> => {
  87. const data = options("PUT");
  88. data.body = JSON.stringify(body);
  89. const response = await fetch(backend(path), data);
  90. const res: Response = await response.json();
  91. return res;
  92. };
  93. export const download = (path: string, name: string) => {
  94. const data = options("GET");
  95. fetch(backend(path), data)
  96. .then((response) => response.blob())
  97. .then((blob) => {
  98. var url = window.URL.createObjectURL(blob);
  99. var a = document.createElement("a");
  100. a.href = url;
  101. a.download = name;
  102. document.body.appendChild(a); // for firefox
  103. a.click();
  104. a.remove();
  105. });
  106. };