-
Notifications
You must be signed in to change notification settings - Fork 467
Refactoring useHttp to httpService #886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chilingling
merged 20 commits into
opentiny:refactor/develop
from
yy-wow:refactor/http-service
Nov 6, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4c9d89f
refactor/http-service
yy-wow d994745
feat: useHttp 2 httpService
yy-wow 1fef16c
fix: delete by mistake
yy-wow 03364a2
fix: undefined
yy-wow 32eebea
fix: dependencies
yy-wow 5e4086e
fix: review
yy-wow c193866
feat: update cli template
yy-wow 4ed0017
fix: review
yy-wow c068e61
fix: delete @opentiny/tiny-engine-http
yy-wow 8897191
fix: review
yy-wow 5c480ed
Merge branch 'refactor/develop' into refactor/http-service
yy-wow 628e8ad
fix: error
yy-wow b57f843
fix: review
yy-wow 7bbea8e
fix: review
yy-wow 4bebcb3
fix: review
yy-wow 4f090aa
fix: appid
yy-wow 861511d
fix: appid
yy-wow 30efb0d
fix: remove unused file
yy-wow 227da30
fix: remove unused dependency
yy-wow 2b16423
fix: preview add httpService
yy-wow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { createApp } from 'vue' | ||
| import { HttpService } from '@opentiny/tiny-engine' | ||
| import { useBroadcastChannel } from '@vueuse/core' | ||
| import { constants } from '@opentiny/tiny-engine-utils' | ||
| import Login from './Login.vue' | ||
|
|
||
| const LOGIN_EXPIRED_CODE = 401 | ||
| const { BROADCAST_CHANNEL } = constants | ||
|
|
||
| const { post: globalNotify } = useBroadcastChannel({ name: BROADCAST_CHANNEL.Notify }) | ||
|
|
||
| const procession = { | ||
| promiseLogin: null, | ||
| mePromise: {} | ||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let loginVM = null | ||
|
|
||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const showError = (url, message) => { | ||
| globalNotify({ | ||
| type: 'error', | ||
| title: '接口报错', | ||
| message: `报错接口: ${url} \n报错信息: ${message ?? ''}` | ||
| }) | ||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const preRequest = (config) => { | ||
| const isDevelopEnv = import.meta.env.MODE?.includes('dev') | ||
|
|
||
| if (isDevelopEnv && config.url.match(/\/generate\//)) { | ||
| config.baseURL = '' | ||
| } | ||
|
|
||
| const isVsCodeEnv = window.vscodeBridge | ||
|
|
||
| if (isVsCodeEnv) { | ||
| config.baseURL = '' | ||
| } | ||
|
|
||
| return config | ||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const preResponse = (res) => { | ||
| if (res.data?.error) { | ||
| showError(res.config?.url, res?.data?.error?.message) | ||
|
|
||
| return Promise.reject(res.data.error) | ||
| } | ||
|
|
||
| return res.data?.data | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const openLogin = () => { | ||
| if (!window.lowcode) { | ||
| const loginDom = document.createElement('div') | ||
| document.body.appendChild(loginDom) | ||
| loginVM = createApp(Login).mount(loginDom) | ||
|
|
||
| window.lowcode = { | ||
| platformCenter: { | ||
| Session: { | ||
| rebuiltCallback: function () { | ||
| loginVM.closeLogin() | ||
|
|
||
| procession.mePromise.resolve('login ok') | ||
| procession.promiseLogin = null | ||
| procession.mePromise = {} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| if (!procession.promiseLogin) { | ||
| procession.promiseLogin = loginVM.openLogin(procession, '/api/rebuildSession') | ||
| procession.promiseLogin.then((response) => { | ||
| HttpService.apis.request(response.config).then(resolve, reject) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const errorResponse = (error) => { | ||
| // 用户信息失效时,弹窗提示登录 | ||
| const { response } = error | ||
|
|
||
| if (response?.status === LOGIN_EXPIRED_CODE) { | ||
| // vscode 插件环境弹出输入框提示登录 | ||
| if (window.vscodeBridge) { | ||
| return Promise.resolve(true) | ||
| } | ||
|
|
||
| // 浏览器环境弹出小窗登录 | ||
| if (response?.headers['x-login-url']) { | ||
| return openLogin() | ||
| } | ||
| } | ||
|
|
||
| showError(error.config?.url, error?.message) | ||
|
|
||
| return response?.data.error ? Promise.reject(response.data.error) : Promise.reject(error.message) | ||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const getConfig = (env = import.meta.env) => { | ||
| const baseURL = env.VITE_ORIGIN | ||
| // 仅在本地开发时,启用 withCredentials | ||
| const dev = env.MODE?.includes('dev') | ||
| // 获取租户 id | ||
| const getTenant = () => new URLSearchParams(location.search).get('tenant') | ||
|
|
||
| return { | ||
| baseURL, | ||
| withCredentials: dev, | ||
| headers: { | ||
| ...(dev && { 'x-lowcode-mode': 'develop' }), | ||
| 'x-lowcode-org': getTenant() | ||
| } | ||
| } | ||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const customizeHttpService = () => { | ||
| const options = { | ||
| axiosConfig: getConfig(), | ||
| interceptors: { | ||
| request: [preRequest], | ||
| response: [[preResponse, errorResponse]] | ||
| } | ||
| } | ||
|
|
||
| HttpService.apis.setOptions(options) | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return HttpService | ||
| } | ||
|
|
||
| export default customizeHttpService() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as HttpService } from './http' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { defineService, META_SERVICE } from '@opentiny/tiny-engine-meta-register' | ||
| import axios from 'axios' | ||
|
|
||
| let http = null | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const createInterceptorHandler = | ||
| (http) => | ||
| ({ data, type }) => { | ||
| if (typeof data === 'function') { | ||
| http.interceptors[type].use(data) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if (Array.isArray(data)) { | ||
| data.forEach((item) => { | ||
| if (!item) return | ||
|
|
||
| if (Array.isArray(item)) { | ||
| http.interceptors[type].use(...item) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if (typeof item === 'function') { | ||
| http.interceptors[type].use(item) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export default defineService({ | ||
| id: META_SERVICE.Http, | ||
| type: 'MetaService', | ||
| options: { | ||
| axiosConfig: { | ||
| // axios 配置 | ||
| baseURL: '', | ||
| withCredentials: false, // 跨域请求时是否需要使用凭证 | ||
| headers: {} // 请求头 | ||
| }, | ||
| interceptors: { | ||
| // 拦截器 | ||
| request: [], // 支持配置多个请求拦截器,先注册后执行 | ||
| response: [] // 支持配置多个响应拦截器,先注册先执行 | ||
| } | ||
| }, | ||
| init: ({ options = {} }) => { | ||
| 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' }) | ||
| }, | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| apis: () => ({ | ||
| getHttp: () => http, | ||
| get: (...args) => http?.get(...args), | ||
| post: (...args) => http?.post(...args), | ||
| request: (...args) => http?.request(...args), | ||
| put: (...args) => http?.put(...args), | ||
| delete: (...args) => http?.delete(...args) | ||
| }) | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export { GenerateCodeService } from './generateCode' | ||
| export { default as GlobalService } from './defaultGlobalService' | ||
| export { default as HttpService } from './http' | ||
yy-wow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.