UNPKG

@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`.

28 lines (24 loc) 702 B
/** * @param {string} jsonPrefix * @returns {import('../index.js').Plugin} */ export function jsonPrefixPlugin(jsonPrefix) { let responseType; return { name: "jsonPrefix", beforeFetch: ({ responseType: type }) => { responseType = type; }, afterFetch: async ({ response }) => { if (jsonPrefix && responseType === "json") { let responseAsText = await response.text(); if (responseAsText.startsWith(jsonPrefix)) { responseAsText = responseAsText.substring(jsonPrefix.length); } return new Response(responseAsText, response); } return response; }, }; } export const jsonPrefix = jsonPrefixPlugin(`)]}',\n`);