diff --git a/README.md b/README.md index f7933115..44cecda0 100644 --- a/README.md +++ b/README.md @@ -282,6 +282,36 @@ const fetchProject = ({ id, projectId, others, ...opts }) => +### fetchClone + +It will be produce a code for fetch function with URL by input and return [**response cloned data**](https://developer.mozilla.org/en-US/docs/Web/API/Response/clone). + + + + + + + + + + + +
InputOutput
+ +```javascript +import { fetchClone } from "fetch.macro"; +const fetchProject = fetchClone`/api/v1/user/:id/project/:projectId/:others`; +``` + + + +```javascript +const fetchProject = ({ id, projectId, others, ...opts }) => + fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.clone()); +``` + +
+ ## Contributors [\[Back to the Table of Contents\] ↑](#toc) diff --git a/src/fetch.macro.js b/src/fetch.macro.js index e14e7a9c..62328f5e 100644 --- a/src/fetch.macro.js +++ b/src/fetch.macro.js @@ -10,7 +10,15 @@ const { createMacro } = require("babel-plugin-macros"); * import { fetchText } from 'fetch.macro' * ``` * */ -const REFERENCES = ["default", "fetchText", "fetchJson", "fetchBlob", "fetchFormData", "fetchArrayBuffer"]; +const REFERENCES = [ + "default", + "fetchText", + "fetchJson", + "fetchBlob", + "fetchFormData", + "fetchArrayBuffer", + "fetchClone", +]; /** * @param {KeyValue} keyValue * @return {boolean} @@ -50,6 +58,7 @@ const memberExpressionTemplate = (ref) => [ref === REFERENCES[3]]: ".then(r => r.blob())", [ref === REFERENCES[4]]: ".then(r => r.formData())", [ref === REFERENCES[5]]: ".then(r => r.arrayBuffer())", + [ref === REFERENCES[6]]: ".then(r => r.clone())", }.true); /** @type { import('babel-plugin-macros').MacroHandler } */ diff --git a/tests/test-cases.js b/tests/test-cases.js index 2d33b0a3..4b9ce143 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -151,6 +151,22 @@ const testCases = [ fetch(\`/api/v1/user/\${id}/project/\${projectId}/\${others}\`, opts).then((r) => r.arrayBuffer()); `, }, + // fetchClone + { + title: "fetchClone with url params", + name: "fetchClone", + description: + "It will be produce a code for fetch function with URL by input and return [**response cloned data**](https://developer.mozilla.org/en-US/docs/Web/API/Response/clone).", + category: "API", + code: ` + import {fetchClone} from '../src/fetch.macro' + const fetchProject = fetchClone\`/api/v1/user/:id/project/:projectId/:others\`; + `, + output: ` + const fetchProject = ({ id, projectId, others, ...opts }) => + fetch(\`/api/v1/user/\${id}/project/\${projectId}/\${others}\`, opts).then((r) => r.clone()); + `, + }, ]; module.exports = { testCases };