@peng_kai/kit 0.1.14 → 0.1.15

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.
Files changed (39) hide show
  1. package/admin/components/filter/src/FilterDrawer.vue +153 -153
  2. package/admin/components/filter/src/FilterParam.vue +78 -78
  3. package/admin/components/scroll-nav/index.ts +1 -1
  4. package/admin/components/scroll-nav/src/ScrollNav.vue +59 -59
  5. package/admin/components/text/index.ts +13 -13
  6. package/admin/components/text/src/Amount.vue +121 -121
  7. package/admin/components/text/src/Datetime.vue +48 -48
  8. package/admin/components/text/src/Duration.vue +26 -26
  9. package/admin/components/text/src/Hash.vue +51 -51
  10. package/admin/components/text/src/createTagGetter.ts +13 -13
  11. package/admin/hooks/useMenu.ts +128 -128
  12. package/admin/hooks/usePage.ts +141 -141
  13. package/admin/hooks/usePageTab.ts +35 -35
  14. package/admin/layout/large/Breadcrumb.vue +69 -69
  15. package/admin/layout/large/Content.vue +24 -24
  16. package/admin/layout/large/Menu.vue +69 -69
  17. package/admin/layout/large/PageTab.vue +71 -71
  18. package/admin/permission/index.ts +4 -4
  19. package/admin/permission/routerGuard.ts +43 -43
  20. package/admin/permission/usePermission.ts +52 -52
  21. package/admin/permission/vuePlugin.ts +30 -30
  22. package/admin/route-guards/index.ts +3 -3
  23. package/admin/route-guards/pageProgress.ts +27 -27
  24. package/admin/route-guards/pageTitle.ts +19 -19
  25. package/admin/styles/globalCover.scss +54 -54
  26. package/antd/components/InputNumberRange.vue +59 -59
  27. package/antd/directives/formLabelAlign.ts +36 -36
  28. package/antd/hooks/useAntdDrawer.ts +73 -73
  29. package/antd/hooks/useAntdTable.ts +115 -115
  30. package/package.json +1 -1
  31. package/request/helpers.ts +49 -49
  32. package/request/interceptors/toLogin.ts +26 -26
  33. package/request/type.d.ts +92 -92
  34. package/stylelint.config.cjs +7 -7
  35. package/tsconfig.json +50 -50
  36. package/utils/index.ts +2 -0
  37. package/vue/components/infinite-query/index.ts +1 -1
  38. package/vue/components/infinite-query/src/InfiniteQuery.vue +205 -205
  39. package/vue/components/infinite-query/src/useCreateTrigger.ts +39 -39
@@ -1,115 +1,115 @@
1
- import { computed, reactive, ref } from 'vue';
2
- import { pick } from 'lodash-es';
3
- import type { UseQueryReturnType } from '@tanstack/vue-query';
4
- import type { Table, TableProps } from 'ant-design-vue';
5
- import type { ColumnType, FilterValue } from 'ant-design-vue/es/table/interface';
6
- import type { ComponentProps } from 'vue-component-type-helpers';
7
-
8
- interface ISorter {
9
- field?: string
10
- order?: string
11
- }
12
-
13
- export function useAntdTable<
14
- UQRR extends UseQueryReturnType<any, any>,
15
- QP extends Partial<{ page?: string | number, page_size?: string | number }>,
16
- >(uqrt: UQRR, queryParams: QP = ({} as any)) {
17
- type RecordType = GetRecordType<UQRR>;
18
- type LocalTableProps = TableProps<RecordType>;
19
- type LocalColumnsType = NonNullable<LocalTableProps['columns']>;
20
- type LocalTableRowSelection = NonNullable<LocalTableProps['rowSelection']>;
21
-
22
- const { data, isFetching, isLoading } = uqrt;
23
- const filters = ref<Record<string, FilterValue>>();
24
- const sorter = ref<ISorter>();
25
- const sorters = ref<ISorter[]>();
26
-
27
- const onChange: ComponentProps<typeof Table>['onChange'] = (_pagination, _filters, _sorter, extra) => {
28
- if (extra.action === 'paginate') {
29
- const page = queryParams.page_size !== _pagination.pageSize ? 1 : _pagination.current;
30
- Object.assign(queryParams, { page, page_size: _pagination.pageSize ?? 10 });
31
- }
32
- else if (extra.action === 'filter') {
33
- filters.value = _filters;
34
- }
35
- else if (extra.action === 'sort') {
36
- if (Array.isArray(_sorter)) {
37
- sorter.value = pick(_sorter[0], ['field', 'order']) as any;
38
- sorters.value = _sorter.map(item => pick(item, ['field', 'order'])) as any;
39
- }
40
- else {
41
- sorter.value = pick(_sorter, ['field', 'order']) as any;
42
- sorters.value = [{ ...sorter.value }];
43
- }
44
- }
45
- };
46
- const defineColumns = (columnsGetter: () => LocalColumnsType) => computed(columnsGetter);
47
- const defineRowSelection = (rowSelectionGetter: () => LocalTableRowSelection) => {
48
- const rowSelection = reactive(rowSelectionGetter());
49
-
50
- rowSelection.selectedRowKeys ??= [];
51
- rowSelection.onChange ??= keys => rowSelection.selectedRowKeys = keys;
52
-
53
- return rowSelection;
54
- };
55
-
56
- const tableProps = computed<LocalTableProps>(() => {
57
- const { list, pagination } = data.value ?? {};
58
-
59
- return {
60
- dataSource: list,
61
- pagination: {
62
- disabled: isFetching.value,
63
- current: Number(queryParams.page ?? 1),
64
- pageSize: Number(queryParams.page_size ?? 10),
65
- total: pagination?.total ?? 0,
66
- showSizeChanger: true,
67
- showQuickJumper: true,
68
- showTotal: total => `共 ${total} 条`,
69
- },
70
- loading: isLoading.value,
71
- scroll: { x: 'max-content' },
72
- sticky: true,
73
- onChange: onChange as any,
74
- };
75
- });
76
- const dataIndexs = new Proxy({} as Record<keyof RecordType, string>, {
77
- get(_, p) {
78
- return p;
79
- },
80
- });
81
- const bodyCellType = {} as {
82
- index: number
83
- text: any
84
- value: any
85
- record: RecordType
86
- column: ColumnType<RecordType>
87
- };
88
-
89
- return {
90
- /** ATable 的预设 Props */
91
- tableProps,
92
- /* 过滤的字段 */
93
- filters,
94
- /* 排序的字段 */
95
- sorter,
96
- /* 多维排序的字段 */
97
- sorters,
98
- /** 【类型辅助】基于接口数据类型推导出的 dataIndex,供 columns 的 dataIndex 使用 */
99
- dataIndexs,
100
- /** 【类型辅助】bodyCell 插槽数据的精确类型描述 */
101
- bodyCellType,
102
- /** 【类型辅助】用于定义出类型精确的 columns */
103
- defineColumns,
104
- /** 【类型辅助】用于定义出类型精确的 rowSelection */
105
- defineRowSelection,
106
- /** 内置的 ATable onChange 事件,默认在 `tableProps` 中 */
107
- onChange,
108
- };
109
- }
110
-
111
- type GetRecordType<T> = T extends UseQueryReturnType<infer D, any>
112
- ? D extends Api.PageData
113
- ? NonNullable<D['list']>[0]
114
- : never
115
- : never;
1
+ import { computed, reactive, ref } from 'vue';
2
+ import { pick } from 'lodash-es';
3
+ import type { UseQueryReturnType } from '@tanstack/vue-query';
4
+ import type { Table, TableProps } from 'ant-design-vue';
5
+ import type { ColumnType, FilterValue } from 'ant-design-vue/es/table/interface';
6
+ import type { ComponentProps } from 'vue-component-type-helpers';
7
+
8
+ interface ISorter {
9
+ field?: string
10
+ order?: string
11
+ }
12
+
13
+ export function useAntdTable<
14
+ UQRR extends UseQueryReturnType<any, any>,
15
+ QP extends Partial<{ page?: string | number, page_size?: string | number }>,
16
+ >(uqrt: UQRR, queryParams: QP = ({} as any)) {
17
+ type RecordType = GetRecordType<UQRR>;
18
+ type LocalTableProps = TableProps<RecordType>;
19
+ type LocalColumnsType = NonNullable<LocalTableProps['columns']>;
20
+ type LocalTableRowSelection = NonNullable<LocalTableProps['rowSelection']>;
21
+
22
+ const { data, isFetching, isLoading } = uqrt;
23
+ const filters = ref<Record<string, FilterValue>>();
24
+ const sorter = ref<ISorter>();
25
+ const sorters = ref<ISorter[]>();
26
+
27
+ const onChange: ComponentProps<typeof Table>['onChange'] = (_pagination, _filters, _sorter, extra) => {
28
+ if (extra.action === 'paginate') {
29
+ const page = queryParams.page_size !== _pagination.pageSize ? 1 : _pagination.current;
30
+ Object.assign(queryParams, { page, page_size: _pagination.pageSize ?? 10 });
31
+ }
32
+ else if (extra.action === 'filter') {
33
+ filters.value = _filters;
34
+ }
35
+ else if (extra.action === 'sort') {
36
+ if (Array.isArray(_sorter)) {
37
+ sorter.value = pick(_sorter[0], ['field', 'order']) as any;
38
+ sorters.value = _sorter.map(item => pick(item, ['field', 'order'])) as any;
39
+ }
40
+ else {
41
+ sorter.value = pick(_sorter, ['field', 'order']) as any;
42
+ sorters.value = [{ ...sorter.value }];
43
+ }
44
+ }
45
+ };
46
+ const defineColumns = (columnsGetter: () => LocalColumnsType) => computed(columnsGetter);
47
+ const defineRowSelection = (rowSelectionGetter: () => LocalTableRowSelection) => {
48
+ const rowSelection = reactive(rowSelectionGetter());
49
+
50
+ rowSelection.selectedRowKeys ??= [];
51
+ rowSelection.onChange ??= keys => rowSelection.selectedRowKeys = keys;
52
+
53
+ return rowSelection;
54
+ };
55
+
56
+ const tableProps = computed<LocalTableProps>(() => {
57
+ const { list, pagination } = data.value ?? {};
58
+
59
+ return {
60
+ dataSource: list,
61
+ pagination: {
62
+ disabled: isFetching.value,
63
+ current: Number(queryParams.page ?? 1),
64
+ pageSize: Number(queryParams.page_size ?? 10),
65
+ total: pagination?.total ?? 0,
66
+ showSizeChanger: true,
67
+ showQuickJumper: true,
68
+ showTotal: total => `共 ${total} 条`,
69
+ },
70
+ loading: isLoading.value,
71
+ scroll: { x: 'max-content' },
72
+ sticky: true,
73
+ onChange: onChange as any,
74
+ };
75
+ });
76
+ const dataIndexs = new Proxy({} as Record<keyof RecordType, string>, {
77
+ get(_, p) {
78
+ return p;
79
+ },
80
+ });
81
+ const bodyCellType = {} as {
82
+ index: number
83
+ text: any
84
+ value: any
85
+ record: RecordType
86
+ column: ColumnType<RecordType>
87
+ };
88
+
89
+ return {
90
+ /** ATable 的预设 Props */
91
+ tableProps,
92
+ /* 过滤的字段 */
93
+ filters,
94
+ /* 排序的字段 */
95
+ sorter,
96
+ /* 多维排序的字段 */
97
+ sorters,
98
+ /** 【类型辅助】基于接口数据类型推导出的 dataIndex,供 columns 的 dataIndex 使用 */
99
+ dataIndexs,
100
+ /** 【类型辅助】bodyCell 插槽数据的精确类型描述 */
101
+ bodyCellType,
102
+ /** 【类型辅助】用于定义出类型精确的 columns */
103
+ defineColumns,
104
+ /** 【类型辅助】用于定义出类型精确的 rowSelection */
105
+ defineRowSelection,
106
+ /** 内置的 ATable onChange 事件,默认在 `tableProps` 中 */
107
+ onChange,
108
+ };
109
+ }
110
+
111
+ type GetRecordType<T> = T extends UseQueryReturnType<infer D, any>
112
+ ? D extends Api.PageData
113
+ ? NonNullable<D['list']>[0]
114
+ : never
115
+ : never;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peng_kai/kit",
3
3
  "type": "module",
4
- "version": "0.1.14",
4
+ "version": "0.1.15",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "ISC",
@@ -1,49 +1,49 @@
1
- import type { AxiosInstance } from 'axios';
2
-
3
- export { isTimeout, getServices, ApiCode, ApiError };
4
-
5
- enum ApiCode {
6
- NORMAL = 0, // 正常
7
- UNKNOWN = -1, // 未知错误
8
- TIMEOUT = -2, // 请求超时
9
- }
10
-
11
- function getServices() {
12
- const serversModule = getServices.modules;
13
- const servers: Record<string, { server: AxiosInstance } | undefined> = {};
14
-
15
- for (const [key, module] of Object.entries(serversModule)) {
16
- const name = key.match(/\/([0-9a-zA-Z]+)\.ts$/)?.[1];
17
-
18
- if (name)
19
- servers[`${name}.api`] = module as any;
20
- }
21
-
22
- return servers;
23
- }
24
- getServices.modules = {} as any;
25
-
26
- function isTimeout(error: any) {
27
- return error?.message?.toLowerCase().includes('timeout');
28
- }
29
-
30
- class ApiError<TResp = any> extends Error {
31
- public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
32
- return !!error?.isApiError;
33
- }
34
-
35
- /**
36
- * 将HTTP status和Api code统一到一个code里
37
- */
38
- public code: number;
39
- public msg: string;
40
- public data: TResp;
41
- public isApiError = true;
42
-
43
- public constructor(info: { code: number, msg: string, data: TResp }) {
44
- super(info.msg);
45
- this.code = info.code;
46
- this.msg = info.msg;
47
- this.data = info.data;
48
- }
49
- }
1
+ import type { AxiosInstance } from 'axios';
2
+
3
+ export { isTimeout, getServices, ApiCode, ApiError };
4
+
5
+ enum ApiCode {
6
+ NORMAL = 0, // 正常
7
+ UNKNOWN = -1, // 未知错误
8
+ TIMEOUT = -2, // 请求超时
9
+ }
10
+
11
+ function getServices() {
12
+ const serversModule = getServices.modules;
13
+ const servers: Record<string, { server: AxiosInstance } | undefined> = {};
14
+
15
+ for (const [key, module] of Object.entries(serversModule)) {
16
+ const name = key.match(/\/([0-9a-zA-Z]+)\.ts$/)?.[1];
17
+
18
+ if (name)
19
+ servers[`${name}.api`] = module as any;
20
+ }
21
+
22
+ return servers;
23
+ }
24
+ getServices.modules = {} as any;
25
+
26
+ function isTimeout(error: any) {
27
+ return error?.message?.toLowerCase().includes('timeout');
28
+ }
29
+
30
+ class ApiError<TResp = any> extends Error {
31
+ public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
32
+ return !!error?.isApiError;
33
+ }
34
+
35
+ /**
36
+ * 将HTTP status和Api code统一到一个code里
37
+ */
38
+ public code: number;
39
+ public msg: string;
40
+ public data: TResp;
41
+ public isApiError = true;
42
+
43
+ public constructor(info: { code: number, msg: string, data: TResp }) {
44
+ super(info.msg);
45
+ this.code = info.code;
46
+ this.msg = info.msg;
47
+ this.data = info.data;
48
+ }
49
+ }
@@ -1,26 +1,26 @@
1
- import type { AxiosInterceptorManager } from 'axios';
2
-
3
- const REDIRECT_KEY = 'redirect';
4
-
5
- export function toLogin(authPath = `${window.location.origin}/auth/login`, code = 15001): Parameters<AxiosInterceptorManager<any>['use']> {
6
- return [
7
- undefined,
8
- (err) => {
9
- if (err.code !== code)
10
- throw err;
11
-
12
- const currentURL = new URL(window.location.href);
13
- const targetURL = new URL(authPath);
14
-
15
- if (currentURL.searchParams.has(REDIRECT_KEY))
16
- targetURL.searchParams.set(REDIRECT_KEY, currentURL.searchParams.get(REDIRECT_KEY)!);
17
- else
18
- targetURL.searchParams.set(REDIRECT_KEY, window.location.href);
19
-
20
- if (currentURL.toString() === targetURL.toString())
21
- return;
22
-
23
- window.location.href = targetURL.toString();
24
- },
25
- ];
26
- }
1
+ import type { AxiosInterceptorManager } from 'axios';
2
+
3
+ const REDIRECT_KEY = 'redirect';
4
+
5
+ export function toLogin(authPath = `${window.location.origin}/auth/login`, code = 15001): Parameters<AxiosInterceptorManager<any>['use']> {
6
+ return [
7
+ undefined,
8
+ (err) => {
9
+ if (err.code !== code)
10
+ throw err;
11
+
12
+ const currentURL = new URL(window.location.href);
13
+ const targetURL = new URL(authPath);
14
+
15
+ if (currentURL.searchParams.has(REDIRECT_KEY))
16
+ targetURL.searchParams.set(REDIRECT_KEY, currentURL.searchParams.get(REDIRECT_KEY)!);
17
+ else
18
+ targetURL.searchParams.set(REDIRECT_KEY, window.location.href);
19
+
20
+ if (currentURL.toString() === targetURL.toString())
21
+ return;
22
+
23
+ window.location.href = targetURL.toString();
24
+ },
25
+ ];
26
+ }
package/request/type.d.ts CHANGED
@@ -1,92 +1,92 @@
1
- declare namespace Api {
2
- type Request = ((reqData: any, options?: Options) => Promise<any>) & {
3
- id: string
4
- setDefaultConfig: (config: Partial<import('axios').AxiosRequestConfig>) => Request
5
- };
6
- // type RequestPagination = (reqData: Partial<PageParam>, options?: Options) => Promise<PaginationData<any>>;
7
- interface PageParam {
8
- page: number
9
- page_size: number
10
- }
11
- interface PageInfo {
12
- has_more: boolean
13
- page: number
14
- page_size: number
15
- total: number
16
- }
17
- interface PageData<T = any> {
18
- list: T[] | null
19
- pagination: PaginationInfo
20
- [k in string]: any
21
- }
22
- interface Result<T = any | PageData<any>> {
23
- code: number
24
- msg: string
25
- data: T
26
- }
27
- type GetParam<A extends Request> = A extends (reqData: infer R) => any ? R : any;
28
- type GetData<A extends Request> = ReturnType<A> extends Promise<infer D> ? D : any;
29
- type GetDataItem<A extends Request> = NonNullable<GetData<A>> extends { list: infer L }
30
- ? NonNullable<L> extends Array<infer I>
31
- ? I
32
- : any
33
- : any;
34
- type GetDataField<R> = R extends { data: infer D } ? (D extends { list: any, pagination: any } ? D : D) : any;
35
-
36
- /**
37
- * 将api返回的分页数据转换成前端使用的分页数据格式,将分页数据格式统一
38
- *
39
- * ```
40
- * {
41
- * code: number,
42
- * msg: string,
43
- * data: {
44
- * list: [],
45
- * pagination: {},
46
- * ...
47
- * }
48
- * }
49
- * ```
50
- */
51
- type TransformPageResult<R> = R extends { pagination: infer P, data: infer D }
52
- ? D extends Record<string, any>
53
- ? D extends { list: any }
54
- ? {
55
- [Rk in Exclude<keyof R, 'data' | 'pagination'>]: R[Rk];
56
- } & {
57
- data: D & { pagination: P }
58
- }
59
- : {
60
- [Rk in Exclude<keyof R, 'data' | 'pagination'>]: R[Rk];
61
- } & {
62
- data: {
63
- list: D
64
- pagination: P
65
- }
66
- }
67
- : R
68
- : R;
69
- }
70
-
71
- // 测试用例 ----------------------------
72
- // type DDD1 = Api.TransformPageResult<{
73
- // code: number
74
- // msg: string
75
- // data: {
76
- // name: string
77
- // }[]
78
- // pagination: {
79
- // page: number
80
- // }
81
- // }>
82
- // type DDD2 = Api.TransformPageResult<{
83
- // code: number
84
- // msg: string
85
- // data: {
86
- // list: { name: string }[]
87
- // total: {}
88
- // }
89
- // pagination: {
90
- // page: number
91
- // }
92
- // }>
1
+ declare namespace Api {
2
+ type Request = ((reqData: any, options?: Options) => Promise<any>) & {
3
+ id: string
4
+ setDefaultConfig: (config: Partial<import('axios').AxiosRequestConfig>) => Request
5
+ };
6
+ // type RequestPagination = (reqData: Partial<PageParam>, options?: Options) => Promise<PaginationData<any>>;
7
+ interface PageParam {
8
+ page: number
9
+ page_size: number
10
+ }
11
+ interface PageInfo {
12
+ has_more: boolean
13
+ page: number
14
+ page_size: number
15
+ total: number
16
+ }
17
+ interface PageData<T = any> {
18
+ list: T[] | null
19
+ pagination: PaginationInfo
20
+ [k in string]: any
21
+ }
22
+ interface Result<T = any | PageData<any>> {
23
+ code: number
24
+ msg: string
25
+ data: T
26
+ }
27
+ type GetParam<A extends Request> = A extends (reqData: infer R) => any ? R : any;
28
+ type GetData<A extends Request> = ReturnType<A> extends Promise<infer D> ? D : any;
29
+ type GetDataItem<A extends Request> = NonNullable<GetData<A>> extends { list: infer L }
30
+ ? NonNullable<L> extends Array<infer I>
31
+ ? I
32
+ : any
33
+ : any;
34
+ type GetDataField<R> = R extends { data: infer D } ? (D extends { list: any, pagination: any } ? D : D) : any;
35
+
36
+ /**
37
+ * 将api返回的分页数据转换成前端使用的分页数据格式,将分页数据格式统一
38
+ *
39
+ * ```
40
+ * {
41
+ * code: number,
42
+ * msg: string,
43
+ * data: {
44
+ * list: [],
45
+ * pagination: {},
46
+ * ...
47
+ * }
48
+ * }
49
+ * ```
50
+ */
51
+ type TransformPageResult<R> = R extends { pagination: infer P, data: infer D }
52
+ ? D extends Record<string, any>
53
+ ? D extends { list: any }
54
+ ? {
55
+ [Rk in Exclude<keyof R, 'data' | 'pagination'>]: R[Rk];
56
+ } & {
57
+ data: D & { pagination: P }
58
+ }
59
+ : {
60
+ [Rk in Exclude<keyof R, 'data' | 'pagination'>]: R[Rk];
61
+ } & {
62
+ data: {
63
+ list: D
64
+ pagination: P
65
+ }
66
+ }
67
+ : R
68
+ : R;
69
+ }
70
+
71
+ // 测试用例 ----------------------------
72
+ // type DDD1 = Api.TransformPageResult<{
73
+ // code: number
74
+ // msg: string
75
+ // data: {
76
+ // name: string
77
+ // }[]
78
+ // pagination: {
79
+ // page: number
80
+ // }
81
+ // }>
82
+ // type DDD2 = Api.TransformPageResult<{
83
+ // code: number
84
+ // msg: string
85
+ // data: {
86
+ // list: { name: string }[]
87
+ // total: {}
88
+ // }
89
+ // pagination: {
90
+ // page: number
91
+ // }
92
+ // }>
@@ -1,7 +1,7 @@
1
- module.exports = {
2
- // root: true,
3
- extends: ['@peng_kai/lint/vue/stylelint_v2'],
4
- rules: {
5
- 'custom-property-pattern': '(antd-([a-z][a-zA-Z0-9]+)*)|(([a-z][a-z0-9]*)(-[a-z0-9]+)*)',
6
- },
7
- };
1
+ module.exports = {
2
+ // root: true,
3
+ extends: ['@peng_kai/lint/vue/stylelint_v2'],
4
+ rules: {
5
+ 'custom-property-pattern': '(antd-([a-z][a-zA-Z0-9]+)*)|(([a-z][a-z0-9]*)(-[a-z0-9]+)*)',
6
+ },
7
+ };