sdnext 0.0.5 → 0.0.6

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,34 @@ export const ${name}Client = createRequestFn(${hasSchema ? `{
70
70
  fn: ${name}Action,
71
71
  }`})
72
72
 
73
+ export const ${name}ClientOptional = (id?: ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, (char)=>char.toUpperCase())} | ` : ""}undefined) => (isNonNullable(id) ? ${name}Client(id) : null)
74
+
75
+ export const use${upName} = createUseQuery({
76
+ queryFn: ${name}Client,
77
+ queryKey: "${key}",
78
+ })
79
+ `;
80
+ const queryHook = `import { createRequestFn } from "deepsea-tools"
81
+ import { createUseQuery } from "soda-tanstack-query"
82
+
83
+ import { ${name}Action } from "@/actions/${join(dir, name)}"
84
+ ${hasSchema ? `
85
+ ${match[0]}
86
+ ` : ""}
87
+ export const ${name}Client = createRequestFn(${hasSchema ? `{
88
+ fn: ${name}Action,
89
+ schema: ${match[1]},
90
+ }` : `{
91
+ fn: ${name}Action,
92
+ }`})
93
+
73
94
  export const use${upName} = createUseQuery({
74
95
  queryFn: ${name}Client,
75
96
  queryKey: "${key}",
76
97
  })
77
98
  `;
78
99
  const map = {
100
+ get: getHook,
79
101
  query: queryHook,
80
102
  mutation: mutationHook
81
103
  };
@@ -123,6 +145,7 @@ async function hook(options, { args }) {
123
145
  choices: [
124
146
  "mutation",
125
147
  "query",
148
+ "get",
126
149
  "skip"
127
150
  ],
128
151
  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.6",
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,36 @@ export const ${name}Client = createRequestFn(${
103
103
  }`
104
104
  })
105
105
 
106
+ export const ${name}ClientOptional = (id?: ${hasSchema ? `${match[1].replace(/Schema$/, "Params").replace(/^./, char => char.toUpperCase())} | ` : ""}undefined) => (isNonNullable(id) ? ${name}Client(id) : null)
107
+
108
+ export const use${upName} = createUseQuery({
109
+ queryFn: ${name}Client,
110
+ queryKey: "${key}",
111
+ })
112
+ `
113
+
114
+ const queryHook = `import { createRequestFn } from "deepsea-tools"
115
+ import { createUseQuery } from "soda-tanstack-query"
116
+
117
+ import { ${name}Action } from "@/actions/${join(dir, name)}"
118
+ ${
119
+ hasSchema
120
+ ? `
121
+ ${match[0]}
122
+ `
123
+ : ""
124
+ }
125
+ export const ${name}Client = createRequestFn(${
126
+ hasSchema
127
+ ? `{
128
+ fn: ${name}Action,
129
+ schema: ${match[1]},
130
+ }`
131
+ : `{
132
+ fn: ${name}Action,
133
+ }`
134
+ })
135
+
106
136
  export const use${upName} = createUseQuery({
107
137
  queryFn: ${name}Client,
108
138
  queryKey: "${key}",
@@ -110,6 +140,7 @@ export const use${upName} = createUseQuery({
110
140
  `
111
141
 
112
142
  const map: HookContentMap = {
143
+ get: getHook,
113
144
  query: queryHook,
114
145
  mutation: mutationHook,
115
146
  }
@@ -179,7 +210,7 @@ export async function hook(options: Record<string, string>, { args }: Command) {
179
210
 
180
211
  const answer = await select<OperationType>({
181
212
  message: path,
182
- choices: ["mutation", "query", "skip"],
213
+ choices: ["mutation", "query", "get", "skip"],
183
214
  default: type,
184
215
  })
185
216