Skip to content

Commit

Permalink
make improvements of the json wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
xieguigang committed May 30, 2024
1 parent 054cd78 commit 1bf7f59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
6 changes: 5 additions & 1 deletion LINQ.ts/Framework/Define/Abstracts/TS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,15 @@
* 请尽量使用upload方法进行文件的上传
*
* @param url 目标数据源,这个参数也支持meta标签的查询语法
* @param options
* 1. sendContentType
* 2. wrapPlantTextError 当后台正常返回一段存文本(但是无法被解析为json),是否封装为一个错误消息给调用函数?
*/
post<T>(url: string, data: object | FormData,
callback?: ((response: IMsg<T>) => void),
options?: {
sendContentType?: boolean
sendContentType?: boolean,
wrapPlantTextError?: boolean
}): void;
/**
* 请注意:这个函数只会接受来自后端的json返回,如果不是json格式,则可能会解析出错
Expand Down
27 changes: 18 additions & 9 deletions LINQ.ts/Framework/Define/Internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ namespace Internal {
ts.post = function (url: string, data: object | FormData,
callback?: ((response: IMsg<{}>) => void),
options?: {
sendContentType?: boolean
sendContentType?: boolean,
wrapPlantTextError?: boolean
}) {

var opts = options ?? {};
var contentType: string = HttpHelpers.measureContentType(data);
var post = <HttpHelpers.PostData>{
type: contentType,
data: data,
sendContentType: (options || {}).sendContentType || true
sendContentType: opts.sendContentType || true
};

HttpHelpers.POST(urlSolver(url), post, function (response, code) {
if (callback) {
if (code == 200) {
callback(handleJSON(response));
callback(handleJSON(response, opts.wrapPlantTextError ?? false));
} else {
// handle page not found and internal server error
callback(<IMsg<{}>>{
Expand Down Expand Up @@ -92,7 +94,7 @@ namespace Internal {
HttpHelpers.GetAsyn(urlSolver(url), function (response, code: number) {
if (callback) {
if (code == 200) {
callback(handleJSON(response));
callback(handleJSON(response, true));
} else {
// handle page not found and internal server error
callback(<IMsg<{}>>{
Expand All @@ -107,7 +109,7 @@ namespace Internal {
ts.upload = function (url: string, file: File, callback?: ((response: IMsg<{}>) => void)) {
HttpHelpers.UploadFile(urlSolver(url), file, null, function (response) {
if (callback) {
callback(handleJSON(response));
callback(handleJSON(response, true));
}
});
};
Expand Down Expand Up @@ -223,7 +225,7 @@ namespace Internal {
return url;
}

function handleJSON(response: any): any {
function handleJSON(response: any, wrapPlantTextError: boolean): any {
if (typeof response == "string") {

/*
Expand All @@ -236,10 +238,17 @@ namespace Internal {
try {
return JSON.parse(response);
} catch (ex) {
console.error("Invalid json text: ");
console.error(response);
if (wrapPlantTextError) {
return <IMsg<string>>{
code: 500,
info: response
}
} else {
console.error("Invalid json text: ");
console.error(response);

throw ex;
throw ex;
}
}
} else {
return response;
Expand Down

0 comments on commit 1bf7f59

Please sign in to comment.