Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 04daa89

Browse files
committed
Add postResponseJson.
1 parent e37e42b commit 04daa89

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

dist/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ interface ILoader {
5959
'credentials'?: 'include' | 'same-origin' | 'omit';
6060
'headers'?: HeadersInit;
6161
}): Promise<Response | null>;
62+
/**
63+
* --- 发送响应为 json 的网络数据,无需 try,失败返回 null ---
64+
* @param url 网址
65+
* @param data 数据对象
66+
* @param opt 选项
67+
*/
68+
postResponseJson(url: string, data: Record<string, any> | FormData, opt?: {
69+
'credentials'?: 'include' | 'same-origin' | 'omit';
70+
'headers'?: HeadersInit;
71+
}): Promise<any | null>;
6272
/**
6373
* --- 获取文件序列 ---
6474
* @param urls 网址列表,已加载的文件不会被再次返回

dist/loader.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,20 @@ return module.exports;`;
456456
}
457457
});
458458
},
459+
postResponseJson: function (url_1, data_1) {
460+
return __awaiter(this, arguments, void 0, function* (url, data, opt = {}) {
461+
const res = yield this.post(url, data, opt);
462+
if (!res) {
463+
return null;
464+
}
465+
try {
466+
return yield res.json();
467+
}
468+
catch (_a) {
469+
return null;
470+
}
471+
});
472+
},
459473
fetchFiles: function (urls_1) {
460474
return __awaiter(this, arguments, void 0, function* (urls, opt = {}) {
461475
return new Promise((resolve) => {

dist/loader.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,22 @@ return module.exports;`;
570570
}
571571
},
572572

573+
postResponseJson: async function(url: string, data: Record<string, any> | FormData, opt: {
574+
'credentials'?: 'include' | 'same-origin' | 'omit';
575+
'headers'?: HeadersInit;
576+
} = {}): Promise<any | null> {
577+
const res = await this.post(url, data, opt);
578+
if (!res) {
579+
return null;
580+
}
581+
try {
582+
return await res.json();
583+
}
584+
catch {
585+
return null;
586+
}
587+
},
588+
573589
fetchFiles: async function(urls: string[], opt: {
574590
'init'?: RequestInit;
575591
'load'?: (url: string) => void;

0 commit comments

Comments
 (0)