@pyreon/feature 0.0.1

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/src/types.ts ADDED
@@ -0,0 +1,159 @@
1
+ import type { Signal, Computed } from '@pyreon/reactivity'
2
+ import type { FormState, SchemaValidateFn } from '@pyreon/form'
3
+ import type { UseQueryResult, UseMutationResult } from '@pyreon/query'
4
+ import type { QueryKey } from '@pyreon/query'
5
+ import type { SortingState } from '@pyreon/table'
6
+ import type { StoreApi } from '@pyreon/store'
7
+ import type { FieldInfo } from './schema'
8
+
9
+ /**
10
+ * Configuration for defining a feature.
11
+ */
12
+ export interface FeatureConfig<TValues extends Record<string, unknown>> {
13
+ /** Unique feature name — used for store ID and query key namespace. */
14
+ name: string
15
+ /** Validation schema (Zod, Valibot, or ArkType). Duck-typed — must have `safeParseAsync` for auto-validation. */
16
+ schema: unknown
17
+ /** Custom schema-level validation function. If provided, overrides auto-detection from schema. */
18
+ validate?: SchemaValidateFn<TValues>
19
+ /** API base path (e.g., '/api/users'). */
20
+ api: string
21
+ /** Default initial values for create forms. If not provided, auto-generated from schema field types. */
22
+ initialValues?: Partial<TValues>
23
+ /** Custom fetch function. Defaults to global fetch. */
24
+ fetcher?: typeof fetch
25
+ }
26
+
27
+ /**
28
+ * Query options for useList.
29
+ */
30
+ export interface ListOptions {
31
+ /** Additional query parameters appended to the URL. */
32
+ params?: Record<string, string | number | boolean>
33
+ /** Reactive page number. When provided, `page` and `pageSize` are appended to query params. */
34
+ page?: number | Signal<number>
35
+ /** Items per page. Defaults to 20 when `page` is provided. */
36
+ pageSize?: number
37
+ /** Override stale time for this query. */
38
+ staleTime?: number
39
+ /** Enable/disable the query. */
40
+ enabled?: boolean
41
+ }
42
+
43
+ /**
44
+ * Form options for useForm.
45
+ */
46
+ export interface FeatureFormOptions<TValues extends Record<string, unknown>> {
47
+ /** 'create' (default) or 'edit'. Edit mode uses PUT instead of POST. */
48
+ mode?: 'create' | 'edit'
49
+ /** Item ID — required when mode is 'edit'. Used to PUT to api/:id and auto-fetch data. */
50
+ id?: string | number
51
+ /** Override initial values (merged with feature defaults). */
52
+ initialValues?: Partial<TValues>
53
+ /** When to validate: 'blur' (default), 'change', or 'submit'. */
54
+ validateOn?: 'blur' | 'change' | 'submit'
55
+ /** Callback after successful create/update. */
56
+ onSuccess?: (result: unknown) => void
57
+ /** Callback on submit error. */
58
+ onError?: (error: unknown) => void
59
+ }
60
+
61
+ /**
62
+ * Table options for useTable.
63
+ */
64
+ export interface FeatureTableOptions<TValues extends Record<string, unknown>> {
65
+ /** Subset of schema fields to show as columns. If not provided, all fields are shown. */
66
+ columns?: (keyof TValues & string)[]
67
+ /** Per-column overrides (header text, cell renderer, size, etc.). */
68
+ columnOverrides?: Partial<
69
+ Record<keyof TValues & string, Record<string, unknown>>
70
+ >
71
+ /** Page size for pagination. If not provided, pagination is disabled. */
72
+ pageSize?: number
73
+ }
74
+
75
+ /**
76
+ * Return type of feature.useTable().
77
+ */
78
+ export interface FeatureTableResult<TValues extends Record<string, unknown>> {
79
+ /** The reactive TanStack Table instance. */
80
+ table: Computed<import('@pyreon/table').Table<TValues>>
81
+ /** Sorting state signal — bind to UI controls. */
82
+ sorting: Signal<SortingState>
83
+ /** Global filter signal — bind to search input. */
84
+ globalFilter: Signal<string>
85
+ /** Column metadata from schema introspection. */
86
+ columns: FieldInfo[]
87
+ }
88
+
89
+ /**
90
+ * Reactive store for a feature's cached data.
91
+ */
92
+ export interface FeatureStore<TValues extends Record<string, unknown>> {
93
+ /** Cached list of items. */
94
+ items: Signal<TValues[]>
95
+ /** Currently selected item. */
96
+ selected: Signal<TValues | null>
97
+ /** Loading state. */
98
+ loading: Signal<boolean>
99
+ /** Set the selected item by ID (finds from items list). */
100
+ select: (id: string | number) => void
101
+ /** Clear the current selection. */
102
+ clear: () => void
103
+ /** Index signature for Record<string, unknown> compatibility. */
104
+ [key: string]: unknown
105
+ }
106
+
107
+ /**
108
+ * The feature object returned by defineFeature().
109
+ */
110
+ export interface Feature<TValues extends Record<string, unknown>> {
111
+ /** Feature name. */
112
+ name: string
113
+ /** API base path. */
114
+ api: string
115
+ /** The schema passed to defineFeature. */
116
+ schema: unknown
117
+ /** Introspected field information from the schema. */
118
+ fields: FieldInfo[]
119
+
120
+ /** Fetch a paginated/filtered list. */
121
+ useList: (options?: ListOptions) => UseQueryResult<TValues[], unknown>
122
+
123
+ /** Fetch a single item by ID. */
124
+ useById: (id: string | number) => UseQueryResult<TValues, unknown>
125
+
126
+ /** Search with a reactive signal term. */
127
+ useSearch: (
128
+ searchTerm: Signal<string>,
129
+ options?: ListOptions,
130
+ ) => UseQueryResult<TValues[], unknown>
131
+
132
+ /** Create mutation — POST to api. */
133
+ useCreate: () => UseMutationResult<TValues, unknown, Partial<TValues>>
134
+
135
+ /** Update mutation — PUT to api/:id with optimistic updates. */
136
+ useUpdate: () => UseMutationResult<
137
+ TValues,
138
+ unknown,
139
+ { id: string | number; data: Partial<TValues> }
140
+ >
141
+
142
+ /** Delete mutation — DELETE to api/:id. */
143
+ useDelete: () => UseMutationResult<void, unknown, string | number>
144
+
145
+ /** Create a form pre-wired with schema validation and API submit. In edit mode with an ID, auto-fetches data. */
146
+ useForm: (options?: FeatureFormOptions<TValues>) => FormState<TValues>
147
+
148
+ /** Create a reactive table with columns inferred from schema. */
149
+ useTable: (
150
+ data: TValues[] | (() => TValues[]),
151
+ options?: FeatureTableOptions<TValues>,
152
+ ) => FeatureTableResult<TValues>
153
+
154
+ /** Reactive store for cached items, selection, and loading state. */
155
+ useStore: () => StoreApi<FeatureStore<TValues>>
156
+
157
+ /** Generate namespaced query keys. */
158
+ queryKey: (suffix?: string | number) => QueryKey
159
+ }