zixulu 1.81.1 → 1.81.2
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/dist/index.js +96 -128
- package/dist/index.js.map +1 -1
- package/dist/src/utils/addApi.d.ts +8 -1
- package/package.json +9 -9
- package/src/index.ts +5 -3
- package/src/utils/addApi.ts +96 -125
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import chalk from "chalk";
|
|
3
|
-
import { Command } from "commander";
|
|
3
|
+
import { Command, Option } from "commander";
|
|
4
4
|
import consola, { consola as external_consola_consola } from "consola";
|
|
5
5
|
import { capitalize, emailReg, getEnumEntries, getEnumKeys, getEnumValues, isNonNullable, json2type } from "deepsea-tools";
|
|
6
6
|
import { compress, execAsync, isPathLike, setDefaultOptions, spawnAsync, unzip, zip } from "soda-nodejs";
|
|
@@ -357,6 +357,10 @@ export default Registry
|
|
|
357
357
|
`);
|
|
358
358
|
consola.success("添加 antd 配置成功");
|
|
359
359
|
}
|
|
360
|
+
const AddApiIdType = {
|
|
361
|
+
字符串: "string",
|
|
362
|
+
数字: "number"
|
|
363
|
+
};
|
|
360
364
|
async function addApi_writeFile(...args) {
|
|
361
365
|
const [path] = args;
|
|
362
366
|
if (isPathLike(path) && existsSync(path)) {
|
|
@@ -369,8 +373,9 @@ async function addApi_writeFile(...args) {
|
|
|
369
373
|
}
|
|
370
374
|
return await writeFile(...args);
|
|
371
375
|
}
|
|
372
|
-
async function addApi({ type, api, hook }) {
|
|
376
|
+
async function addApi({ type, api, hook, idType = AddApiIdType.字符串, name }) {
|
|
373
377
|
type = capitalize(type);
|
|
378
|
+
name ??= type;
|
|
374
379
|
api ??= "apis";
|
|
375
380
|
hook ??= "hooks";
|
|
376
381
|
await mkdir(api, {
|
|
@@ -392,7 +397,7 @@ async function addApi({ type, api, hook }) {
|
|
|
392
397
|
"delete"
|
|
393
398
|
]
|
|
394
399
|
});
|
|
395
|
-
const query = `import { Page } from "deepsea-tools"
|
|
400
|
+
const query = `import type { Page } from "deepsea-tools"
|
|
396
401
|
|
|
397
402
|
import { request } from "@/utils/request"
|
|
398
403
|
|
|
@@ -402,14 +407,10 @@ export interface Query${type}Params {
|
|
|
402
407
|
}
|
|
403
408
|
|
|
404
409
|
export interface ${type} {
|
|
405
|
-
id:
|
|
410
|
+
id: ${idType}
|
|
406
411
|
name: string
|
|
407
412
|
}
|
|
408
413
|
|
|
409
|
-
export type ${type}Id = ${type}["id"]
|
|
410
|
-
|
|
411
|
-
export const ${type}Name = "${type}"
|
|
412
|
-
|
|
413
414
|
export async function query${type}(params: Query${type}Params) {
|
|
414
415
|
const response = await request<Page<${type}>>("/${type2}/query", {
|
|
415
416
|
method: "POST",
|
|
@@ -417,11 +418,18 @@ export async function query${type}(params: Query${type}Params) {
|
|
|
417
418
|
})
|
|
418
419
|
return response
|
|
419
420
|
}
|
|
421
|
+
|
|
422
|
+
export async function getAll${type}() {
|
|
423
|
+
const response = await request<${type}[]>("/${type2}/getAll", {
|
|
424
|
+
method: "POST",
|
|
425
|
+
})
|
|
426
|
+
return response
|
|
427
|
+
}
|
|
420
428
|
`;
|
|
421
429
|
if (items.includes("query")) await addApi_writeFile(join(api, `query${type}.ts`), query);
|
|
422
430
|
const add = `import { request } from "@/utils/request"
|
|
423
431
|
|
|
424
|
-
import { ${type} } from "./query${type}"
|
|
432
|
+
import type { ${type} } from "./query${type}"
|
|
425
433
|
|
|
426
434
|
export interface Add${type}Params extends Pick<${type}, "name"> {}
|
|
427
435
|
|
|
@@ -436,7 +444,7 @@ export async function add${type}(params: Add${type}Params) {
|
|
|
436
444
|
if (items.includes("add")) await addApi_writeFile(join(api, `add${type}.ts`), add);
|
|
437
445
|
const update = `import { request } from "@/utils/request"
|
|
438
446
|
|
|
439
|
-
import { ${type} } from "./query${type}"
|
|
447
|
+
import type { ${type} } from "./query${type}"
|
|
440
448
|
|
|
441
449
|
export interface Update${type}Params extends Pick<${type}, "id" | "name"> {}
|
|
442
450
|
|
|
@@ -451,11 +459,9 @@ export async function update${type}(params: Update${type}Params) {
|
|
|
451
459
|
if (items.includes("update")) await addApi_writeFile(join(api, `update${type}.ts`), update);
|
|
452
460
|
const _delete = `import { request } from "@/utils/request"
|
|
453
461
|
|
|
454
|
-
import
|
|
455
|
-
|
|
456
|
-
export type Delete${type}Params = ${type}Id
|
|
462
|
+
import type { ${type} } from "./query${type}"
|
|
457
463
|
|
|
458
|
-
export async function delete${type}(id:
|
|
464
|
+
export async function delete${type}(id: ${idType}) {
|
|
459
465
|
const response = await request<${type}>(\`/${type2}/delete/\${id}\`, {
|
|
460
466
|
method: "DELETE",
|
|
461
467
|
})
|
|
@@ -465,11 +471,9 @@ export async function delete${type}(id: Delete${type}Params) {
|
|
|
465
471
|
if (items.includes("delete")) await addApi_writeFile(join(api, `delete${type}.ts`), _delete);
|
|
466
472
|
const get = `import { request } from "@/utils/request"
|
|
467
473
|
|
|
468
|
-
import
|
|
469
|
-
|
|
470
|
-
export type Get${type}Params = ${type}Id
|
|
474
|
+
import type { ${type} } from "./query${type}"
|
|
471
475
|
|
|
472
|
-
export async function get${type}(id:
|
|
476
|
+
export async function get${type}(id: ${idType}) {
|
|
473
477
|
const response = await request<${type}>(\`/${type2}/get/\${id}\`, {
|
|
474
478
|
method: "POST",
|
|
475
479
|
})
|
|
@@ -477,185 +481,147 @@ export async function get${type}(id: Get${type}Params) {
|
|
|
477
481
|
}
|
|
478
482
|
`;
|
|
479
483
|
if (items.includes("get")) await addApi_writeFile(join(api, `get${type}.ts`), get);
|
|
480
|
-
const useQuery = `import {
|
|
484
|
+
const useQuery = `import { createUseQuery } from "soda-tanstack-query"
|
|
481
485
|
|
|
482
|
-
import {
|
|
486
|
+
import { getAll${type}, query${type} } from "@/apis/query${type}"
|
|
483
487
|
|
|
484
|
-
export
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
488
|
+
export const useQuery${type} = createUseQuery({
|
|
489
|
+
queryFn: query${type},
|
|
490
|
+
queryKey: "query-${type2}",
|
|
491
|
+
})
|
|
492
|
+
|
|
493
|
+
export const useGetAll${type} = createUseQuery({
|
|
494
|
+
queryFn: getAll${type},
|
|
495
|
+
queryKey: "get-all-${type2}",
|
|
496
|
+
staleTime: Infinity,
|
|
497
|
+
gcTime: Infinity,
|
|
498
|
+
})
|
|
490
499
|
`;
|
|
491
500
|
if (items.includes("query")) await addApi_writeFile(join(hook, `useQuery${type}.ts`), useQuery);
|
|
492
|
-
const useGet = `import {
|
|
493
|
-
import {
|
|
501
|
+
const useGet = `import { isNonNullable } from "deepsea-tools"
|
|
502
|
+
import { createUseQuery } from "soda-tanstack-query"
|
|
494
503
|
|
|
495
|
-
import {
|
|
504
|
+
import { get${type} } from "@/apis/get${type}"
|
|
496
505
|
|
|
497
|
-
export
|
|
498
|
-
|
|
499
|
-
enabled?: boolean
|
|
506
|
+
export function get${type}Optional(params?: Parameters<typeof get${type}>[0] | undefined) {
|
|
507
|
+
return isNonNullable(params) ? get${type}(params) : null
|
|
500
508
|
}
|
|
501
509
|
|
|
502
|
-
export
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
queryKey: ["get-${type2}", id],
|
|
507
|
-
queryFn: isNonNullable(id) ? () => get${type}(id) : resolveNull,
|
|
508
|
-
enabled,
|
|
509
|
-
})
|
|
510
|
-
}
|
|
510
|
+
export const useGet${type} = createUseQuery({
|
|
511
|
+
queryFn: get${type}Optional,
|
|
512
|
+
queryKey: "get-${type2}",
|
|
513
|
+
})
|
|
511
514
|
`;
|
|
512
515
|
if (items.includes("get")) await addApi_writeFile(join(hook, `useGet${type}.ts`), useGet);
|
|
513
516
|
const useAdd = `import { useId } from "react"
|
|
514
|
-
import { UseMutationOptions, useMutation, useQueryClient } from "@tanstack/react-query"
|
|
515
517
|
|
|
516
|
-
import {
|
|
517
|
-
import { ${type}, ${type}Name } from "@/apis/query${type}"
|
|
518
|
+
import { createUseMutation } from "soda-tanstack-query"
|
|
518
519
|
|
|
519
|
-
|
|
520
|
-
extends Omit<UseMutationOptions<${type}, Error, Add${type}Params, TContext>, "mutationFn"> {}
|
|
520
|
+
import { add${type} } from "@/apis/add${type}"
|
|
521
521
|
|
|
522
|
-
export
|
|
522
|
+
export const useAdd${type} = createUseMutation(() => {
|
|
523
523
|
const key = useId()
|
|
524
|
-
const queryClient = useQueryClient()
|
|
525
524
|
|
|
526
|
-
return
|
|
525
|
+
return {
|
|
527
526
|
mutationFn: add${type},
|
|
528
|
-
onMutate(variables) {
|
|
527
|
+
onMutate(variables, context) {
|
|
529
528
|
message.open({
|
|
530
529
|
key,
|
|
531
530
|
type: "loading",
|
|
532
|
-
content:
|
|
531
|
+
content: ${JSON.stringify(`新增${name}中...`)},
|
|
533
532
|
duration: 0,
|
|
534
533
|
})
|
|
535
|
-
return onMutate?.(variables)
|
|
536
534
|
},
|
|
537
|
-
onSuccess(data, variables, context) {
|
|
535
|
+
onSuccess(data, variables, onMutateResult, context) {
|
|
536
|
+
context.client.invalidateQueries({ queryKey: ["query-${type2}"] })
|
|
537
|
+
context.client.invalidateQueries({ queryKey: ["get-${type2}"] })
|
|
538
|
+
|
|
538
539
|
message.open({
|
|
539
540
|
key,
|
|
540
541
|
type: "success",
|
|
541
|
-
content:
|
|
542
|
-
})
|
|
543
|
-
return onSuccess?.(data, variables, context)
|
|
544
|
-
},
|
|
545
|
-
onError(error, variables, context) {
|
|
546
|
-
message.open({
|
|
547
|
-
key,
|
|
548
|
-
type: "error",
|
|
549
|
-
content: \`新增\${${type}Name}失败\`,
|
|
542
|
+
content: ${JSON.stringify(`新增${name}成功`)},
|
|
550
543
|
})
|
|
551
|
-
return onError?.(error, variables, context)
|
|
552
544
|
},
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
return onSettled?.(data, error, variables, context)
|
|
545
|
+
onError(error, variables, onMutateResult, context) {
|
|
546
|
+
message.destroy(key)
|
|
556
547
|
},
|
|
557
|
-
|
|
558
|
-
}
|
|
559
|
-
}
|
|
548
|
+
onSettled(data, error, variables, onMutateResult, context) {},
|
|
549
|
+
}
|
|
550
|
+
})
|
|
560
551
|
`;
|
|
561
552
|
if (items.includes("add")) await addApi_writeFile(join(hook, `useAdd${type}.ts`), useAdd);
|
|
562
553
|
const useUpdate = `import { useId } from "react"
|
|
563
|
-
import { UseMutationOptions, useMutation, useQueryClient } from "@tanstack/react-query"
|
|
564
554
|
|
|
565
|
-
import {
|
|
566
|
-
import { Update${type}Params, update${type} } from "@/apis/update${type}"
|
|
555
|
+
import { createUseMutation } from "soda-tanstack-query"
|
|
567
556
|
|
|
568
|
-
|
|
569
|
-
extends Omit<UseMutationOptions<${type}, Error, Update${type}Params, TContext>, "mutationFn"> {}
|
|
557
|
+
import { update${type} } from "@/apis/update${type}"
|
|
570
558
|
|
|
571
|
-
export
|
|
559
|
+
export const useUpdate${type} = createUseMutation(() => {
|
|
572
560
|
const key = useId()
|
|
573
|
-
const queryClient = useQueryClient()
|
|
574
561
|
|
|
575
|
-
return
|
|
562
|
+
return {
|
|
576
563
|
mutationFn: update${type},
|
|
577
|
-
onMutate(variables) {
|
|
564
|
+
onMutate(variables, context) {
|
|
578
565
|
message.open({
|
|
579
566
|
key,
|
|
580
567
|
type: "loading",
|
|
581
|
-
content:
|
|
568
|
+
content: ${JSON.stringify(`修改${name}中...`)},
|
|
582
569
|
duration: 0,
|
|
583
570
|
})
|
|
584
|
-
return onMutate?.(variables)
|
|
585
571
|
},
|
|
586
|
-
onSuccess(data, variables, context) {
|
|
572
|
+
onSuccess(data, variables, onMutateResult, context) {
|
|
573
|
+
context.client.invalidateQueries({ queryKey: ["query-${type2}"] })
|
|
574
|
+
context.client.invalidateQueries({ queryKey: ["get-${type2}"] })
|
|
575
|
+
|
|
587
576
|
message.open({
|
|
588
577
|
key,
|
|
589
578
|
type: "success",
|
|
590
|
-
content:
|
|
579
|
+
content: ${JSON.stringify(`修改${name}成功`)},
|
|
591
580
|
})
|
|
592
|
-
return onSuccess?.(data, variables, context)
|
|
593
581
|
},
|
|
594
|
-
onError(error, variables, context) {
|
|
595
|
-
message.
|
|
596
|
-
key,
|
|
597
|
-
type: "error",
|
|
598
|
-
content: \`更新\${${type}Name}失败\`,
|
|
599
|
-
})
|
|
600
|
-
return onError?.(error, variables, context)
|
|
582
|
+
onError(error, variables, onMutateResult, context) {
|
|
583
|
+
message.destroy(key)
|
|
601
584
|
},
|
|
602
|
-
onSettled(data, error, variables, context) {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
return onSettled?.(data, error, variables, context)
|
|
606
|
-
},
|
|
607
|
-
...rest,
|
|
608
|
-
})
|
|
609
|
-
}
|
|
585
|
+
onSettled(data, error, variables, onMutateResult, context) {},
|
|
586
|
+
}
|
|
587
|
+
})
|
|
610
588
|
`;
|
|
611
589
|
if (items.includes("update")) await addApi_writeFile(join(hook, `useUpdate${type}.ts`), useUpdate);
|
|
612
590
|
const useDelete = `import { useId } from "react"
|
|
613
|
-
import { UseMutationOptions, useMutation, useQueryClient } from "@tanstack/react-query"
|
|
614
591
|
|
|
615
|
-
import {
|
|
616
|
-
import { ${type}, ${type}Name } from "@/apis/query${type}"
|
|
592
|
+
import { createUseMutation } from "soda-tanstack-query"
|
|
617
593
|
|
|
618
|
-
|
|
619
|
-
extends Omit<UseMutationOptions<${type}, Error, Delete${type}Params, TContext>, "mutationFn"> {}
|
|
594
|
+
import { delete${type} } from "@/apis/delete${type}"
|
|
620
595
|
|
|
621
|
-
export
|
|
596
|
+
export const useDelete${type} = createUseMutation(() => {
|
|
622
597
|
const key = useId()
|
|
623
|
-
const queryClient = useQueryClient()
|
|
624
598
|
|
|
625
|
-
return
|
|
599
|
+
return {
|
|
626
600
|
mutationFn: delete${type},
|
|
627
|
-
onMutate(variables) {
|
|
601
|
+
onMutate(variables, context) {
|
|
628
602
|
message.open({
|
|
629
603
|
key,
|
|
630
604
|
type: "loading",
|
|
631
|
-
content:
|
|
605
|
+
content: ${JSON.stringify(`删除${name}中...`)},
|
|
632
606
|
duration: 0,
|
|
633
607
|
})
|
|
634
|
-
return onMutate?.(variables)
|
|
635
608
|
},
|
|
636
|
-
onSuccess(data, variables, context) {
|
|
609
|
+
onSuccess(data, variables, onMutateResult, context) {
|
|
610
|
+
context.client.invalidateQueries({ queryKey: ["query-${type2}"] })
|
|
611
|
+
context.client.invalidateQueries({ queryKey: ["get-${type2}"] })
|
|
612
|
+
|
|
637
613
|
message.open({
|
|
638
614
|
key,
|
|
639
615
|
type: "success",
|
|
640
|
-
content:
|
|
641
|
-
})
|
|
642
|
-
return onSuccess?.(data, variables, context)
|
|
643
|
-
},
|
|
644
|
-
onError(error, variables, context) {
|
|
645
|
-
message.open({
|
|
646
|
-
key,
|
|
647
|
-
type: "error",
|
|
648
|
-
content: \`删除\${${type}Name}失败\`,
|
|
616
|
+
content: ${JSON.stringify(`删除${name}成功`)},
|
|
649
617
|
})
|
|
650
|
-
return onError?.(error, variables, context)
|
|
651
618
|
},
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
return onSettled?.(data, error, variables, context)
|
|
619
|
+
onError(error, variables, onMutateResult, context) {
|
|
620
|
+
message.destroy(key)
|
|
655
621
|
},
|
|
656
|
-
|
|
657
|
-
}
|
|
658
|
-
}
|
|
622
|
+
onSettled(data, error, variables, onMutateResult, context) {},
|
|
623
|
+
}
|
|
624
|
+
})
|
|
659
625
|
`;
|
|
660
626
|
if (items.includes("delete")) await addApi_writeFile(join(hook, `useDelete${type}.ts`), useDelete);
|
|
661
627
|
}
|
|
@@ -6489,10 +6455,12 @@ program.command("set-env").description("设置环境变量").argument("key", "
|
|
|
6489
6455
|
await setEnv(key, value);
|
|
6490
6456
|
consola.success("设置环境变量成功");
|
|
6491
6457
|
});
|
|
6492
|
-
program.command("add-api").description("添加 api 文件").argument("type", "api 类型").option("-a, --api <api>", "api 文件夹路径").option("-h, --hook <hook>", "hook 文件夹路径").action(async (type, { api, hook })=>addApi({
|
|
6458
|
+
program.command("add-api").description("添加 api 文件").argument("type", "api 类型").option("-a, --api <api>", "api 文件夹路径").option("-h, --hook <hook>", "hook 文件夹路径").addOption(new Option("--id-type <idType>", "id 类型,可选 string 或 number").choices(Object.values(AddApiIdType)).default(AddApiIdType["字符串"])).option("-n, --name <name>", "名称,默认与 type 一致").action(async (type, { api, hook, idType, name })=>addApi({
|
|
6493
6459
|
type,
|
|
6494
6460
|
api,
|
|
6495
|
-
hook
|
|
6461
|
+
hook,
|
|
6462
|
+
idType,
|
|
6463
|
+
name
|
|
6496
6464
|
}));
|
|
6497
6465
|
program.command("rslib").description("rslib 配置").action(rslib);
|
|
6498
6466
|
program.command("init-node").description("初始化 node 项目").action(initNode);
|