Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions packages/common/composable/http/index.js

This file was deleted.

113 changes: 113 additions & 0 deletions packages/common/composable/http/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { defineService, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse, type CreateAxiosDefaults } from 'axios'

// 请求拦截器函数类型
type RequestInterceptorFunction =
| ((config: AxiosRequestConfig) => AxiosRequestConfig | Promise<AxiosRequestConfig>)
| [(config: AxiosRequestConfig) => AxiosRequestConfig | Promise<AxiosRequestConfig>, (error: any) => any]

// 响应拦截器函数类型
type ResponseInterceptorFunction =
| ((response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>)
| [(response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>, (error: any) => any]

// 请求拦截器配置类型(支持单个函数、数组、嵌套数组)
type RequestInterceptorConfig = RequestInterceptorFunction | (RequestInterceptorFunction | undefined)[]

// 响应拦截器配置类型(支持单个函数、数组、嵌套数组)
type ResponseInterceptorConfig = ResponseInterceptorFunction | (ResponseInterceptorFunction | undefined)[]

// 拦截器配置接口
interface InterceptorsConfig {
request?: RequestInterceptorConfig
response?: ResponseInterceptorConfig
}

// HTTP 服务选项接口
interface HttpServiceOptions {
axiosConfig?: CreateAxiosDefaults
interceptors?: InterceptorsConfig
}

let http: AxiosInstance | null = null

/**
* 创建拦截器处理器
* @param http - axios 实例
* @returns 拦截器添加函数
*/
const createInterceptorHandler =
(http: AxiosInstance) =>
({ data, type }: { data: any; type: 'request' | 'response' }): void => {
if (typeof data === 'function') {
http.interceptors[type].use(data as any)

return
}

if (Array.isArray(data)) {
data.forEach((item) => {
if (!item) return

if (Array.isArray(item)) {
http.interceptors[type].use(...(item as any))

return
}

if (typeof item === 'function') {
http.interceptors[type].use(item as any)
}
})
}
}

export default defineService({
id: META_SERVICE.Http,
type: 'MetaService',
initialState: {},
options: {
axiosConfig: {
// axios 配置
baseURL: '',
withCredentials: false, // 跨域请求时是否需要使用凭证
headers: {} // 请求头
},
interceptors: {
// 拦截器
request: [], // 支持配置多个请求拦截器,先注册后执行
response: [] // 支持配置多个响应拦截器,先注册先执行
}
} as HttpServiceOptions,
init: ({ options = {} }: { options?: HttpServiceOptions }) => {
const { axiosConfig = {}, interceptors = {} } = options
const { request = [], response = [] } = interceptors

http = axios.create(axiosConfig)
const addInterceptors = createInterceptorHandler(http)
addInterceptors({ data: request, type: 'request' })
addInterceptors({ data: response, type: 'response' })
},
apis: () => ({
/** 获取 axios 实例 */
getHttp: (): AxiosInstance | null => http,
/** GET 请求 */
get: (...args: any[]) => (http?.get as any)?.(...args),
/** POST 请求 */
post: (...args: any[]) => (http?.post as any)?.(...args),
/** 通用请求方法 */
request: (...args: any[]) => (http?.request as any)?.(...args),
/** PUT 请求 */
put: (...args: any[]) => (http?.put as any)?.(...args),
/** DELETE 请求 */
delete: (...args: any[]) => (http?.delete as any)?.(...args),
/** 流式请求 */
stream: (config: AxiosRequestConfig): Promise<AxiosResponse<any, any>> | undefined => {
const streamConfig: AxiosRequestConfig = {
responseType: 'stream',
...config
}
return http?.request(streamConfig)
}
})
})
File renamed without changes.
File renamed without changes.
14 changes: 13 additions & 1 deletion packages/common/js/canvas.js → packages/common/js/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@
import { PAGE_STATUS } from './constants'
import { useResource, getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'

export const getCanvasStatus = (data) => {
// 占用者信息接口
interface Occupier {
id: number
username: string
}

// 画布状态返回值接口
interface CanvasStatus {
state: string
data: Occupier | undefined
}

export const getCanvasStatus = (data: Occupier | undefined): CanvasStatus => {
const globalState = getMetaApi(META_SERVICE.GlobalService).getState()
const isDemo = useResource().appSchemaState.isDemo
let state = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
*/

export const getCommentByKey = (key) => ({
export const getCommentByKey = (key: string): { start: string; end: string } => ({
start: `start-${key} 设计器生成的代码,为了避免出现问题,请勿修改`,
end: `end-${key}`
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import eslintRecommended from '@eslint/js/src/configs/eslint-recommended.js'
import eslintRecommended from '@eslint/js/src/configs/eslint-recommended'

export default {
...eslintRecommended.rules,
'no-console': 'error',
Expand Down
File renamed without changes.
13 changes: 7 additions & 6 deletions packages/common/js/css.js → packages/common/js/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

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

const { hyphenate } = utils
Expand All @@ -22,13 +23,13 @@ const { hyphenate } = utils
* @param {string} styleStr css 字符串
* @returns object { [string]: string }
*/
export const getCssObjectFromStyleStr = (styleStr) => {
const ast = cssTree.parse(styleStr)
const cssObject = {}
export const getCssObjectFromStyleStr = (styleStr: string): Record<string, string> => {
const ast = cssTree.parse(styleStr) as StyleSheet
const cssObject: Record<string, string> = {}

ast.children
.filter(({ type }) => type === 'Rule')
.forEach((item) => {
.filter((node): node is Rule => node.type === 'Rule')
.forEach((item: Rule) => {
const matchCode = cssTree.generate(item).match(/^(.+){(.+)}$/)

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

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

return addRoot ? styleStrAddRoot(list.join('\n ')) : ` { \n ${list.join('\n ')} \n}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const VITE_CDN_TYPE = import.meta.env.VITE_CDN_TYPE

export const isMock = VITE_API_MOCK === 'mock'

export const isVsCodeEnv = window.vscodeBridge
export const isVsCodeEnv = (window as any).vscodeBridge as boolean

export const isDevelopEnv = MODE?.includes('dev')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const exampleMap = {
] `
}

export const getExample = (name) => {
export const getExample = (name: string): string => {
const resetName = `${name || ''}`.toLocaleLowerCase()
return exampleMap[resetName]
return exampleMap[resetName as keyof typeof exampleMap] || ''
}
89 changes: 0 additions & 89 deletions packages/common/js/http.js

This file was deleted.

Loading
Loading