@ywfe/fe-tools 1.2.1-beta.3 → 1.2.1-beta.31
Sign up to get free protection for your applications and to get access to all the features.
- package/JSON2String.ts +17 -0
- package/index.ts +1 -0
- package/lib/JSON2String.d.ts +9 -0
- package/lib/index.d.ts +1 -0
- package/lib/userInputToJson.d.ts +1 -1
- package/lib/ywfe-tools.cjs +53 -25
- package/lib/ywfe-tools.cjs.map +1 -1
- package/lib/ywfe-tools.esm.js +53 -26
- package/lib/ywfe-tools.esm.js.map +1 -1
- package/lib/ywfe-tools.umd.js +4 -0
- package/lib/ywfe-tools.umd.js.map +1 -0
- package/package.json +1 -2
- package/request.ts +35 -36
- package/rollup.config.js +1 -4
- package/userInputToJson.ts +19 -12
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"private": false,
|
3
3
|
"name": "@ywfe/fe-tools",
|
4
|
-
"version": "1.2.1-beta.
|
4
|
+
"version": "1.2.1-beta.31",
|
5
5
|
"description": "工具函数库",
|
6
6
|
"main": "./lib/ywfe-tools.cjs",
|
7
7
|
"module": "./lib/ywfe-tools.esm.js",
|
@@ -12,7 +12,6 @@
|
|
12
12
|
"prepublishOnly": "npm run build && npm run build:type",
|
13
13
|
"test": "jest --coverage"
|
14
14
|
},
|
15
|
-
"repository": "ywfe",
|
16
15
|
"author": {
|
17
16
|
"name": "YWFE",
|
18
17
|
"email": "ywfe@ywwl.com",
|
package/request.ts
CHANGED
@@ -1,49 +1,48 @@
|
|
1
1
|
// Ctrl-S保存代码
|
2
|
-
import {querystring} from
|
2
|
+
import { querystring } from "@ywfe/utils";
|
3
3
|
interface RequestOptions {
|
4
|
-
url:string
|
5
|
-
method?:string
|
6
|
-
params?:any
|
4
|
+
url: string;
|
5
|
+
method?: string;
|
6
|
+
params?: any;
|
7
7
|
}
|
8
|
-
async function request({input}:{input:RequestOptions}){
|
9
|
-
const method = input.method ||
|
8
|
+
async function request({ input }: { input: RequestOptions }) {
|
9
|
+
const method = input.method || "GET";
|
10
10
|
const basePathMap = new Map([
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
])
|
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
16
|
const getBasePath = () => {
|
17
|
-
|
18
|
-
|
17
|
+
const env = "{{env}}";
|
18
|
+
return basePathMap.get(env);
|
19
19
|
};
|
20
|
-
const isValidURL = (url:string)=> {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
return !!pattern.test(url);
|
29
|
-
}
|
30
|
-
const prefix = getBasePath()
|
31
|
-
let requestUrl = isValidURL(input.url) ? input.url :`${prefix}${input.url}`
|
32
|
-
const fetchOption: {[key:string]:any} = {
|
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 } = {
|
33
28
|
method: method,
|
34
29
|
headers: {
|
35
|
-
|
36
|
-
|
30
|
+
"Content-type": "application/json",
|
31
|
+
"x-token": "{{token}}",
|
37
32
|
},
|
38
33
|
};
|
39
|
-
if (method.toUpperCase() ===
|
40
|
-
|
41
|
-
}else if(method.toUpperCase() ===
|
42
|
-
requestUrl = `${requestUrl}?${querystring.stringify(input.params || {})}
|
43
|
-
|
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 };
|
44
43
|
}
|
45
|
-
|
46
|
-
|
47
|
-
return res.json();
|
44
|
+
const result = await response.json();
|
45
|
+
return result;
|
48
46
|
}
|
47
|
+
|
49
48
|
export default request
|
package/rollup.config.js
CHANGED
@@ -39,7 +39,7 @@ const formats = {
|
|
39
39
|
|
40
40
|
const config = {
|
41
41
|
input: './index.ts',
|
42
|
-
inlineDynamicImports: true,
|
42
|
+
// inlineDynamicImports: true,
|
43
43
|
output: [formats.esm, formats.commonjs],
|
44
44
|
external: ['react', 'react-dom'],
|
45
45
|
plugins: [
|
@@ -49,9 +49,6 @@ const config = {
|
|
49
49
|
progress({
|
50
50
|
clearLine: false,
|
51
51
|
}),
|
52
|
-
replace({
|
53
|
-
NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
|
54
|
-
}),
|
55
52
|
nodeResolver(),
|
56
53
|
commonjs(),
|
57
54
|
typescript(),
|
package/userInputToJson.ts
CHANGED
@@ -1,17 +1,24 @@
|
|
1
|
-
// Ctrl-S保存代码
|
2
1
|
import {request} from './index'
|
2
|
+
// Ctrl-S保存代码
|
3
3
|
interface userInput {
|
4
|
-
userInput?:string
|
5
|
-
rules?:string
|
4
|
+
userInput?: string;
|
5
|
+
rules?: string;
|
6
6
|
}
|
7
|
-
async function userInputToJson({input}:{input:userInput}){
|
8
|
-
|
9
|
-
const res
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
async function userInputToJson({ input }: { input: userInput }) {
|
8
|
+
console.log("input", input);
|
9
|
+
const res: { message: string; success: boolean; data: any } =
|
10
|
+
await request({
|
11
|
+
input: {
|
12
|
+
url: "https://fc-typechat.ywwl.com/userInputToJson",
|
13
|
+
method: "GET",
|
14
|
+
params: input,
|
15
|
+
},
|
16
|
+
});
|
17
|
+
if (res.success) {
|
18
|
+
const result = res.data;
|
19
|
+
return result.success ? result.data : result;
|
20
|
+
} else {
|
21
|
+
return res.success ? res.data : res;
|
22
|
+
}
|
16
23
|
}
|
17
24
|
export default userInputToJson
|