adorn-api 1.1.4 → 1.1.6

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.
@@ -1,14 +1,15 @@
1
-
1
+
2
2
  import type { BelongsToReference, HasManyCollection, HasOneReference, ManyToManyCollection } from "metal-orm";
3
3
  import type { DtoOptions, ErrorResponseOptions, FieldOverride } from "../../core/decorators";
4
4
  import type { SchemaNode } from "../../core/schema";
5
5
  import type { DtoConstructor } from "../../core/types";
6
-
7
- /**
8
- * Metal ORM DTO modes.
9
- */
10
- export type MetalDtoMode = "response" | "create" | "update";
11
-
6
+ import type { RequestContext } from "../express/types";
7
+
8
+ /**
9
+ * Metal ORM DTO modes.
10
+ */
11
+ export type MetalDtoMode = "response" | "create" | "update";
12
+
12
13
  /**
13
14
  * Options for Metal ORM DTOs.
14
15
  * @extends DtoOptions
@@ -25,47 +26,47 @@ export interface MetalDtoOptions extends DtoOptions {
25
26
  /** Whether to throw errors instead of warnings for invalid metadata (default: false) */
26
27
  strict?: boolean;
27
28
  }
28
-
29
- /**
30
- * Metal ORM DTO target type.
31
- */
32
- export type MetalDtoTarget = Parameters<typeof import("metal-orm").getColumnMap>[0];
33
-
34
- /**
35
- * Pagination configuration.
36
- */
37
- export interface PaginationConfig {
38
- /** Default page size */
39
- defaultPageSize?: number;
40
- /** Maximum page size */
41
- maxPageSize?: number;
42
- }
43
-
44
- /**
45
- * Pagination parsing options.
46
- */
47
- export interface PaginationOptions {
48
- /** Minimum value */
49
- min?: number;
50
- /** Maximum value */
51
- max?: number;
52
- /** Whether to clamp values */
53
- clamp?: boolean;
54
- }
55
-
56
- /**
57
- * Parsed pagination result.
58
- */
59
- export interface ParsedPagination {
60
- /** Page number */
61
- page: number;
62
- /** Page size */
63
- pageSize: number;
64
- }
65
-
66
- /**
67
- * Filter field mapping.
68
- */
29
+
30
+ /**
31
+ * Metal ORM DTO target type.
32
+ */
33
+ export type MetalDtoTarget = Parameters<typeof import("metal-orm").getColumnMap>[0];
34
+
35
+ /**
36
+ * Pagination configuration.
37
+ */
38
+ export interface PaginationConfig {
39
+ /** Default page size */
40
+ defaultPageSize?: number;
41
+ /** Maximum page size */
42
+ maxPageSize?: number;
43
+ }
44
+
45
+ /**
46
+ * Pagination parsing options.
47
+ */
48
+ export interface PaginationOptions {
49
+ /** Minimum value */
50
+ min?: number;
51
+ /** Maximum value */
52
+ max?: number;
53
+ /** Whether to clamp values */
54
+ clamp?: boolean;
55
+ }
56
+
57
+ /**
58
+ * Parsed pagination result.
59
+ */
60
+ export interface ParsedPagination {
61
+ /** Page number */
62
+ page: number;
63
+ /** Page size */
64
+ pageSize: number;
65
+ }
66
+
67
+ /**
68
+ * Filter field mapping.
69
+ */
69
70
  export interface FilterFieldMapping {
70
71
  /** Maps query keys to field names */
71
72
  [queryKey: string]: string;
@@ -118,20 +119,20 @@ export type FilterFieldPathArray<T> = FilterFieldPathSegments<T>;
118
119
  * Supported filter field input types.
119
120
  */
120
121
  export type FilterFieldInput<T> = FilterFieldPath<T> | FilterFieldPathArray<T>;
121
-
122
- /**
123
- * Filter mapping configuration.
124
- */
122
+
123
+ /**
124
+ * Filter mapping configuration.
125
+ */
125
126
  export interface FilterMapping<T = Record<string, unknown>> {
126
127
  /** Field name */
127
128
  field: FilterFieldInput<T>;
128
129
  /** Filter operator */
129
130
  operator: FilterOperator;
130
131
  }
131
-
132
- /**
133
- * Options for parsing filters.
134
- */
132
+
133
+ /**
134
+ * Options for parsing filters.
135
+ */
135
136
  export interface ParseFilterOptions<T = Record<string, unknown>> {
136
137
  /** Query parameters */
137
138
  query?: Record<string, unknown>;
@@ -156,6 +157,8 @@ export interface ParseSortOptions<T = Record<string, unknown>> {
156
157
  sortByKey?: string;
157
158
  /** Sort direction query key */
158
159
  sortDirectionKey?: string;
160
+ /** Query key for legacy sort order param (default: "sortOrder") */
161
+ sortOrderKey?: string;
159
162
  /** Default sort field */
160
163
  defaultSortBy?: string;
161
164
  /** Default sort direction */
@@ -174,6 +177,30 @@ export interface ParsedSort<T = Record<string, unknown>> {
174
177
  field?: FilterFieldInput<T>;
175
178
  }
176
179
 
180
+ /**
181
+ * Ready-to-use list/query configuration extracted from CRUD DTO class generation.
182
+ * Eliminates the need for consumers to reassemble filter/sort/pagination config
183
+ * in their service or repository layer.
184
+ */
185
+ export interface ListConfig<T = Record<string, unknown>> {
186
+ /** Execution-ready filter mappings for parseFilter */
187
+ filterMappings: Record<string, FilterMapping<T>>;
188
+ /** Execution-ready sortable column mappings for parseSort */
189
+ sortableColumns: MetalCrudSortableColumns<T>;
190
+ /** Default sort field key */
191
+ defaultSortBy?: string;
192
+ /** Default sort direction */
193
+ defaultSortDirection: SortDirection;
194
+ /** Default page size */
195
+ defaultPageSize: number;
196
+ /** Maximum page size */
197
+ maxPageSize: number;
198
+ /** Sort field query key */
199
+ sortByKey: string;
200
+ /** Sort direction query key */
201
+ sortDirectionKey: string;
202
+ }
203
+
177
204
  /**
178
205
  * Filter operator.
179
206
  */
@@ -211,34 +238,34 @@ export type Filter<T, K extends keyof T> = {
211
238
  mode?: "default" | "insensitive";
212
239
  };
213
240
  };
214
-
215
- /**
216
- * Options for paged query DTOs.
217
- */
218
- export interface PagedQueryDtoOptions {
219
- /** Default page size */
220
- defaultPageSize?: number;
221
- /** Maximum page size */
222
- maxPageSize?: number;
223
- /** DTO name */
224
- name?: string;
225
- }
226
-
227
- /**
228
- * Options for paged response DTOs.
229
- */
230
- export interface PagedResponseDtoOptions {
231
- /** Item DTO constructor */
232
- itemDto: DtoConstructor;
233
- /** DTO description */
234
- description?: string;
235
- /** DTO name */
236
- name?: string;
237
- }
238
-
239
- /**
240
- * Filter field definition.
241
- */
241
+
242
+ /**
243
+ * Options for paged query DTOs.
244
+ */
245
+ export interface PagedQueryDtoOptions {
246
+ /** Default page size */
247
+ defaultPageSize?: number;
248
+ /** Maximum page size */
249
+ maxPageSize?: number;
250
+ /** DTO name */
251
+ name?: string;
252
+ }
253
+
254
+ /**
255
+ * Options for paged response DTOs.
256
+ */
257
+ export interface PagedResponseDtoOptions {
258
+ /** Item DTO constructor */
259
+ itemDto: DtoConstructor;
260
+ /** DTO description */
261
+ description?: string;
262
+ /** DTO name */
263
+ name?: string;
264
+ }
265
+
266
+ /**
267
+ * Filter field definition.
268
+ */
242
269
  export interface FilterFieldDef {
243
270
  /** Field schema */
244
271
  schema?: SchemaNode;
@@ -320,18 +347,18 @@ export interface MetalCrudStandardErrorsOptions {
320
347
  /** 404 not found error config (set false to disable) */
321
348
  notFound?: false | ErrorResponseOptions;
322
349
  }
323
-
324
- /**
325
- * Options for paged filter query DTOs.
326
- * @extends PagedQueryDtoOptions
327
- */
328
- export interface PagedFilterQueryDtoOptions extends PagedQueryDtoOptions {
329
- /** Filter definitions */
330
- filters: Record<string, FilterFieldDef>;
331
- /** DTO name */
332
- name?: string;
333
- }
334
-
350
+
351
+ /**
352
+ * Options for paged filter query DTOs.
353
+ * @extends PagedQueryDtoOptions
354
+ */
355
+ export interface PagedFilterQueryDtoOptions extends PagedQueryDtoOptions {
356
+ /** Filter definitions */
357
+ filters: Record<string, FilterFieldDef>;
358
+ /** DTO name */
359
+ name?: string;
360
+ }
361
+
335
362
  /**
336
363
  * Options for Metal CRUD DTOs.
337
364
  */
@@ -361,26 +388,26 @@ export interface MetalCrudDtoOptions<T = Record<string, unknown>> {
361
388
  /** Whether to throw errors instead of warnings for invalid metadata (default: false) */
362
389
  strict?: boolean;
363
390
  }
364
-
365
- /**
366
- * Metal CRUD DTO decorators.
367
- */
368
- export interface MetalCrudDtoDecorators {
369
- /** Response DTO decorator */
370
- response: (target: DtoConstructor) => void;
371
- /** Create DTO decorator */
372
- create: (target: DtoConstructor) => void;
373
- /** Replace DTO decorator */
374
- replace: (target: DtoConstructor) => void;
375
- /** Update DTO decorator */
376
- update: (target: DtoConstructor) => void;
377
- /** Params DTO decorator */
378
- params: (target: DtoConstructor) => void;
379
- }
380
-
381
- /**
382
- * Metal CRUD DTO class names.
383
- */
391
+
392
+ /**
393
+ * Metal CRUD DTO decorators.
394
+ */
395
+ export interface MetalCrudDtoDecorators {
396
+ /** Response DTO decorator */
397
+ response: (target: DtoConstructor) => void;
398
+ /** Create DTO decorator */
399
+ create: (target: DtoConstructor) => void;
400
+ /** Replace DTO decorator */
401
+ replace: (target: DtoConstructor) => void;
402
+ /** Update DTO decorator */
403
+ update: (target: DtoConstructor) => void;
404
+ /** Params DTO decorator */
405
+ params: (target: DtoConstructor) => void;
406
+ }
407
+
408
+ /**
409
+ * Metal CRUD DTO class names.
410
+ */
384
411
  export type RouteErrorsDecorator = (
385
412
  value: unknown,
386
413
  context: ClassMethodDecoratorContext
@@ -401,7 +428,7 @@ export type MetalCrudDtoClassNameKey =
401
428
  * Metal CRUD DTO class names.
402
429
  */
403
430
  export type MetalCrudDtoClassNames = Partial<Record<MetalCrudDtoClassNameKey, string>>;
404
-
431
+
405
432
  /**
406
433
  * Options for Metal CRUD DTO classes.
407
434
  * @extends MetalCrudDtoOptions
@@ -414,19 +441,19 @@ export interface MetalCrudDtoClassOptions<T = Record<string, unknown>> extends M
414
441
  /** Whether to throw errors instead of warnings for invalid metadata (default: false) */
415
442
  strict?: boolean;
416
443
  }
417
-
418
- /**
419
- * Metal CRUD DTO classes.
420
- */
444
+
445
+ /**
446
+ * Metal CRUD DTO classes.
447
+ */
421
448
  export interface MetalCrudDtoClasses<T = Record<string, unknown>> {
422
449
  /** Response DTO class */
423
450
  response: DtoConstructor;
424
451
  /** Create DTO class */
425
452
  create: DtoConstructor;
426
- /** Replace DTO class */
427
- replace: DtoConstructor;
428
- /** Update DTO class */
429
- update: DtoConstructor;
453
+ /** Replace DTO class */
454
+ replace: DtoConstructor;
455
+ /** Update DTO class */
456
+ update: DtoConstructor;
430
457
  /** Params DTO class */
431
458
  params: DtoConstructor;
432
459
  /** Query DTO class (paged + filters + sort) */
@@ -445,6 +472,88 @@ export interface MetalCrudDtoClasses<T = Record<string, unknown>> {
445
472
  filterMappings: Record<string, FilterMapping<T>>;
446
473
  /** Execution-ready sortable column mappings */
447
474
  sortableColumns: MetalCrudSortableColumns<T>;
475
+ /** Ready-to-use config for list/query endpoints (combines filters, sort, pagination defaults) */
476
+ listConfig: ListConfig<T>;
477
+ }
478
+
479
+ /**
480
+ * Awaitable helper for CRUD service methods.
481
+ */
482
+ export type Awaitable<T> = T | Promise<T>;
483
+
484
+ /**
485
+ * Input for CRUD controller service: class or ready instance.
486
+ */
487
+ export type CrudControllerServiceInput<TDtos extends MetalCrudDtoClasses<any>> =
488
+ CrudControllerService<TDtos>
489
+ | (new () => CrudControllerService<TDtos>);
490
+
491
+ /**
492
+ * CRUD controller service contract used by createCrudController.
493
+ */
494
+ export interface CrudControllerService<TDtos extends MetalCrudDtoClasses<any>> {
495
+ list(
496
+ ctx: RequestContext<unknown, InstanceType<TDtos["queryDto"]>>
497
+ ): Awaitable<InstanceType<TDtos["pagedResponseDto"]>>;
498
+ options?(
499
+ ctx: RequestContext<unknown, InstanceType<TDtos["optionsQueryDto"]>>
500
+ ): Awaitable<InstanceType<TDtos["optionsDto"]>>;
501
+ getById(
502
+ id: number,
503
+ ctx: RequestContext<unknown, undefined, InstanceType<TDtos["params"]>>
504
+ ): Awaitable<InstanceType<TDtos["response"]>>;
505
+ create(
506
+ body: InstanceType<TDtos["create"]>,
507
+ ctx: RequestContext<InstanceType<TDtos["create"]>>
508
+ ): Awaitable<InstanceType<TDtos["response"]>>;
509
+ replace?(
510
+ id: number,
511
+ body: InstanceType<TDtos["replace"]>,
512
+ ctx: RequestContext<
513
+ InstanceType<TDtos["replace"]>,
514
+ undefined,
515
+ InstanceType<TDtos["params"]>
516
+ >
517
+ ): Awaitable<InstanceType<TDtos["response"]>>;
518
+ update?(
519
+ id: number,
520
+ body: InstanceType<TDtos["update"]>,
521
+ ctx: RequestContext<
522
+ InstanceType<TDtos["update"]>,
523
+ undefined,
524
+ InstanceType<TDtos["params"]>
525
+ >
526
+ ): Awaitable<InstanceType<TDtos["response"]>>;
527
+ delete?(
528
+ id: number,
529
+ ctx: RequestContext<unknown, undefined, InstanceType<TDtos["params"]>>
530
+ ): Awaitable<void>;
531
+ }
532
+
533
+ /**
534
+ * createCrudController options.
535
+ */
536
+ export interface CreateCrudControllerOptions<
537
+ TDtos extends MetalCrudDtoClasses<any>
538
+ > {
539
+ /** Controller path. */
540
+ path: string;
541
+ /** Service instance or class (new () => service). */
542
+ service: CrudControllerServiceInput<TDtos>;
543
+ /** DTO bundle produced by createMetalCrudDtoClasses. */
544
+ dtos: TDtos;
545
+ /** Entity label used by parseIdOrThrow messages. */
546
+ entityName: string;
547
+ /** Generate GET /options route (default: true). */
548
+ withOptionsRoute?: boolean;
549
+ /** Generate PUT /:id route (default: true). */
550
+ withReplace?: boolean;
551
+ /** Generate PATCH /:id route (default: true). */
552
+ withPatch?: boolean;
553
+ /** Generate DELETE /:id route (default: true). */
554
+ withDelete?: boolean;
555
+ /** Optional OpenAPI tags for generated controller. */
556
+ tags?: string[];
448
557
  }
449
558
 
450
559
  /**
@@ -518,25 +627,25 @@ export interface MetalTreeDtoClasses {
518
627
  * @extends MetalDtoOptions
519
628
  */
520
629
  export interface NestedCreateDtoOptions extends MetalDtoOptions {
521
- /** Additional fields to exclude */
522
- additionalExclude?: string[];
523
- /** DTO name */
524
- name: string;
525
- /** Parent entity name */
526
- parentEntity?: string;
527
- }
528
-
529
- /**
530
- * Options for error DTOs.
531
- */
532
- export interface ErrorDtoOptions {
533
- /** Whether to include error details */
534
- withDetails?: boolean;
535
- /** Whether to include trace ID */
536
- includeTraceId?: boolean;
537
- }
538
-
539
- /**
540
- * Function type for creating ORM sessions.
541
- */
542
- export type CreateSessionFn = () => import("metal-orm").OrmSession;
630
+ /** Additional fields to exclude */
631
+ additionalExclude?: string[];
632
+ /** DTO name */
633
+ name: string;
634
+ /** Parent entity name */
635
+ parentEntity?: string;
636
+ }
637
+
638
+ /**
639
+ * Options for error DTOs.
640
+ */
641
+ export interface ErrorDtoOptions {
642
+ /** Whether to include error details */
643
+ withDetails?: boolean;
644
+ /** Whether to include trace ID */
645
+ includeTraceId?: boolean;
646
+ }
647
+
648
+ /**
649
+ * Function type for creating ORM sessions.
650
+ */
651
+ export type CreateSessionFn = () => import("metal-orm").OrmSession;