@pelatform/starter.db 0.1.0 → 0.1.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/dist/index.d.ts CHANGED
@@ -6,6 +6,84 @@ declare const createIdCustom: ({ prefix }: {
6
6
  prefix: string;
7
7
  }) => string;
8
8
 
9
+ /**
10
+ * Represents a failed API operation.
11
+ *
12
+ * Used as a standardized error result across services and API layers.
13
+ * The `code` should be a machine-readable identifier,
14
+ * while `message` should be human-readable.
15
+ */
16
+ type ApiErrorResult = {
17
+ /**
18
+ * Indicates the operation failed.
19
+ */
20
+ ok: false;
21
+ /**
22
+ * Error payload containing a code and message.
23
+ */
24
+ error: {
25
+ /**
26
+ * Machine-readable error code.
27
+ * Example: "NOT_FOUND", "UNAUTHORIZED", "VALIDATION_ERROR"
28
+ */
29
+ code: string;
30
+ /**
31
+ * Human-readable error message.
32
+ * Safe to display to end users.
33
+ */
34
+ message: string;
35
+ };
36
+ };
37
+ /**
38
+ * Represents a successful API operation.
39
+ *
40
+ * Wraps the returned data in a consistent response shape.
41
+ *
42
+ * @template T - Type of the successful response data
43
+ */
44
+ type ApiSuccessResult<T> = {
45
+ /**
46
+ * Indicates the operation was successful.
47
+ */
48
+ ok: true;
49
+ /**
50
+ * Payload returned from the operation.
51
+ */
52
+ data: T;
53
+ };
54
+ /**
55
+ * Creates a standardized API error response.
56
+ *
57
+ * This helper ensures all error responses share
58
+ * the same structure and can be handled consistently
59
+ * by clients or higher-level handlers.
60
+ *
61
+ * @param code - Machine-readable error code
62
+ * @param message - Human-readable error message
63
+ * @returns ApiErrorResult object
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * return ApiError("NOT_FOUND", "User not found");
68
+ * ```
69
+ */
70
+ declare function ApiError(code: string, message: string): ApiErrorResult;
71
+ /**
72
+ * Creates a standardized API success response.
73
+ *
74
+ * Wraps the provided data in a consistent success shape.
75
+ *
76
+ * @template T - Type of the returned data
77
+ * @param data - Data to return to the client
78
+ * @returns ApiSuccessResult containing the data
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * return ApiSuccess(user);
83
+ * ```
84
+ */
85
+ declare function ApiSuccess<T>(data: T): ApiSuccessResult<T>;
86
+
9
87
  /**
10
88
  * Creates a new email log entry in the database.
11
89
  *
@@ -30,7 +108,7 @@ declare function createEmailLog(db: any, email: string, metadata: any): Promise<
30
108
  * @param customSelect - Optional Prisma select object to customize returned fields.
31
109
  * @returns The user object with role and active workspace, or an error object.
32
110
  */
33
- declare function getUser(db: any, session: any, customSelect?: any): Promise<any>;
111
+ declare function getUser(db: any, session: any, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
34
112
  /**
35
113
  * Updates a user's information in the database.
36
114
  *
@@ -43,7 +121,7 @@ declare function getUser(db: any, session: any, customSelect?: any): Promise<any
43
121
  * @param customSelect - Optional Prisma select object to customize returned fields.
44
122
  * @returns The updated user object with role and active workspace, or an error object.
45
123
  */
46
- declare function updateUser(db: any, session: any, data: Record<string, any>, customSelect?: any): Promise<any>;
124
+ declare function updateUser(db: any, session: any, data: Record<string, any>, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
47
125
  /**
48
126
  * Updates a specific user's data by their ID.
49
127
  *
@@ -82,7 +160,7 @@ declare function getInitialUserData(db: any, session: any): Promise<{
82
160
  * @param customSelect - Optional Prisma select object.
83
161
  * @returns The workspace object with currentUserRole, or an error object.
84
162
  */
85
- declare function getWorkspacebySlug(db: any, session: any, slug: string, customSelect?: any): Promise<any>;
163
+ declare function getWorkspacebySlug(db: any, session: any, slug: string, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
86
164
  /**
87
165
  * Retrieves a list of workspaces the user is a member of.
88
166
  *
@@ -91,7 +169,7 @@ declare function getWorkspacebySlug(db: any, session: any, slug: string, customS
91
169
  * @param customSelect - Optional Prisma select object.
92
170
  * @returns Array of workspace objects with currentUserRole, or an error object.
93
171
  */
94
- declare function getWorkspaceList(db: any, session: any, customSelect?: any): Promise<any>;
172
+ declare function getWorkspaceList(db: any, session: any, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
95
173
  /**
96
174
  * Fetches the initial workspace data for the active user.
97
175
  *
@@ -216,4 +294,4 @@ declare const workspaceSelect: {
216
294
  };
217
295
  };
218
296
 
219
- export { createEmailLog, createId, createIdCustom, getInitialUserData, getInitialWorkspaceData, getLastActiveWorkspace, getUser, getWorkspaceList, getWorkspacebySlug, updateUser, updateUserData, userSelect, validateWorkspaceAccess, workspaceSelect };
297
+ export { ApiError, type ApiErrorResult, ApiSuccess, type ApiSuccessResult, createEmailLog, createId, createIdCustom, getInitialUserData, getInitialWorkspaceData, getLastActiveWorkspace, getUser, getWorkspaceList, getWorkspacebySlug, updateUser, updateUserData, userSelect, validateWorkspaceAccess, workspaceSelect };
package/dist/index.js CHANGED
@@ -25,6 +25,20 @@ var createIdCustom = ({ prefix }) => {
25
25
  return `${prefix}${id}`;
26
26
  };
27
27
 
28
+ // src/lib/utils.ts
29
+ function ApiError(code, message) {
30
+ return {
31
+ ok: false,
32
+ error: { code, message }
33
+ };
34
+ }
35
+ function ApiSuccess(data) {
36
+ return {
37
+ ok: true,
38
+ data
39
+ };
40
+ }
41
+
28
42
  // src/queries/email-log.ts
29
43
  async function createEmailLog(db, email, metadata) {
30
44
  await db.emailLog.create({
@@ -65,7 +79,7 @@ var userSelect = (workspaceId) => ({
65
79
  // src/queries/user.ts
66
80
  async function getUser(db, session, customSelect) {
67
81
  if (!session || !session.user) {
68
- return { error: true, message: "User not authenticated" };
82
+ return ApiError("UNAUTHORIZED", "User not authenticated");
69
83
  }
70
84
  try {
71
85
  const result = await db.user.findUnique({
@@ -73,22 +87,22 @@ async function getUser(db, session, customSelect) {
73
87
  select: customSelect || userSelect(session?.session?.activeOrganizationId || "")
74
88
  });
75
89
  if (!result) {
76
- return { error: true, message: "User not found" };
90
+ return ApiError("NOT_FOUND", "User not found");
77
91
  }
78
92
  const user = {
79
93
  ...result,
80
94
  workspaceRole: result?.members[0]?.role || null,
81
95
  activeWorkspace: result?.members[0]?.organization || null
82
96
  };
83
- return user;
97
+ return ApiSuccess(user);
84
98
  } catch (error) {
85
99
  console.error("Error fetching user data:", error);
86
- return { error: true, message: "Error fetching user data." };
100
+ return ApiError("INTERNAL_SERVER_ERROR", "Error fetching user data.");
87
101
  }
88
102
  }
89
103
  async function updateUser(db, session, data, customSelect) {
90
104
  if (!session || !session.user) {
91
- return { error: true, message: "User not authenticated" };
105
+ return ApiError("UNAUTHORIZED", "User not authenticated");
92
106
  }
93
107
  try {
94
108
  const result = await db.user.update({
@@ -97,17 +111,17 @@ async function updateUser(db, session, data, customSelect) {
97
111
  select: customSelect || userSelect(session?.session?.activeOrganizationId || "")
98
112
  });
99
113
  if (!result) {
100
- return { error: true, message: "User not found" };
114
+ return ApiError("NOT_FOUND", "User not found");
101
115
  }
102
116
  const user = {
103
117
  ...result,
104
118
  workspaceRole: result?.members[0]?.role || null,
105
119
  activeWorkspace: result?.members[0]?.organization || null
106
120
  };
107
- return user;
121
+ return ApiSuccess(user);
108
122
  } catch (error) {
109
123
  console.error("Error updating user data:", error);
110
- return { error: true, message: "Error updating user data." };
124
+ return ApiError("INTERNAL_SERVER_ERROR", "Error updating user data.");
111
125
  }
112
126
  }
113
127
  async function updateUserData(db, userId, data) {
@@ -220,7 +234,7 @@ var workspaceSelect = {
220
234
  // src/queries/workspace.ts
221
235
  async function getWorkspacebySlug(db, session, slug, customSelect) {
222
236
  if (!session || !session.user) {
223
- return { error: true, message: "User not authenticated" };
237
+ return ApiError("UNAUTHORIZED", "User not authenticated");
224
238
  }
225
239
  try {
226
240
  const result = await db.organization.findUnique({
@@ -228,11 +242,11 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
228
242
  select: customSelect || workspaceSelect
229
243
  });
230
244
  if (!result) {
231
- return { error: true, message: "Workspace not found" };
245
+ return ApiError("NOT_FOUND", "Workspace not found");
232
246
  }
233
247
  const isUserMember = result.members.some((member) => member.userId === session.user.id);
234
248
  if (!isUserMember) {
235
- return { error: true, message: "User is not a member of the workspace" };
249
+ return ApiError("FORBIDDEN", "User is not a member of the workspace");
236
250
  }
237
251
  const currentUserMember = result.members.find(
238
252
  (member) => member.userId === session.user.id
@@ -242,15 +256,15 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
242
256
  ...result,
243
257
  currentUserRole
244
258
  };
245
- return workspace;
259
+ return ApiSuccess(workspace);
246
260
  } catch (error) {
247
261
  console.error("Error fetching workspace data:", error);
248
- return { error: true, message: "Error fetching workspace data." };
262
+ return ApiError("INTERNAL_SERVER_ERROR", "Error fetching workspace data.");
249
263
  }
250
264
  }
251
265
  async function getWorkspaceList(db, session, customSelect) {
252
266
  if (!session || !session.user) {
253
- return { error: true, message: "User not authenticated" };
267
+ return ApiError("UNAUTHORIZED", "User not authenticated");
254
268
  }
255
269
  try {
256
270
  const result = await db.organization.findMany({
@@ -273,10 +287,10 @@ async function getWorkspaceList(db, session, customSelect) {
273
287
  currentUserRole: currentUserMember?.role || null
274
288
  };
275
289
  });
276
- return workspaces;
290
+ return ApiSuccess(workspaces);
277
291
  } catch (error) {
278
292
  console.error("Error fetching workspace list:", error);
279
- return { error: true, message: "Error fetching workspace list." };
293
+ return ApiError("INTERNAL_SERVER_ERROR", "Error fetching workspace list.");
280
294
  }
281
295
  }
282
296
  async function getInitialWorkspaceData(db, session, customSelect) {
@@ -371,6 +385,8 @@ async function validateWorkspaceAccess(db, session, slug) {
371
385
  return Boolean(workspace);
372
386
  }
373
387
  export {
388
+ ApiError,
389
+ ApiSuccess,
374
390
  createEmailLog,
375
391
  createId,
376
392
  createIdCustom,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pelatform/starter.db",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A part of SaaS starter kit for Pelatform applications.",
5
5
  "author": "Pelatform",
6
6
  "license": "MIT",