index.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. const getGQL = (url) => {
  12. return async (query, variables = {}) => {
  13. let params = { headers: { "Content-Type": "application/json" } };
  14. let authToken = localStorage.getItem("authToken");
  15. if (authToken) {
  16. params.headers.Authorization = "Bearer " + authToken;
  17. }
  18. params.body = JSON.stringify({
  19. query: query,
  20. variables: variables,
  21. });
  22. params.method = "POST";
  23. let res = await fetch(url, params).then((res) => res.json());
  24. if ("data" in res) {
  25. return res.data[Object.keys(res.data)[0]];
  26. } else if ("errors" in res) {
  27. throw res.errors[Object.keys(res.errors)[0]];
  28. }
  29. };
  30. };
  31. const catByID = async (id) => {
  32. const gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql?");
  33. let catDataOrError = await gql(
  34. `
  35. query catByID($q:String){
  36. CategoryFindOne(query:$q){
  37. name
  38. createdAt
  39. }
  40. }
  41. `,
  42. {
  43. q: `[{ \"_id\": \"${id}\" }]`,
  44. }
  45. );
  46. return catDataOrError;
  47. };
  48. const goodByID = async (id) => {
  49. const gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql?");
  50. let goodDataOrError = await gql(
  51. `
  52. query goodByID($q:String){
  53. GoodFindOne(query:$q){
  54. name
  55. createdAt
  56. description
  57. price
  58. }
  59. }
  60. `,
  61. {
  62. q: `[{ \"_id\": \"${id}\" }]`,
  63. }
  64. );
  65. return goodDataOrError;
  66. };
  67. const register = async (login, password) => {
  68. localStorage.removeItem("authToken");
  69. const gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql?");
  70. let respOrError = await gql(
  71. `mutation register($login:String,$password:String){
  72. UserUpsert(user:{login:$login,password:$password}){
  73. _id login
  74. }
  75. }`,
  76. {
  77. login: login,
  78. password: password,
  79. }
  80. );
  81. return respOrError;
  82. };
  83. const login = async (login, password) => {
  84. const gql = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql?");
  85. localStorage.removeItem("authToken");
  86. const token = await gql(
  87. `query log($login:String,$password:String){
  88. login(login:$login,password:$password)
  89. }
  90. `,
  91. {
  92. login: login,
  93. password: password,
  94. }
  95. );
  96. localStorage.setItem("authToken", token);
  97. return token;
  98. };
  99. catByID("5dc49f4d5df9d670df48cc64").then((data) => console.log(data));
  100. goodByID("61714b33ef4e1b3e3b67703a").then((data) => console.log(data));
  101. </script>
  102. </body>
  103. </html>