request.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { GraphQLError } from "graphql";
  2. import { get as get_token } from "./reducers/session";
  3. export const upload = () => {
  4. return {
  5. Authorization: `Bearer ${get_token()}`,
  6. };
  7. };
  8. export const options = (method: string): RequestInit => {
  9. return {
  10. credentials: "include",
  11. headers: {
  12. Authorization: `Bearer ${get_token()}`,
  13. "Content-Type": "application/json; charset=utf-8",
  14. },
  15. mode: "cors",
  16. method,
  17. };
  18. };
  19. export const get = async <R>(path: string): Promise<R> => {
  20. const response = await fetch(path, options("GET"));
  21. const res: R = await response.json();
  22. return res;
  23. };
  24. export const delete_ = async <R>(path: string): Promise<R> => {
  25. const response = await fetch(path, options("DELETE"));
  26. const res: R = await response.json();
  27. return res;
  28. };
  29. export const delete_2 = async <Q, R>(path: string, body: Q): Promise<R> => {
  30. const data = options("DELETE");
  31. data.body = JSON.stringify(body);
  32. const response = await fetch(path, data);
  33. const res: R = await response.json();
  34. return res;
  35. };
  36. export interface IGraphqlResponse<R> {
  37. data?: R;
  38. errors?: GraphQLError[];
  39. }
  40. export const graphql = async <Q, R>(
  41. query: string,
  42. variables: Q
  43. ): Promise<IGraphqlResponse<R>> => {
  44. const res: IGraphqlResponse<R> = await post("/graphql", { query, variables });
  45. return res;
  46. };
  47. // https://github.github.io/fetch/#options
  48. export const post = async <Q, R>(path: string, body: Q): Promise<R> => {
  49. const data = options("POST");
  50. data.body = JSON.stringify(body);
  51. const response = await fetch(path, data);
  52. const res: R = await response.json();
  53. return res;
  54. };
  55. export const patch = <Request, Response>(
  56. path: string,
  57. body: Request
  58. ): Promise<Response> => {
  59. const data = options("PATCH");
  60. data.body = JSON.stringify(body);
  61. return fetch(path, data).then((res) => {
  62. if (res.status === 200) {
  63. return res.json();
  64. }
  65. throw res.text();
  66. });
  67. };
  68. export const put = <Request, Response>(
  69. path: string,
  70. body: Request
  71. ): Promise<Response> => {
  72. const data = options("PUT");
  73. data.body = JSON.stringify(body);
  74. return fetch(path, data).then((res) =>
  75. res.status === 200
  76. ? res.json()
  77. : res.json().then((err) => {
  78. throw err;
  79. })
  80. );
  81. };
  82. export const download = (path: string, name: string) => {
  83. const data = options("GET");
  84. fetch(path, data)
  85. .then((response) => response.blob())
  86. .then((blob) => {
  87. const url = window.URL.createObjectURL(blob);
  88. const a = document.createElement("a");
  89. a.href = url;
  90. a.download = name;
  91. document.body.appendChild(a); // for firefox
  92. a.click();
  93. a.remove();
  94. });
  95. };