-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
220 lines (187 loc) · 7.18 KB
/
schema.ts
File metadata and controls
220 lines (187 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { Scope } from './types'
import type { FieldConfig, GroupConfig, ActionConfig, SchemaProvide, FieldProxy, ScopeValue, ComponentContract, FormContract, TableContract, PaginateParams, PaginatedResult } from './types'
import type { FieldDefinition } from "./fields"
import type { GroupDefinition } from './group'
import type { ActionDefinition } from './action'
type InferState<F extends Record<string, FieldDefinition>> = {
[K in keyof F]: F[K] extends FieldDefinition<infer T> ? T : unknown
}
export type EventContext<F extends Record<string, FieldDefinition>> = {
state: InferState<F>
schema: { [K in keyof F]: FieldProxy }
}
type EventHandler<F extends Record<string, FieldDefinition>> =
(context: EventContext<F>) => void
type SchemaEvents<F extends Record<string, FieldDefinition>> = {
[K in keyof F]?: Record<string, EventHandler<F>>
}
type BaseHandlerContext<BF extends Record<string, FieldDefinition>> = {
state: InferState<BF> & Record<string, unknown>
component: ComponentContract
form?: FormContract
table?: TableContract
}
type BaseActionHandler<BF extends Record<string, FieldDefinition>> =
(context: BaseHandlerContext<BF>) => void | Promise<void>
type ActionContext<F extends Record<string, FieldDefinition>> = {
state: InferState<F>
component: ComponentContract
form?: FormContract
table?: TableContract
}
type ActionHandler<F extends Record<string, FieldDefinition>> =
(context: ActionContext<F>) => void | Promise<void>
type SchemaHandlers<F extends Record<string, FieldDefinition>> =
Record<string, ActionHandler<F>>
type HookBootstrapContext<F extends Record<string, FieldDefinition>> = {
context: Record<string, unknown>
schema: { [K in keyof F]: FieldProxy }
component: ComponentContract
}
type HookBootstrapFn<F extends Record<string, FieldDefinition>> =
(ctx: HookBootstrapContext<F>) => void | Promise<void>
type HookFetchContext =
| { type: 'record'; context: Record<string, unknown>; params: PaginateParams; hydrate(data: Record<string, unknown>): void; component: ComponentContract }
| { type: 'collection'; context: Record<string, unknown>; params: PaginateParams; hydrate(result: PaginatedResult<Record<string, unknown>>): void; component: ComponentContract }
type HookFetchFn = (ctx: HookFetchContext) => void | Promise<void>
type SchemaHooks<F extends Record<string, FieldDefinition>> = {
bootstrap?: Partial<Record<ScopeValue, HookBootstrapFn<F>>>
fetch?: Partial<Record<ScopeValue, HookFetchFn>>
}
export interface BaseSchemaConfig<BF extends Record<string, FieldDefinition> = Record<string, never>> {
identity?: string | string[]
display?: string | ((record: Record<string, unknown>) => string)
scopes?: ScopeValue[]
groups?: Record<string, GroupDefinition>
fields?: BF
actions?: Record<string, ActionDefinition>
handlers?: Record<string, BaseActionHandler<BF>>
}
export type ActionEntry = ActionDefinition | null
export interface SchemaOptions<F extends Record<string, FieldDefinition>> {
identity?: string | string[]
display?: string | ((record: Record<string, unknown>) => string)
scopes?: ScopeValue[]
groups?: Record<string, GroupDefinition>
fields: F
actions?: Record<string, ActionEntry>
}
export class SchemaDefinition<F extends Record<string, FieldDefinition>> {
constructor(
public readonly domain: string,
private options: SchemaOptions<F>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private baseHandlers: Record<string, (...args: any[]) => any> = {},
) {}
get identity(): string | string[] {
return this.options.identity!
}
get display() {
return this.options.display
}
get scopes(): ScopeValue[] {
return this.options.scopes ?? Object.values(Scope)
}
getFields(): Record<string, FieldConfig> {
const result: Record<string, FieldConfig> = {}
for (const [key, def] of Object.entries(this.options.fields)) {
result[key] = def.toConfig()
}
return result
}
getGroups(): Record<string, GroupConfig> {
if (!this.options.groups) return {}
const result: Record<string, GroupConfig> = {}
for (const [key, def] of Object.entries(this.options.groups)) {
result[key] = def.toConfig()
}
return result
}
getActions(): Record<string, ActionConfig> {
if (!this.options.actions) return {}
const result: Record<string, ActionConfig> = {}
for (const [key, def] of Object.entries(this.options.actions)) {
if (def !== null) {
result[key] = def.toConfig()
}
}
return result
}
extend<U extends Record<string, FieldDefinition>>(
domain: string,
extra: Partial<SchemaOptions<U>>,
): SchemaDefinition<F & U> {
const merged: SchemaOptions<F & U> = {
identity: extra.identity ?? this.options.identity,
display: extra.display ?? this.options.display,
groups: { ...this.options.groups, ...extra.groups },
fields: { ...this.options.fields, ...extra.fields } as F & U,
actions: { ...this.options.actions, ...extra.actions },
}
return new SchemaDefinition(domain, merged, this.baseHandlers)
}
pick<K extends keyof F>(...keys: K[]): SchemaDefinition<Pick<F, K>> {
const fields = {} as Pick<F, K>
for (const key of keys) {
fields[key] = this.options.fields[key]
}
return new SchemaDefinition(this.domain, { ...this.options, fields }, this.baseHandlers)
}
omit<K extends keyof F>(...keys: K[]): SchemaDefinition<Omit<F, K>> {
const fields = { ...this.options.fields }
for (const key of keys) {
delete fields[key]
}
return new SchemaDefinition(this.domain, { ...this.options, fields: fields as Omit<F, K> }, this.baseHandlers)
}
events(bindings: SchemaEvents<F>): SchemaEvents<F> {
return bindings
}
handlers(bindings: SchemaHandlers<F>): SchemaHandlers<F> {
return { ...this.baseHandlers, ...bindings } as SchemaHandlers<F>
}
hooks(bindings: SchemaHooks<F>): SchemaHooks<F> {
return bindings
}
provide(): SchemaProvide {
return {
domain: this.domain,
identity: this.identity,
display: this.display,
scopes: this.scopes,
groups: this.getGroups(),
fields: this.getFields(),
actions: this.getActions(),
}
}
}
export interface SchemaFactory<BF extends Record<string, FieldDefinition> = Record<string, never>> {
create<F extends Record<string, FieldDefinition>>(
domain: string,
options: SchemaOptions<F>,
): SchemaDefinition<BF & F>
}
export function configure<BF extends Record<string, FieldDefinition>>(
base: BaseSchemaConfig<BF>,
): SchemaFactory<BF> {
return {
create<F extends Record<string, FieldDefinition>>(
domain: string,
options: SchemaOptions<F>,
): SchemaDefinition<BF & F> {
const mergedActions: Record<string, ActionEntry> = {
...base.actions,
...options.actions,
}
const merged: SchemaOptions<BF & F> = {
identity: options.identity ?? base.identity,
display: options.display ?? base.display,
scopes: options.scopes ?? base.scopes,
groups: { ...base.groups, ...options.groups },
fields: { ...base.fields, ...options.fields } as BF & F,
actions: mergedActions,
}
return new SchemaDefinition(domain, merged, base.handlers ?? {})
},
}
}