Skip to content

Commit f603f92

Browse files
committed
add some code
1 parent 4340e49 commit f603f92

22 files changed

Lines changed: 1619 additions & 564 deletions

example/WebProject1.Client.TypeScript/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "jest",
8-
"fetch": "download http://localhost:5077/rest-model/ts?SendFunctionImportModelName=../base > src/client/api.ts"
8+
"fetch": "download http://localhost:5077/rest-model/ts?SendFunctionImportModelName=../base > src/client/api.ts",
9+
"fetchTest": "download http://localhost:5077/rest-model/ts?SendFunctionImportModelName=../base > test/test.ts"
910
},
1011
"author": "",
1112
"license": "ISC",
Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from 'axios';
1+
import axios, { AxiosRequestConfig } from 'axios';
22

33
export interface RestInfo {
44
url: string,
@@ -14,10 +14,18 @@ axios.defaults.timeout = 100000;
1414

1515
const multipartFormDataHeader = { 'Content-type': 'multipart/form-data' };
1616
const applicationJsonDataHeader = { 'Content-type': 'application/json' }
17+
const formUrlEncodedHeader = { 'Content-type': 'application/x-www-form-urlencoded' }
1718

1819
function paramsSerializer(params: any): string {
19-
let entries = Object.entries(params || {}).flatMap(([k, v]) =>
20-
Array.isArray(v) ? v.map(t => [k, t]) : [[k, v]]
20+
let entries = Object.entries(params || {}).flatMap(([k, v]) => {
21+
if (Array.isArray(v)) {
22+
return v.map(t => [k, t]);
23+
} else if (v instanceof Object) {
24+
return Object.entries(v);
25+
} else {
26+
return [[k, v]];
27+
}
28+
}
2129
);
2230
let segments = entries.map(p => `${p[0]}=${encodeURIComponent(p[1] ?? '')}`);
2331
return segments.join('&');
@@ -26,27 +34,55 @@ function buildFormData(forms: any) {
2634
let formData = new FormData();
2735
Object.entries(forms).forEach(([k, v]) => {
2836
if (v instanceof Blob) {
29-
37+
formData.append(k, v);
3038
} else {
31-
39+
formData.append(k, String(v));
3240
}
3341
});
3442
return formData;
3543
}
3644

3745
export async function send<T>(req: RestInfo): Promise<T> {
38-
let reqInfo = {
46+
let reqInfo = req.forms ? (
47+
req.method.toLowerCase() === "get" ? buildUrlEncodedFormRequest(req)
48+
: buildMultipartFormDataFormRequest(req))
49+
: buildCommonRequest(req);
50+
const res = await axios.request(reqInfo);
51+
return res.data;
52+
}
53+
function buildUrlEncodedFormRequest(req: RestInfo): AxiosRequestConfig<any> {
54+
return {
55+
url: req.url,
56+
method: req.method,
57+
headers: { ...formUrlEncodedHeader, ...req.headers },
58+
params: { ...req.params, ...req.forms },
59+
paramsSerializer: {
60+
serialize: paramsSerializer
61+
}
62+
}
63+
}
64+
function buildMultipartFormDataFormRequest(req: RestInfo): AxiosRequestConfig<any> {
65+
return {
3966
url: req.url,
4067
method: req.method,
41-
headers: req.forms ? { ...multipartFormDataHeader, ...req.headers } : { ...applicationJsonDataHeader, ...req.headers },
68+
headers: { ...multipartFormDataHeader, ...req.headers },
4269
params: req.params,
43-
data: req.forms ? req.forms : req.body,
44-
70+
data: req.forms,
4571
paramsSerializer: {
4672
serialize: paramsSerializer
47-
},
48-
73+
}
4974
}
50-
const res = await axios.request(reqInfo);
51-
return res.data;
5275
}
76+
function buildCommonRequest(req: RestInfo): AxiosRequestConfig<any> {
77+
return {
78+
url: req.url,
79+
method: req.method,
80+
headers: { ...applicationJsonDataHeader, ...req.headers },
81+
params: req.params,
82+
data: req.body,
83+
paramsSerializer: {
84+
serialize: paramsSerializer
85+
},
86+
};
87+
}
88+

0 commit comments

Comments
 (0)