@pelatform/starter.db 0.1.1 → 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 +83 -49
- package/dist/index.js +32 -31
- package/package.json +1 -1
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,18 +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<
|
|
34
|
-
ok: boolean;
|
|
35
|
-
error: {
|
|
36
|
-
code: string;
|
|
37
|
-
message: string;
|
|
38
|
-
};
|
|
39
|
-
data?: undefined;
|
|
40
|
-
} | {
|
|
41
|
-
ok: boolean;
|
|
42
|
-
data: any;
|
|
43
|
-
error?: undefined;
|
|
44
|
-
}>;
|
|
111
|
+
declare function getUser(db: any, session: any, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
|
|
45
112
|
/**
|
|
46
113
|
* Updates a user's information in the database.
|
|
47
114
|
*
|
|
@@ -54,18 +121,7 @@ declare function getUser(db: any, session: any, customSelect?: any): Promise<{
|
|
|
54
121
|
* @param customSelect - Optional Prisma select object to customize returned fields.
|
|
55
122
|
* @returns The updated user object with role and active workspace, or an error object.
|
|
56
123
|
*/
|
|
57
|
-
declare function updateUser(db: any, session: any, data: Record<string, any>, customSelect?: any): Promise<
|
|
58
|
-
ok: boolean;
|
|
59
|
-
error: {
|
|
60
|
-
code: string;
|
|
61
|
-
message: string;
|
|
62
|
-
};
|
|
63
|
-
data?: undefined;
|
|
64
|
-
} | {
|
|
65
|
-
ok: boolean;
|
|
66
|
-
data: any;
|
|
67
|
-
error?: undefined;
|
|
68
|
-
}>;
|
|
124
|
+
declare function updateUser(db: any, session: any, data: Record<string, any>, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
|
|
69
125
|
/**
|
|
70
126
|
* Updates a specific user's data by their ID.
|
|
71
127
|
*
|
|
@@ -104,18 +160,7 @@ declare function getInitialUserData(db: any, session: any): Promise<{
|
|
|
104
160
|
* @param customSelect - Optional Prisma select object.
|
|
105
161
|
* @returns The workspace object with currentUserRole, or an error object.
|
|
106
162
|
*/
|
|
107
|
-
declare function getWorkspacebySlug(db: any, session: any, slug: string, customSelect?: any): Promise<
|
|
108
|
-
ok: boolean;
|
|
109
|
-
error: {
|
|
110
|
-
code: string;
|
|
111
|
-
message: string;
|
|
112
|
-
};
|
|
113
|
-
data?: undefined;
|
|
114
|
-
} | {
|
|
115
|
-
ok: boolean;
|
|
116
|
-
data: any;
|
|
117
|
-
error?: undefined;
|
|
118
|
-
}>;
|
|
163
|
+
declare function getWorkspacebySlug(db: any, session: any, slug: string, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
|
|
119
164
|
/**
|
|
120
165
|
* Retrieves a list of workspaces the user is a member of.
|
|
121
166
|
*
|
|
@@ -124,18 +169,7 @@ declare function getWorkspacebySlug(db: any, session: any, slug: string, customS
|
|
|
124
169
|
* @param customSelect - Optional Prisma select object.
|
|
125
170
|
* @returns Array of workspace objects with currentUserRole, or an error object.
|
|
126
171
|
*/
|
|
127
|
-
declare function getWorkspaceList(db: any, session: any, customSelect?: any): Promise<
|
|
128
|
-
ok: boolean;
|
|
129
|
-
error: {
|
|
130
|
-
code: string;
|
|
131
|
-
message: string;
|
|
132
|
-
};
|
|
133
|
-
data?: undefined;
|
|
134
|
-
} | {
|
|
135
|
-
ok: boolean;
|
|
136
|
-
data: any;
|
|
137
|
-
error?: undefined;
|
|
138
|
-
}>;
|
|
172
|
+
declare function getWorkspaceList(db: any, session: any, customSelect?: any): Promise<ApiErrorResult | ApiSuccessResult<any>>;
|
|
139
173
|
/**
|
|
140
174
|
* Fetches the initial workspace data for the active user.
|
|
141
175
|
*
|
|
@@ -260,4 +294,4 @@ declare const workspaceSelect: {
|
|
|
260
294
|
};
|
|
261
295
|
};
|
|
262
296
|
|
|
263
|
-
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
|
|
82
|
+
return ApiError("UNAUTHORIZED", "User not authenticated");
|
|
69
83
|
}
|
|
70
84
|
try {
|
|
71
85
|
const result = await db.user.findUnique({
|
|
@@ -73,25 +87,22 @@ async function getUser(db, session, customSelect) {
|
|
|
73
87
|
select: customSelect || userSelect(session?.session?.activeOrganizationId || "")
|
|
74
88
|
});
|
|
75
89
|
if (!result) {
|
|
76
|
-
return
|
|
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
|
|
97
|
+
return ApiSuccess(user);
|
|
84
98
|
} catch (error) {
|
|
85
99
|
console.error("Error fetching user data:", error);
|
|
86
|
-
return
|
|
87
|
-
ok: false,
|
|
88
|
-
error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching user data." }
|
|
89
|
-
};
|
|
100
|
+
return ApiError("INTERNAL_SERVER_ERROR", "Error fetching user data.");
|
|
90
101
|
}
|
|
91
102
|
}
|
|
92
103
|
async function updateUser(db, session, data, customSelect) {
|
|
93
104
|
if (!session || !session.user) {
|
|
94
|
-
return
|
|
105
|
+
return ApiError("UNAUTHORIZED", "User not authenticated");
|
|
95
106
|
}
|
|
96
107
|
try {
|
|
97
108
|
const result = await db.user.update({
|
|
@@ -100,20 +111,17 @@ async function updateUser(db, session, data, customSelect) {
|
|
|
100
111
|
select: customSelect || userSelect(session?.session?.activeOrganizationId || "")
|
|
101
112
|
});
|
|
102
113
|
if (!result) {
|
|
103
|
-
return
|
|
114
|
+
return ApiError("NOT_FOUND", "User not found");
|
|
104
115
|
}
|
|
105
116
|
const user = {
|
|
106
117
|
...result,
|
|
107
118
|
workspaceRole: result?.members[0]?.role || null,
|
|
108
119
|
activeWorkspace: result?.members[0]?.organization || null
|
|
109
120
|
};
|
|
110
|
-
return
|
|
121
|
+
return ApiSuccess(user);
|
|
111
122
|
} catch (error) {
|
|
112
123
|
console.error("Error updating user data:", error);
|
|
113
|
-
return
|
|
114
|
-
ok: false,
|
|
115
|
-
error: { code: "INTERNAL_SERVER_ERROR", message: "Error updating user data." }
|
|
116
|
-
};
|
|
124
|
+
return ApiError("INTERNAL_SERVER_ERROR", "Error updating user data.");
|
|
117
125
|
}
|
|
118
126
|
}
|
|
119
127
|
async function updateUserData(db, userId, data) {
|
|
@@ -226,7 +234,7 @@ var workspaceSelect = {
|
|
|
226
234
|
// src/queries/workspace.ts
|
|
227
235
|
async function getWorkspacebySlug(db, session, slug, customSelect) {
|
|
228
236
|
if (!session || !session.user) {
|
|
229
|
-
return
|
|
237
|
+
return ApiError("UNAUTHORIZED", "User not authenticated");
|
|
230
238
|
}
|
|
231
239
|
try {
|
|
232
240
|
const result = await db.organization.findUnique({
|
|
@@ -234,14 +242,11 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
|
|
|
234
242
|
select: customSelect || workspaceSelect
|
|
235
243
|
});
|
|
236
244
|
if (!result) {
|
|
237
|
-
return
|
|
245
|
+
return ApiError("NOT_FOUND", "Workspace not found");
|
|
238
246
|
}
|
|
239
247
|
const isUserMember = result.members.some((member) => member.userId === session.user.id);
|
|
240
248
|
if (!isUserMember) {
|
|
241
|
-
return
|
|
242
|
-
ok: false,
|
|
243
|
-
error: { code: "FORBIDDEN", message: "User is not a member of the workspace" }
|
|
244
|
-
};
|
|
249
|
+
return ApiError("FORBIDDEN", "User is not a member of the workspace");
|
|
245
250
|
}
|
|
246
251
|
const currentUserMember = result.members.find(
|
|
247
252
|
(member) => member.userId === session.user.id
|
|
@@ -251,18 +256,15 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
|
|
|
251
256
|
...result,
|
|
252
257
|
currentUserRole
|
|
253
258
|
};
|
|
254
|
-
return
|
|
259
|
+
return ApiSuccess(workspace);
|
|
255
260
|
} catch (error) {
|
|
256
261
|
console.error("Error fetching workspace data:", error);
|
|
257
|
-
return
|
|
258
|
-
ok: false,
|
|
259
|
-
error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching workspace data." }
|
|
260
|
-
};
|
|
262
|
+
return ApiError("INTERNAL_SERVER_ERROR", "Error fetching workspace data.");
|
|
261
263
|
}
|
|
262
264
|
}
|
|
263
265
|
async function getWorkspaceList(db, session, customSelect) {
|
|
264
266
|
if (!session || !session.user) {
|
|
265
|
-
return
|
|
267
|
+
return ApiError("UNAUTHORIZED", "User not authenticated");
|
|
266
268
|
}
|
|
267
269
|
try {
|
|
268
270
|
const result = await db.organization.findMany({
|
|
@@ -285,13 +287,10 @@ async function getWorkspaceList(db, session, customSelect) {
|
|
|
285
287
|
currentUserRole: currentUserMember?.role || null
|
|
286
288
|
};
|
|
287
289
|
});
|
|
288
|
-
return
|
|
290
|
+
return ApiSuccess(workspaces);
|
|
289
291
|
} catch (error) {
|
|
290
292
|
console.error("Error fetching workspace list:", error);
|
|
291
|
-
return
|
|
292
|
-
ok: false,
|
|
293
|
-
error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching workspace list." }
|
|
294
|
-
};
|
|
293
|
+
return ApiError("INTERNAL_SERVER_ERROR", "Error fetching workspace list.");
|
|
295
294
|
}
|
|
296
295
|
}
|
|
297
296
|
async function getInitialWorkspaceData(db, session, customSelect) {
|
|
@@ -386,6 +385,8 @@ async function validateWorkspaceAccess(db, session, slug) {
|
|
|
386
385
|
return Boolean(workspace);
|
|
387
386
|
}
|
|
388
387
|
export {
|
|
388
|
+
ApiError,
|
|
389
|
+
ApiSuccess,
|
|
389
390
|
createEmailLog,
|
|
390
391
|
createId,
|
|
391
392
|
createIdCustom,
|