feat(mobile-app): prepare native app packaging
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import type { ApiPayload } from '@/types/api';
|
||||
import { clearAuth, getAuth, redirectToLoginForAuthLoss } from './auth';
|
||||
import { isNativeAppRuntime } from './speech-capture';
|
||||
|
||||
const apiBase = import.meta.env.VITE_API_BASE || '/dev-api';
|
||||
const configuredApiBase = import.meta.env.VITE_API_BASE;
|
||||
const currentApiBase = () => configuredApiBase
|
||||
|| (isNativeAppRuntime() ? 'https://peilian.njzhmj.top/dev-api' : '/dev-api');
|
||||
|
||||
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
|
||||
@@ -31,7 +34,7 @@ export const isAuthApiError = (error: unknown): boolean =>
|
||||
error instanceof ApiError
|
||||
&& (error.statusCode === 401 || error.statusCode === 403 || error.bizCode === 401 || error.bizCode === 403);
|
||||
|
||||
const normalizeUrl = (url: string) => `${apiBase}${url.startsWith('/') ? url : `/${url}`}`;
|
||||
const normalizeUrl = (url: string) => `${currentApiBase()}${url.startsWith('/') ? url : `/${url}`}`;
|
||||
|
||||
export const apiUrl = normalizeUrl;
|
||||
|
||||
@@ -77,6 +80,8 @@ export const parsePayloadText = (statusCode: number, text: string, clearAuthOnUn
|
||||
export const readTextPayload = <T>(statusCode: number, text: string): T =>
|
||||
readPayload<T>(statusCode, parsePayloadText(statusCode, text));
|
||||
|
||||
const shouldUseXhr = () => !isNativeAppRuntime() && typeof XMLHttpRequest !== 'undefined';
|
||||
|
||||
const requestWithXhr = async <T>(options: RequestOptions): Promise<T> => {
|
||||
const method = options.method || 'GET';
|
||||
const hasBody = method !== 'GET' && options.data !== undefined;
|
||||
@@ -109,7 +114,7 @@ const requestWithXhr = async <T>(options: RequestOptions): Promise<T> => {
|
||||
};
|
||||
|
||||
export const apiRequest = async <T>(options: RequestOptions): Promise<T> => {
|
||||
if (typeof XMLHttpRequest !== 'undefined') {
|
||||
if (shouldUseXhr()) {
|
||||
return requestWithXhr<T>(options);
|
||||
}
|
||||
|
||||
@@ -133,7 +138,7 @@ export const apiRequest = async <T>(options: RequestOptions): Promise<T> => {
|
||||
|
||||
export const authenticatedFileUrl = async (url: string): Promise<string> => {
|
||||
const fullUrl = normalizeUrl(url);
|
||||
if (typeof XMLHttpRequest !== 'undefined' && typeof URL !== 'undefined') {
|
||||
if (shouldUseXhr() && typeof URL !== 'undefined') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', fullUrl);
|
||||
|
||||
Reference in New Issue
Block a user