@pelatform/starter.db 0.1.1 → 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/dist/index.d.ts +83 -66
- package/dist/index.js +32 -48
- 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
|
*
|
|
@@ -241,23 +275,6 @@ declare const workspaceSelect: {
|
|
|
241
275
|
readonly expiresAt: true;
|
|
242
276
|
};
|
|
243
277
|
};
|
|
244
|
-
readonly subscription: {
|
|
245
|
-
readonly select: {
|
|
246
|
-
readonly id: true;
|
|
247
|
-
readonly userId: true;
|
|
248
|
-
readonly workspaceId: true;
|
|
249
|
-
readonly subscriptionId: true;
|
|
250
|
-
readonly externalId: true;
|
|
251
|
-
readonly provider: true;
|
|
252
|
-
readonly status: true;
|
|
253
|
-
readonly plan: true;
|
|
254
|
-
readonly currentPeriodStart: true;
|
|
255
|
-
readonly currentPeriodEnd: true;
|
|
256
|
-
readonly cancelAtPeriodEnd: true;
|
|
257
|
-
readonly canceledAt: true;
|
|
258
|
-
readonly isLifetime: true;
|
|
259
|
-
};
|
|
260
|
-
};
|
|
261
278
|
};
|
|
262
279
|
|
|
263
|
-
export { createEmailLog, createId, createIdCustom, getInitialUserData, getInitialWorkspaceData, getLastActiveWorkspace, getUser, getWorkspaceList, getWorkspacebySlug, updateUser, updateUserData, userSelect, validateWorkspaceAccess, workspaceSelect };
|
|
280
|
+
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) {
|
|
@@ -203,30 +211,13 @@ var workspaceSelect = {
|
|
|
203
211
|
status: true,
|
|
204
212
|
expiresAt: true
|
|
205
213
|
}
|
|
206
|
-
},
|
|
207
|
-
subscription: {
|
|
208
|
-
select: {
|
|
209
|
-
id: true,
|
|
210
|
-
userId: true,
|
|
211
|
-
workspaceId: true,
|
|
212
|
-
subscriptionId: true,
|
|
213
|
-
externalId: true,
|
|
214
|
-
provider: true,
|
|
215
|
-
status: true,
|
|
216
|
-
plan: true,
|
|
217
|
-
currentPeriodStart: true,
|
|
218
|
-
currentPeriodEnd: true,
|
|
219
|
-
cancelAtPeriodEnd: true,
|
|
220
|
-
canceledAt: true,
|
|
221
|
-
isLifetime: true
|
|
222
|
-
}
|
|
223
214
|
}
|
|
224
215
|
};
|
|
225
216
|
|
|
226
217
|
// src/queries/workspace.ts
|
|
227
218
|
async function getWorkspacebySlug(db, session, slug, customSelect) {
|
|
228
219
|
if (!session || !session.user) {
|
|
229
|
-
return
|
|
220
|
+
return ApiError("UNAUTHORIZED", "User not authenticated");
|
|
230
221
|
}
|
|
231
222
|
try {
|
|
232
223
|
const result = await db.organization.findUnique({
|
|
@@ -234,14 +225,11 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
|
|
|
234
225
|
select: customSelect || workspaceSelect
|
|
235
226
|
});
|
|
236
227
|
if (!result) {
|
|
237
|
-
return
|
|
228
|
+
return ApiError("NOT_FOUND", "Workspace not found");
|
|
238
229
|
}
|
|
239
230
|
const isUserMember = result.members.some((member) => member.userId === session.user.id);
|
|
240
231
|
if (!isUserMember) {
|
|
241
|
-
return
|
|
242
|
-
ok: false,
|
|
243
|
-
error: { code: "FORBIDDEN", message: "User is not a member of the workspace" }
|
|
244
|
-
};
|
|
232
|
+
return ApiError("FORBIDDEN", "User is not a member of the workspace");
|
|
245
233
|
}
|
|
246
234
|
const currentUserMember = result.members.find(
|
|
247
235
|
(member) => member.userId === session.user.id
|
|
@@ -251,18 +239,15 @@ async function getWorkspacebySlug(db, session, slug, customSelect) {
|
|
|
251
239
|
...result,
|
|
252
240
|
currentUserRole
|
|
253
241
|
};
|
|
254
|
-
return
|
|
242
|
+
return ApiSuccess(workspace);
|
|
255
243
|
} catch (error) {
|
|
256
244
|
console.error("Error fetching workspace data:", error);
|
|
257
|
-
return
|
|
258
|
-
ok: false,
|
|
259
|
-
error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching workspace data." }
|
|
260
|
-
};
|
|
245
|
+
return ApiError("INTERNAL_SERVER_ERROR", "Error fetching workspace data.");
|
|
261
246
|
}
|
|
262
247
|
}
|
|
263
248
|
async function getWorkspaceList(db, session, customSelect) {
|
|
264
249
|
if (!session || !session.user) {
|
|
265
|
-
return
|
|
250
|
+
return ApiError("UNAUTHORIZED", "User not authenticated");
|
|
266
251
|
}
|
|
267
252
|
try {
|
|
268
253
|
const result = await db.organization.findMany({
|
|
@@ -285,13 +270,10 @@ async function getWorkspaceList(db, session, customSelect) {
|
|
|
285
270
|
currentUserRole: currentUserMember?.role || null
|
|
286
271
|
};
|
|
287
272
|
});
|
|
288
|
-
return
|
|
273
|
+
return ApiSuccess(workspaces);
|
|
289
274
|
} catch (error) {
|
|
290
275
|
console.error("Error fetching workspace list:", error);
|
|
291
|
-
return
|
|
292
|
-
ok: false,
|
|
293
|
-
error: { code: "INTERNAL_SERVER_ERROR", message: "Error fetching workspace list." }
|
|
294
|
-
};
|
|
276
|
+
return ApiError("INTERNAL_SERVER_ERROR", "Error fetching workspace list.");
|
|
295
277
|
}
|
|
296
278
|
}
|
|
297
279
|
async function getInitialWorkspaceData(db, session, customSelect) {
|
|
@@ -386,6 +368,8 @@ async function validateWorkspaceAccess(db, session, slug) {
|
|
|
386
368
|
return Boolean(workspace);
|
|
387
369
|
}
|
|
388
370
|
export {
|
|
371
|
+
ApiError,
|
|
372
|
+
ApiSuccess,
|
|
389
373
|
createEmailLog,
|
|
390
374
|
createId,
|
|
391
375
|
createIdCustom,
|