sdnext 0.0.5 → 0.0.7

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 = "query" | "mutation";
2
+ export type HookType = "get" | "query" | "mutation";
3
3
  export type HookContentMap = Record<HookType, string>;
4
4
  export interface HookData extends HookContentMap {
5
5
  overwrite: boolean;
@@ -2,11 +2,13 @@ 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|query)[^a-z]/.test(name)) return "query";
5
+ if (/^get[^a-z]/.test(name)) return "get";
6
+ if (/^query[^a-z]/.test(name)) return "query";
6
7
  return "mutation";
7
8
  }
8
9
  function getHookTypeFromContent(content) {
9
10
  if (content.includes("useMutation")) return "mutation";
11
+ if (content.includes("IdOrParams")) return "get";
10
12
  if (content.includes("useQuery")) return "query";
11
13
  }
12
14
  async function createHook(path, hookMap) {
@@ -28,9 +30,7 @@ ${match[0]}
28
30
  export const ${name}Client = createRequestFn(${hasSchema ? `{
29
31
  fn: ${name}Action,
30
32
  schema: ${match[1]},
31
- }` : `{
32
- fn: ${name}Action,
33
- }`})
33
+ }` : `${name}Action`})
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,7 +56,7 @@ export function use${upName}<TOnMutateResult = unknown>({ onMutate, onSuccess, o
56
56
  })
57
57
  }
58
58
  `;
59
- const queryHook = `import { createRequestFn } from "deepsea-tools"
59
+ const getHook = `import { createRequestFn, isNonNullable } from "deepsea-tools"
60
60
  import { createUseQuery } from "soda-tanstack-query"
61
61
 
62
62
  import { ${name}Action } from "@/actions/${join(dir, name)}"
@@ -70,12 +70,36 @@ export const ${name}Client = createRequestFn(${hasSchema ? `{
70
70
  fn: ${name}Action,
71
71
  }`})
72
72
 
73
+ export function ${name}ClientOptional(id?: ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, (char)=>char.toUpperCase())} | ` : ""}undefined) {
74
+ return isNonNullable(id) ? ${name}Client(id) : null
75
+ }
76
+
77
+ export const use${upName} = createUseQuery({
78
+ queryFn: ${name}ClientOptional,
79
+ queryKey: "${key}",
80
+ })
81
+ `;
82
+ const queryHook = `import { createRequestFn } from "deepsea-tools"
83
+ import { createUseQuery } from "soda-tanstack-query"
84
+
85
+ import { ${name}Action } from "@/actions/${join(dir, name)}"
86
+ ${hasSchema ? `
87
+ ${match[0]}
88
+ ` : ""}
89
+ export const ${name}Client = createRequestFn(${hasSchema ? `{
90
+ fn: ${name}Action,
91
+ schema: ${match[1]},
92
+ }` : `{
93
+ fn: ${name}Action,
94
+ }`})
95
+
73
96
  export const use${upName} = createUseQuery({
74
97
  queryFn: ${name}Client,
75
98
  queryKey: "${key}",
76
99
  })
77
100
  `;
78
101
  const map = {
102
+ get: getHook,
79
103
  query: queryHook,
80
104
  mutation: mutationHook
81
105
  };
@@ -123,6 +147,7 @@ async function hook(options, { args }) {
123
147
  choices: [
124
148
  "mutation",
125
149
  "query",
150
+ "get",
126
151
  "skip"
127
152
  ],
128
153
  default: type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdnext",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
package/src/utils/hook.ts CHANGED
@@ -4,17 +4,19 @@ 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 = "query" | "mutation"
7
+ export type HookType = "get" | "query" | "mutation"
8
8
 
9
9
  export type HookContentMap = Record<HookType, string>
10
10
 
11
11
  function getHookTypeFromName(name: string): HookType {
12
- if (/^(get|query)[^a-z]/.test(name)) return "query"
12
+ if (/^get[^a-z]/.test(name)) return "get"
13
+ if (/^query[^a-z]/.test(name)) return "query"
13
14
  return "mutation"
14
15
  }
15
16
 
16
17
  function getHookTypeFromContent(content: string): HookType | undefined {
17
18
  if (content.includes("useMutation")) return "mutation"
19
+ if (content.includes("IdOrParams")) return "get"
18
20
  if (content.includes("useQuery")) return "query"
19
21
  return undefined
20
22
  }
@@ -51,9 +53,7 @@ export const ${name}Client = createRequestFn(${
51
53
  fn: ${name}Action,
52
54
  schema: ${match[1]},
53
55
  }`
54
- : `{
55
- fn: ${name}Action,
56
- }`
56
+ : `${name}Action`
57
57
  })
58
58
 
59
59
  export interface Use${upName}Params<TOnMutateResult = unknown> extends Omit<
@@ -81,7 +81,7 @@ export function use${upName}<TOnMutateResult = unknown>({ onMutate, onSuccess, o
81
81
  }
82
82
  `
83
83
 
84
- const queryHook = `import { createRequestFn } from "deepsea-tools"
84
+ const getHook = `import { createRequestFn, isNonNullable } from "deepsea-tools"
85
85
  import { createUseQuery } from "soda-tanstack-query"
86
86
 
87
87
  import { ${name}Action } from "@/actions/${join(dir, name)}"
@@ -103,6 +103,38 @@ export const ${name}Client = createRequestFn(${
103
103
  }`
104
104
  })
105
105
 
106
+ export function ${name}ClientOptional(id?: ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, char => char.toUpperCase())} | ` : ""}undefined) {
107
+ return isNonNullable(id) ? ${name}Client(id) : null
108
+ }
109
+
110
+ export const use${upName} = createUseQuery({
111
+ queryFn: ${name}ClientOptional,
112
+ queryKey: "${key}",
113
+ })
114
+ `
115
+
116
+ const queryHook = `import { createRequestFn } from "deepsea-tools"
117
+ import { createUseQuery } from "soda-tanstack-query"
118
+
119
+ import { ${name}Action } from "@/actions/${join(dir, name)}"
120
+ ${
121
+ hasSchema
122
+ ? `
123
+ ${match[0]}
124
+ `
125
+ : ""
126
+ }
127
+ export const ${name}Client = createRequestFn(${
128
+ hasSchema
129
+ ? `{
130
+ fn: ${name}Action,
131
+ schema: ${match[1]},
132
+ }`
133
+ : `{
134
+ fn: ${name}Action,
135
+ }`
136
+ })
137
+
106
138
  export const use${upName} = createUseQuery({
107
139
  queryFn: ${name}Client,
108
140
  queryKey: "${key}",
@@ -110,6 +142,7 @@ export const use${upName} = createUseQuery({
110
142
  `
111
143
 
112
144
  const map: HookContentMap = {
145
+ get: getHook,
113
146
  query: queryHook,
114
147
  mutation: mutationHook,
115
148
  }
@@ -179,7 +212,7 @@ export async function hook(options: Record<string, string>, { args }: Command) {
179
212
 
180
213
  const answer = await select<OperationType>({
181
214
  message: path,
182
- choices: ["mutation", "query", "skip"],
215
+ choices: ["mutation", "query", "get", "skip"],
183
216
  default: type,
184
217
  })
185
218