@stacksjs/orm 0.72.56 → 0.72.60

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.
@@ -0,0 +1,82 @@
1
+ import type { IndexPaginator } from './auto-crud';
2
+ import type { ModelCreateData, ModelRow } from './model-types';
3
+ /** The listing envelope: rows, the flat paginator, and the deprecated `meta`. */
4
+ export declare interface ApiIndexResponse<TRow> extends IndexPaginator {
5
+ data: TRow[]
6
+ meta: Record<string, unknown>
7
+ }
8
+ /** The single-row envelope, used by show, store and update alike. */
9
+ export declare interface ApiItemResponse<TRow> {
10
+ data: TRow
11
+ }
12
+ /** The definition behind a model, whether it was passed bare or via `defineModel`. */
13
+ declare type Definition<M> = M extends { attributes: unknown } ? M : never;
14
+ declare type ApiTrait<M> = Definition<M> extends { traits?: { useApi?: infer A } } ? A : never;
15
+ /** `useApi: { uri: 'products' }` → `'products'`, else the table, else name + "s". */
16
+ declare type ApiUri<M> = ApiTrait<M> extends { uri: infer U extends string }
17
+ ? U
18
+ : (Definition<M> extends { table: infer T extends string }
19
+ ? T
20
+ : (Definition<M> extends { name: infer N extends string } ? `${Lowercase<N>}s` : never));
21
+ declare type ApiPrefix<M> = ApiTrait<M> extends { prefix: infer P extends string } ? `${P}/` : '';
22
+ /** `/api/{prefix/}{uri}` — the same shape `apiBasePath()` builds. */
23
+ export type ApiBasePathFor<M> = `/api/${ApiPrefix<M>}${ApiUri<M>}`;
24
+ declare type DefaultRoutes = 'index' | 'show' | 'store' | 'update' | 'destroy';
25
+ /** Which of the CRUD verbs this model publishes. */
26
+ declare type EnabledRoutes<M> = ApiTrait<M> extends { routes: infer R extends readonly string[] }
27
+ ? R[number]
28
+ : DefaultRoutes;
29
+ declare type Attributes<M> = Definition<M> extends { attributes: infer A } ? A : never;
30
+ /** Attributes flagged `hidden: true`, which never appear in a response. */
31
+ declare type HiddenKeys<M> = {
32
+ [K in keyof Attributes<M>]: Attributes<M>[K] extends { hidden: true } ? K : never
33
+ }[keyof Attributes<M>];
34
+ /** A row as the generated endpoints actually return it: hidden fields removed. */
35
+ export type ApiRow<M> = Omit<ModelRow<M>, Extract<HiddenKeys<M>, keyof ModelRow<M>>>;
36
+ /** What a write accepts: fillable fields, minus anything hidden. */
37
+ export type ApiWriteData<M> = Omit<ModelCreateData<M>, Extract<HiddenKeys<M>, keyof ModelCreateData<M>>>;
38
+ declare type Params<TId extends string = 'id'> = { [K in TId]: string }
39
+ declare type IndexRoute<M, TBase extends string> = 'index' extends EnabledRoutes<M>
40
+ ? { [K in `GET ${TBase}`]: { input: Record<string, never>, output: ApiIndexResponse<ApiRow<M>>, params: Record<string, never> } }
41
+ : Record<string, never>;
42
+ declare type ShowRoute<M, TBase extends string> = 'show' extends EnabledRoutes<M>
43
+ ? { [K in `GET ${TBase}/{id}`]: { input: Record<string, never>, output: ApiItemResponse<ApiRow<M>>, params: Params } }
44
+ : Record<string, never>;
45
+ declare type StoreRoute<M, TBase extends string> = 'store' extends EnabledRoutes<M>
46
+ ? { [K in `POST ${TBase}`]: { input: ApiWriteData<M>, output: ApiItemResponse<ApiRow<M>>, params: Record<string, never> } }
47
+ : Record<string, never>;
48
+ declare type UpdateRoute<M, TBase extends string> = 'update' extends EnabledRoutes<M>
49
+ ? {
50
+ [K in `PUT ${TBase}/{id}` | `PATCH ${TBase}/{id}`]: {
51
+ input: ApiWriteData<M>
52
+ output: ApiItemResponse<ApiRow<M>>
53
+ params: Params
54
+ }
55
+ }
56
+ : Record<string, never>;
57
+ declare type DestroyRoute<M, TBase extends string> = 'destroy' extends EnabledRoutes<M>
58
+ ? {
59
+ [K in `DELETE ${TBase}/{id}`]: { input: Record<string, never>, output: undefined, params: Params }
60
+ } & {
61
+ [K in `POST ${TBase}/bulk-delete`]: {
62
+ input: { ids: Array<number | string> }
63
+ output: { message: string }
64
+ params: Record<string, never>
65
+ }
66
+ }
67
+ : Record<string, never>;
68
+ /**
69
+ * Every endpoint `useApi` publishes for this model, as a route map the typed
70
+ * client understands.
71
+ *
72
+ * Intersect several of them - one per model - to describe a whole generated
73
+ * API, and intersect that with `RoutesOf<typeof api>` to describe an app that
74
+ * has both generated and hand-written routes.
75
+ */
76
+ export type ApiRoutesFor<M> = ApiBasePathFor<M> extends infer TBase extends string
77
+ ? IndexRoute<M, TBase>
78
+ & ShowRoute<M, TBase>
79
+ & StoreRoute<M, TBase>
80
+ & UpdateRoute<M, TBase>
81
+ & DestroyRoute<M, TBase>
82
+ : never;
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export type { PrunableOptions } from './utils/prunable';
3
3
  export type { AuditHelpers } from './traits/audit';
4
4
  // Re-export soft-delete option types so user code can `satisfies SoftDeleteOptions`.
5
5
  export type { SoftDeleteOptions, SoftDeleteHelpers } from './traits/soft-deletes';
6
+ export type * from './api-routes';
6
7
  export type { GenerateSchemaOptions, GenerateSchemaResult } from './generate-database-schema';
7
8
  export type { CursorPaginator, Paginator, SimplePaginator } from './paginator';
8
9
  export type { ResolvedPageArgs } from './paginator-request';