nuxt-auto-crud 1.2.2 → 1.4.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.
Files changed (31) hide show
  1. package/README.md +189 -90
  2. package/dist/module.d.mts +49 -1
  3. package/dist/module.json +1 -1
  4. package/dist/module.mjs +24 -1
  5. package/dist/runtime/server/api/[model]/[id].delete.d.ts +1 -1
  6. package/dist/runtime/server/api/[model]/[id].delete.js +20 -2
  7. package/dist/runtime/server/api/[model]/[id].get.d.ts +1 -1
  8. package/dist/runtime/server/api/[model]/[id].get.js +21 -4
  9. package/dist/runtime/server/api/[model]/[id].patch.d.ts +1 -1
  10. package/dist/runtime/server/api/[model]/[id].patch.js +29 -5
  11. package/dist/runtime/server/api/[model]/index.get.js +25 -4
  12. package/dist/runtime/server/api/[model]/index.post.d.ts +1 -1
  13. package/dist/runtime/server/api/[model]/index.post.js +23 -8
  14. package/dist/runtime/server/utils/auth.d.ts +2 -0
  15. package/dist/runtime/server/utils/auth.js +39 -0
  16. package/dist/runtime/server/utils/config.d.ts +2 -0
  17. package/dist/runtime/server/utils/config.js +4 -0
  18. package/dist/runtime/server/utils/jwt.d.ts +2 -0
  19. package/dist/runtime/server/utils/jwt.js +19 -0
  20. package/dist/runtime/server/utils/modelMapper.d.ts +31 -0
  21. package/dist/runtime/server/utils/modelMapper.js +38 -0
  22. package/package.json +20 -10
  23. package/src/runtime/server/api/[model]/[id].delete.ts +29 -3
  24. package/src/runtime/server/api/[model]/[id].get.ts +29 -5
  25. package/src/runtime/server/api/[model]/[id].patch.ts +40 -9
  26. package/src/runtime/server/api/[model]/index.get.ts +33 -5
  27. package/src/runtime/server/api/[model]/index.post.ts +32 -15
  28. package/src/runtime/server/utils/auth.ts +55 -0
  29. package/src/runtime/server/utils/config.ts +6 -0
  30. package/src/runtime/server/utils/jwt.ts +23 -0
  31. package/src/runtime/server/utils/modelMapper.ts +83 -0
@@ -6,12 +6,18 @@ import { pascalCase } from 'scule'
6
6
  import { getTableColumns as getDrizzleTableColumns } from 'drizzle-orm'
7
7
  import type { SQLiteTable } from 'drizzle-orm/sqlite-core'
8
8
  import { createError } from 'h3'
9
+ import { useRuntimeConfig } from '#imports'
9
10
 
10
11
  /**
11
12
  * Fields that should never be updatable via PATCH requests
12
13
  */
13
14
  const PROTECTED_FIELDS = ['id', 'createdAt', 'created_at']
14
15
 
16
+ /**
17
+ * Fields that should never be returned in API responses
18
+ */
19
+ const HIDDEN_FIELDS = ['password', 'secret', 'token']
20
+
15
21
  /**
16
22
  * Custom updatable fields configuration (optional)
17
23
  * Only define here if you want to override the auto-detection
@@ -26,6 +32,14 @@ export const customUpdatableFields: Record<string, string[]> = {
26
32
  // By default, all fields except PROTECTED_FIELDS are updatable
27
33
  }
28
34
 
35
+ /**
36
+ * Custom hidden fields configuration (optional)
37
+ * Only define here if you want to override the default hidden fields
38
+ */
39
+ export const customHiddenFields: Record<string, string[]> = {
40
+ // Add custom hidden fields here if needed
41
+ }
42
+
29
43
  /**
30
44
  * Automatically builds a map of all exported tables from the schema
31
45
  * No manual configuration needed!
@@ -163,3 +177,72 @@ export function getModelPluralName(modelName: string): string {
163
177
  export function getAvailableModels(): string[] {
164
178
  return Object.keys(modelTableMap)
165
179
  }
180
+
181
+ /**
182
+ * Gets the hidden fields for a model
183
+ * @param modelName - The name of the model
184
+ * @returns Array of field names that should be hidden
185
+ */
186
+ export function getHiddenFields(modelName: string): string[] {
187
+ // Check if custom hidden fields are defined for this model
188
+ if (customHiddenFields[modelName]) {
189
+ return customHiddenFields[modelName]
190
+ }
191
+
192
+ return HIDDEN_FIELDS
193
+ }
194
+
195
+ /**
196
+ * Gets the public columns for a model
197
+ * @param modelName - The name of the model
198
+ * @returns Array of field names that are public (or undefined if all are public)
199
+ */
200
+ export function getPublicColumns(modelName: string): string[] | undefined {
201
+ const { resources } = useRuntimeConfig().autoCrud
202
+ return resources?.[modelName]?.publicColumns
203
+ }
204
+
205
+ /**
206
+ * Filters an object to only include public columns (if configured)
207
+ * @param modelName - The name of the model
208
+ * @param data - The data object to filter
209
+ * @returns Filtered object
210
+ */
211
+ export function filterPublicColumns(modelName: string, data: Record<string, unknown>): Record<string, unknown> {
212
+ const publicColumns = getPublicColumns(modelName)
213
+
214
+ // If no public columns configured, return all (except hidden)
215
+ if (!publicColumns) {
216
+ return filterHiddenFields(modelName, data)
217
+ }
218
+
219
+ const filtered: Record<string, unknown> = {}
220
+
221
+ for (const [key, value] of Object.entries(data)) {
222
+ // Must be in publicColumns AND not in hidden fields (double safety)
223
+ if (publicColumns.includes(key) && !getHiddenFields(modelName).includes(key)) {
224
+ filtered[key] = value
225
+ }
226
+ }
227
+
228
+ return filtered
229
+ }
230
+
231
+ /**
232
+ * Filters an object to exclude hidden fields
233
+ * @param modelName - The name of the model
234
+ * @param data - The data object to filter
235
+ * @returns Filtered object without hidden fields
236
+ */
237
+ export function filterHiddenFields(modelName: string, data: Record<string, unknown>): Record<string, unknown> {
238
+ const hiddenFields = getHiddenFields(modelName)
239
+ const filtered: Record<string, unknown> = {}
240
+
241
+ for (const [key, value] of Object.entries(data)) {
242
+ if (!hiddenFields.includes(key)) {
243
+ filtered[key] = value
244
+ }
245
+ }
246
+
247
+ return filtered
248
+ }