Skip to content

Commit

Permalink
* core: support to custom json parser in ajax request.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Feb 26, 2024
1 parent 4fccc54 commit 1bf2320
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/core/src/ajax/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class Ajax<T> {
}
this._init();

const {timeout, dataType: dataTypeSetting, accepts, dataFilter, throws} = this.setting;
const {timeout, dataType: dataTypeSetting, accepts, dataFilter, throws, jsonParser} = this.setting;
if (timeout) {
this._timeoutID = window.setTimeout(() => {
this.abort(new Error('timeout'));
Expand All @@ -269,7 +269,12 @@ export class Ajax<T> {
if (isAttachment || dataType === 'blob' || dataType === 'file') {
data = await response.blob();
} else if (dataType === 'json') {
data = await response.json();
if (typeof jsonParser === 'function') {
data = await response.text();
data = jsonParser(data as string);
} else {
data = await response.json();
}
} else {
data = await response.text();
}
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/ajax/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface AjaxSetting extends RequestInit {
dataType?: string;
timeout?: number;
processData?: boolean;
jsonParser?: (text: string) => unknown;
// global?: boolean;
crossDomain?: boolean;
traditional?: boolean;
Expand Down

0 comments on commit 1bf2320

Please sign in to comment.