beamsocial 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -4,8 +4,8 @@ import { Session } from './models/auth.js';
4
4
  import { Post } from './models/posts.js';
5
5
  export declare class Client {
6
6
  private readonly baseURL;
7
- private token;
8
- private session;
7
+ private token?;
8
+ private session?;
9
9
  constructor(baseURL: string, token?: string);
10
10
  login(username: string, password: string): Promise<Session | null>;
11
11
  refresh(token?: string): Promise<void>;
@@ -13,6 +13,9 @@ export declare class Client {
13
13
  update_settings(settings: ProfileSettings): Promise<void>;
14
14
  /*************************************/
15
15
  getUser(name: string): Promise<User | null>;
16
+ /*************************************/
17
+ writePost(content: string, visibility?: "me" | "friends" | "followers" | "everyone"): Promise<Post>;
18
+ getPost(id: string): Promise<Post>;
16
19
  fetchUserPosts(name: string): Promise<Post[]>;
17
20
  }
18
21
  export { User } from './models/users.js';
package/lib/index.js CHANGED
@@ -10,10 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import axios from 'axios';
11
11
  import { User } from './models/users.js';
12
12
  import { Session } from './models/auth.js';
13
+ import { Post } from './models/posts.js';
13
14
  export class Client {
14
15
  constructor(baseURL, token) {
15
- this.token = null;
16
- this.session = null;
17
16
  this.baseURL = baseURL;
18
17
  if (token)
19
18
  this.token = token;
@@ -23,7 +22,7 @@ export class Client {
23
22
  try {
24
23
  const res = yield axios.post(this.baseURL + '/auth/login', { username, password });
25
24
  yield this.refresh(res.data.token);
26
- return this.session;
25
+ return this.session || null;
27
26
  }
28
27
  catch (err) {
29
28
  if (axios.isAxiosError(err) && err.response) {
@@ -46,7 +45,7 @@ export class Client {
46
45
  if (token)
47
46
  this.token = token;
48
47
  let session = yield this.me();
49
- this.session = session;
48
+ this.session = session || undefined;
50
49
  });
51
50
  }
52
51
  me() {
@@ -58,7 +57,7 @@ export class Client {
58
57
  },
59
58
  });
60
59
  let session;
61
- session = new Session(res.data.id, this.token);
60
+ session = new Session(res.data.id, this.token || null);
62
61
  session.__load(res.data);
63
62
  return session;
64
63
  }
@@ -118,15 +117,15 @@ export class Client {
118
117
  },
119
118
  });
120
119
  let user;
121
- user = new User(res.data.id, this.token, this.baseURL + '/users/' + name);
122
- user.__load(res.data);
120
+ user = new User(res.data.id);
121
+ user.__load(res.data, this.session, this.baseURL);
123
122
  return user;
124
123
  }
125
124
  catch (err) {
126
125
  if (axios.isAxiosError(err) && err.response) {
127
126
  const res = err.response;
128
127
  if (res.status === 403) {
129
- throw new Error('User is private');
128
+ throw new Error('Private account');
130
129
  }
131
130
  else if (res.status === 404) {
132
131
  return null;
@@ -141,6 +140,77 @@ export class Client {
141
140
  }
142
141
  });
143
142
  }
143
+ /*************************************/
144
+ writePost(content_1) {
145
+ return __awaiter(this, arguments, void 0, function* (content, visibility = "followers") {
146
+ try {
147
+ const res = yield axios.post(this.baseURL + '/posts/new', {
148
+ content: content,
149
+ visibility: visibility
150
+ }, {
151
+ headers: {
152
+ Authorization: `Bearer ${this.token}`,
153
+ },
154
+ });
155
+ const data = res.data;
156
+ const post = new Post(data.id);
157
+ post.__load(data, this.session, this.baseURL);
158
+ return post;
159
+ }
160
+ catch (err) {
161
+ if (axios.isAxiosError(err) && err.response) {
162
+ const res = err.response;
163
+ if (res.status == 401) {
164
+ throw new Error('Not logged in');
165
+ }
166
+ else if (res.status == 413) {
167
+ throw new Error('Post too long');
168
+ }
169
+ else {
170
+ throw new Error('Error writing post');
171
+ }
172
+ }
173
+ else {
174
+ throw err;
175
+ }
176
+ }
177
+ });
178
+ }
179
+ getPost(id) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ try {
182
+ const res = yield axios.get(this.baseURL + '/posts/' + id, {
183
+ headers: {
184
+ Authorization: `Bearer ${this.token}`,
185
+ },
186
+ });
187
+ const data = res.data;
188
+ const post = new Post(data.id);
189
+ post.__load(data, this.session, this.baseURL);
190
+ return post;
191
+ }
192
+ catch (err) {
193
+ if (axios.isAxiosError(err) && err.response) {
194
+ const res = err.response;
195
+ if (res.status == 401) {
196
+ throw new Error('Not logged in');
197
+ }
198
+ else if (res.status == 403) {
199
+ throw new Error('Private account');
200
+ }
201
+ else if (res.status == 404) {
202
+ throw new Error('Post not found');
203
+ }
204
+ else {
205
+ throw new Error('Error writing post');
206
+ }
207
+ }
208
+ else {
209
+ throw err;
210
+ }
211
+ }
212
+ });
213
+ }
144
214
  fetchUserPosts(name) {
145
215
  return __awaiter(this, void 0, void 0, function* () {
146
216
  try {
@@ -149,7 +219,14 @@ export class Client {
149
219
  Authorization: `Bearer ${this.token}`,
150
220
  },
151
221
  });
152
- return res.data;
222
+ const data = res.data;
223
+ const posts = [];
224
+ for (const p of data) {
225
+ let new_p = new Post(p.id);
226
+ new_p.__load(p, this.session, this.baseURL);
227
+ posts.push(new_p);
228
+ }
229
+ return posts;
153
230
  }
154
231
  catch (err) {
155
232
  if (axios.isAxiosError(err) && err.response) {
@@ -1,11 +1,11 @@
1
1
  import type { Profile, ProfileSettings } from "./types/profiles";
2
2
  export declare class Session {
3
3
  readonly id: string;
4
- readonly token: string;
4
+ readonly token: string | null;
5
5
  profile: Profile;
6
6
  settings: ProfileSettings;
7
7
  inbox: {};
8
- constructor(id: string, token: string);
8
+ constructor(id: string, token: string | null);
9
9
  __load(data: {
10
10
  profile?: Profile;
11
11
  settings?: ProfileSettings;
@@ -1,5 +1,6 @@
1
1
  export class Session {
2
2
  constructor(id, token) {
3
+ this.token = null;
3
4
  this.profile = {};
4
5
  this.settings = {};
5
6
  this.inbox = {};
@@ -1,17 +1,20 @@
1
1
  import type { RawPost, RawComment, RawFile } from "./types/interactions";
2
+ import { User } from "./users.js";
3
+ import { Session } from "./auth.js";
2
4
  export declare class Post {
3
- private readonly __token?;
4
- private readonly __url?;
5
+ private __session?;
6
+ private __token?;
7
+ private __url?;
5
8
  id: string;
6
- author_id: string;
9
+ author: User | null;
7
10
  content: string;
8
11
  attachments: RawFile[];
9
12
  visibility: "everyone" | "followers" | "friends" | "me";
10
- likes: string[];
11
- reposts: string[];
12
- comments: RawComment[];
13
- constructor(id: string, __token?: string, __url?: string);
14
- __load(data: RawPost): void;
13
+ likes: number;
14
+ reposts: number;
15
+ comments: number;
16
+ constructor(id: string);
17
+ __load(data: RawPost, __session?: Session, __url?: string): void;
15
18
  like(): Promise<void>;
16
19
  unlike(): Promise<void>;
17
20
  repost(): Promise<void>;
@@ -7,33 +7,100 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import axios from "axios";
11
+ import { User } from "./users.js";
10
12
  export class Post {
11
- constructor(id, __token, __url) {
12
- this.author_id = '';
13
+ constructor(id) {
14
+ this.author = null;
13
15
  this.content = '';
14
16
  this.attachments = [];
15
17
  this.visibility = "followers";
16
- this.likes = [];
17
- this.reposts = [];
18
- this.comments = [];
18
+ this.likes = 0;
19
+ this.reposts = 0;
20
+ this.comments = 0;
19
21
  this.id = id;
20
- this.__token = __token;
21
- this.__url = __url;
22
22
  }
23
- __load(data) {
24
- this.author_id = data.author_id;
23
+ __load(data, __session, __url) {
24
+ this.__token = (__session === null || __session === void 0 ? void 0 : __session.token) || this.__token;
25
+ this.__session = __session;
26
+ this.__url = __url + '/posts/' + data.id;
27
+ this.author = new User(data.author.id);
28
+ this.author.__load(data.author, __session, __url);
25
29
  this.content = data.content;
26
- this.attachments = data.attachements;
30
+ this.attachments = data.attachments;
27
31
  this.visibility = data.visibility;
28
32
  this.likes = data.likes;
29
33
  this.reposts = data.reposts;
30
34
  this.comments = data.comments;
31
35
  }
32
36
  like() {
33
- return __awaiter(this, void 0, void 0, function* () { });
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ var _a;
39
+ try {
40
+ const res = yield axios.post(this.__url + `/like`, {}, {
41
+ headers: {
42
+ Authorization: `Bearer ${this.__token}`,
43
+ },
44
+ });
45
+ this.likes += 1;
46
+ (_a = this.__session) === null || _a === void 0 ? void 0 : _a.profile.likes.push(this.__session.id);
47
+ return;
48
+ }
49
+ catch (err) {
50
+ if (axios.isAxiosError(err) && err.response) {
51
+ const res = err.response;
52
+ if (res.status == 401) {
53
+ throw new Error('Not logged in');
54
+ }
55
+ else if (res.status == 403) {
56
+ throw new Error('Private account');
57
+ }
58
+ else if (res.status == 404) {
59
+ throw new Error('User not found');
60
+ }
61
+ else {
62
+ throw new Error('Error following user');
63
+ }
64
+ }
65
+ else {
66
+ throw err;
67
+ }
68
+ }
69
+ });
34
70
  }
35
71
  unlike() {
36
- return __awaiter(this, void 0, void 0, function* () { });
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ try {
74
+ const res = yield axios.post(this.__url + `/like`, {}, {
75
+ headers: {
76
+ Authorization: `Bearer ${this.__token}`,
77
+ },
78
+ });
79
+ this.likes -= 1;
80
+ this.__session.profile.likes = this.__session.profile.likes.filter((id) => id !== this.__session.id);
81
+ return;
82
+ }
83
+ catch (err) {
84
+ if (axios.isAxiosError(err) && err.response) {
85
+ const res = err.response;
86
+ if (res.status == 401) {
87
+ throw new Error('Not logged in');
88
+ }
89
+ else if (res.status == 403) {
90
+ throw new Error('Private account');
91
+ }
92
+ else if (res.status == 404) {
93
+ throw new Error('User not found');
94
+ }
95
+ else {
96
+ throw new Error('Error following user');
97
+ }
98
+ }
99
+ else {
100
+ throw err;
101
+ }
102
+ }
103
+ });
37
104
  }
38
105
  repost() {
39
106
  return __awaiter(this, void 0, void 0, function* () { });
@@ -1,3 +1,4 @@
1
+ import { Profile } from "./profiles";
1
2
  export type RawFile = {
2
3
  id: string;
3
4
  author_id: string;
@@ -13,15 +14,15 @@ export type RawComment = {
13
14
  post_id: string;
14
15
  content: string;
15
16
  visibility: "everyone" | "followers" | "friends" | "me";
16
- likes: string[];
17
+ likes: number;
17
18
  };
18
19
  export type RawPost = {
19
20
  id: string;
20
- author_id: string;
21
+ author: Profile;
21
22
  content: string;
22
- attachements: RawFile[];
23
+ attachments: RawFile[];
23
24
  visibility: "everyone" | "followers" | "friends" | "me";
24
- likes: string[];
25
- reposts: string[];
26
- comments: RawComment[];
25
+ likes: number;
26
+ reposts: number;
27
+ comments: number;
27
28
  };
@@ -6,6 +6,7 @@ export type Profile = {
6
6
  update_date: string;
7
7
  followers: string[];
8
8
  following: string[];
9
+ likes: string[];
9
10
  status: string | null;
10
11
  birthday: string | null;
11
12
  pronouns: string | null;
@@ -1,7 +1,9 @@
1
- import type { Profile } from "./types/profiles";
1
+ import type { Profile } from "./types/profiles.js";
2
+ import { Session } from "./auth.js";
2
3
  export declare class User {
3
- private readonly __token?;
4
- private readonly __url?;
4
+ private __token?;
5
+ private __url?;
6
+ private __session?;
5
7
  id: string;
6
8
  name: string;
7
9
  display_name: string | null;
@@ -14,8 +16,8 @@ export declare class User {
14
16
  pronouns: string | null;
15
17
  account_type: string | null;
16
18
  level: number;
17
- constructor(id: string, __token?: string, __url?: string);
18
- __load(data: Profile): void;
19
+ constructor(id: string);
20
+ __load(data: Profile, __session?: Session, __url?: string): void;
19
21
  follow(): Promise<void>;
20
22
  unfollow(): Promise<void>;
21
23
  block(): Promise<void>;
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import axios from "axios";
11
11
  export class User {
12
- constructor(id, __token, __url) {
12
+ constructor(id) {
13
13
  this.name = '';
14
14
  this.display_name = null;
15
15
  this.creation_date = '1970-01-01T00:00:00Z';
@@ -22,10 +22,11 @@ export class User {
22
22
  this.account_type = null;
23
23
  this.level = 0;
24
24
  this.id = id;
25
- this.__token = __token;
26
- this.__url = __url;
27
25
  }
28
- __load(data) {
26
+ __load(data, __session, __url) {
27
+ this.__token = (__session === null || __session === void 0 ? void 0 : __session.token) || this.__token;
28
+ this.__session = __session;
29
+ this.__url = __url + '/users/' + data.name;
29
30
  this.name = data.name;
30
31
  this.display_name = data.display_name;
31
32
  this.creation_date = data.creation_date;
@@ -46,6 +47,7 @@ export class User {
46
47
  Authorization: `Bearer ${this.__token}`,
47
48
  },
48
49
  });
50
+ this.followers.push(this.__session.id);
49
51
  return;
50
52
  }
51
53
  catch (err) {
@@ -78,6 +80,7 @@ export class User {
78
80
  Authorization: `Bearer ${this.__token}`,
79
81
  },
80
82
  });
83
+ this.followers = this.followers.filter(item => item !== this.__session.id);
81
84
  return;
82
85
  }
83
86
  catch (err) {
@@ -104,12 +107,14 @@ export class User {
104
107
  }
105
108
  block() {
106
109
  return __awaiter(this, void 0, void 0, function* () {
110
+ var _a;
107
111
  try {
108
112
  const res = yield axios.post(this.__url + `/block`, {}, {
109
113
  headers: {
110
114
  Authorization: `Bearer ${this.__token}`,
111
115
  },
112
116
  });
117
+ (_a = this.__session) === null || _a === void 0 ? void 0 : _a.settings.blocklist.push(this.id);
113
118
  return;
114
119
  }
115
120
  catch (err) {
@@ -139,6 +144,9 @@ export class User {
139
144
  Authorization: `Bearer ${this.__token}`,
140
145
  },
141
146
  });
147
+ if (this.__session && this.__session.settings) {
148
+ this.__session.settings.blocklist = this.__session.settings.blocklist.filter(item => item !== this.id);
149
+ }
142
150
  return;
143
151
  }
144
152
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beamsocial",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "API-wrapper pour Beam",
6
6
  "main": "index.js",