-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b76329b
commit ed68c8f
Showing
22 changed files
with
245 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 * from './proxy'; |
This file contains 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,38 @@ | ||
import type { ProxyOptions } from 'vite'; | ||
import { createServiceConfig, createProxyPattern } from '../../env.config'; | ||
|
||
/** | ||
* set http proxy | ||
* @param env - the current env | ||
*/ | ||
export function createViteProxy(env: Env.ImportMeta) { | ||
const isEnableHttpProxy = env.VITE_HTTP_PROXY === 'Y'; | ||
|
||
if (!isEnableHttpProxy) return undefined; | ||
|
||
const { baseURL, otherBaseURL } = createServiceConfig(env); | ||
|
||
const defaultProxyPattern = createProxyPattern(); | ||
|
||
const proxy: Record<string, ProxyOptions> = { | ||
[defaultProxyPattern]: { | ||
target: baseURL, | ||
changeOrigin: true, | ||
rewrite: path => path.replace(new RegExp(`^${defaultProxyPattern}`), '') | ||
} | ||
}; | ||
|
||
const otherURLEntries = Object.entries(otherBaseURL); | ||
|
||
for (const [key, url] of otherURLEntries) { | ||
const proxyPattern = createProxyPattern(key); | ||
|
||
proxy[proxyPattern] = { | ||
target: url, | ||
changeOrigin: true, | ||
rewrite: path => path.replace(new RegExp(`^${proxyPattern}`), '') | ||
}; | ||
} | ||
|
||
return proxy; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,44 @@ | ||
/** | ||
* create service config by current env | ||
* @param env the current env | ||
*/ | ||
export function createServiceConfig(env: Env.ImportMeta) { | ||
const mockURL = 'https://mock.apifox.com/m1/3109515-0-default'; | ||
|
||
const serviceConfigMap = { | ||
dev: { | ||
baseURL: mockURL, | ||
otherBaseURL: { | ||
demo: 'http://localhost:9528' | ||
} | ||
}, | ||
test: { | ||
baseURL: mockURL, | ||
otherBaseURL: { | ||
demo: 'http://localhost:9529' | ||
} | ||
}, | ||
prod: { | ||
baseURL: mockURL, | ||
otherBaseURL: { | ||
demo: 'http://localhost:9530' | ||
} | ||
} | ||
} satisfies App.Service.ServiceConfigMap; | ||
|
||
const { VITE_SERVICE_ENV = 'dev' } = env; | ||
|
||
return serviceConfigMap[VITE_SERVICE_ENV]; | ||
} | ||
|
||
/** | ||
* get proxy pattern of service url | ||
* @param key if not set, will use the default key | ||
*/ | ||
export function createProxyPattern(key?: string) { | ||
if (!key) { | ||
return '/proxy'; | ||
} | ||
|
||
return `/proxy-${key}`; | ||
} |
This file contains 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 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,10 @@ | ||
import axios from 'axios'; | ||
import type { CreateAxiosDefaults } from 'axios'; | ||
|
||
export function createAxios(config?: CreateAxiosDefaults) { | ||
const instance = axios.create(config); | ||
|
||
return instance; | ||
} | ||
|
||
export default createAxios; |
This file contains 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,10 +1,4 @@ | ||
import { ofetch } from 'ofetch'; | ||
import type { FetchOptions } from 'ofetch'; | ||
import { createAxios } from './axios'; | ||
import { createOfetch } from './ofetch'; | ||
|
||
export function createRequest(options: FetchOptions) { | ||
const request = ofetch.create(options); | ||
|
||
return request; | ||
} | ||
|
||
export default createRequest; | ||
export { createAxios, createOfetch }; |
Empty file.
This file contains 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,10 @@ | ||
import { ofetch } from 'ofetch'; | ||
import type { FetchOptions } from 'ofetch'; | ||
|
||
export function createOfetch(options: FetchOptions) { | ||
const request = ofetch.create(options); | ||
|
||
return request; | ||
} | ||
|
||
export default createOfetch; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,17 @@ | ||
import { request } from '../request'; | ||
|
||
/** | ||
* login | ||
* @param userName user name | ||
* @param password password | ||
*/ | ||
export function fetchLogin(userName: string, password: string) { | ||
return request<App.Service.Response<Api.Auth.LoginToken>>({ | ||
url: '/auth/login', | ||
method: 'post', | ||
data: { | ||
userName, | ||
password | ||
} | ||
}); | ||
} |
This file contains 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,2 @@ | ||
export * from './auth'; | ||
// export * from './route'; |
File renamed without changes.
This file contains 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,15 @@ | ||
import { createAxios } from '@sa/request'; | ||
import { createServiceConfig, createProxyPattern } from '~/env.config'; | ||
|
||
const { baseURL, otherBaseURL } = createServiceConfig(import.meta.env); | ||
|
||
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y'; | ||
|
||
export const request = createAxios({ | ||
baseURL: isHttpProxy ? createProxyPattern() : baseURL, | ||
headers: { | ||
apifoxToken: 'XL299LiMEDZ0H5h3A29PxwQXdMJqWyY2' | ||
} | ||
}); | ||
|
||
export const demoRequest = createAxios({ baseURL: isHttpProxy ? createProxyPattern('demo') : otherBaseURL.demo }); |
This file contains 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,22 @@ | ||
/** | ||
* namespace Api | ||
* @description all backend api type | ||
*/ | ||
declare namespace Api { | ||
/** | ||
* namespace Auth | ||
* @description backend api module: "auth" | ||
*/ | ||
namespace Auth { | ||
interface LoginToken { | ||
token: string; | ||
refreshToken: string; | ||
} | ||
} | ||
|
||
/** | ||
* namespace Route | ||
* @description backend api module: "route" | ||
*/ | ||
namespace Route {} | ||
} |
This file contains 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 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 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 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 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 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