summaryrefslogtreecommitdiff
path: root/app/public/api.js
blob: 632190931221115ecd6fe9b74fa0e1470ae43016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
export async function getNote(id) {
  const res = await fetch(`${id}`);
  const note = await res.json();
  //console.log(note);
  return note;
}

export async function getList() {
  const res = await fetch("/list");
  const list = await res.json();
  //console.log(list);

  return Promise.all(list.map(id => getNote(id)));
}

export async function saveNote(id, content) {
  const res = await fetch("/"+id, {
    method: "POST",
    body: JSON.stringify({id, content}),
  });

  const note = await res.json();
  //console.log(note);
}

export async function newNote() {
  const res = await fetch("/new", {
    method: "POST",
  });

  const note = await res.json();
  //console.log(note);
  return note;
}

export async function getUserData() {
  const res = await fetch(`/user`);
  const user = await res.json();
  //console.log(user);
  return user;
}

export async function getUserSessions() {
  const res = await fetch(`/session-list`);
  const sessions = await res.json();
  //console.log(sessions);
  return sessions;
}

export async function signIn({username, password, keepSession}) {
  const res = await fetch("/sign-in", {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({username, password, keepSession})
  });

  if (res.ok) return res.json();
  return {code: res.status, error: res.statusText}; // TODO
}

export async function changePassword({username, password, newPassword, keepSession}) {
  const res = await fetch("/change-password", {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({username, password, newPassword, keepSession})
  });

  return res.json();
}

export async function changeUsername({newUsername, password}) {
  const res = await fetch("/change-username", {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({newUsername, password})
  });

  return res.json();
}

export async function signOut() {
  const res = await fetch("/sign-out", {
    method: "POST",
  });
}

export async function signOutEverywhere() {
  const res = await fetch("/deauth-all", {
    method: "POST",
  });
}