create-bubbles 0.1.0 → 0.1.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.
Files changed (44) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
  3. package/template-react-rsbuild-biome/.prettierrc +1 -2
  4. package/template-react-rsbuild-biome/.vscode/settings.json +6 -10
  5. package/template-react-rsbuild-biome/biome.json +17 -5
  6. package/template-react-rsbuild-biome/package.json +22 -22
  7. package/template-react-rsbuild-biome/rsbuild.config.ts +2 -2
  8. package/template-react-rsbuild-biome/src/App.tsx +5 -6
  9. package/template-react-rsbuild-biome/src/index.tsx +1 -3
  10. package/template-react-rsbuild-biome/src/pages/home/index.tsx +1 -1
  11. package/template-react-rsbuild-biome/src/styles/index.scss +2 -1
  12. package/template-react-rsbuild-biome/src/utils/request/core/index.ts +166 -0
  13. package/template-react-rsbuild-biome/src/utils/request/core/utils.ts +38 -0
  14. package/template-react-rsbuild-biome/src/utils/request/index.ts +34 -68
  15. package/template-react-rsbuild-biome/uno.config.ts +3 -5
  16. package/template-vue-rolldown-oxc/.env +1 -3
  17. package/template-vue-rolldown-oxc/.env.development +1 -0
  18. package/template-vue-rolldown-oxc/.env.production +1 -0
  19. package/template-vue-rolldown-oxc/.prettierignore +34 -0
  20. package/template-vue-rolldown-oxc/package.json +17 -15
  21. package/template-vue-rolldown-oxc/src/main.ts +4 -6
  22. package/template-vue-rolldown-oxc/src/store/modules/user.ts +17 -0
  23. package/template-vue-rolldown-oxc/src/types/auto-import.d.ts +3 -1
  24. package/template-vue-rolldown-oxc/src/utils/env.ts +8 -3
  25. package/template-vue-rolldown-oxc/src/utils/request/core/index.ts +166 -0
  26. package/template-vue-rolldown-oxc/src/utils/request/core/utils.ts +38 -0
  27. package/template-vue-rolldown-oxc/src/utils/request/index.ts +41 -0
  28. package/template-vue-rolldown-oxc/vite.config.ts +2 -0
  29. package/template-vue-rsbuild-biome/.env +4 -2
  30. package/template-vue-rsbuild-biome/.env.development +1 -0
  31. package/template-vue-rsbuild-biome/.env.production +1 -0
  32. package/template-vue-rsbuild-biome/.vscode/settings.json +6 -0
  33. package/template-vue-rsbuild-biome/biome.json +3 -4
  34. package/template-vue-rsbuild-biome/package.json +14 -11
  35. package/template-vue-rsbuild-biome/rsbuild.config.ts +0 -1
  36. package/template-vue-rsbuild-biome/src/index.ts +0 -2
  37. package/template-vue-rsbuild-biome/src/utils/env.ts +10 -0
  38. package/template-vue-rsbuild-biome/src/utils/request/core/index.ts +166 -0
  39. package/template-vue-rsbuild-biome/src/utils/request/core/utils.ts +38 -0
  40. package/template-vue-rsbuild-biome/src/utils/request/index.ts +31 -69
  41. package/template-vue-rsbuild-biome/tsconfig.json +1 -1
  42. package/template-react-rsbuild-biome/src/utils/request/axios.ts +0 -83
  43. package/template-vue-rolldown-oxc/postcss.config.js +0 -5
  44. package/template-vue-rsbuild-biome/src/utils/request/axios.ts +0 -83
@@ -0,0 +1,166 @@
1
+ import { type AlovaGenerics, type AlovaOptions, createAlova } from 'alova'
2
+ import { deepMergeObject, isReadableStream } from './utils'
3
+ import adapterFetch from 'alova/fetch'
4
+
5
+ interface statusMap {
6
+ success?: number
7
+ unAuthorized?: number
8
+ }
9
+
10
+ interface codeMap {
11
+ success?: number[]
12
+ unAuthorized?: number[]
13
+ }
14
+
15
+ export interface baseRequestOption<AG extends AlovaGenerics> {
16
+ baseUrl?: string
17
+ timeout?: number
18
+ commonHeaders?: Record<string, string | (() => string)>
19
+ statusMap?: statusMap
20
+ codeMap?: codeMap
21
+ responseDataKey?: string
22
+ responseMessageKey?: string
23
+ isTransformResponse?: boolean
24
+ isShowSuccessMessage?: boolean
25
+ successDefaultMessage?: string
26
+ isShowErrorMessage?: boolean
27
+ errorDefaultMessage?: string
28
+ statesHook?: AlovaOptions<AG>['statesHook']
29
+ successMessageFunc?: (message: string) => void
30
+ errorMessageFunc?: (message: string) => void
31
+ unAuthorizedResponseFunc?: () => void
32
+ requestAdapter?: AlovaOptions<AG>['requestAdapter']
33
+ }
34
+
35
+ export interface CustomConfig {
36
+ isTransformResponse?: boolean
37
+ isShowSuccessMessage?: boolean
38
+ isShowErrorMessage?: boolean
39
+ }
40
+
41
+ type requestOption = baseRequestOption<AlovaGenerics> & CustomConfig
42
+
43
+ export const createInstance = (option: requestOption) => {
44
+ const defaultOption: requestOption = {
45
+ baseUrl: '/',
46
+ timeout: 0,
47
+ statusMap: {
48
+ success: 200,
49
+ unAuthorized: 401,
50
+ },
51
+ codeMap: {
52
+ success: [200],
53
+ unAuthorized: [401],
54
+ },
55
+ responseDataKey: 'data',
56
+ responseMessageKey: 'message',
57
+ isTransformResponse: true,
58
+ isShowSuccessMessage: false,
59
+ successDefaultMessage: '操作成功',
60
+ isShowErrorMessage: true,
61
+ errorDefaultMessage: '服务异常',
62
+ requestAdapter: adapterFetch(),
63
+ }
64
+
65
+ const mergeOption: baseRequestOption<AlovaGenerics> & CustomConfig = deepMergeObject(
66
+ defaultOption,
67
+ option,
68
+ )
69
+
70
+ const instance = createAlova({
71
+ baseURL: mergeOption.baseUrl,
72
+ timeout: mergeOption.timeout,
73
+ statesHook: mergeOption?.statesHook,
74
+ requestAdapter: mergeOption.requestAdapter as AlovaOptions<AlovaGenerics>['requestAdapter'],
75
+ beforeRequest: async (method) => {
76
+ for (const [key, value] of Object.entries(option?.commonHeaders ?? {})) {
77
+ method.config.headers[key] = typeof value === 'function' ? value() : value
78
+ }
79
+ },
80
+ responded: {
81
+ onSuccess: async (response) => {
82
+ if (!mergeOption?.isTransformResponse) return response
83
+ const { status } = response
84
+
85
+ // 判断响应类型:如果使用 adapterFetch,response.data 是可读流,则调用 json();否则直接使用 response.data
86
+ const data =
87
+ response?.body && isReadableStream(response.body)
88
+ ? await response.json() // adapterFetch 的响应,使用 json() 解析可读流
89
+ : response.data // 其他适配器的响应
90
+ // 不成功的情况
91
+ if (status !== mergeOption.statusMap?.success) {
92
+ // 如果后端使用status 字段来表示未授权,则返回401
93
+ if (mergeOption?.statusMap?.unAuthorized === status) {
94
+ mergeOption?.unAuthorizedResponseFunc?.()
95
+ }
96
+ return Promise.reject(response)
97
+ }
98
+
99
+ const {
100
+ responseDataKey,
101
+ codeMap,
102
+ isShowSuccessMessage,
103
+ responseMessageKey,
104
+ isShowErrorMessage,
105
+ } = mergeOption
106
+ const {
107
+ code,
108
+ [responseDataKey as string]: responseData,
109
+ [responseMessageKey as string]: responseMessage,
110
+ } = data
111
+ if (!codeMap?.success?.includes(+code)) {
112
+ // code unAuthorized 处理
113
+ if (codeMap?.unAuthorized?.includes(+code)) {
114
+ mergeOption?.unAuthorizedResponseFunc?.()
115
+ return Promise.reject(response)
116
+ }
117
+ // 其他错误直接打印msg
118
+
119
+ const errorMessage = data[responseMessageKey as string] ?? mergeOption.errorDefaultMessage
120
+ if (isShowErrorMessage) mergeOption?.errorMessageFunc?.(errorMessage)
121
+ return Promise.reject(response)
122
+ }
123
+ if (isShowSuccessMessage)
124
+ mergeOption?.successMessageFunc?.(responseMessage ?? mergeOption.successDefaultMessage)
125
+ return responseData
126
+ },
127
+ onError: (error) => {
128
+ if (mergeOption?.isShowErrorMessage)
129
+ mergeOption.errorMessageFunc?.(
130
+ error.response?.data?.message ?? mergeOption?.errorDefaultMessage,
131
+ )
132
+ },
133
+ // onComplete: (_method) => {},
134
+ },
135
+ })
136
+
137
+ return instance
138
+ }
139
+
140
+ // 🚀 创建双重调用实例的工厂函数
141
+ export const createDualCallInstance = (baseConfig: baseRequestOption<AlovaGenerics>) => {
142
+ // 创建默认实例
143
+ const defaultInstance = createInstance(baseConfig)
144
+
145
+ // 双重调用函数
146
+ const dualInstance = (option?: CustomConfig) => {
147
+ if (option) {
148
+ // 合并配置并创建新实例
149
+ const mergedConfig = { ...baseConfig, ...option }
150
+ return createInstance(mergedConfig)
151
+ }
152
+ return defaultInstance
153
+ }
154
+
155
+ // 🎯 直接绑定 HTTP 方法,无需复杂类型注释
156
+ dualInstance.Get = defaultInstance.Get.bind(defaultInstance)
157
+ dualInstance.Post = defaultInstance.Post.bind(defaultInstance)
158
+ dualInstance.Put = defaultInstance.Put.bind(defaultInstance)
159
+ dualInstance.Delete = defaultInstance.Delete.bind(defaultInstance)
160
+ dualInstance.Patch = defaultInstance.Patch.bind(defaultInstance)
161
+ dualInstance.Head = defaultInstance.Head.bind(defaultInstance)
162
+ dualInstance.Options = defaultInstance.Options.bind(defaultInstance)
163
+ dualInstance.Request = defaultInstance.Request.bind(defaultInstance)
164
+
165
+ return dualInstance
166
+ }
@@ -0,0 +1,38 @@
1
+ export const deepMergeObject = <T = any>(source: T, target: Partial<T>): T => {
2
+ const isObject = (obj: any): obj is Record<string, any> =>
3
+ obj && typeof obj === 'object' && !Array.isArray(obj)
4
+
5
+ const merge = (src: any, tgt: any): any => {
6
+ if (isObject(src) && isObject(tgt)) {
7
+ Object.keys(tgt).forEach((key) => {
8
+ if (isObject(tgt[key])) {
9
+ src[key] = merge(src[key] || {}, tgt[key])
10
+ } else {
11
+ src[key] = tgt[key]
12
+ }
13
+ })
14
+ }
15
+ return src
16
+ }
17
+
18
+ return merge({ ...source }, target)
19
+ }
20
+
21
+ /**
22
+ * 判断一个变量是不是可读流
23
+ * @param data
24
+ */
25
+ export const isReadableStream = (data: unknown): boolean => {
26
+ if (!(data instanceof ReadableStream)) {
27
+ return false
28
+ }
29
+ if (typeof data.locked !== 'boolean') return false
30
+
31
+ const instanceFunc = ['cancel', 'getReader', 'pipeThrough', 'pipeTo', 'tee'] as const
32
+
33
+ for (const func of instanceFunc) {
34
+ if (typeof data[func] !== 'function') return false
35
+ }
36
+
37
+ return true
38
+ }
@@ -1,77 +1,39 @@
1
- import type { AxiosRequestConfig } from 'axios'
1
+ import { envVariables } from '../env'
2
+ import { createDualCallInstance } from './core'
3
+ import { router } from '@/router'
4
+ import { ElMessage } from 'element-plus'
5
+ import 'element-plus/es/components/message/style/css'
6
+ import vueHook from 'alova/vue'
7
+ import { axiosRequestAdapter } from '@alova/adapter-axios'
2
8
 
3
- import request, { type CustomConfig } from './axios'
4
-
5
- interface createAxiosType {
6
- get: <T = any>(
7
- config: {
8
- url: string
9
- params?: AxiosRequestConfig['params']
10
- config?: AxiosRequestConfig<any>
9
+ const getBaseConfig = (): Parameters<typeof createDualCallInstance>[0] => {
10
+ return {
11
+ baseUrl: `/${envVariables.PUBLIC_PORT}`,
12
+ statusMap: {
13
+ success: 200,
14
+ unAuthorized: 401,
11
15
  },
12
- option?: CustomConfig,
13
- ) => Promise<T>
14
- post: <T = any>(
15
- config: {
16
- url: string
17
- data?: AxiosRequestConfig['data']
18
- params?: AxiosRequestConfig['params']
19
- config?: AxiosRequestConfig<any>
16
+ codeMap: {
17
+ success: [200],
20
18
  },
21
- option?: CustomConfig,
22
- ) => Promise<T>
23
- put: <T = any>(
24
- config: {
25
- url: string
26
- data?: AxiosRequestConfig['data']
27
- config?: AxiosRequestConfig<any>
19
+ responseDataKey: 'data',
20
+ responseMessageKey: 'msg',
21
+ commonHeaders: {},
22
+ successMessageFunc: (msg: string) => {
23
+ ElMessage.success(msg)
28
24
  },
29
- option?: CustomConfig,
30
- ) => Promise<T>
31
- delete: <T = any>(
32
- config: {
33
- url: string
34
- data?: AxiosRequestConfig['data']
35
- config?: AxiosRequestConfig<any>
25
+ errorMessageFunc: (msg: string) => {
26
+ ElMessage.error(msg)
36
27
  },
37
- option?: CustomConfig,
38
- ) => Promise<T>
39
- }
40
-
41
- const createAxios: createAxiosType = {
42
- get: (config, option) =>
43
- request(option).get(config.url, {
44
- params: config.params,
45
- ...config?.config,
46
- }),
47
- post: (
48
- config: {
49
- url: string
50
- params?: Record<string, string>
51
- data?: any
52
- config?: AxiosRequestConfig<any>
28
+ unAuthorizedResponseFunc: () => {
29
+ router.push('/login')
30
+ ElMessage.error('登录过期或未登录')
53
31
  },
54
- option?: CustomConfig,
55
- ) => {
56
- let url = config.url
57
- if (config.params instanceof Object) {
58
- url += '?'
59
- const entries = Object.entries(config.params)
60
- for (let i = 0; i < entries.length; i++) {
61
- const [key, value] = entries[i]
62
- url += `${key}=${value}${i < entries.length - 1 ? '&' : ''}`
63
- }
64
- }
65
- return request(option).post(url, config?.data, config?.config)
66
- },
67
- put: (
68
- config: { url: string; data?: any; config?: AxiosRequestConfig<any> },
69
- option?: CustomConfig,
70
- ) => request(option).put(config.url, config?.data, config?.config),
71
- delete: (
72
- config: { url: string; config?: AxiosRequestConfig<any> },
73
- customConfig?: CustomConfig,
74
- ) => request(customConfig).delete(config.url, config?.config),
32
+ statesHook: vueHook,
33
+ requestAdapter: axiosRequestAdapter(),
34
+ }
75
35
  }
76
36
 
77
- export default createAxios
37
+ const alovaRequest = createDualCallInstance(getBaseConfig())
38
+
39
+ export default alovaRequest
@@ -22,7 +22,7 @@
22
22
 
23
23
  "baseUrl": ".",
24
24
  "paths": {
25
- "@": ["src"]
25
+ "@/*": ["src/*"]
26
26
  }
27
27
  },
28
28
  "include": ["src"]
@@ -1,83 +0,0 @@
1
- import { message } from 'antd'
2
- import axios, { type AxiosResponse } from 'axios'
3
- import { envVariables } from '@/utils/env'
4
-
5
- import { router } from '@/router'
6
- import { useUserStore } from '@/store/modules/user'
7
-
8
- export interface CustomConfig {
9
- /** 直接返回响应体 */
10
- isTransformResponse?: boolean
11
- // 成功需要提示
12
- isShowSuccessMsg?: boolean
13
- }
14
-
15
- export enum CodeEnum {
16
- SUCCESS = 200,
17
- UNAUTHORIZED = 401,
18
- }
19
-
20
- const unAuthFunc = () => {
21
- message.error('暂未登录或登录已过期,请重新登录')
22
- router.navigate('/login')
23
- }
24
- export default (customConfig?: CustomConfig) => {
25
- const token = useUserStore((state) => state.token)
26
-
27
- const instance = axios.create({
28
- baseURL: envVariables.PUBLIC_API_AFFIX,
29
- })
30
-
31
- instance.interceptors.request.use(
32
- (config) => {
33
- config.headers.set('X-Access-Token', token)
34
- config.headers.set('Allow-Control-Allow-Origin', '*')
35
- return config
36
- },
37
- (error) => {
38
- message.error('请求错误')
39
- return Promise.reject(error)
40
- },
41
- )
42
-
43
- interface ResponseData<T = any> {
44
- code: number
45
- message: string
46
- requestId: string
47
- responseTime: number
48
- result: T
49
- }
50
-
51
- instance.interceptors.response.use(
52
- (response: AxiosResponse<ResponseData>) => {
53
- const { status, data } = response
54
- if (status === 200) {
55
- if (customConfig?.isTransformResponse === false) return data
56
- const { code, message: msg } = data
57
- if (code === CodeEnum.SUCCESS) {
58
- if (customConfig?.isShowSuccessMsg) {
59
- message.success(msg)
60
- }
61
- return data.result
62
- }
63
- message.error(msg)
64
- return Promise.reject(data)
65
- }
66
- if (status === CodeEnum.UNAUTHORIZED) {
67
- unAuthFunc()
68
- } else return Promise.reject(data.message)
69
- },
70
- (error) => {
71
- const response = error.response
72
- const { status, data } = response
73
- if (status === CodeEnum.UNAUTHORIZED) {
74
- unAuthFunc()
75
- } else {
76
- message.error(data?.message || '系统异常')
77
- return Promise.reject(error)
78
- }
79
- },
80
- )
81
-
82
- return instance
83
- }
@@ -1,5 +0,0 @@
1
- import UnoCSS from '@unocss/postcss'
2
-
3
- export default {
4
- plugins: [UnoCSS()],
5
- }
@@ -1,83 +0,0 @@
1
- import { message } from 'element-plus'
2
- import axios, { type AxiosResponse } from 'axios'
3
-
4
- import { router } from '@/router'
5
- import { useUserStore } from '@/store/modules/user'
6
-
7
- export interface CustomConfig {
8
- /** 直接返回响应体 */
9
- isTransformResponse?: boolean
10
- // 成功需要提示
11
- isShowSuccessMsg?: boolean
12
- }
13
-
14
- export enum CodeEnum {
15
- SUCCESS = 200,
16
- UNAUTHORIZED = 401,
17
- }
18
-
19
- const unAuthFunc = () => {
20
- message.error('暂未登录或登录已过期,请重新登录')
21
- router.navigate('/login')
22
- }
23
- export default (customConfig?: CustomConfig) => {
24
- const userStore = useUserStore()
25
- const token = userStore.token
26
-
27
- const instance = axios.create({
28
- baseURL: import.meta.env.PUBLIC_API_AFFIX,
29
- })
30
-
31
- instance.interceptors.request.use(
32
- (config) => {
33
- config.headers.set('X-Access-Token', token)
34
- config.headers.set('Allow-Control-Allow-Origin', '*')
35
- return config
36
- },
37
- (error) => {
38
- message.error('请求错误')
39
- return Promise.reject(error)
40
- },
41
- )
42
-
43
- interface ResponseData<T = any> {
44
- code: number
45
- message: string
46
- requestId: string
47
- responseTime: number
48
- result: T
49
- }
50
-
51
- instance.interceptors.response.use(
52
- (response: AxiosResponse<ResponseData>) => {
53
- const { status, data } = response
54
- if (status === 200) {
55
- if (customConfig?.isTransformResponse === false) return data
56
- const { code, message: msg } = data
57
- if (code === CodeEnum.SUCCESS) {
58
- if (customConfig?.isShowSuccessMsg) {
59
- message.success(msg)
60
- }
61
- return data.result
62
- }
63
- message.error(msg)
64
- return Promise.reject(data)
65
- }
66
- if (status === CodeEnum.UNAUTHORIZED) {
67
- unAuthFunc()
68
- } else return Promise.reject(data.message)
69
- },
70
- (error) => {
71
- const response = error.response
72
- const { status, data } = response
73
- if (status === CodeEnum.UNAUTHORIZED) {
74
- unAuthFunc()
75
- } else {
76
- message.error(data?.message || '系统异常')
77
- return Promise.reject(error)
78
- }
79
- },
80
- )
81
-
82
- return instance
83
- }