beamsocial 0.1.2 → 0.1.3

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
@@ -11,5 +11,5 @@ export declare class Client {
11
11
  me(): Promise<Session | null>;
12
12
  update_settings(settings: ProfileSettings): Promise<void>;
13
13
  /*************************************/
14
- getUser(name: string): Promise<User>;
14
+ getUser(name: string): Promise<User | null>;
15
15
  }
package/lib/index.js CHANGED
@@ -20,18 +20,25 @@ export class Client {
20
20
  }
21
21
  login(username, password) {
22
22
  return __awaiter(this, void 0, void 0, function* () {
23
- const res = yield axios.post(this.baseURL + '/auth/login', { username, password });
24
- if (res.status === 200) {
25
- this.token = res.data.token;
23
+ try {
24
+ const res = yield axios.post(this.baseURL + '/auth/login', { username, password });
25
+ yield this.refresh(res.data.token);
26
+ return this.session;
26
27
  }
27
- else if (res.status === 401) {
28
- throw new Error('Invalid credentials');
28
+ catch (err) {
29
+ if (axios.isAxiosError(err) && err.response) {
30
+ const res = err.response;
31
+ if (res.status === 401) {
32
+ return null;
33
+ }
34
+ else {
35
+ throw new Error('Error logging in');
36
+ }
37
+ }
38
+ else {
39
+ throw err;
40
+ }
29
41
  }
30
- else {
31
- throw new Error('Error logging in');
32
- }
33
- yield this.refresh();
34
- return this.session;
35
42
  });
36
43
  }
37
44
  refresh(token) {
@@ -44,69 +51,93 @@ export class Client {
44
51
  }
45
52
  me() {
46
53
  return __awaiter(this, void 0, void 0, function* () {
47
- const res = yield axios.get(this.baseURL + '/me', {
48
- headers: {
49
- Authorization: `Bearer ${this.token}`,
50
- },
51
- });
52
- let session;
53
- if (res.status === 200) {
54
+ try {
55
+ const res = yield axios.get(this.baseURL + '/me', {
56
+ headers: {
57
+ Authorization: `Bearer ${this.token}`,
58
+ },
59
+ });
60
+ let session;
54
61
  session = new Session(res.data.id, this.token);
55
62
  session.__load(res.data);
56
63
  return session;
57
64
  }
58
- else if (res.status === 401) {
59
- return null;
60
- }
61
- else {
62
- throw new Error('Error fetching session');
65
+ catch (err) {
66
+ if (axios.isAxiosError(err) && err.response) {
67
+ const res = err.response;
68
+ if (res.status === 401) {
69
+ return null;
70
+ }
71
+ else {
72
+ throw new Error('Error fetching session');
73
+ }
74
+ }
75
+ else {
76
+ throw err;
77
+ }
63
78
  }
64
79
  });
65
80
  }
66
81
  update_settings(settings) {
67
82
  return __awaiter(this, void 0, void 0, function* () {
68
- const res = yield axios.post(this.baseURL + '/me/update_settings', {
69
- privacy: settings.preferences.privacy,
70
- appearence: settings.preferences.appearance
71
- }, {
72
- headers: {
73
- Authorization: `Bearer ${this.token}`,
74
- },
75
- });
76
- if (res.status == 200) {
83
+ try {
84
+ yield axios.post(this.baseURL + '/me/update_settings', {
85
+ privacy: settings.preferences.privacy,
86
+ appearence: settings.preferences.appearance
87
+ }, {
88
+ headers: {
89
+ Authorization: `Bearer ${this.token}`,
90
+ },
91
+ });
77
92
  yield this.refresh();
78
93
  return;
79
94
  }
80
- else if (res.status == 401) {
81
- throw new Error('Not logged in');
82
- }
83
- else {
84
- throw new Error('Error updating settings');
95
+ catch (err) {
96
+ if (axios.isAxiosError(err) && err.response) {
97
+ const res = err.response;
98
+ if (res.status == 401) {
99
+ throw new Error('Not logged in');
100
+ }
101
+ else {
102
+ throw new Error('Error updating settings');
103
+ }
104
+ }
105
+ else {
106
+ throw err;
107
+ }
85
108
  }
86
109
  });
87
110
  }
88
111
  /*************************************/
89
112
  getUser(name) {
90
113
  return __awaiter(this, void 0, void 0, function* () {
91
- const res = yield axios.get(this.baseURL + '/users/' + name, {
92
- headers: {
93
- Authorization: `Bearer ${this.token}`,
94
- },
95
- });
96
- let user;
97
- if (res.status === 200) {
114
+ try {
115
+ const res = yield axios.get(this.baseURL + '/users/' + name, {
116
+ headers: {
117
+ Authorization: `Bearer ${this.token}`,
118
+ },
119
+ });
120
+ let user;
98
121
  user = new User(res.data.id, this.token, this.baseURL + '/users/' + name);
99
122
  user.__load(res.data);
100
123
  return user;
101
124
  }
102
- else if (res.status === 403) {
103
- throw new Error('User is private');
104
- }
105
- else if (res.status === 404) {
106
- throw new Error('User not found');
107
- }
108
- else {
109
- throw new Error('Error fetching user');
125
+ catch (err) {
126
+ if (axios.isAxiosError(err) && err.response) {
127
+ const res = err.response;
128
+ if (res.status === 403) {
129
+ throw new Error('User is private');
130
+ }
131
+ else if (res.status === 404) {
132
+ return null;
133
+ }
134
+ else {
135
+ throw new Error('Error fetching user');
136
+ }
137
+ }
138
+ else {
139
+ throw err;
140
+ }
110
141
  }
111
142
  });
112
143
  }
@@ -40,88 +40,123 @@ export class User {
40
40
  }
41
41
  follow() {
42
42
  return __awaiter(this, void 0, void 0, function* () {
43
- const res = yield axios.post(this.__url + `/follow`, {}, {
44
- headers: {
45
- Authorization: `Bearer ${this.__token}`,
46
- },
47
- });
48
- if (res.status == 200 || res.status == 202 || res.status == 204) {
43
+ try {
44
+ const res = yield axios.post(this.__url + `/follow`, {}, {
45
+ headers: {
46
+ Authorization: `Bearer ${this.__token}`,
47
+ },
48
+ });
49
49
  return;
50
50
  }
51
- else if (res.status == 401) {
52
- throw new Error('Not logged in');
53
- }
54
- else if (res.status == 403) {
55
- throw new Error('Private account');
56
- }
57
- else if (res.status == 404) {
58
- throw new Error('User not found');
59
- }
60
- else {
61
- throw new Error('Error following user');
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
+ }
62
70
  }
63
71
  });
64
72
  }
65
73
  unfollow() {
66
74
  return __awaiter(this, void 0, void 0, function* () {
67
- const res = yield axios.post(this.__url + `/unfollow`, {}, {
68
- headers: {
69
- Authorization: `Bearer ${this.__token}`,
70
- },
71
- });
72
- if (res.status == 200 || res.status == 204) {
75
+ try {
76
+ const res = yield axios.post(this.__url + `/unfollow`, {}, {
77
+ headers: {
78
+ Authorization: `Bearer ${this.__token}`,
79
+ },
80
+ });
73
81
  return;
74
82
  }
75
- else if (res.status == 401) {
76
- throw new Error('Not logged in');
77
- }
78
- else if (res.status == 404) {
79
- throw new Error('User not found');
80
- }
81
- else {
82
- throw new Error('Error unfollowing user');
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 unfollowing user');
97
+ }
98
+ }
99
+ else {
100
+ throw err;
101
+ }
83
102
  }
84
103
  });
85
104
  }
86
105
  block() {
87
106
  return __awaiter(this, void 0, void 0, function* () {
88
- const res = yield axios.post(this.__url + `/block`, {}, {
89
- headers: {
90
- Authorization: `Bearer ${this.__token}`,
91
- },
92
- });
93
- if (res.status == 200 || res.status == 204) {
107
+ try {
108
+ const res = yield axios.post(this.__url + `/block`, {}, {
109
+ headers: {
110
+ Authorization: `Bearer ${this.__token}`,
111
+ },
112
+ });
94
113
  return;
95
114
  }
96
- else if (res.status == 401) {
97
- throw new Error('Not logged in');
98
- }
99
- else if (res.status == 404) {
100
- throw new Error('User not found');
101
- }
102
- else {
103
- throw new Error('Error blocking user');
115
+ catch (err) {
116
+ if (axios.isAxiosError(err) && err.response) {
117
+ const res = err.response;
118
+ if (res.status == 401) {
119
+ throw new Error('Not logged in');
120
+ }
121
+ else if (res.status == 404) {
122
+ throw new Error('User not found');
123
+ }
124
+ else {
125
+ throw new Error('Error block user');
126
+ }
127
+ }
128
+ else {
129
+ throw err;
130
+ }
104
131
  }
105
132
  });
106
133
  }
107
134
  unblock() {
108
135
  return __awaiter(this, void 0, void 0, function* () {
109
- const res = yield axios.post(this.__url + `/unblock`, {}, {
110
- headers: {
111
- Authorization: `Bearer ${this.__token}`,
112
- },
113
- });
114
- if (res.status == 200 || res.status == 204) {
136
+ try {
137
+ const res = yield axios.post(this.__url + `/unblock`, {}, {
138
+ headers: {
139
+ Authorization: `Bearer ${this.__token}`,
140
+ },
141
+ });
115
142
  return;
116
143
  }
117
- else if (res.status == 401) {
118
- throw new Error('Not logged in');
119
- }
120
- else if (res.status == 404) {
121
- throw new Error('User not found');
122
- }
123
- else {
124
- throw new Error('Error blocking user');
144
+ catch (err) {
145
+ if (axios.isAxiosError(err) && err.response) {
146
+ const res = err.response;
147
+ if (res.status == 401) {
148
+ throw new Error('Not logged in');
149
+ }
150
+ else if (res.status == 404) {
151
+ throw new Error('User not found');
152
+ }
153
+ else {
154
+ throw new Error('Error unblocking user');
155
+ }
156
+ }
157
+ else {
158
+ throw err;
159
+ }
125
160
  }
126
161
  });
127
162
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beamsocial",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "description": "API-wrapper pour Beam",
6
6
  "main": "index.js",