@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oyolincc/create",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "@oyolincc/create",
5
5
  "homepage": "https://github.com/oyolincc/lib#readme",
6
6
  "bugs": {
@@ -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 listExamples = () => request<Example[]>('examples')
10
- export const createExample = (input: CreateExampleInput) =>
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 processExample = (input: ProcessExampleInput) =>
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 { createExample, listExamples, processExample } from '../api'
3
+ import { defineMutationHook, defineQueryHook } from '#/shared/network/query-hooks'
4
+ import { apiExampleCreateOne, apiExampleFindMany, apiExampleProcess } from '../api'
3
5
 
4
- export const exampleQueryKey = ['examples'] as 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 useExamples = () => useQuery({ queryKey: exampleQueryKey, queryFn: listExamples })
7
- export function useCreateExample() {
17
+ export const useCreateExampleMutation = defineMutationHook(() => {
8
18
  const queryClient = useQueryClient()
9
19
  return useMutation({
10
- mutationFn: createExample,
11
- onSuccess: () => queryClient.invalidateQueries({ queryKey: exampleQueryKey }),
20
+ mutationFn: (input: CreateExampleInput) => apiExampleCreateOne(input),
21
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: exampleQueryKeys.all }),
12
22
  })
13
- }
14
- export const useProcessExample = () => useMutation({ mutationFn: processExample })
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 { useCreateExample, useExamples, useProcessExample } from '#/modules/example/hooks/api-hooks'
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 = useExamples()
17
- const create = useCreateExample()
18
- const process = useProcessExample()
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={process.isPending}
45
+ processing={isProcessing}
37
46
  onMessageChange={setMessage}
38
47
  onSave={save}
39
- onProcess={() => process.mutate({ message })}
48
+ onProcess={() => processExample({ message })}
40
49
  />
41
- {process.data && (
42
- <pre className="rounded bg-zinc-900 p-4">{JSON.stringify(process.data, null, 2)}</pre>
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 || process.error || examples.error} />
45
- <ExampleList examples={examples.data} />
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
+ }
@@ -8,6 +8,10 @@
8
8
  }
9
9
  },
10
10
  "codeGenerator": {
11
- "root": "."
11
+ "root": ".",
12
+ "scope": "@repo",
13
+ "author": "",
14
+ "gitUrlBase": "",
15
+ "license": "MIT"
12
16
  }
13
17
  }
@@ -8,6 +8,10 @@
8
8
  }
9
9
  },
10
10
  "codeGenerator": {
11
- "root": "."
11
+ "root": ".",
12
+ "scope": "@repo",
13
+ "author": "",
14
+ "gitUrlBase": "",
15
+ "license": "MIT"
12
16
  }
13
17
  }
@@ -6,6 +6,10 @@
6
6
  }
7
7
  },
8
8
  "codeGenerator": {
9
- "root": "."
9
+ "root": ".",
10
+ "scope": "@repo",
11
+ "author": "",
12
+ "gitUrlBase": "",
13
+ "license": "MIT"
10
14
  }
11
15
  }