add request support
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import process from 'process';
|
||||
|
||||
/**
|
||||
* Get URL of backend
|
||||
* @param path Relative path to base url, begins with `/`
|
||||
* @returns Full url
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ``` tsx
|
||||
* import { getUrl } from '@/service';
|
||||
* import { Component, ReactNode } from 'react';
|
||||
* import Taro from '@tarojs/taro';
|
||||
*
|
||||
* class Index extends Component {
|
||||
* state = {
|
||||
* testData: {},
|
||||
* }
|
||||
* componentDidMount(): void {
|
||||
* Taro.request({
|
||||
* url: getUrl('/testdata'),
|
||||
* method: 'GET',
|
||||
* }).then(res => {
|
||||
* console.log(res.data);
|
||||
* this.setState({ testData: res.data.data });
|
||||
* });
|
||||
* }
|
||||
* render() { ... }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function getUrl(path: string): string {
|
||||
const baseUrl = process.env.TARO_APP_API;
|
||||
// console.log('bu:', baseUrl);
|
||||
if (baseUrl) {
|
||||
return baseUrl + path;
|
||||
} else {
|
||||
console.log('env TARO_APP_API is undefined');
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL of Oreo backend
|
||||
* @param path Relative path to base url, begins with `/`
|
||||
* @returns Full url
|
||||
*
|
||||
* @example
|
||||
* // see example of `getUrl`
|
||||
*/
|
||||
export function getUrlOreo(path: string): string {
|
||||
const baseUrlOreo = process.env.TARO_APP_API_OREO;
|
||||
// console.log('buo:', baseUrlOreo);
|
||||
if (baseUrlOreo) {
|
||||
return baseUrlOreo + path;
|
||||
} else {
|
||||
console.log('env TARO_APP_API_OREO is undefined');
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user