beamsocial 0.3.0 → 0.4.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.token, 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.token, 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.token, 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.token, 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,18 @@
1
1
  import type { RawPost, RawComment, RawFile } from "./types/interactions";
2
+ import { User } from "./users.js";
2
3
  export declare class Post {
3
- private readonly __token?;
4
- private readonly __url?;
4
+ private __token?;
5
+ private __url?;
5
6
  id: string;
6
- author_id: string;
7
+ author: User | null;
7
8
  content: string;
8
9
  attachments: RawFile[];
9
10
  visibility: "everyone" | "followers" | "friends" | "me";
10
11
  likes: string[];
11
12
  reposts: string[];
12
13
  comments: RawComment[];
13
- constructor(id: string, __token?: string, __url?: string);
14
- __load(data: RawPost): void;
14
+ constructor(id: string);
15
+ __load(data: RawPost, __token?: string, __url?: string): void;
15
16
  like(): Promise<void>;
16
17
  unlike(): Promise<void>;
17
18
  repost(): Promise<void>;
@@ -7,9 +7,10 @@ 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 { User } from "./users.js";
10
11
  export class Post {
11
- constructor(id, __token, __url) {
12
- this.author_id = '';
12
+ constructor(id) {
13
+ this.author = null;
13
14
  this.content = '';
14
15
  this.attachments = [];
15
16
  this.visibility = "followers";
@@ -17,13 +18,14 @@ export class Post {
17
18
  this.reposts = [];
18
19
  this.comments = [];
19
20
  this.id = id;
20
- this.__token = __token;
21
- this.__url = __url;
22
21
  }
23
- __load(data) {
24
- this.author_id = data.author_id;
22
+ __load(data, __token, __url) {
23
+ this.__token = __token;
24
+ this.__url = __url + '/posts/' + data.id;
25
+ this.author = new User(data.author.id);
26
+ this.author.__load(data.author, __token, __url + '/users/' + data.author.name);
25
27
  this.content = data.content;
26
- this.attachments = data.attachements;
28
+ this.attachments = data.attachments;
27
29
  this.visibility = data.visibility;
28
30
  this.likes = data.likes;
29
31
  this.reposts = data.reposts;
@@ -1,3 +1,4 @@
1
+ import { Profile } from "./profiles";
1
2
  export type RawFile = {
2
3
  id: string;
3
4
  author_id: string;
@@ -17,9 +18,9 @@ export type RawComment = {
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
25
  likes: string[];
25
26
  reposts: string[];
@@ -1,7 +1,7 @@
1
1
  import type { Profile } from "./types/profiles";
2
2
  export declare class User {
3
- private readonly __token?;
4
- private readonly __url?;
3
+ private __token?;
4
+ private __url?;
5
5
  id: string;
6
6
  name: string;
7
7
  display_name: string | null;
@@ -14,8 +14,8 @@ export declare class User {
14
14
  pronouns: string | null;
15
15
  account_type: string | null;
16
16
  level: number;
17
- constructor(id: string, __token?: string, __url?: string);
18
- __load(data: Profile): void;
17
+ constructor(id: string);
18
+ __load(data: Profile, __token?: string, __url?: string): void;
19
19
  follow(): Promise<void>;
20
20
  unfollow(): Promise<void>;
21
21
  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,10 @@ export class User {
22
22
  this.account_type = null;
23
23
  this.level = 0;
24
24
  this.id = id;
25
+ }
26
+ __load(data, __token, __url) {
25
27
  this.__token = __token;
26
28
  this.__url = __url;
27
- }
28
- __load(data) {
29
29
  this.name = data.name;
30
30
  this.display_name = data.display_name;
31
31
  this.creation_date = data.creation_date;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beamsocial",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "API-wrapper pour Beam",
6
6
  "main": "index.js",