sdnext 0.0.4 → 0.0.5

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.
@@ -1,5 +1,5 @@
1
1
  import { Command } from "commander";
2
- export type HookType = "get" | "query" | "mutation";
2
+ export type HookType = "query" | "mutation";
3
3
  export type HookContentMap = Record<HookType, string>;
4
4
  export interface HookData extends HookContentMap {
5
5
  overwrite: boolean;
@@ -2,13 +2,11 @@ import { mkdir, readFile, readdir, stat, writeFile } from "fs/promises";
2
2
  import { join, parse, relative } from "path";
3
3
  import { checkbox as prompts_checkbox, select as prompts_select } from "@inquirer/prompts";
4
4
  function getHookTypeFromName(name) {
5
- if (/^get[^a-z]/.test(name)) return "get";
6
- if (/^query[^a-z]/.test(name)) return "query";
5
+ if (/^(get|query)[^a-z]/.test(name)) return "query";
7
6
  return "mutation";
8
7
  }
9
8
  function getHookTypeFromContent(content) {
10
9
  if (content.includes("useMutation")) return "mutation";
11
- if (content.includes("IdOrParams")) return "get";
12
10
  if (content.includes("useQuery")) return "query";
13
11
  }
14
12
  async function createHook(path, hookMap) {
@@ -30,7 +28,9 @@ ${match[0]}
30
28
  export const ${name}Client = createRequestFn(${hasSchema ? `{
31
29
  fn: ${name}Action,
32
30
  schema: ${match[1]},
33
- }` : `${name}Action`})
31
+ }` : `{
32
+ fn: ${name}Action,
33
+ }`})
34
34
 
35
35
  export interface Use${upName}Params<TOnMutateResult = unknown> extends Omit<
36
36
  UseMutationOptions<Awaited<ReturnType<typeof ${name}Client>>, Error, Parameters<typeof ${name}Client>[0], TOnMutateResult>,
@@ -56,8 +56,8 @@ export function use${upName}<TOnMutateResult = unknown>({ onMutate, onSuccess, o
56
56
  })
57
57
  }
58
58
  `;
59
- const getHook = `import { useQuery } from "@tanstack/react-query"
60
- import { createRequestFn, isNonNullable } from "deepsea-tools"
59
+ const queryHook = `import { createRequestFn } from "deepsea-tools"
60
+ import { createUseQuery } from "soda-tanstack-query"
61
61
 
62
62
  import { ${name}Action } from "@/actions/${join(dir, name)}"
63
63
  ${hasSchema ? `
@@ -66,44 +66,16 @@ ${match[0].replace(match[1], `${match[1].replace(/Schema$/, "Params").replace(/^
66
66
  export const ${name}Client = createRequestFn(${hasSchema ? `{
67
67
  fn: ${name}Action,
68
68
  schema: ${match[1]},
69
- }` : `${name}Action`})
70
-
71
- export interface Use${upName}Params {
72
- id?: ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, (char)=>char.toUpperCase())} | ` : ""}undefined
73
- enabled?: boolean
74
- }
75
-
76
- export function use${upName}(idOrParams?: Use${upName}Params | ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, (char)=>char.toUpperCase())} | ` : ""}undefined) {
77
- const { id, enabled = true } = typeof idOrParams === "object" ? idOrParams : { id: idOrParams, enabled: true }
78
-
79
- return useQuery({
80
- queryKey: ["${key}", id],
81
- queryFn: () => (isNonNullable(id) ? ${name}Client(id) : null),
82
- enabled,
83
- })
84
- }
85
- `;
86
- const queryHook = `import { useQuery } from "@tanstack/react-query"
87
- import { createRequestFn } from "deepsea-tools"
88
-
89
- import { ${name}Action } from "@/actions/${join(dir, name)}"
90
- ${hasSchema ? `
91
- ${match[0].replace(match[1], `${match[1].replace(/Schema$/, "Params").replace(/^./, (char)=>char.toUpperCase())}, ${match[1]}`)}
92
- ` : ""}
93
- export const ${name}Client = createRequestFn(${hasSchema ? `{
69
+ }` : `{
94
70
  fn: ${name}Action,
95
- schema: ${match[1]},
96
- }` : `${name}Action`})
71
+ }`})
97
72
 
98
- export function use${upName}(${hasSchema ? `params: ${match[1].replace(/Schema$/, "Params").replace(/^./, (char)=>char.toUpperCase())} = {}` : ""}) {
99
- return useQuery({
100
- queryKey: ["${key}", params],
101
- queryFn: () => ${name}Client(params),
102
- })
103
- }
73
+ export const use${upName} = createUseQuery({
74
+ queryFn: ${name}Client,
75
+ queryKey: "${key}",
76
+ })
104
77
  `;
105
78
  const map = {
106
- get: getHook,
107
79
  query: queryHook,
108
80
  mutation: mutationHook
109
81
  };
@@ -151,7 +123,6 @@ async function hook(options, { args }) {
151
123
  choices: [
152
124
  "mutation",
153
125
  "query",
154
- "get",
155
126
  "skip"
156
127
  ],
157
128
  default: type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdnext",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,33 +1,33 @@
1
- import { spawn } from "child_process"
2
- import { readdir, stat } from "fs/promises"
3
- import { join } from "path"
4
-
5
- import { Command } from "commander"
6
-
7
- import { createAction } from "./createAction"
8
- import { excludeActions } from "./excludeActions"
9
-
10
- export async function buildFolder(dir: string) {
11
- const content = await readdir(dir)
12
-
13
- for (const item of content) {
14
- const path = join(dir, item)
15
- const stats = await stat(path)
16
-
17
- if (stats.isDirectory()) await buildFolder(path)
18
- else await createAction(path)
19
- }
20
- }
21
-
22
- export async function build(options: Record<string, string>, { args }: Command) {
23
- await excludeActions()
24
-
25
- await buildFolder("shared")
26
-
27
- if (args.length === 0) return
28
-
29
- spawn(args.join(" "), {
30
- stdio: "inherit",
31
- shell: true,
32
- })
33
- }
1
+ import { spawn } from "child_process"
2
+ import { readdir, stat } from "fs/promises"
3
+ import { join } from "path"
4
+
5
+ import { Command } from "commander"
6
+
7
+ import { createAction } from "./createAction"
8
+ import { excludeActions } from "./excludeActions"
9
+
10
+ export async function buildFolder(dir: string) {
11
+ const content = await readdir(dir)
12
+
13
+ for (const item of content) {
14
+ const path = join(dir, item)
15
+ const stats = await stat(path)
16
+
17
+ if (stats.isDirectory()) await buildFolder(path)
18
+ else await createAction(path)
19
+ }
20
+ }
21
+
22
+ export async function build(options: Record<string, string>, { args }: Command) {
23
+ await excludeActions()
24
+
25
+ await buildFolder("shared")
26
+
27
+ if (args.length === 0) return
28
+
29
+ spawn(args.join(" "), {
30
+ stdio: "inherit",
31
+ shell: true,
32
+ })
33
+ }
package/src/utils/hook.ts CHANGED
@@ -4,19 +4,17 @@ import { join, parse, relative } from "path"
4
4
  import { checkbox, select } from "@inquirer/prompts"
5
5
  import { Command } from "commander"
6
6
 
7
- export type HookType = "get" | "query" | "mutation"
7
+ export type HookType = "query" | "mutation"
8
8
 
9
9
  export type HookContentMap = Record<HookType, string>
10
10
 
11
11
  function getHookTypeFromName(name: string): HookType {
12
- if (/^get[^a-z]/.test(name)) return "get"
13
- if (/^query[^a-z]/.test(name)) return "query"
12
+ if (/^(get|query)[^a-z]/.test(name)) return "query"
14
13
  return "mutation"
15
14
  }
16
15
 
17
16
  function getHookTypeFromContent(content: string): HookType | undefined {
18
17
  if (content.includes("useMutation")) return "mutation"
19
- if (content.includes("IdOrParams")) return "get"
20
18
  if (content.includes("useQuery")) return "query"
21
19
  return undefined
22
20
  }
@@ -53,7 +51,9 @@ export const ${name}Client = createRequestFn(${
53
51
  fn: ${name}Action,
54
52
  schema: ${match[1]},
55
53
  }`
56
- : `${name}Action`
54
+ : `{
55
+ fn: ${name}Action,
56
+ }`
57
57
  })
58
58
 
59
59
  export interface Use${upName}Params<TOnMutateResult = unknown> extends Omit<
@@ -81,8 +81,8 @@ export function use${upName}<TOnMutateResult = unknown>({ onMutate, onSuccess, o
81
81
  }
82
82
  `
83
83
 
84
- const getHook = `import { useQuery } from "@tanstack/react-query"
85
- import { createRequestFn, isNonNullable } from "deepsea-tools"
84
+ const queryHook = `import { createRequestFn } from "deepsea-tools"
85
+ import { createUseQuery } from "soda-tanstack-query"
86
86
 
87
87
  import { ${name}Action } from "@/actions/${join(dir, name)}"
88
88
  ${
@@ -98,55 +98,18 @@ export const ${name}Client = createRequestFn(${
98
98
  fn: ${name}Action,
99
99
  schema: ${match[1]},
100
100
  }`
101
- : `${name}Action`
102
- })
103
-
104
- export interface Use${upName}Params {
105
- id?: ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, char => char.toUpperCase())} | ` : ""}undefined
106
- enabled?: boolean
107
- }
108
-
109
- export function use${upName}(idOrParams?: Use${upName}Params | ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, char => char.toUpperCase())} | ` : ""}undefined) {
110
- const { id, enabled = true } = typeof idOrParams === "object" ? idOrParams : { id: idOrParams, enabled: true }
111
-
112
- return useQuery({
113
- queryKey: ["${key}", id],
114
- queryFn: () => (isNonNullable(id) ? ${name}Client(id) : null),
115
- enabled,
116
- })
117
- }
118
- `
119
-
120
- const queryHook = `import { useQuery } from "@tanstack/react-query"
121
- import { createRequestFn } from "deepsea-tools"
122
-
123
- import { ${name}Action } from "@/actions/${join(dir, name)}"
124
- ${
125
- hasSchema
126
- ? `
127
- ${match[0].replace(match[1], `${match[1].replace(/Schema$/, "Params").replace(/^./, char => char.toUpperCase())}, ${match[1]}`)}
128
- `
129
- : ""
130
- }
131
- export const ${name}Client = createRequestFn(${
132
- hasSchema
133
- ? `{
101
+ : `{
134
102
  fn: ${name}Action,
135
- schema: ${match[1]},
136
103
  }`
137
- : `${name}Action`
138
104
  })
139
105
 
140
- export function use${upName}(${hasSchema ? `params: ${match[1].replace(/Schema$/, "Params").replace(/^./, char => char.toUpperCase())} = {}` : ""}) {
141
- return useQuery({
142
- queryKey: ["${key}", params],
143
- queryFn: () => ${name}Client(params),
144
- })
145
- }
106
+ export const use${upName} = createUseQuery({
107
+ queryFn: ${name}Client,
108
+ queryKey: "${key}",
109
+ })
146
110
  `
147
111
 
148
112
  const map: HookContentMap = {
149
- get: getHook,
150
113
  query: queryHook,
151
114
  mutation: mutationHook,
152
115
  }
@@ -216,7 +179,7 @@ export async function hook(options: Record<string, string>, { args }: Command) {
216
179
 
217
180
  const answer = await select<OperationType>({
218
181
  message: path,
219
- choices: ["mutation", "query", "get", "skip"],
182
+ choices: ["mutation", "query", "skip"],
220
183
  default: type,
221
184
  })
222
185
 
@@ -1,27 +1,27 @@
1
- import { rm } from "fs/promises"
2
- import { join, relative } from "path"
3
-
4
- import { watch } from "chokidar"
5
-
6
- import { createAction } from "./createAction"
7
-
8
- const watcher = watch("shared", {
9
- awaitWriteFinish: true,
10
- persistent: true,
11
- })
12
-
13
- watcher.on("add", createAction)
14
-
15
- watcher.on("change", createAction)
16
-
17
- watcher.on("unlink", async path => {
18
- path = relative("shared", path).replace(/\\/g, "/")
19
- const actionPath = join("actions", path)
20
- await rm(actionPath, { recursive: true, force: true })
21
- })
22
-
23
- watcher.on("unlinkDir", async path => {
24
- path = relative("shared", path).replace(/\\/g, "/")
25
- const actionPath = join("actions", path)
26
- await rm(actionPath, { recursive: true, force: true })
27
- })
1
+ import { rm } from "fs/promises"
2
+ import { join, relative } from "path"
3
+
4
+ import { watch } from "chokidar"
5
+
6
+ import { createAction } from "./createAction"
7
+
8
+ const watcher = watch("shared", {
9
+ awaitWriteFinish: true,
10
+ persistent: true,
11
+ })
12
+
13
+ watcher.on("add", createAction)
14
+
15
+ watcher.on("change", createAction)
16
+
17
+ watcher.on("unlink", async path => {
18
+ path = relative("shared", path).replace(/\\/g, "/")
19
+ const actionPath = join("actions", path)
20
+ await rm(actionPath, { recursive: true, force: true })
21
+ })
22
+
23
+ watcher.on("unlinkDir", async path => {
24
+ path = relative("shared", path).replace(/\\/g, "/")
25
+ const actionPath = join("actions", path)
26
+ await rm(actionPath, { recursive: true, force: true })
27
+ })