@oyolincc/create 0.3.0 → 0.4.0
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/package.json +1 -1
- package/templates/hono-fullstack/apps/stack/frontend/modules/example/api.ts +3 -3
- package/templates/hono-fullstack/apps/stack/frontend/modules/example/hooks/api-hooks.ts +21 -8
- package/templates/hono-fullstack/apps/stack/frontend/routes/index.tsx +21 -10
- package/templates/hono-fullstack/apps/stack/frontend/shared/network/query-hooks.ts +33 -0
- package/templates/hono-fullstack/oyo.config.json +5 -1
- package/templates/hono-server/oyo.config.json +5 -1
- package/templates/monorepo/oyo.config.json +5 -1
package/package.json
CHANGED
|
@@ -6,8 +6,8 @@ import type {
|
|
|
6
6
|
} from '@repo/definition/frontend'
|
|
7
7
|
import { request } from '#/shared/network/client'
|
|
8
8
|
|
|
9
|
-
export const
|
|
10
|
-
export const
|
|
9
|
+
export const apiExampleFindMany = () => request<Example[]>('examples')
|
|
10
|
+
export const apiExampleCreateOne = (input: CreateExampleInput) =>
|
|
11
11
|
request<Example>('examples', { method: 'post', json: input })
|
|
12
|
-
export const
|
|
12
|
+
export const apiExampleProcess = (input: ProcessExampleInput) =>
|
|
13
13
|
request<ProcessExampleOutput>('examples/process', { method: 'post', json: input })
|
|
@@ -1,14 +1,27 @@
|
|
|
1
|
+
import type { CreateExampleInput, ProcessExampleInput } from '@repo/definition/frontend'
|
|
1
2
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
2
|
-
import {
|
|
3
|
+
import { defineMutationHook, defineQueryHook } from '#/shared/network/query-hooks'
|
|
4
|
+
import { apiExampleCreateOne, apiExampleFindMany, apiExampleProcess } from '../api'
|
|
3
5
|
|
|
4
|
-
export const
|
|
6
|
+
export const exampleQueryKeys = {
|
|
7
|
+
all: ['examples'] as const,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const useExamplesQuery = defineQueryHook(() =>
|
|
11
|
+
useQuery({
|
|
12
|
+
queryKey: exampleQueryKeys.all,
|
|
13
|
+
queryFn: () => apiExampleFindMany(),
|
|
14
|
+
}),
|
|
15
|
+
)
|
|
5
16
|
|
|
6
|
-
export const
|
|
7
|
-
export function useCreateExample() {
|
|
17
|
+
export const useCreateExampleMutation = defineMutationHook(() => {
|
|
8
18
|
const queryClient = useQueryClient()
|
|
9
19
|
return useMutation({
|
|
10
|
-
mutationFn:
|
|
11
|
-
onSuccess: () => queryClient.invalidateQueries({ queryKey:
|
|
20
|
+
mutationFn: (input: CreateExampleInput) => apiExampleCreateOne(input),
|
|
21
|
+
onSuccess: () => queryClient.invalidateQueries({ queryKey: exampleQueryKeys.all }),
|
|
12
22
|
})
|
|
13
|
-
}
|
|
14
|
-
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export const useProcessExampleMutation = defineMutationHook(() =>
|
|
26
|
+
useMutation({ mutationFn: (input: ProcessExampleInput) => apiExampleProcess(input) }),
|
|
27
|
+
)
|
|
@@ -6,16 +6,25 @@ import { Page } from '#/components/layouts/Page'
|
|
|
6
6
|
import { PageContainer } from '#/components/layouts/PageContainer'
|
|
7
7
|
import { ExampleForm } from '#/modules/example/components/ExampleForm'
|
|
8
8
|
import { ExampleList } from '#/modules/example/components/ExampleList'
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
useCreateExampleMutation,
|
|
11
|
+
useExamplesQuery,
|
|
12
|
+
useProcessExampleMutation,
|
|
13
|
+
} from '#/modules/example/hooks/api-hooks'
|
|
10
14
|
import { exampleMessageAtom } from '#/modules/example/stores'
|
|
11
15
|
|
|
12
16
|
export const Route = createFileRoute('/')({ component: Home })
|
|
13
17
|
|
|
14
18
|
function Home() {
|
|
15
19
|
const [message, setMessage] = useAtom(exampleMessageAtom)
|
|
16
|
-
const examples =
|
|
17
|
-
const create =
|
|
18
|
-
const
|
|
20
|
+
const { data: examples, error: examplesError } = useExamplesQuery()
|
|
21
|
+
const create = useCreateExampleMutation()
|
|
22
|
+
const {
|
|
23
|
+
data: processedExample,
|
|
24
|
+
error: processError,
|
|
25
|
+
isPending: isProcessing,
|
|
26
|
+
mutate: processExample,
|
|
27
|
+
} = useProcessExampleMutation()
|
|
19
28
|
|
|
20
29
|
const save = async () => {
|
|
21
30
|
await create.mutateAsync({ message })
|
|
@@ -33,16 +42,18 @@ function Home() {
|
|
|
33
42
|
<ExampleForm
|
|
34
43
|
message={message}
|
|
35
44
|
saving={create.isPending}
|
|
36
|
-
processing={
|
|
45
|
+
processing={isProcessing}
|
|
37
46
|
onMessageChange={setMessage}
|
|
38
47
|
onSave={save}
|
|
39
|
-
onProcess={() =>
|
|
48
|
+
onProcess={() => processExample({ message })}
|
|
40
49
|
/>
|
|
41
|
-
{
|
|
42
|
-
<pre className="rounded bg-zinc-900 p-4">
|
|
50
|
+
{processedExample && (
|
|
51
|
+
<pre className="rounded bg-zinc-900 p-4">
|
|
52
|
+
{JSON.stringify(processedExample, null, 2)}
|
|
53
|
+
</pre>
|
|
43
54
|
)}
|
|
44
|
-
<ErrorMessage error={create.error ||
|
|
45
|
-
<ExampleList examples={examples
|
|
55
|
+
<ErrorMessage error={create.error || processError || examplesError} />
|
|
56
|
+
<ExampleList examples={examples} />
|
|
46
57
|
</Page>
|
|
47
58
|
</PageContainer>
|
|
48
59
|
</AppLayout>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { UseMutationResult, UseQueryResult } from '@tanstack/react-query'
|
|
2
|
+
|
|
3
|
+
export interface QueryHookOptions {
|
|
4
|
+
enabled?: boolean
|
|
5
|
+
staleTime?: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface MutationHookOptions {}
|
|
9
|
+
|
|
10
|
+
type AnyUseQueryResult = UseQueryResult<any, any>
|
|
11
|
+
type AnyUseMutationResult = UseMutationResult<any, any, any, any>
|
|
12
|
+
type HookArgs<TParams, TOptions> = [] | [params?: TParams] | [params: TParams, options?: TOptions]
|
|
13
|
+
|
|
14
|
+
export type MutationHook<TParams, TResult extends AnyUseMutationResult> = (
|
|
15
|
+
params: TParams,
|
|
16
|
+
options?: MutationHookOptions,
|
|
17
|
+
) => TResult
|
|
18
|
+
|
|
19
|
+
export function defineQueryHook<
|
|
20
|
+
TParams,
|
|
21
|
+
TArgs extends HookArgs<TParams, QueryHookOptions>,
|
|
22
|
+
TResult extends AnyUseQueryResult,
|
|
23
|
+
>(hook: (...args: TArgs) => TResult) {
|
|
24
|
+
return hook
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function defineMutationHook<
|
|
28
|
+
TParams,
|
|
29
|
+
TArgs extends HookArgs<TParams, MutationHookOptions>,
|
|
30
|
+
TResult extends AnyUseMutationResult,
|
|
31
|
+
>(hook: (...args: TArgs) => TResult) {
|
|
32
|
+
return hook
|
|
33
|
+
}
|