@thepassle/app-tools
Version:
Collection of tools I regularly use to build apps. Maybe they're useful to somebody else. Maybe not. Most of these are thin wrappers around native API's, like the native `<dialog>` element, `fetch` API, and `URLPattern`.
27 lines (24 loc) • 679 B
JavaScript
/**
* @param {string} jsonPrefix
* @returns {import('../index.js').Plugin}
*/
export function jsonPrefixPlugin(jsonPrefix) {
let responseType;
return {
name: 'jsonPrefix',
beforeFetch: ({responseType: type}) => {
responseType = type;
},
afterFetch: async (res) => {
if(jsonPrefix && responseType === 'json') {
let responseAsText = await res.text();
if(responseAsText.startsWith(jsonPrefix)) {
responseAsText = responseAsText.substring(jsonPrefix.length);
}
return new Response(responseAsText, res);
}
return res;
}
}
}
export const jsonPrefix = jsonPrefixPlugin(`)]}',\n`);