1- import type {
2- DefaultError ,
3- UseMutationOptions ,
4- UseMutationResult ,
5- UseQueryOptions ,
6- UseQueryResult ,
1+ import {
2+ useMutation ,
3+ useQuery ,
4+ type DefaultError ,
5+ type UseMutationOptions ,
6+ type UseMutationResult ,
7+ type UseQueryOptions ,
8+ type UseQueryResult ,
79} from '@tanstack/react-query' ;
810import type { CreateArgs , FindArgs , ModelResult , SelectSubset } from '@zenstackhq/runtime' ;
9- import type { GetModels , SchemaDef } from '@zenstackhq/runtime/schema' ;
11+ import { type GetModels , type SchemaDef } from '@zenstackhq/runtime/schema' ;
12+ import { useContext } from 'react' ;
13+ import { getQueryKey , type MutationMethod , type MutationOperation , type QueryOperation } from './runtime/common' ;
14+ import { RequestHandlerContext } from './runtime/react' ;
1015
11- export type toHooks < Schema extends SchemaDef > = {
12- [ Model in GetModels < Schema > as Uncapitalize < Model > ] : ToModelHooks < Schema , Model > ;
16+ export type useHooks < Schema extends SchemaDef > = {
17+ [ Model in GetModels < Schema > as Uncapitalize < Model > ] : UseModelHooks < Schema , Model > ;
1318} ;
1419
15- type ToModelHooks < Schema extends SchemaDef , Model extends GetModels < Schema > > = {
16- findMany < T extends FindArgs < Schema , Model , true > > (
20+ type UseModelHooks < Schema extends SchemaDef , Model extends GetModels < Schema > > = {
21+ useFindFirst < T extends FindArgs < Schema , Model , true > > (
1722 args ?: SelectSubset < T , FindArgs < Schema , Model , true > > ,
18- options ?: Omit < UseQueryOptions < ModelResult < Schema , Model , T > [ ] > , 'queryKey' > ,
19- ) : UseQueryResult < ModelResult < Schema , Model , T > [ ] > ;
20-
21- findFirst < T extends FindArgs < Schema , Model , true > > (
22- args ?: SelectSubset < T , FindArgs < Schema , Model , true > > ,
23- options ?: Omit < UseQueryOptions < ModelResult < Schema , Model , T > [ ] > , 'queryKey' > ,
23+ options ?: Omit < UseQueryOptions < ModelResult < Schema , Model , T > [ ] > , 'queryKey' | 'queryFn' > ,
2424 ) : UseQueryResult < ModelResult < Schema , Model , T > | null > ;
2525
26- create < T extends CreateArgs < Schema , Model > > (
26+ useCreate < T extends CreateArgs < Schema , Model > > (
2727 options ?: UseMutationOptions < ModelResult < Schema , Model , T > , DefaultError , T > ,
2828 ) : UseMutationResult < ModelResult < Schema , Model , T > , DefaultError , T > ;
2929} ;
@@ -32,47 +32,77 @@ function uncapitalize(s: string) {
3232 return s . charAt ( 0 ) . toLowerCase ( ) + s . slice ( 1 ) ;
3333}
3434
35- export function toHooks < Schema extends SchemaDef > ( schema : Schema ) : toHooks < Schema > {
35+ export function useHooks < Schema extends SchemaDef > ( schema : Schema ) : useHooks < Schema > {
3636 return Object . entries ( schema . models ) . reduce (
3737 ( acc , [ model , _ ] ) =>
3838 Object . assign ( acc , {
39- [ uncapitalize ( model ) ] : toModelHooks ( schema , model as GetModels < Schema > ) ,
39+ [ uncapitalize ( model ) ] : useModelHooks ( schema , model as GetModels < Schema > ) ,
4040 } ) ,
41- { } as toHooks < Schema > ,
41+ { } as useHooks < Schema > ,
4242 ) ;
4343}
4444
45- function toModelHooks < Schema extends SchemaDef , Model extends GetModels < Schema > > ( schema : Schema , model : Model ) : any {
45+ function useModelHooks < Schema extends SchemaDef , Model extends GetModels < Schema > > ( schema : Schema , model : Model ) : any {
4646 const modelDef = schema . models [ model ] ;
4747 if ( ! modelDef ) {
4848 throw new Error ( `Model ${ model } not found in schema` ) ;
4949 }
5050
5151 return {
52- findMany : ( ) => {
53- return {
54- data : [ ] ,
55- isLoading : false ,
56- isError : false ,
57- } ;
58- } ,
52+ useFindFirst : useModelQuery ( schema , model , 'findFirst' ) ,
53+ useCreate : useModelMutation ( schema , model , 'create' , 'POST' ) ,
54+ } ;
55+ }
5956
60- findFirst : ( ) => {
61- return {
62- data : null ,
63- isLoading : false ,
64- isError : false ,
65- } ;
66- } ,
57+ export function useModelQuery <
58+ Schema extends SchemaDef ,
59+ Model extends GetModels < Schema > ,
60+ > (
61+ schema : Schema ,
62+ model : Model ,
63+ operation : QueryOperation ,
64+ ) {
65+ const context = useContext ( RequestHandlerContext ) ;
66+ if ( ! context ) {
67+ throw new Error ( 'Missing context' ) ;
68+ }
69+
70+ const queryKey = getQueryKey ( schema , model , operation , { } ) ;
71+ const query = useQuery ( {
72+ queryKey,
73+ queryFn : async ( ) => {
74+ const response = await context . fetch ! ( context . endpoint ! ) ;
6775
68- create : ( ) => {
69- return {
70- mutate : async ( ) => {
71- return null ;
72- } ,
73- isLoading : false ,
74- isError : false ,
75- } ;
76+ return response ;
7677 } ,
77- } ;
78+ } ) ;
79+
80+ return query ;
7881}
82+
83+ export function useModelMutation <
84+ Schema extends SchemaDef ,
85+ Model extends GetModels < Schema > ,
86+ > (
87+ schema : Schema ,
88+ model : Model ,
89+ operation : MutationOperation ,
90+ method : MutationMethod ,
91+ ) {
92+ const context = useContext ( RequestHandlerContext ) ;
93+ if ( ! context ) {
94+ throw new Error ( 'Missing context' ) ;
95+ }
96+
97+ const mutation = useMutation ( {
98+ mutationFn : async ( ) => {
99+ const response = await context . fetch ! ( context . endpoint ! , {
100+ method,
101+ } ) ;
102+
103+ return response ;
104+ }
105+ } ) ;
106+
107+ return mutation ;
108+ }
0 commit comments