beamsocial 0.2.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
@@ -1,10 +1,11 @@
1
1
  import type { ProfileSettings } from './models/types/profiles.js';
2
2
  import { User } from './models/users.js';
3
3
  import { Session } from './models/auth.js';
4
+ import { Post } from './models/posts.js';
4
5
  export declare class Client {
5
6
  private readonly baseURL;
6
- private token;
7
- private session;
7
+ private token?;
8
+ private session?;
8
9
  constructor(baseURL: string, token?: string);
9
10
  login(username: string, password: string): Promise<Session | null>;
10
11
  refresh(token?: string): Promise<void>;
@@ -12,6 +13,11 @@ export declare class Client {
12
13
  update_settings(settings: ProfileSettings): Promise<void>;
13
14
  /*************************************/
14
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>;
19
+ fetchUserPosts(name: string): Promise<Post[]>;
15
20
  }
16
21
  export { User } from './models/users.js';
17
22
  export { Session } from './models/auth.js';
23
+ export { Post } from './models/posts.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,117 @@ 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
+ }
214
+ fetchUserPosts(name) {
215
+ return __awaiter(this, void 0, void 0, function* () {
216
+ try {
217
+ const res = yield axios.get(this.baseURL + '/users/' + name + `/posts`, {
218
+ headers: {
219
+ Authorization: `Bearer ${this.token}`,
220
+ },
221
+ });
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;
230
+ }
231
+ catch (err) {
232
+ if (axios.isAxiosError(err) && err.response) {
233
+ const res = err.response;
234
+ if (res.status == 401) {
235
+ throw new Error('Not logged in');
236
+ }
237
+ else if (res.status == 403) {
238
+ throw new Error('Private account');
239
+ }
240
+ else if (res.status == 404) {
241
+ throw new Error('User not found');
242
+ }
243
+ else {
244
+ throw new Error('Error fetching posts');
245
+ }
246
+ }
247
+ else {
248
+ throw err;
249
+ }
250
+ }
251
+ });
252
+ }
144
253
  }
145
254
  export { User } from './models/users.js';
146
255
  export { Session } from './models/auth.js';
256
+ export { Post } from './models/posts.js';
@@ -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 = {};
@@ -0,0 +1,24 @@
1
+ import type { RawPost, RawComment, RawFile } from "./types/interactions";
2
+ import { User } from "./users.js";
3
+ export declare class Post {
4
+ private __token?;
5
+ private __url?;
6
+ id: string;
7
+ author: User | null;
8
+ content: string;
9
+ attachments: RawFile[];
10
+ visibility: "everyone" | "followers" | "friends" | "me";
11
+ likes: string[];
12
+ reposts: string[];
13
+ comments: RawComment[];
14
+ constructor(id: string);
15
+ __load(data: RawPost, __token?: string, __url?: string): void;
16
+ like(): Promise<void>;
17
+ unlike(): Promise<void>;
18
+ repost(): Promise<void>;
19
+ undo_repost(): Promise<void>;
20
+ add_comment(comment: RawComment): Promise<void>;
21
+ remove_comment(comment: RawComment): Promise<void>;
22
+ report(): Promise<void>;
23
+ delete(): Promise<void>;
24
+ }
@@ -0,0 +1,58 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { User } from "./users.js";
11
+ export class Post {
12
+ constructor(id) {
13
+ this.author = null;
14
+ this.content = '';
15
+ this.attachments = [];
16
+ this.visibility = "followers";
17
+ this.likes = [];
18
+ this.reposts = [];
19
+ this.comments = [];
20
+ this.id = id;
21
+ }
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);
27
+ this.content = data.content;
28
+ this.attachments = data.attachments;
29
+ this.visibility = data.visibility;
30
+ this.likes = data.likes;
31
+ this.reposts = data.reposts;
32
+ this.comments = data.comments;
33
+ }
34
+ like() {
35
+ return __awaiter(this, void 0, void 0, function* () { });
36
+ }
37
+ unlike() {
38
+ return __awaiter(this, void 0, void 0, function* () { });
39
+ }
40
+ repost() {
41
+ return __awaiter(this, void 0, void 0, function* () { });
42
+ }
43
+ undo_repost() {
44
+ return __awaiter(this, void 0, void 0, function* () { });
45
+ }
46
+ add_comment(comment) {
47
+ return __awaiter(this, void 0, void 0, function* () { });
48
+ }
49
+ remove_comment(comment) {
50
+ return __awaiter(this, void 0, void 0, function* () { });
51
+ }
52
+ report() {
53
+ return __awaiter(this, void 0, void 0, function* () { });
54
+ }
55
+ delete() {
56
+ return __awaiter(this, void 0, void 0, function* () { });
57
+ }
58
+ }
@@ -1,19 +1,28 @@
1
- export type Post = {
1
+ import { Profile } from "./profiles";
2
+ export type RawFile = {
2
3
  id: string;
3
4
  author_id: string;
4
- content: string;
5
- attachements: string[];
6
- visibility: number;
7
- likes: string[];
8
- reposts: string[];
9
- comments: string[];
5
+ path: string;
6
+ type: string;
7
+ date: string;
8
+ hidden: boolean;
10
9
  };
11
- export type Comment = {
10
+ export type RawComment = {
12
11
  id: string;
13
12
  author_id: string;
14
13
  parent_id: string;
15
14
  post_id: string;
16
15
  content: string;
17
- visibility: number;
16
+ visibility: "everyone" | "followers" | "friends" | "me";
17
+ likes: string[];
18
+ };
19
+ export type RawPost = {
20
+ id: string;
21
+ author: Profile;
22
+ content: string;
23
+ attachments: RawFile[];
24
+ visibility: "everyone" | "followers" | "friends" | "me";
18
25
  likes: string[];
26
+ reposts: string[];
27
+ comments: RawComment[];
19
28
  };
@@ -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,9 +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;
19
- getPosts(): Promise<any>;
17
+ constructor(id: string);
18
+ __load(data: Profile, __token?: string, __url?: string): void;
20
19
  follow(): Promise<void>;
21
20
  unfollow(): Promise<void>;
22
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;
@@ -38,38 +38,6 @@ export class User {
38
38
  this.account_type = data.account_type;
39
39
  this.level = data.level;
40
40
  }
41
- getPosts() {
42
- return __awaiter(this, void 0, void 0, function* () {
43
- try {
44
- const res = yield axios.get(this.__url + `/posts`, {
45
- headers: {
46
- Authorization: `Bearer ${this.__token}`,
47
- },
48
- });
49
- return res.data;
50
- }
51
- catch (err) {
52
- if (axios.isAxiosError(err) && err.response) {
53
- const res = err.response;
54
- if (res.status == 401) {
55
- throw new Error('Not logged in');
56
- }
57
- else if (res.status == 403) {
58
- throw new Error('Private account');
59
- }
60
- else if (res.status == 404) {
61
- throw new Error('User not found');
62
- }
63
- else {
64
- throw new Error('Error following user');
65
- }
66
- }
67
- else {
68
- throw err;
69
- }
70
- }
71
- });
72
- }
73
41
  follow() {
74
42
  return __awaiter(this, void 0, void 0, function* () {
75
43
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beamsocial",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "API-wrapper pour Beam",
6
6
  "main": "index.js",