lion/packages/ajax/src/transformers.js
2020-11-12 15:42:42 +01:00

19 lines
476 B
JavaScript

/**
* @param {string} prefix
*/
export function jsonPrefixTransformerFactory(prefix) {
return /** @param {string} data */ data => {
let result = data;
if (typeof result === 'string') {
if (prefix.length > 0 && result.indexOf(prefix) === 0) {
result = result.substring(prefix.length);
}
try {
result = JSON.parse(result);
} catch (e) {
/* ignore to allow non-JSON responses */
}
}
return result;
};
}