chefu-academy-sdk 1.0.1 โ†’ 1.0.2

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
@@ -1,55 +1,129 @@
1
- # CheFu Academy SDK
2
-
3
- A TypeScript SDK for interacting with the CheFu Academy platform. This SDK provides authentication, client management, and course-related functionalities for developers integrating with CheFu Academy services.
4
-
5
- ## Features
6
- - Authentication utilities
7
- - Client management
8
- - Course management
9
- - TypeScript support
10
-
11
- ## Installation
12
-
13
- ```
14
- npm install chefu-academy-sdk
15
- ```
16
-
17
- ## Usage
18
-
19
- Import the SDK modules as needed:
20
-
21
- ```typescript
22
- import { authenticate } from './src/auth';
23
- import { Client } from './src/client';
24
- import { getCourses } from './src/courses';
25
- ```
26
-
27
- ### Example
28
-
29
- ```typescript
30
- import { authenticate } from './src/auth';
31
- import { Client } from './src/client';
32
- import { getCourses } from './src/courses';
33
-
34
- const client = new Client({ apiKey: 'YOUR_API_KEY' });
35
- authenticate(client)
36
- .then(() => getCourses(client))
37
- .then(courses => console.log(courses));
38
- ```
39
-
40
- ## Development
41
-
42
- - Build: `npm run build`
43
- - Lint: `npm run lint`
44
- - Test: `npm test`
45
-
46
- ## Project Structure
47
-
48
- - `src/auth.ts` - Authentication logic
49
- - `src/client.ts` - API client
50
- - `src/courses.ts` - Course management
51
- - `src/index.ts` - SDK entry point
52
-
53
- ## License
54
-
55
- MIT
1
+ # CheFu Academy SDK
2
+
3
+ A fully typed **TypeScript SDK** for interacting with the **CheFu Academy** platform.
4
+ This SDK simplifies authentication, API requests, and access to CheFu Academy resources such as courses.
5
+
6
+ Designed for **Node.js** and modern JavaScript/TypeScript projects.
7
+
8
+ ---
9
+
10
+ ## โœจ Features
11
+
12
+ - ๐Ÿ” API keyโ€“based authentication
13
+ - ๐Ÿ“ฆ Centralized API client
14
+ - ๐Ÿ“š Course listing & retrieval
15
+ - ๐Ÿง  Clean, minimal SDK design
16
+ - ๐Ÿงพ Full TypeScript typings
17
+ - ๐Ÿš€ Ready for production & npm
18
+
19
+ ---
20
+
21
+ ## ๐Ÿ“ฆ Installation
22
+
23
+ ```bash
24
+ npm install chefu-academy-sdk
25
+ ```
26
+
27
+ or using yarn:
28
+
29
+ ```bash
30
+ yarn add chefu-academy-sdk
31
+ ```
32
+
33
+ ---
34
+
35
+ ## ๐Ÿš€ Quick Start
36
+
37
+ ### Import the SDK
38
+
39
+ ```ts
40
+ import { CheFuAcademy } from "chefu-academy-sdk";
41
+ ```
42
+
43
+ ### Initialize the client
44
+
45
+ ```ts
46
+ const sdk = new CheFuAcademy({
47
+ apiKey: "YOUR_API_KEY",
48
+ });
49
+ ```
50
+
51
+ ### Fetch courses
52
+
53
+ ```ts
54
+ async function main() {
55
+ const courses = await sdk.courses.getAll();
56
+ console.log(courses);
57
+ }
58
+
59
+ main();
60
+ ```
61
+
62
+ ---
63
+
64
+ ## ๐Ÿ“š Available APIs
65
+
66
+ ### Courses
67
+
68
+ ```ts
69
+ sdk.courses.getAll(); // List all courses
70
+ sdk.courses.getById(courseId); // Get a single course
71
+ ```
72
+
73
+ ### Authentication
74
+
75
+ Authentication is handled automatically using your API key.
76
+
77
+ ```ts
78
+ new CheFuAcademy({
79
+ apiKey: process.env.CHEFU_API_KEY,
80
+ });
81
+ ```
82
+
83
+ ---
84
+
85
+ ## ๐Ÿ›  Development
86
+
87
+ ### Scripts
88
+
89
+ ```bash
90
+ npm run build # Build SDK
91
+ npm run lint # Lint code
92
+ npm test # Run tests
93
+ ```
94
+
95
+ ### Project Structure
96
+
97
+ ```
98
+ src/
99
+ โ”œโ”€ auth.ts # Authentication logic
100
+ โ”œโ”€ client.ts # HTTP client
101
+ โ”œโ”€ courses.ts # Course endpoints
102
+ โ”œโ”€ index.ts # Public SDK exports
103
+ ```
104
+
105
+ ---
106
+
107
+ ## ๐Ÿ“„ Example Project
108
+
109
+ ```ts
110
+ import { CheFuAcademy } from "chefu-academy-sdk";
111
+
112
+ const sdk = new CheFuAcademy({ apiKey: "sk_live_xxx" });
113
+
114
+ const courses = await sdk.courses.getAll();
115
+ console.log(courses);
116
+ ```
117
+
118
+ ---
119
+
120
+ ## ๐Ÿงช Requirements
121
+
122
+ - Node.js 18+
123
+ - TypeScript 5+ (optional but recommended)
124
+
125
+ ---
126
+
127
+ ## ๐Ÿ“œ License
128
+
129
+ MIT License ยฉ CheFu Academy
package/dist/index.d.mts CHANGED
@@ -1,35 +1,108 @@
1
- import * as axios from 'axios';
2
- import axios__default from 'axios';
3
-
4
- interface CheFuConfig {
1
+ interface CheFuAcademyConfig {
5
2
  apiKey: string;
6
3
  baseURL?: string;
4
+ timeout?: number;
7
5
  }
8
- declare class CheFuClient {
9
- apiKey: string;
10
- baseURL: string;
11
- constructor(config: CheFuConfig);
12
- request(path: string, data?: any): Promise<axios__default.AxiosResponse<any, any, {}>>;
6
+ declare class CheFuAcademyClient {
7
+ private apiKey;
8
+ private baseURL;
9
+ private client;
10
+ constructor(config: CheFuAcademyConfig);
11
+ /**
12
+ * Axios response & error interceptors
13
+ */
14
+ private setupInterceptors;
15
+ /**
16
+ * Centralized error handler
17
+ */
18
+ private handleError;
19
+ /**
20
+ * HTTP GET
21
+ */
22
+ get<T = any>(path: string): Promise<T>;
23
+ /**
24
+ * HTTP POST
25
+ */
26
+ post<T = any>(path: string, data?: any): Promise<T>;
13
27
  }
14
28
 
29
+ interface LoginResponse {
30
+ token: string;
31
+ user: {
32
+ id: string;
33
+ email: string;
34
+ fullname: string;
35
+ };
36
+ }
37
+ interface RegisterResponse {
38
+ message: string;
39
+ user: {
40
+ id: string;
41
+ email: string;
42
+ fullname: string;
43
+ };
44
+ }
15
45
  declare class Auth {
16
- client: CheFuClient;
17
- constructor(client: CheFuClient);
18
- login(email: string, password: string): Promise<axios.AxiosResponse<any, any, {}>>;
19
- register(email: string, password: string): Promise<axios.AxiosResponse<any, any, {}>>;
46
+ private client;
47
+ constructor(client: CheFuAcademyClient);
48
+ /**
49
+ * Login user
50
+ */
51
+ login(email: string, password: string): Promise<LoginResponse>;
52
+ /**
53
+ * Register user
54
+ */
55
+ register(email: string, password: string, fullname: string): Promise<RegisterResponse>;
56
+ /**
57
+ * Auth-specific error mapping
58
+ */
59
+ private handleAuthError;
20
60
  }
21
61
 
62
+ /**
63
+ * Course types
64
+ */
65
+ interface Course {
66
+ id: string;
67
+ title: string;
68
+ description: string;
69
+ bannerImage?: string;
70
+ category?: string;
71
+ createdAt: string;
72
+ updatedAt: string;
73
+ }
74
+ interface CourseListResponse {
75
+ courses: Course[];
76
+ total: number;
77
+ }
78
+ /**
79
+ * Courses API wrapper
80
+ */
22
81
  declare class Courses {
23
- client: CheFuClient;
24
- constructor(client: CheFuClient);
25
- getAll(): Promise<axios.AxiosResponse<any, any, {}>>;
26
- getById(courseId: string): Promise<axios.AxiosResponse<any, any, {}>>;
82
+ private client;
83
+ constructor(client: CheFuAcademyClient);
84
+ /**
85
+ * Get all courses
86
+ */
87
+ getAll(): Promise<CourseListResponse>;
88
+ /**
89
+ * Get a single course by ID
90
+ */
91
+ getById(courseId: string): Promise<Course>;
92
+ /**
93
+ * Centralized course error handler
94
+ */
95
+ private handleCourseError;
27
96
  }
28
97
 
98
+ /**
99
+ * Main CheFu Academy SDK
100
+ */
29
101
  declare class CheFuAcademy {
30
- auth: Auth;
31
- courses: Courses;
32
- constructor(config: CheFuConfig);
102
+ readonly auth: Auth;
103
+ readonly courses: Courses;
104
+ readonly client: CheFuAcademyClient;
105
+ constructor(config: CheFuAcademyConfig);
33
106
  }
34
107
 
35
- export { CheFuAcademy as default };
108
+ export { CheFuAcademy, CheFuAcademy as default };
package/dist/index.d.ts CHANGED
@@ -1,35 +1,108 @@
1
- import * as axios from 'axios';
2
- import axios__default from 'axios';
3
-
4
- interface CheFuConfig {
1
+ interface CheFuAcademyConfig {
5
2
  apiKey: string;
6
3
  baseURL?: string;
4
+ timeout?: number;
7
5
  }
8
- declare class CheFuClient {
9
- apiKey: string;
10
- baseURL: string;
11
- constructor(config: CheFuConfig);
12
- request(path: string, data?: any): Promise<axios__default.AxiosResponse<any, any, {}>>;
6
+ declare class CheFuAcademyClient {
7
+ private apiKey;
8
+ private baseURL;
9
+ private client;
10
+ constructor(config: CheFuAcademyConfig);
11
+ /**
12
+ * Axios response & error interceptors
13
+ */
14
+ private setupInterceptors;
15
+ /**
16
+ * Centralized error handler
17
+ */
18
+ private handleError;
19
+ /**
20
+ * HTTP GET
21
+ */
22
+ get<T = any>(path: string): Promise<T>;
23
+ /**
24
+ * HTTP POST
25
+ */
26
+ post<T = any>(path: string, data?: any): Promise<T>;
13
27
  }
14
28
 
29
+ interface LoginResponse {
30
+ token: string;
31
+ user: {
32
+ id: string;
33
+ email: string;
34
+ fullname: string;
35
+ };
36
+ }
37
+ interface RegisterResponse {
38
+ message: string;
39
+ user: {
40
+ id: string;
41
+ email: string;
42
+ fullname: string;
43
+ };
44
+ }
15
45
  declare class Auth {
16
- client: CheFuClient;
17
- constructor(client: CheFuClient);
18
- login(email: string, password: string): Promise<axios.AxiosResponse<any, any, {}>>;
19
- register(email: string, password: string): Promise<axios.AxiosResponse<any, any, {}>>;
46
+ private client;
47
+ constructor(client: CheFuAcademyClient);
48
+ /**
49
+ * Login user
50
+ */
51
+ login(email: string, password: string): Promise<LoginResponse>;
52
+ /**
53
+ * Register user
54
+ */
55
+ register(email: string, password: string, fullname: string): Promise<RegisterResponse>;
56
+ /**
57
+ * Auth-specific error mapping
58
+ */
59
+ private handleAuthError;
20
60
  }
21
61
 
62
+ /**
63
+ * Course types
64
+ */
65
+ interface Course {
66
+ id: string;
67
+ title: string;
68
+ description: string;
69
+ bannerImage?: string;
70
+ category?: string;
71
+ createdAt: string;
72
+ updatedAt: string;
73
+ }
74
+ interface CourseListResponse {
75
+ courses: Course[];
76
+ total: number;
77
+ }
78
+ /**
79
+ * Courses API wrapper
80
+ */
22
81
  declare class Courses {
23
- client: CheFuClient;
24
- constructor(client: CheFuClient);
25
- getAll(): Promise<axios.AxiosResponse<any, any, {}>>;
26
- getById(courseId: string): Promise<axios.AxiosResponse<any, any, {}>>;
82
+ private client;
83
+ constructor(client: CheFuAcademyClient);
84
+ /**
85
+ * Get all courses
86
+ */
87
+ getAll(): Promise<CourseListResponse>;
88
+ /**
89
+ * Get a single course by ID
90
+ */
91
+ getById(courseId: string): Promise<Course>;
92
+ /**
93
+ * Centralized course error handler
94
+ */
95
+ private handleCourseError;
27
96
  }
28
97
 
98
+ /**
99
+ * Main CheFu Academy SDK
100
+ */
29
101
  declare class CheFuAcademy {
30
- auth: Auth;
31
- courses: Courses;
32
- constructor(config: CheFuConfig);
102
+ readonly auth: Auth;
103
+ readonly courses: Courses;
104
+ readonly client: CheFuAcademyClient;
105
+ constructor(config: CheFuAcademyConfig);
33
106
  }
34
107
 
35
- export { CheFuAcademy as default };
108
+ export { CheFuAcademy, CheFuAcademy as default };
package/dist/index.js CHANGED
@@ -30,39 +30,218 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- default: () => CheFuAcademy
33
+ CheFuAcademy: () => CheFuAcademy,
34
+ default: () => index_default
34
35
  });
35
36
  module.exports = __toCommonJS(index_exports);
36
37
 
38
+ // src/client.ts
39
+ var import_axios = __toESM(require("axios"));
40
+ var CheFuAcademyError = class extends Error {
41
+ constructor(message, statusCode, details) {
42
+ super(message);
43
+ this.name = "CheFuAcademyError";
44
+ this.statusCode = statusCode;
45
+ this.details = details;
46
+ }
47
+ };
48
+ var CheFuAcademyClient = class {
49
+ constructor(config) {
50
+ if (!config.apiKey) {
51
+ throw new CheFuAcademyError(
52
+ "[CheFu Academy SDK] API key is required",
53
+ 401
54
+ );
55
+ }
56
+ this.apiKey = config.apiKey;
57
+ this.baseURL = config.baseURL || "https://api.chefuacademy.com/api";
58
+ this.client = import_axios.default.create({
59
+ baseURL: this.baseURL,
60
+ timeout: config.timeout || 1e4,
61
+ // 10s timeout
62
+ headers: {
63
+ Authorization: `Bearer ${this.apiKey}`,
64
+ "Content-Type": "application/json"
65
+ }
66
+ });
67
+ this.setupInterceptors();
68
+ }
69
+ /**
70
+ * Axios response & error interceptors
71
+ */
72
+ setupInterceptors() {
73
+ this.client.interceptors.response.use(
74
+ (response) => response,
75
+ (error) => {
76
+ throw this.handleError(error);
77
+ }
78
+ );
79
+ }
80
+ /**
81
+ * Centralized error handler
82
+ */
83
+ handleError(error) {
84
+ if (!error.response) {
85
+ return new CheFuAcademyError(
86
+ "Network error. Please check your internet connection."
87
+ );
88
+ }
89
+ const { status, data } = error.response;
90
+ switch (status) {
91
+ case 400:
92
+ return new CheFuAcademyError(
93
+ (data == null ? void 0 : data.message) || "Bad request.",
94
+ 400,
95
+ data
96
+ );
97
+ case 401:
98
+ return new CheFuAcademyError(
99
+ "Invalid or expired API key.",
100
+ 401
101
+ );
102
+ case 403:
103
+ return new CheFuAcademyError(
104
+ "You do not have permission to perform this action.",
105
+ 403
106
+ );
107
+ case 404:
108
+ return new CheFuAcademyError(
109
+ "Requested resource not found.",
110
+ 404
111
+ );
112
+ case 429:
113
+ return new CheFuAcademyError(
114
+ "Rate limit exceeded. Please slow down your requests.",
115
+ 429
116
+ );
117
+ case 500:
118
+ return new CheFuAcademyError(
119
+ "CheFu Academy server error. Please try again later.",
120
+ 500
121
+ );
122
+ default:
123
+ return new CheFuAcademyError(
124
+ (data == null ? void 0 : data.message) || "Unexpected error occurred.",
125
+ status,
126
+ data
127
+ );
128
+ }
129
+ }
130
+ /**
131
+ * HTTP GET
132
+ */
133
+ async get(path) {
134
+ const res = await this.client.get(path);
135
+ return res.data;
136
+ }
137
+ /**
138
+ * HTTP POST
139
+ */
140
+ async post(path, data) {
141
+ const res = await this.client.post(path, data);
142
+ return res.data;
143
+ }
144
+ };
145
+
37
146
  // src/auth.ts
38
147
  var Auth = class {
39
148
  constructor(client) {
40
149
  this.client = client;
41
150
  }
42
- login(email, password) {
43
- return this.client.request("/auth/login", { email, password });
44
- }
45
- register(email, password) {
46
- return this.client.request("/auth/register", { email, password });
151
+ /**
152
+ * Login user
153
+ */
154
+ async login(email, password) {
155
+ if (!email || !password) {
156
+ throw new CheFuAcademyError(
157
+ "Email and password are required.",
158
+ 422
159
+ );
160
+ }
161
+ try {
162
+ return await this.client.post("/auth/login", {
163
+ email,
164
+ password
165
+ });
166
+ } catch (error) {
167
+ this.handleAuthError(error, "login");
168
+ }
47
169
  }
48
- };
49
-
50
- // src/client.ts
51
- var import_axios = __toESM(require("axios"));
52
- var CheFuClient = class {
53
- constructor(config) {
54
- this.apiKey = config.apiKey;
55
- this.baseURL = config.baseURL || "https://api.chefuacademy.com";
56
- if (!this.apiKey) {
57
- throw new Error("[CheFuAcademy]: API key is required");
170
+ /**
171
+ * Register user
172
+ */
173
+ async register(email, password, fullname) {
174
+ if (!email) {
175
+ throw new CheFuAcademyError(
176
+ "Email is required.",
177
+ 422
178
+ );
179
+ }
180
+ if (!password) {
181
+ throw new CheFuAcademyError(
182
+ "Password is required.",
183
+ 422
184
+ );
185
+ }
186
+ if (!fullname) {
187
+ throw new CheFuAcademyError(
188
+ "Full name is required.",
189
+ 422
190
+ );
191
+ }
192
+ if (password.length < 6) {
193
+ throw new CheFuAcademyError(
194
+ "Password must be at least 6 characters long.",
195
+ 422
196
+ );
197
+ }
198
+ try {
199
+ return await this.client.post(
200
+ "/auth/register",
201
+ { email, password, fullname }
202
+ );
203
+ } catch (error) {
204
+ this.handleAuthError(error, "register");
58
205
  }
59
206
  }
60
- request(path, data) {
61
- return import_axios.default.post(`${this.baseURL}${path}`, data, {
62
- headers: {
63
- Authorization: `Bearer ${this.apiKey}`
207
+ /**
208
+ * Auth-specific error mapping
209
+ */
210
+ handleAuthError(error, action) {
211
+ if (error instanceof CheFuAcademyError) {
212
+ switch (error.statusCode) {
213
+ case 401:
214
+ throw new CheFuAcademyError(
215
+ "Invalid email or password.",
216
+ 401
217
+ );
218
+ case 409:
219
+ throw new CheFuAcademyError(
220
+ "An account with this email already exists.",
221
+ 409
222
+ );
223
+ case 422:
224
+ throw new CheFuAcademyError(
225
+ "Invalid input. Please check your details.",
226
+ 422,
227
+ error.details
228
+ );
229
+ case 429:
230
+ throw new CheFuAcademyError(
231
+ "Too many attempts. Please try again later.",
232
+ 429
233
+ );
234
+ default:
235
+ throw new CheFuAcademyError(
236
+ `Failed to ${action}. Please try again.`,
237
+ error.statusCode,
238
+ error.details
239
+ );
64
240
  }
65
- });
241
+ }
242
+ throw new CheFuAcademyError(
243
+ `Unexpected error during ${action}.`
244
+ );
66
245
  }
67
246
  };
68
247
 
@@ -71,19 +250,84 @@ var Courses = class {
71
250
  constructor(client) {
72
251
  this.client = client;
73
252
  }
74
- getAll() {
75
- return this.client.request("/courses");
253
+ /**
254
+ * Get all courses
255
+ */
256
+ async getAll() {
257
+ try {
258
+ return await this.client.get("/courses");
259
+ } catch (error) {
260
+ this.handleCourseError(error, "fetch courses");
261
+ }
76
262
  }
77
- getById(courseId) {
78
- return this.client.request("/courses/view", { courseId });
263
+ /**
264
+ * Get a single course by ID
265
+ */
266
+ async getById(courseId) {
267
+ if (!courseId) {
268
+ throw new CheFuAcademyError(
269
+ "Course ID is required.",
270
+ 422
271
+ );
272
+ }
273
+ try {
274
+ return await this.client.get(
275
+ `/courses/view/${courseId}`
276
+ );
277
+ } catch (error) {
278
+ this.handleCourseError(error, "fetch course");
279
+ }
280
+ }
281
+ /**
282
+ * Centralized course error handler
283
+ */
284
+ handleCourseError(error, action) {
285
+ if (error instanceof CheFuAcademyError) {
286
+ switch (error.statusCode) {
287
+ case 401:
288
+ throw new CheFuAcademyError(
289
+ "Unauthorized. Please check your API key.",
290
+ 401
291
+ );
292
+ case 404:
293
+ throw new CheFuAcademyError(
294
+ "Course not found.",
295
+ 404
296
+ );
297
+ case 429:
298
+ throw new CheFuAcademyError(
299
+ "Rate limit exceeded while trying to " + action + ".",
300
+ 429
301
+ );
302
+ default:
303
+ throw new CheFuAcademyError(
304
+ `Failed to ${action}.`,
305
+ error.statusCode,
306
+ error.details
307
+ );
308
+ }
309
+ }
310
+ throw new CheFuAcademyError(
311
+ `Unexpected error while trying to ${action}.`
312
+ );
79
313
  }
80
314
  };
81
315
 
82
316
  // src/index.ts
83
317
  var CheFuAcademy = class {
84
318
  constructor(config) {
85
- const client = new CheFuClient(config);
86
- this.auth = new Auth(client);
87
- this.courses = new Courses(client);
319
+ if (!config) {
320
+ throw new Error(
321
+ "[CheFu Academy SDK] Configuration object is required."
322
+ );
323
+ }
324
+ this.client = new CheFuAcademyClient(config);
325
+ this.auth = new Auth(this.client);
326
+ this.courses = new Courses(this.client);
88
327
  }
89
328
  };
329
+ var index_default = CheFuAcademy;
330
+ // Annotate the CommonJS export names for ESM import in node:
331
+ 0 && (module.exports = {
332
+ CheFuAcademy
333
+ });
package/dist/index.mjs CHANGED
@@ -1,32 +1,210 @@
1
+ // src/client.ts
2
+ import axios from "axios";
3
+ var CheFuAcademyError = class extends Error {
4
+ constructor(message, statusCode, details) {
5
+ super(message);
6
+ this.name = "CheFuAcademyError";
7
+ this.statusCode = statusCode;
8
+ this.details = details;
9
+ }
10
+ };
11
+ var CheFuAcademyClient = class {
12
+ constructor(config) {
13
+ if (!config.apiKey) {
14
+ throw new CheFuAcademyError(
15
+ "[CheFu Academy SDK] API key is required",
16
+ 401
17
+ );
18
+ }
19
+ this.apiKey = config.apiKey;
20
+ this.baseURL = config.baseURL || "https://api.chefuacademy.com/api";
21
+ this.client = axios.create({
22
+ baseURL: this.baseURL,
23
+ timeout: config.timeout || 1e4,
24
+ // 10s timeout
25
+ headers: {
26
+ Authorization: `Bearer ${this.apiKey}`,
27
+ "Content-Type": "application/json"
28
+ }
29
+ });
30
+ this.setupInterceptors();
31
+ }
32
+ /**
33
+ * Axios response & error interceptors
34
+ */
35
+ setupInterceptors() {
36
+ this.client.interceptors.response.use(
37
+ (response) => response,
38
+ (error) => {
39
+ throw this.handleError(error);
40
+ }
41
+ );
42
+ }
43
+ /**
44
+ * Centralized error handler
45
+ */
46
+ handleError(error) {
47
+ if (!error.response) {
48
+ return new CheFuAcademyError(
49
+ "Network error. Please check your internet connection."
50
+ );
51
+ }
52
+ const { status, data } = error.response;
53
+ switch (status) {
54
+ case 400:
55
+ return new CheFuAcademyError(
56
+ (data == null ? void 0 : data.message) || "Bad request.",
57
+ 400,
58
+ data
59
+ );
60
+ case 401:
61
+ return new CheFuAcademyError(
62
+ "Invalid or expired API key.",
63
+ 401
64
+ );
65
+ case 403:
66
+ return new CheFuAcademyError(
67
+ "You do not have permission to perform this action.",
68
+ 403
69
+ );
70
+ case 404:
71
+ return new CheFuAcademyError(
72
+ "Requested resource not found.",
73
+ 404
74
+ );
75
+ case 429:
76
+ return new CheFuAcademyError(
77
+ "Rate limit exceeded. Please slow down your requests.",
78
+ 429
79
+ );
80
+ case 500:
81
+ return new CheFuAcademyError(
82
+ "CheFu Academy server error. Please try again later.",
83
+ 500
84
+ );
85
+ default:
86
+ return new CheFuAcademyError(
87
+ (data == null ? void 0 : data.message) || "Unexpected error occurred.",
88
+ status,
89
+ data
90
+ );
91
+ }
92
+ }
93
+ /**
94
+ * HTTP GET
95
+ */
96
+ async get(path) {
97
+ const res = await this.client.get(path);
98
+ return res.data;
99
+ }
100
+ /**
101
+ * HTTP POST
102
+ */
103
+ async post(path, data) {
104
+ const res = await this.client.post(path, data);
105
+ return res.data;
106
+ }
107
+ };
108
+
1
109
  // src/auth.ts
2
110
  var Auth = class {
3
111
  constructor(client) {
4
112
  this.client = client;
5
113
  }
6
- login(email, password) {
7
- return this.client.request("/auth/login", { email, password });
8
- }
9
- register(email, password) {
10
- return this.client.request("/auth/register", { email, password });
114
+ /**
115
+ * Login user
116
+ */
117
+ async login(email, password) {
118
+ if (!email || !password) {
119
+ throw new CheFuAcademyError(
120
+ "Email and password are required.",
121
+ 422
122
+ );
123
+ }
124
+ try {
125
+ return await this.client.post("/auth/login", {
126
+ email,
127
+ password
128
+ });
129
+ } catch (error) {
130
+ this.handleAuthError(error, "login");
131
+ }
11
132
  }
12
- };
13
-
14
- // src/client.ts
15
- import axios from "axios";
16
- var CheFuClient = class {
17
- constructor(config) {
18
- this.apiKey = config.apiKey;
19
- this.baseURL = config.baseURL || "https://api.chefuacademy.com";
20
- if (!this.apiKey) {
21
- throw new Error("[CheFuAcademy]: API key is required");
133
+ /**
134
+ * Register user
135
+ */
136
+ async register(email, password, fullname) {
137
+ if (!email) {
138
+ throw new CheFuAcademyError(
139
+ "Email is required.",
140
+ 422
141
+ );
142
+ }
143
+ if (!password) {
144
+ throw new CheFuAcademyError(
145
+ "Password is required.",
146
+ 422
147
+ );
148
+ }
149
+ if (!fullname) {
150
+ throw new CheFuAcademyError(
151
+ "Full name is required.",
152
+ 422
153
+ );
154
+ }
155
+ if (password.length < 6) {
156
+ throw new CheFuAcademyError(
157
+ "Password must be at least 6 characters long.",
158
+ 422
159
+ );
160
+ }
161
+ try {
162
+ return await this.client.post(
163
+ "/auth/register",
164
+ { email, password, fullname }
165
+ );
166
+ } catch (error) {
167
+ this.handleAuthError(error, "register");
22
168
  }
23
169
  }
24
- request(path, data) {
25
- return axios.post(`${this.baseURL}${path}`, data, {
26
- headers: {
27
- Authorization: `Bearer ${this.apiKey}`
170
+ /**
171
+ * Auth-specific error mapping
172
+ */
173
+ handleAuthError(error, action) {
174
+ if (error instanceof CheFuAcademyError) {
175
+ switch (error.statusCode) {
176
+ case 401:
177
+ throw new CheFuAcademyError(
178
+ "Invalid email or password.",
179
+ 401
180
+ );
181
+ case 409:
182
+ throw new CheFuAcademyError(
183
+ "An account with this email already exists.",
184
+ 409
185
+ );
186
+ case 422:
187
+ throw new CheFuAcademyError(
188
+ "Invalid input. Please check your details.",
189
+ 422,
190
+ error.details
191
+ );
192
+ case 429:
193
+ throw new CheFuAcademyError(
194
+ "Too many attempts. Please try again later.",
195
+ 429
196
+ );
197
+ default:
198
+ throw new CheFuAcademyError(
199
+ `Failed to ${action}. Please try again.`,
200
+ error.statusCode,
201
+ error.details
202
+ );
28
203
  }
29
- });
204
+ }
205
+ throw new CheFuAcademyError(
206
+ `Unexpected error during ${action}.`
207
+ );
30
208
  }
31
209
  };
32
210
 
@@ -35,22 +213,84 @@ var Courses = class {
35
213
  constructor(client) {
36
214
  this.client = client;
37
215
  }
38
- getAll() {
39
- return this.client.request("/courses");
216
+ /**
217
+ * Get all courses
218
+ */
219
+ async getAll() {
220
+ try {
221
+ return await this.client.get("/courses");
222
+ } catch (error) {
223
+ this.handleCourseError(error, "fetch courses");
224
+ }
225
+ }
226
+ /**
227
+ * Get a single course by ID
228
+ */
229
+ async getById(courseId) {
230
+ if (!courseId) {
231
+ throw new CheFuAcademyError(
232
+ "Course ID is required.",
233
+ 422
234
+ );
235
+ }
236
+ try {
237
+ return await this.client.get(
238
+ `/courses/view/${courseId}`
239
+ );
240
+ } catch (error) {
241
+ this.handleCourseError(error, "fetch course");
242
+ }
40
243
  }
41
- getById(courseId) {
42
- return this.client.request("/courses/view", { courseId });
244
+ /**
245
+ * Centralized course error handler
246
+ */
247
+ handleCourseError(error, action) {
248
+ if (error instanceof CheFuAcademyError) {
249
+ switch (error.statusCode) {
250
+ case 401:
251
+ throw new CheFuAcademyError(
252
+ "Unauthorized. Please check your API key.",
253
+ 401
254
+ );
255
+ case 404:
256
+ throw new CheFuAcademyError(
257
+ "Course not found.",
258
+ 404
259
+ );
260
+ case 429:
261
+ throw new CheFuAcademyError(
262
+ "Rate limit exceeded while trying to " + action + ".",
263
+ 429
264
+ );
265
+ default:
266
+ throw new CheFuAcademyError(
267
+ `Failed to ${action}.`,
268
+ error.statusCode,
269
+ error.details
270
+ );
271
+ }
272
+ }
273
+ throw new CheFuAcademyError(
274
+ `Unexpected error while trying to ${action}.`
275
+ );
43
276
  }
44
277
  };
45
278
 
46
279
  // src/index.ts
47
280
  var CheFuAcademy = class {
48
281
  constructor(config) {
49
- const client = new CheFuClient(config);
50
- this.auth = new Auth(client);
51
- this.courses = new Courses(client);
282
+ if (!config) {
283
+ throw new Error(
284
+ "[CheFu Academy SDK] Configuration object is required."
285
+ );
286
+ }
287
+ this.client = new CheFuAcademyClient(config);
288
+ this.auth = new Auth(this.client);
289
+ this.courses = new Courses(this.client);
52
290
  }
53
291
  };
292
+ var index_default = CheFuAcademy;
54
293
  export {
55
- CheFuAcademy as default
294
+ CheFuAcademy,
295
+ index_default as default
56
296
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chefu-academy-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {