UNPKG

@dldl/api-fixtures-generator

Version:

Generator able to create fixtures files by spying fetch requests.

22 lines (15 loc) 623 B
import md5 from "md5" const oldFetch = window.fetch window.fetch = async (url, body) => { const response = await oldFetch(url, body) const originalResponse = response.clone() const contentType = response.headers.get("Content-Type") const hash = md5(url + JSON.stringify(body)) const format = contentType.split("/")[1].split(";")[0] const fileName = `${hash}.${format}` const downloadLink = document.createElement("a") downloadLink.download = fileName downloadLink.href = `data:${contentType};base64,${btoa(await response.text())}` downloadLink.click() return Promise.resolve(originalResponse) }