@ywfe/fe-tools 1.2.1-beta.34 → 1.2.1-beta.35

Sign up to get free protection for your applications and to get access to all the features.
package/request.ts DELETED
@@ -1,48 +0,0 @@
1
- // Ctrl-S保存代码
2
- import { querystring } from "@ywfe/utils";
3
- interface RequestOptions {
4
- url: string;
5
- method?: string;
6
- params?: any;
7
- }
8
- async function request({ input }: { input: RequestOptions }) {
9
- const method = input.method || "GET";
10
- const basePathMap = new Map([
11
- ["prod", "https://gateway.ywwl.com"],
12
- ["test", "https://test-gateway.ywwl.com"],
13
- ["test2", "https://test2-gateway.ywwl.com"],
14
- ["dev", "https://dev-gateway.ywwl.com"],
15
- ]);
16
- const getBasePath = () => {
17
- const env = "{{env}}";
18
- return basePathMap.get(env);
19
- };
20
- const isValidURL = (url:string) => {
21
- const regex = /^https?:\/\//;
22
- return regex.test(url);
23
- };
24
- const prefix = getBasePath();
25
- let requestUrl = isValidURL(input.url) ? input.url : `${prefix}${input.url}`;
26
- console.log('requestUrl',requestUrl,isValidURL(input.url))
27
- const fetchOption: { [key: string]: any } = {
28
- method: method,
29
- headers: {
30
- "Content-type": "application/json",
31
- "x-token": "{{token}}",
32
- },
33
- };
34
- if (method.toUpperCase() === "POST") {
35
- fetchOption.body = JSON.stringify(input.params);
36
- } else if (method.toUpperCase() === "GET") {
37
- requestUrl = `${requestUrl}?${querystring.stringify(input.params || {})}`;
38
- }
39
- const response = await fetch(requestUrl, fetchOption);
40
- if (!response.ok) {
41
- const message = `An error has occured: ${response.status}`;
42
- return { success: false, message };
43
- }
44
- const result = await response.json();
45
- return result;
46
- }
47
-
48
- export default request