Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/selfish-taxis-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@genseki/react": patch
---

[Fix] improve type and use collection form
46 changes: 0 additions & 46 deletions packages/react/src/core/builder.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,49 +659,3 @@ export function getOptionsRoute<TPath extends string>(

return { route }
}

export type CollectionDefaultAdminApiRouter<
TSlug extends string,
TOptions extends {
create: { fields: Fields } | undefined
update: { fields: Fields } | undefined
list: { fields: Fields } | undefined
one: { fields: Fields } | undefined
delete: { fields: Fields } | undefined
},
> = (TOptions['create'] extends { fields: Fields }
? {
create: CollectionCreateApiRoute<TSlug, TOptions['create']['fields']>
}
: {}) &
(TOptions['create'] extends { options: FieldsOptions<any, any> }
? {
createOptions: CollectionFieldsOptionsApiRoute<`/${TSlug}/create/options`>
}
: {}) &
(TOptions['update'] extends { fields: Fields }
? {
update: CollectionUpdateApiRoute<TSlug, TOptions['update']['fields']>
updateDefault: CollectionUpdateDefaultApiRoute<TSlug, TOptions['update']['fields']>
}
: {}) &
(TOptions['update'] extends { options: FieldsOptions<any, any> }
? {
updateOptions: CollectionFieldsOptionsApiRoute<`/${TSlug}/update/options`>
}
: {}) &
(TOptions['list'] extends { fields: Fields }
? {
findMany: CollectionListApiRoute<TSlug, TOptions['list']['fields']>
}
: {}) &
(TOptions['one'] extends { fields: Fields }
? {
findOne: CollectionFindOneApiRoute<TSlug, TOptions['one']['fields']>
}
: {}) &
(TOptions['delete'] extends { fields: Fields }
? {
delete: CollectionDeleteApiRoute<TSlug>
}
: {})
93 changes: 57 additions & 36 deletions packages/react/src/core/collection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type React from 'react'
import type { PropsWithChildren } from 'react'

import type { ColumnDef } from '@tanstack/react-table'
import type { ConditionalExcept, Promisable, Simplify } from 'type-fest'
import type { UndefinedToOptional } from 'type-fest/source/internal'
import type { Promisable, Simplify } from 'type-fest'

import {
type CollectionLayoutProps,
Expand Down Expand Up @@ -57,7 +56,11 @@ import type { DataType, InferDataType, ModelSchemas } from '../model'
import type { GensekiPluginBuilderOptions } from '../plugin'
import { GensekiUiCommonId, type GensekiUiCommonProps } from '../ui'

type SimplifyConditionalExcept<Base, Condition> = Simplify<ConditionalExcept<Base, Condition>>
type UndefinedToOptional<T extends object> = {
[Key in keyof T as undefined extends T[Key] ? Key : never]?: T[Key]
} & {
[Key in keyof T as undefined extends T[Key] ? never : Key]: T[Key]
}

export const ApiDefaultMethod = {
CREATE: 'create',
Expand All @@ -81,7 +84,7 @@ export type InferUpdateOneRelationFieldShape<
('create' extends TKeys
? {
create: TFieldShape extends FieldRelationCreateShape | FieldRelationConnectOrCreateShape
? InferUpdateFields<TFieldShape['fields']>
? _InferUpdateFields<TFieldShape['fields']>
: never
}
: {}) &
Expand Down Expand Up @@ -112,11 +115,11 @@ export type InferUpdateRelationField<
export type InferUpdateFieldShape<TFieldShape extends FieldShape> = ApplyFieldProperty<
TFieldShape extends FieldRelationShapeBase
? TFieldShape extends FieldRelationCreateShape
? Simplify<InferUpdateRelationField<TFieldShape, 'create' | 'connect' | 'disconnect'>>
? InferUpdateRelationField<TFieldShape, 'create' | 'connect' | 'disconnect'>
: TFieldShape extends FieldRelationConnectShape
? Simplify<InferUpdateRelationField<TFieldShape, 'connect' | 'disconnect'>>
? InferUpdateRelationField<TFieldShape, 'connect' | 'disconnect'>
: TFieldShape extends FieldRelationConnectOrCreateShape
? Simplify<InferUpdateRelationField<TFieldShape, 'create' | 'connect' | 'disconnect'>>
? InferUpdateRelationField<TFieldShape, 'create' | 'connect' | 'disconnect'>
: never
: TFieldShape extends FieldColumnShape
? TFieldShape['$server']['column']['isList'] extends true
Expand All @@ -130,9 +133,15 @@ export type InferUpdateFieldShape<TFieldShape extends FieldShape> = ApplyFieldPr
TFieldShape
>

export type InferUpdateFields<TFields extends Fields> = UndefinedToOptional<{
type _InferUpdateFields<TFields extends Fields> = {
-readonly [TKey in keyof TFields['shape']]: InferUpdateFieldShape<TFields['shape'][TKey]>
}>
}

export type InferUpdateFields<TFields extends Fields> = Simplify<
UndefinedToOptional<{
-readonly [TKey in keyof TFields['shape']]: InferUpdateFieldShape<TFields['shape'][TKey]>
}>
>

export type InferCreateOneRelationFieldShape<
TFieldShape extends FieldRelationShapeBase,
Expand All @@ -141,7 +150,7 @@ export type InferCreateOneRelationFieldShape<
('create' extends TKeys
? {
create: TFieldShape extends FieldRelationCreateShape | FieldRelationConnectOrCreateShape
? InferCreateFields<TFieldShape['fields']>
? _InferCreateFields<TFieldShape['fields']>
: never
}
: {}) &
Expand Down Expand Up @@ -172,11 +181,11 @@ export type InferCreateRelationField<
export type InferCreateFieldShape<TFieldShape extends FieldShape> = ApplyFieldProperty<
TFieldShape extends FieldRelationShapeBase
? TFieldShape extends FieldRelationCreateShape
? Simplify<InferCreateRelationField<TFieldShape, 'create'>>
? InferCreateRelationField<TFieldShape, 'create'>
: TFieldShape extends FieldRelationConnectShape
? Simplify<InferCreateRelationField<TFieldShape, 'connect'>>
? InferCreateRelationField<TFieldShape, 'connect'>
: TFieldShape extends FieldRelationConnectOrCreateShape
? Simplify<InferCreateRelationField<TFieldShape, 'create' | 'connect'>>
? InferCreateRelationField<TFieldShape, 'create' | 'connect'>
: never
: TFieldShape extends FieldColumnShape
? TFieldShape['$server']['column']['isList'] extends true
Expand All @@ -190,11 +199,19 @@ export type InferCreateFieldShape<TFieldShape extends FieldShape> = ApplyFieldPr
TFieldShape
>

export type InferCreateFields<TFields extends Fields> = UndefinedToOptional<{
type _InferCreateFields<TFields extends Fields> = {
-readonly [TShapeKey in keyof TFields['shape']]: InferCreateFieldShape<
TFields['shape'][TShapeKey]
>
}>
}

export type InferCreateFields<TFields extends Fields> = Simplify<
UndefinedToOptional<{
-readonly [TShapeKey in keyof TFields['shape']]: InferCreateFieldShape<
TFields['shape'][TShapeKey]
>
}>
>

export type InferRelationField<
TFieldShape extends FieldRelationShape,
Expand Down Expand Up @@ -222,14 +239,11 @@ export type InferField<TField extends FieldShapeBase> = TField extends FieldRela
: InferDataType<TField['$server']['column']['dataType']> | undefined | null
: never

type _InferFields<TFields extends Fields> = SimplifyConditionalExcept<
{
-readonly [TKey in keyof TFields['shape']]: TFields['shape'][TKey] extends FieldShapeBase
? InferField<TFields['shape'][TKey]>
: never
} & { __id: string | number; __pk: string | number },
never
>
type _InferFields<TFields extends Fields> = {
-readonly [TKey in keyof TFields['shape']]: TFields['shape'][TKey] extends FieldShapeBase
? InferField<TFields['shape'][TKey]>
: never
} & { __id: string | number; __pk: string | number }

export type InferFields<TFields extends Fields> = UndefinedToOptional<_InferFields<TFields>>

Expand Down Expand Up @@ -262,7 +276,7 @@ export type CollectionCreateConfig<
TFields extends Fields = Fields,
> = {
api?: CollectionCreateApiHandler<TContext, TFields>
options?: Simplify<FieldsOptions<TContext, TFields>>
options?: FieldsOptions<TContext, TFields>
page?: React.FC
}

Expand Down Expand Up @@ -373,7 +387,7 @@ export type CollectionUpdateConfig<
> = {
updateApi?: CollectionUpdateApiHandler<TContext, TFields>
updateDefaultApi?: CollectionUpdateDefaultApiHandler<TContext, TFields>
options?: Simplify<FieldsOptions<TContext, TFields>>
options?: FieldsOptions<TContext, TFields>
}

export interface CollectionDeleteApiArgs<TContext extends AnyContextable, TFields extends Fields>
Expand Down Expand Up @@ -560,7 +574,7 @@ export class CollectionBuilder<
})

return {
list: route as Simplify<CollectionListApiRoute<TSlug, TFields>>,
list: route as unknown as CollectionListApiRoute<TSlug, TFields>,
}
}

Expand Down Expand Up @@ -602,7 +616,10 @@ export class CollectionBuilder<
},
})

return { ui: ui, api: route }
return {
ui: ui,
api: route,
}
}
}

Expand All @@ -615,7 +632,7 @@ export class CollectionBuilder<
})

return {
one: route as Simplify<CollectionFindOneApiRoute<TSlug, TFields>>,
one: route as unknown as CollectionFindOneApiRoute<TSlug, TFields>,
}
}

Expand Down Expand Up @@ -652,7 +669,10 @@ export class CollectionBuilder<
},
})

return { ui: ui, api: route }
return {
ui: ui,
api: route,
}
}
}

Expand All @@ -675,7 +695,7 @@ export class CollectionBuilder<
)

return {
create: route as Simplify<CollectionCreateApiRoute<TSlug, TFields>>,
create: route as CollectionCreateApiRoute<TSlug, TFields>,
createOptions: createOptionsRoute,
}
}
Expand Down Expand Up @@ -721,7 +741,10 @@ export class CollectionBuilder<
},
})

return { ui: ui, api: route }
return {
ui: ui,
api: route,
}
}
}

Expand Down Expand Up @@ -752,10 +775,8 @@ export class CollectionBuilder<
)

return {
update: updateRoute as Simplify<CollectionUpdateApiRoute<TSlug, TFields>>,
updateDefault: updateDefaultRoute as Simplify<
CollectionUpdateDefaultApiRoute<TSlug, TFields>
>,
update: updateRoute as CollectionUpdateApiRoute<TSlug, TFields>,
updateDefault: updateDefaultRoute as CollectionUpdateDefaultApiRoute<TSlug, TFields>,
updateOptions: updateOptionsRoute,
}
}
Expand All @@ -769,7 +790,7 @@ export class CollectionBuilder<
})

return {
delete: route as Simplify<CollectionDeleteApiRoute<TSlug>>,
delete: route as CollectionDeleteApiRoute<TSlug>,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/core/field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PartialDeep, Promisable, Simplify } from 'type-fest'
import type { PartialDeep, Promisable } from 'type-fest'
import z from 'zod'

import type { InferFields } from './collection'
Expand Down Expand Up @@ -609,14 +609,14 @@ export class FieldBuilder<
modelName: TModelName,
optionsFn: (fb: FieldBuilder<TContext, TModelSchemas, TModelName>) => TFieldsShape,
config?: { identifierColumn?: string }
): Simplify<{
): {
shape: TFieldsShape
config: {
primaryColumn: string
identifierColumn: string
prismaModelName: TModelName
}
}> {
} {
const fb = new FieldBuilder({
context: this.options.context,
modelSchemas: this.options.modelSchemas,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export type ToZodObject<T extends Record<string, any>> = ZodObject<{

export function appendFieldNameToFields<TFieldsShape extends FieldsShape>(
fieldsShape: TFieldsShape
): Simplify<TFieldsShape> {
): TFieldsShape {
return Object.fromEntries(
Object.entries(fieldsShape).map(([key, field]) => {
field.$client.fieldName = key
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

import { type DefaultValues, type FieldValues, useForm, type UseFormProps } from 'react-hook-form'

import { getDefaultValueFromFieldsClient } from '../../../../core'
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading