-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig.ts
More file actions
234 lines (211 loc) · 7.09 KB
/
config.ts
File metadata and controls
234 lines (211 loc) · 7.09 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
import * as R from 'remeda'
import type { Simplify } from 'type-fest'
import {
type AuthClient,
type AuthConfig,
type AuthHandlers,
createAuth,
getAuthClient,
} from './auth'
import {
type ClientCollection,
type Collection,
type ExtractAllCollectionCustomEndpoints,
type ExtractAllCollectionDefaultEndpoints,
getAllCollectionEndpoints,
type ToClientCollection,
type ToClientCollectionList,
} from './collection'
import {
type ApiRoute,
type ApiRouter,
type ClientApiRouter,
getClientEndpoint,
type ToClientApiRouteSchema,
type ToRecordApiRouteSchema,
} from './endpoint'
import type { Field, FieldClient, Fields, FieldsClient } from './field'
import type { KivotosPlugin, MergePlugins } from './plugins'
import { isRelationField } from './utils'
export type MinimalContext<
TFullSchema extends Record<string, unknown> = Record<string, unknown>,
TContext extends Record<string, unknown> = Record<string, unknown>,
> = Simplify<
TContext & {
db: NodePgDatabase<TFullSchema>
}
> & {}
export interface BaseConfigOptions<
TFullSchema extends Record<string, unknown> = Record<string, unknown>,
TContext extends Record<string, unknown> = Record<string, unknown>,
> {
db: NodePgDatabase<TFullSchema>
schema: TFullSchema
context?: TContext
auth: AuthConfig
}
export interface BaseConfig<
TFullSchema extends Record<string, unknown> = Record<string, unknown>,
TContext extends MinimalContext<TFullSchema> = MinimalContext<TFullSchema>,
> extends BaseConfigOptions<TFullSchema, TContext> {
context: TContext
}
export interface ServerConfig<
TFullSchema extends Record<string, unknown> = Record<string, unknown>,
TContext extends MinimalContext<TFullSchema> = MinimalContext<TFullSchema>,
TCollections extends Record<string, Collection<any, any, any, any, any, any>> = Record<
string,
Collection<any, any, any, any, any, any>
>,
TApiRouter extends ApiRouter<TContext> = AuthHandlers & ApiRouter<any>,
> extends BaseConfig<TFullSchema> {
context: TContext
collections: TCollections
endpoints: TApiRouter
}
export interface AnyServerConfig
extends ServerConfig<
Record<string, unknown>,
MinimalContext<Record<string, unknown>, {}>,
{},
{}
> {}
export type InferApiRouterFromServerConfig<TServerConfig extends ServerConfig<any, any, any, any>> =
TServerConfig extends ServerConfig<any, any, any, infer TApiRouter>
? TApiRouter extends ApiRouter<any>
? TApiRouter
: never
: never
export function defineBaseConfig<
const TFullSchema extends Record<string, unknown> = Record<string, unknown>,
const TContext extends Record<string, unknown> = Record<string, unknown>,
>(
config: BaseConfigOptions<TFullSchema, TContext>
): BaseConfig<TFullSchema, MinimalContext<TFullSchema, TContext>> {
const context = {
...config.context,
db: config.db,
} as MinimalContext<TFullSchema, TContext>
return {
...config,
context,
}
}
export function defineServerConfig<
const TFullSchema extends Record<string, unknown> = Record<string, unknown>,
const TContext extends MinimalContext<TFullSchema> = MinimalContext<TFullSchema>,
const TCollections extends Record<string, Collection<any, any, any, any, any, any>> = Record<
string,
Collection<any, any, any, any, any, any>
>,
const TEndpoints extends ApiRouter<MinimalContext<TFullSchema, TContext>> = {},
const TPlugins extends KivotosPlugin<any>[] = [...KivotosPlugin<any>[]],
>(
baseConfig: BaseConfig<TFullSchema, TContext>,
config: { collections: TCollections; endpoints?: TEndpoints; plugins?: TPlugins }
) {
const auth = createAuth(baseConfig.auth, baseConfig.context)
const collectionEndpoints = getAllCollectionEndpoints(config.collections)
let serverConfig = {
...baseConfig,
collections: config.collections,
endpoints: {
...config.endpoints,
...auth.handlers,
...collectionEndpoints,
} as TEndpoints &
AuthHandlers &
ExtractAllCollectionCustomEndpoints<TCollections> &
ExtractAllCollectionDefaultEndpoints<TCollections>,
} satisfies ServerConfig<
TFullSchema,
MinimalContext<TFullSchema, TContext>,
TCollections,
TEndpoints &
AuthHandlers &
ExtractAllCollectionCustomEndpoints<TCollections> &
ExtractAllCollectionDefaultEndpoints<TCollections>
>
for (const plugin of config.plugins ?? []) {
serverConfig = plugin.plugin(serverConfig) as any
}
return serverConfig as MergePlugins<typeof serverConfig, TPlugins>
}
export interface ClientConfig<
TCollections extends Record<string, ClientCollection<any, any, any, any, any, any>> = Record<
string,
ClientCollection<any, any, any, any, any, any>
>,
TApiRouter extends ClientApiRouter = ClientApiRouter,
> {
auth: AuthClient
collections: TCollections
endpoints: TApiRouter
}
export function getFieldClient(name: string, field: Field): FieldClient & { fieldName: string } {
if (isRelationField(field)) {
if (field._.source === 'relation') {
const sanitizedFields = Object.fromEntries(
Object.entries(field.fields).map(([key, value]) => {
return [key, getFieldClient(key, value)]
})
)
return R.omit(
{
...field,
fields: sanitizedFields,
},
['_', 'options' as any]
) as FieldClient & { fieldName: string }
}
return R.omit(
{
...field,
label: field.label ?? name,
placeholder: field.placeholder ?? name,
},
['_', 'options' as any]
) as FieldClient & { fieldName: string }
}
return R.omit(
{
...field,
label: field.label ?? name,
placeholder: field.placeholder ?? name,
},
['_', 'options' as any]
) as FieldClient & { fieldName: string }
}
export function getFieldsClient(fields: Fields<any>): FieldsClient {
return R.mapValues(fields, (value, key) => getFieldClient(key, value))
}
export function getClientCollection<
const TCollection extends Collection<any, any, any, any, any, any>,
>(collection: TCollection): ToClientCollection<TCollection> {
return R.pipe(collection, R.omit(['_', 'admin']), (collection) => ({
...collection,
fields: getFieldsClient(collection.fields),
})) as unknown as ToClientCollection<TCollection>
}
export function getClientConfig<
TCollections extends Record<string, Collection<any, any, any, any, any, any>>,
TApiRouter extends ApiRouter<any>,
>(
serverConfig: ServerConfig<any, any, TCollections, TApiRouter>
): ClientConfig<ToClientCollectionList<TCollections>, ToClientApiRouteSchema<TApiRouter>> & {
$types: ToRecordApiRouteSchema<TApiRouter>
} {
const collections = serverConfig.collections
const clientEndpoints = R.mapValues(serverConfig.endpoints, (value) =>
getClientEndpoint(value as ApiRoute<any>)
) as ToClientApiRouteSchema<TApiRouter>
return {
auth: getAuthClient(serverConfig.auth),
collections: R.mapValues(collections, (s) =>
getClientCollection(s as Collection<any, any, any, any, any, any>)
) as ToClientCollectionList<TCollections>,
endpoints: clientEndpoints,
$types: undefined as any,
}
}