index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // practises
  2. // let str = "asdasdadsdsaads";
  3. // function checkSum(str) {
  4. // let result = 0;
  5. // for (let letter of str) {
  6. // result += letter.charCodeAt();
  7. // }
  8. // return result;
  9. // }
  10. // console.log(checkSum(str));
  11. // function sign(data, salt) {
  12. // const json = JSON.stringify(data);
  13. // const sum = checkSum(json + salt);
  14. // return json + sum;
  15. // }
  16. // console.log(sign({ id: 100500 }, "FBSALT"));
  17. // function verify(signed, salt) {
  18. // const json = signed.slice(0, signed.lastIndexOf("}") + 1);
  19. // const tokenSum = +signed.slice(signed.lastIndexOf("}") + 1);
  20. // const sum = checkSum(json + salt);
  21. // console.log(json, tokenSum, sum);
  22. // return sum === tokenSum;
  23. // }
  24. // console.log(verify('{"id":100500}1317', "FBSALT"));
  25. // homework
  26. const backURL = "http://shop-roles.asmer.fs.a-level.com.ua";
  27. localStorage.authToken =
  28. "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2MWE0ZGIwZWM3NTBjMTJiYTZiYTQwMWYiLCJsb2dpbiI6ImFib2JhIiwiYWNsIjpbIjYxYTRkYjBlYzc1MGMxMmJhNmJhNDAxZiIsInVzZXIiXX0sImlhdCI6MTYzODE5NTk1Mn0.qyOhmkFMZk1Xqx-cx8FyI2LiWbYgJyPZLOzk4Ng_zDY";
  29. const getGQL = (url) => (query, variables) =>
  30. fetch(url, {
  31. method: "POST",
  32. headers: {
  33. "Content-Type": "application/json",
  34. ...(localStorage.authToken
  35. ? { Authorization: "Bearer " + localStorage.authToken }
  36. : {}),
  37. },
  38. body: JSON.stringify({ query, variables }),
  39. })
  40. .then((res) => res.json())
  41. .then((data) => {
  42. if ("errors" in data) {
  43. console.log(data.errors);
  44. }
  45. return data.data;
  46. });
  47. const gql = getGQL(`${backURL}/graphql`);
  48. async function registration() {
  49. return await gql(
  50. `mutation reg($login:String, $password:String){
  51. UserUpsert(user:{login:$login, password:$password, nick:$login}){
  52. _id login
  53. }
  54. }`,
  55. { login: "asdasd939", password: "123456" }
  56. );
  57. }
  58. async function login() {
  59. return await gql(
  60. `query log($login:String, $password:String){
  61. login(login:$login, password: $password)
  62. }`,
  63. { login: "aboba", password: "123456" }
  64. );
  65. }
  66. async function getOrder() {
  67. return await gql(
  68. `query o($q:String){
  69. OrderFind(query:$q){
  70. _id total orderGoods{
  71. price count total
  72. good{
  73. name categories{
  74. name
  75. }
  76. }
  77. }
  78. }
  79. }`,
  80. { q: "[{}]" }
  81. );
  82. }
  83. async function showData() {
  84. const reg = await registration();
  85. const log = await login();
  86. const order = await getOrder();
  87. console.log(reg);
  88. console.log(log);
  89. console.log(order);
  90. }
  91. showData();