Skip to content

Commit 5928998

Browse files
committed
chore: enhance common package types
1 parent ce6f4a7 commit 5928998

23 files changed

Lines changed: 590 additions & 325 deletions

packages/common/composable/http/index.js

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { defineService, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
2+
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse, type CreateAxiosDefaults } from 'axios'
3+
4+
// 请求拦截器函数类型
5+
type RequestInterceptorFunction =
6+
| ((config: AxiosRequestConfig) => AxiosRequestConfig | Promise<AxiosRequestConfig>)
7+
| [(config: AxiosRequestConfig) => AxiosRequestConfig | Promise<AxiosRequestConfig>, (error: any) => any]
8+
9+
// 响应拦截器函数类型
10+
type ResponseInterceptorFunction =
11+
| ((response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>)
12+
| [(response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>, (error: any) => any]
13+
14+
// 请求拦截器配置类型(支持单个函数、数组、嵌套数组)
15+
type RequestInterceptorConfig = RequestInterceptorFunction | (RequestInterceptorFunction | undefined)[]
16+
17+
// 响应拦截器配置类型(支持单个函数、数组、嵌套数组)
18+
type ResponseInterceptorConfig = ResponseInterceptorFunction | (ResponseInterceptorFunction | undefined)[]
19+
20+
// 拦截器配置接口
21+
interface InterceptorsConfig {
22+
request?: RequestInterceptorConfig
23+
response?: ResponseInterceptorConfig
24+
}
25+
26+
// HTTP 服务选项接口
27+
interface HttpServiceOptions {
28+
axiosConfig?: CreateAxiosDefaults
29+
interceptors?: InterceptorsConfig
30+
}
31+
32+
let http: AxiosInstance | null = null
33+
34+
/**
35+
* 创建拦截器处理器
36+
* @param http - axios 实例
37+
* @returns 拦截器添加函数
38+
*/
39+
const createInterceptorHandler =
40+
(http: AxiosInstance) =>
41+
({ data, type }: { data: any; type: 'request' | 'response' }): void => {
42+
if (typeof data === 'function') {
43+
http.interceptors[type].use(data as any)
44+
45+
return
46+
}
47+
48+
if (Array.isArray(data)) {
49+
data.forEach((item) => {
50+
if (!item) return
51+
52+
if (Array.isArray(item)) {
53+
http.interceptors[type].use(...(item as any))
54+
55+
return
56+
}
57+
58+
if (typeof item === 'function') {
59+
http.interceptors[type].use(item as any)
60+
}
61+
})
62+
}
63+
}
64+
65+
export default defineService({
66+
id: META_SERVICE.Http,
67+
type: 'MetaService',
68+
initialState: {},
69+
options: {
70+
axiosConfig: {
71+
// axios 配置
72+
baseURL: '',
73+
withCredentials: false, // 跨域请求时是否需要使用凭证
74+
headers: {} // 请求头
75+
},
76+
interceptors: {
77+
// 拦截器
78+
request: [], // 支持配置多个请求拦截器,先注册后执行
79+
response: [] // 支持配置多个响应拦截器,先注册先执行
80+
}
81+
} as HttpServiceOptions,
82+
init: ({ options = {} }: { options?: HttpServiceOptions }) => {
83+
const { axiosConfig = {}, interceptors = {} } = options
84+
const { request = [], response = [] } = interceptors
85+
86+
http = axios.create(axiosConfig)
87+
const addInterceptors = createInterceptorHandler(http)
88+
addInterceptors({ data: request, type: 'request' })
89+
addInterceptors({ data: response, type: 'response' })
90+
},
91+
apis: () => ({
92+
/** 获取 axios 实例 */
93+
getHttp: (): AxiosInstance | null => http,
94+
/** GET 请求 */
95+
get: (...args: any[]) => (http?.get as any)?.(...args),
96+
/** POST 请求 */
97+
post: (...args: any[]) => (http?.post as any)?.(...args),
98+
/** 通用请求方法 */
99+
request: (...args: any[]) => (http?.request as any)?.(...args),
100+
/** PUT 请求 */
101+
put: (...args: any[]) => (http?.put as any)?.(...args),
102+
/** DELETE 请求 */
103+
delete: (...args: any[]) => (http?.delete as any)?.(...args),
104+
/** 流式请求 */
105+
stream: (config: AxiosRequestConfig): Promise<AxiosResponse<any, any>> | undefined => {
106+
const streamConfig: AxiosRequestConfig = {
107+
responseType: 'stream',
108+
...config
109+
}
110+
return http?.request(streamConfig)
111+
}
112+
})
113+
})
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@
1313
import { PAGE_STATUS } from './constants'
1414
import { useResource, getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
1515

16-
export const getCanvasStatus = (data) => {
16+
// 占用者信息接口
17+
interface Occupier {
18+
id: number
19+
username: string
20+
}
21+
22+
// 画布状态返回值接口
23+
interface CanvasStatus {
24+
state: string
25+
data: Occupier | undefined
26+
}
27+
28+
export const getCanvasStatus = (data: Occupier | undefined): CanvasStatus => {
1729
const globalState = getMetaApi(META_SERVICE.GlobalService).getState()
1830
const isDemo = useResource().appSchemaState.isDemo
1931
let state = ''
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*/
1212

13-
export const getCommentByKey = (key) => ({
13+
export const getCommentByKey = (key: string): { start: string; end: string } => ({
1414
start: `start-${key} 设计器生成的代码,为了避免出现问题,请勿修改`,
1515
end: `end-${key}`
1616
})

packages/common/js/config-files/eslint-rule.js renamed to packages/common/js/config-files/eslint-rule.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import eslintRecommended from '@eslint/js/src/configs/eslint-recommended.js'
1+
import eslintRecommended from '@eslint/js/src/configs/eslint-recommended'
2+
23
export default {
34
...eslintRecommended.rules,
45
'no-console': 'error',
File renamed without changes.
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import * as cssTree from 'css-tree'
14+
import type { StyleSheet, Rule } from 'css-tree'
1415
import { utils } from '@opentiny/tiny-engine-utils'
1516

1617
const { hyphenate } = utils
@@ -22,13 +23,13 @@ const { hyphenate } = utils
2223
* @param {string} styleStr css 字符串
2324
* @returns object { [string]: string }
2425
*/
25-
export const getCssObjectFromStyleStr = (styleStr) => {
26-
const ast = cssTree.parse(styleStr)
27-
const cssObject = {}
26+
export const getCssObjectFromStyleStr = (styleStr: string): Record<string, string> => {
27+
const ast = cssTree.parse(styleStr) as StyleSheet
28+
const cssObject: Record<string, string> = {}
2829

2930
ast.children
30-
.filter(({ type }) => type === 'Rule')
31-
.forEach((item) => {
31+
.filter((node): node is Rule => node.type === 'Rule')
32+
.forEach((item: Rule) => {
3233
const matchCode = cssTree.generate(item).match(/^(.+){(.+)}$/)
3334

3435
if (!matchCode) {
@@ -47,7 +48,7 @@ export const styleStrAddRoot = (str = '') => {
4748
return `:root { ${str}\n}`
4849
}
4950

50-
export const obj2StyleStr = (obj = {}, addRoot = true) => {
51+
export const obj2StyleStr = (obj: Record<string, string> = {}, addRoot = true) => {
5152
const list = Object.entries(obj).map(([key, value]) => (value ? `${hyphenate(key)}: ${value};` : ''))
5253

5354
return addRoot ? styleStrAddRoot(list.join('\n ')) : ` { \n ${list.join('\n ')} \n}`

0 commit comments

Comments
 (0)