@peng_kai/kit 0.2.0-beta.25 → 0.2.0-beta.27

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,68 +1,68 @@
1
- import type { AxiosInstance } from 'axios';
2
-
3
- export { isTimeout, getServices, getBaseUrlByEnv, 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
- function getBaseUrlByEnv(service: string, env: Record<string, string>) {
31
- let serviceName = '';
32
-
33
- if (/^https?:\/\//.test(service)) {
34
- const fileName = service.split('/').pop();
35
- serviceName = fileName?.split('.').shift() ?? '';
36
- }
37
- else {
38
- serviceName = service;
39
- }
40
-
41
- if (!serviceName)
42
- return '';
43
-
44
- const [prefix, target] = env[`VITE_HTTP_PREFIX_${serviceName}`]?.split(' => ') ?? ['', ''];
45
-
46
- return (prefix && target) ? prefix : '';
47
- }
48
-
49
- class ApiError<TResp = any> extends Error {
50
- public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
51
- return !!error?.isApiError;
52
- }
53
-
54
- /**
55
- * 将HTTP status和Api code统一到一个code里
56
- */
57
- public code: number;
58
- public msg: string;
59
- public data: TResp;
60
- public isApiError = true;
61
-
62
- public constructor(info: { code: number, msg: string, data: TResp }) {
63
- super(info.msg);
64
- this.code = info.code;
65
- this.msg = info.msg;
66
- this.data = info.data;
67
- }
68
- }
1
+ import type { AxiosInstance } from 'axios';
2
+
3
+ export { isTimeout, getServices, getBaseUrlByEnv, 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
+ function getBaseUrlByEnv(service: string, env: Record<string, string>) {
31
+ let serviceName = '';
32
+
33
+ if (/^https?:\/\//.test(service)) {
34
+ const fileName = service.split('/').pop();
35
+ serviceName = fileName?.split('.').shift() ?? '';
36
+ }
37
+ else {
38
+ serviceName = service;
39
+ }
40
+
41
+ if (!serviceName)
42
+ return '';
43
+
44
+ const [prefix, target] = env[`VITE_HTTP_PREFIX_${serviceName}`]?.split(' => ') ?? ['', ''];
45
+
46
+ return (prefix && target) ? prefix : '';
47
+ }
48
+
49
+ class ApiError<TResp = any> extends Error {
50
+ public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
51
+ return !!error?.isApiError;
52
+ }
53
+
54
+ /**
55
+ * 将HTTP status和Api code统一到一个code里
56
+ */
57
+ public code: number;
58
+ public msg: string;
59
+ public data: TResp;
60
+ public isApiError = true;
61
+
62
+ public constructor(info: { code: number, msg: string, data: TResp }) {
63
+ super(info.msg);
64
+ this.code = info.code;
65
+ this.msg = info.msg;
66
+ this.data = info.data;
67
+ }
68
+ }
@@ -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
+ };
package/tsconfig.json CHANGED
@@ -1,50 +1,50 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "jsx": "preserve",
5
- "jsxFactory": "h",
6
- "jsxFragmentFactory": "Fragment",
7
- "lib": [
8
- "ESNext",
9
- "DOM",
10
- "DOM.Iterable"
11
- ],
12
- "useDefineForClassFields": true,
13
- "module": "ESNext",
14
-
15
- /* Bundler mode */
16
- "moduleResolution": "bundler",
17
- "resolveJsonModule": true,
18
- "allowImportingTsExtensions": true,
19
-
20
- /* Linting */
21
- "strict": true,
22
- "noFallthroughCasesInSwitch": true,
23
- "noImplicitAny": false,
24
- "noUnusedLocals": true,
25
- "noUnusedParameters": true,
26
- "noEmit": true,
27
- "allowSyntheticDefaultImports": true,
28
- "esModuleInterop": true,
29
- "isolatedModules": true,
30
- "skipLibCheck": true
31
- },
32
- "include": [
33
- "admin/**/*.ts",
34
- "admin/**/*.d.ts",
35
- "admin/**/*.tsx",
36
- "admin/**/*.vue",
37
- "antd/**/*.ts",
38
- "antd/**/*.d.ts",
39
- "antd/**/*.tsx",
40
- "antd/**/*.vue",
41
- "request/**/*.ts",
42
- "request/**/*.d.ts",
43
- "utils/**/*.ts",
44
- "utils/**/*.d.ts",
45
- "vue/**/*.ts",
46
- "vue/**/*.d.ts",
47
- "vue/**/*.tsx",
48
- "vue/**/*.vue"
49
- ]
50
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "jsx": "preserve",
5
+ "jsxFactory": "h",
6
+ "jsxFragmentFactory": "Fragment",
7
+ "lib": [
8
+ "ESNext",
9
+ "DOM",
10
+ "DOM.Iterable"
11
+ ],
12
+ "useDefineForClassFields": true,
13
+ "module": "ESNext",
14
+
15
+ /* Bundler mode */
16
+ "moduleResolution": "bundler",
17
+ "resolveJsonModule": true,
18
+ "allowImportingTsExtensions": true,
19
+
20
+ /* Linting */
21
+ "strict": true,
22
+ "noFallthroughCasesInSwitch": true,
23
+ "noImplicitAny": false,
24
+ "noUnusedLocals": true,
25
+ "noUnusedParameters": true,
26
+ "noEmit": true,
27
+ "allowSyntheticDefaultImports": true,
28
+ "esModuleInterop": true,
29
+ "isolatedModules": true,
30
+ "skipLibCheck": true
31
+ },
32
+ "include": [
33
+ "admin/**/*.ts",
34
+ "admin/**/*.d.ts",
35
+ "admin/**/*.tsx",
36
+ "admin/**/*.vue",
37
+ "antd/**/*.ts",
38
+ "antd/**/*.d.ts",
39
+ "antd/**/*.tsx",
40
+ "antd/**/*.vue",
41
+ "request/**/*.ts",
42
+ "request/**/*.d.ts",
43
+ "utils/**/*.ts",
44
+ "utils/**/*.d.ts",
45
+ "vue/**/*.ts",
46
+ "vue/**/*.d.ts",
47
+ "vue/**/*.tsx",
48
+ "vue/**/*.vue"
49
+ ]
50
+ }
@@ -1 +1 @@
1
- export { default as InfiniteQuery } from './src/InfiniteQuery.vue';
1
+ export { default as InfiniteQuery } from './src/InfiniteQuery.vue';