Skip to content

Commit

Permalink
feat(fetchClone): add fetchClone API for produce fetch for cloned res…
Browse files Browse the repository at this point in the history
…ponse (#38)

#22 for fetchClone API
  • Loading branch information
afrianjunior committed Oct 9, 2022
1 parent 0104faa commit d88c4f7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Expand Up @@ -282,6 +282,36 @@ const fetchProject = ({ id, projectId, others, ...opts }) =>
</tr>
</table>

### 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).

<table>
<tr>
<td>Input</td>
<td>Output</td>
</tr>
<tr>
<td>

```javascript
import { fetchClone } from "fetch.macro";
const fetchProject = fetchClone`/api/v1/user/:id/project/:projectId/:others`;
```

</td>

<td>

```javascript
const fetchProject = ({ id, projectId, others, ...opts }) =>
fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.clone());
```

</td>
</tr>
</table>

## Contributors

[\[Back to the Table of Contents\]](#toc)
Expand Down
11 changes: 10 additions & 1 deletion src/fetch.macro.js
Expand Up @@ -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}
Expand Down Expand Up @@ -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 } */
Expand Down
16 changes: 16 additions & 0 deletions tests/test-cases.js
Expand Up @@ -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 };

0 comments on commit d88c4f7

Please sign in to comment.