@foru-ms/sdk 1.2.7 → 1.3.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/README.md CHANGED
@@ -201,7 +201,7 @@ Check the `/examples` directory for detailed examples:
201
201
  ### Auth (`client.auth`)
202
202
 
203
203
  * `login(payload: { login: string; password: string })`: Login a user. Returns `{ token: string }`.
204
- * `register(payload: RegisterPayload)`: Register a new user.
204
+ * `register(payload: RegisterPayload)`: Register a new user. Returns `{ user: User, token: string }`. Token is automatically set on the client.
205
205
  * `me()`: Get specific details of the currently authenticated user.
206
206
  * `forgotPassword(email: string)`: Initiate password reset flow.
207
207
  * `resetPassword(payload: { password: string; oldPassword?: string; email?: string; token?: string })`: Reset password using token or old password.
@@ -382,6 +382,7 @@ import {
382
382
  RegisterPayload,
383
383
  User,
384
384
  LoginResponse,
385
+ RegisterResponse,
385
386
  SecurityInfo,
386
387
 
387
388
  // Thread Types
@@ -552,6 +553,12 @@ We welcome contributions! Please see our contributing guidelines for more inform
552
553
 
553
554
  ## Changelog
554
555
 
556
+ ### v1.3.0
557
+ - Added Auth resource, authentication example, and related types
558
+
559
+ ### v1.2.8
560
+ - Added Poll resource for poll management
561
+
555
562
  ### v1.2.7
556
563
  - Fixed issue with optional parameters not being optional in typescript
557
564
 
@@ -1,5 +1,5 @@
1
1
  import { ForumClient } from '../Client';
2
- import { LoginResponse } from '../types';
2
+ import { LoginResponse, RegisterResponse } from '../types';
3
3
  export declare class AuthResource {
4
4
  private client;
5
5
  constructor(client: ForumClient);
@@ -7,7 +7,7 @@ export declare class AuthResource {
7
7
  login: string;
8
8
  password: string;
9
9
  }): Promise<LoginResponse>;
10
- register(payload: import('../types').RegisterPayload): Promise<import('../types').User>;
10
+ register(payload: import('../types').RegisterPayload): Promise<RegisterResponse>;
11
11
  me(): Promise<import('../types').User>;
12
12
  forgotPassword(email: string): Promise<{
13
13
  resetToken?: string;
@@ -16,10 +16,14 @@ class AuthResource {
16
16
  return response;
17
17
  }
18
18
  async register(payload) {
19
- return this.client.request('/auth/register', {
19
+ const response = await this.client.request('/auth/register', {
20
20
  method: 'POST',
21
21
  body: JSON.stringify(payload),
22
22
  });
23
+ if (response.token) {
24
+ this.client.setToken(response.token);
25
+ }
26
+ return response;
23
27
  }
24
28
  async me() {
25
29
  return this.client.request('/auth/me', {
@@ -1,5 +1,6 @@
1
1
  import { ForumClient } from '../Client';
2
2
  import { ThreadListResponse, ThreadFilter, InteractionType } from '../types';
3
+ import { PollResults } from '../response-types';
3
4
  export declare class ThreadsResource {
4
5
  private client;
5
6
  constructor(client: ForumClient);
@@ -57,6 +58,7 @@ export declare class ThreadsResource {
57
58
  limit?: number;
58
59
  }): Promise<any>;
59
60
  getPoll(threadId: string, userId?: string): Promise<any>;
61
+ getPollResults(threadId: string, userId?: string): Promise<PollResults>;
60
62
  vote(id: string, optionId: string, userId?: string): Promise<any>;
61
63
  voteUpdate(id: string, optionId: string, userId?: string): Promise<any>;
62
64
  unvote(id: string, userId?: string): Promise<any>;
@@ -175,6 +175,14 @@ class ThreadsResource {
175
175
  const query = searchParams.toString();
176
176
  return this.client.request(`/thread/${threadId}/poll${query ? `?${query}` : ''}`, { method: 'GET' });
177
177
  }
178
+ async getPollResults(threadId, userId) {
179
+ const searchParams = new URLSearchParams();
180
+ if (userId) {
181
+ searchParams.append('userId', userId);
182
+ }
183
+ const query = searchParams.toString();
184
+ return this.client.request(`/thread/${threadId}/poll/results${query ? `?${query}` : ''}`, { method: 'GET' });
185
+ }
178
186
  async vote(id, optionId, userId) {
179
187
  return this.client.request(`/thread/${id}/poll/votes`, {
180
188
  method: 'POST',
@@ -78,6 +78,28 @@ export interface PollOption {
78
78
  /** Extended data */
79
79
  extendedData?: Record<string, any>;
80
80
  }
81
+ /**
82
+ * Simplified poll option for results
83
+ */
84
+ export interface PollOptionResult {
85
+ /** The ID of the poll option */
86
+ id: string;
87
+ /** The title of the poll option */
88
+ title: string;
89
+ /** The color of the poll option */
90
+ color?: string;
91
+ /** The number of votes this option received */
92
+ votes: number;
93
+ }
94
+ /**
95
+ * Poll results data
96
+ */
97
+ export interface PollResults {
98
+ /** List of poll options with results */
99
+ options: PollOptionResult[];
100
+ /** The option ID that the user voted for, or null if they haven't voted */
101
+ userVote?: string | null;
102
+ }
81
103
  /**
82
104
  * Response for batch operations
83
105
  */
package/dist/types.d.ts CHANGED
@@ -113,6 +113,10 @@ export interface PostListResponse {
113
113
  export interface LoginResponse {
114
114
  token: string;
115
115
  }
116
+ export interface RegisterResponse {
117
+ user: User;
118
+ token: string;
119
+ }
116
120
  export interface SecurityInfo {
117
121
  userId: string;
118
122
  username: string;
@@ -14,13 +14,15 @@ async function main() {
14
14
  try {
15
15
  // Example 1: User Registration
16
16
  console.log('=== User Registration ===');
17
- const newUser = await client.auth.register({
17
+ const { user, token } = await client.auth.register({
18
18
  username: 'john_doe',
19
19
  email: 'john@example.com',
20
20
  password: 'securePassword123',
21
21
  displayName: 'John Doe',
22
22
  });
23
- console.log('User registered:', newUser);
23
+ console.log('User registered:', user);
24
+ console.log('Auth token:', token);
25
+ // Note: Token is automatically set on the client
24
26
 
25
27
  // Example 2: User Login
26
28
  console.log('\n=== User Login ===');
@@ -29,9 +31,7 @@ async function main() {
29
31
  password: 'securePassword123',
30
32
  });
31
33
  console.log('Login successful, token:', loginResponse.token);
32
-
33
- // Store the token for authenticated requests
34
- client.setToken(loginResponse.token);
34
+ // Note: Token is automatically set on the client
35
35
 
36
36
  // Example 3: Get Current User
37
37
  console.log('\n=== Get Current User ===');
@@ -1,3 +1,4 @@
1
+ // @ts-ignore
1
2
  import { ForumClient } from '@foru-ms/sdk';
2
3
 
3
4
  /**
@@ -70,8 +71,12 @@ async function main() {
70
71
  await client.threads.vote(pollThread.id, 'option-id-1', userId);
71
72
  console.log('Vote cast');
72
73
 
74
+ // Get poll
75
+ const poll = await client.threads.getPoll(pollThread.id, userId);
76
+ console.log('Poll:', poll);
77
+
73
78
  // Get poll results
74
- const pollResults = await client.threads.getPoll(pollThread.id, userId);
79
+ const pollResults = await client.threads.getPollResults(pollThread.id, userId);
75
80
  console.log('Poll results:', pollResults);
76
81
 
77
82
  // Example 7: Add Posts to Thread
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foru-ms/sdk",
3
- "version": "1.2.7",
3
+ "version": "1.3.0",
4
4
  "description": "JavaScript SDK for Foru.ms",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,5 +1,5 @@
1
1
  import { ForumClient } from '../Client';
2
- import { LoginResponse } from '../types';
2
+ import { LoginResponse, RegisterResponse } from '../types';
3
3
 
4
4
  export class AuthResource {
5
5
  private client: ForumClient;
@@ -21,11 +21,17 @@ export class AuthResource {
21
21
  return response;
22
22
  }
23
23
 
24
- async register(payload: import('../types').RegisterPayload): Promise<import('../types').User> {
25
- return this.client.request<import('../types').User>('/auth/register', {
24
+ async register(payload: import('../types').RegisterPayload): Promise<RegisterResponse> {
25
+ const response = await this.client.request<RegisterResponse>('/auth/register', {
26
26
  method: 'POST',
27
27
  body: JSON.stringify(payload),
28
28
  });
29
+
30
+ if (response.token) {
31
+ this.client.setToken(response.token);
32
+ }
33
+
34
+ return response;
29
35
  }
30
36
 
31
37
  async me(): Promise<import('../types').User> {
@@ -1,5 +1,6 @@
1
1
  import { ForumClient } from '../Client';
2
2
  import { ThreadListResponse, ThreadFilter, InteractionType } from '../types';
3
+ import { PollResults } from '../response-types';
3
4
 
4
5
  export class ThreadsResource {
5
6
  private client: ForumClient;
@@ -232,6 +233,19 @@ export class ThreadsResource {
232
233
  );
233
234
  }
234
235
 
236
+ async getPollResults(threadId: string, userId?: string): Promise<PollResults> {
237
+ const searchParams = new URLSearchParams();
238
+ if (userId) {
239
+ searchParams.append('userId', userId);
240
+ }
241
+
242
+ const query = searchParams.toString();
243
+ return this.client.request<PollResults>(
244
+ `/thread/${threadId}/poll/results${query ? `?${query}` : ''}`,
245
+ { method: 'GET' }
246
+ );
247
+ }
248
+
235
249
  async vote(id: string, optionId: string, userId?: string): Promise<any> {
236
250
  return this.client.request(`/thread/${id}/poll/votes`, {
237
251
  method: 'POST',
@@ -85,6 +85,30 @@ export interface PollOption {
85
85
  extendedData?: Record<string, any>;
86
86
  }
87
87
 
88
+ /**
89
+ * Simplified poll option for results
90
+ */
91
+ export interface PollOptionResult {
92
+ /** The ID of the poll option */
93
+ id: string;
94
+ /** The title of the poll option */
95
+ title: string;
96
+ /** The color of the poll option */
97
+ color?: string;
98
+ /** The number of votes this option received */
99
+ votes: number;
100
+ }
101
+
102
+ /**
103
+ * Poll results data
104
+ */
105
+ export interface PollResults {
106
+ /** List of poll options with results */
107
+ options: PollOptionResult[];
108
+ /** The option ID that the user voted for, or null if they haven't voted */
109
+ userVote?: string | null;
110
+ }
111
+
88
112
  /**
89
113
  * Response for batch operations
90
114
  */
package/src/types.ts CHANGED
@@ -128,6 +128,11 @@ export interface LoginResponse {
128
128
  token: string;
129
129
  }
130
130
 
131
+ export interface RegisterResponse {
132
+ user: User;
133
+ token: string;
134
+ }
135
+
131
136
  export interface SecurityInfo {
132
137
  userId: string;
133
138
  username: string;