Skip to content

Commit

Permalink
feat(fetchArrayBuffer): add fetchArrayBuffer API for produce fetch fo…
Browse files Browse the repository at this point in the history
…r arrayBuffer response (#34)

#22 for `fetchArrayBuffer`
  • Loading branch information
abdmmar authored Oct 4, 2022
1 parent 16ee4eb commit 1136e05
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,34 @@ const fetchProject = ({ id, projectId, others, ...opts }) =>
fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.formData());
```

### fetchArrayBuffer

It will be produce a code for fetch function with URL by input and return [**response arrayBuffer**](https://fetch.spec.whatwg.org/#dom-body-arraybuffer).

#### Input

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

#### Output

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

## Contributors

[\[Back to the Table of Contents\]](#toc)

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore-start -->

<!-- markdownlint-disable -->

<table>
<tbody>
<tr>
Expand All @@ -212,11 +233,12 @@ const fetchProject = ({ id, projectId, others, ...opts }) =>
</tr>
</tbody>
<tfoot>

</tfoot>
</table>

<!-- markdownlint-restore -->

<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->
Expand Down
22 changes: 3 additions & 19 deletions src/fetch.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,13 @@ const memberExpressionTemplate = (ref) =>
[ref === "fetchJson"]: ".then(r => r.json())",
[ref === "fetchBlob"]: ".then(r => r.blob())",
[ref === "fetchFormData"]: ".then(r => r.formData())",
/**
* @todo {https://github.com/r17x/fetch.macro/issues/22}
* - [ ] fetchArrayBuffer
*/
[ref === "fetchArrayBuffer"]: ".then(r => r.arrayBuffer())",
}.true);

module.exports = createMacro(
({
babel: { types: t, template },
references: {
default: paths,
fetchText,
fetchJson,
fetchBlob,
fetchFormData
/**
* @todo {https://github.com/r17x/fetch.macro/issues/22}
* - [ ] fetchArrayBuffer
*/
},
references: { default: paths, fetchText, fetchJson, fetchBlob, fetchFormData, fetchArrayBuffer },
}) => {
const transform =
(reference) =>
Expand Down Expand Up @@ -89,9 +76,6 @@ module.exports = createMacro(
(fetchJson || []).forEach(transform("fetchJson"));
(fetchBlob || []).forEach(transform("fetchBlob"));
(fetchFormData || []).forEach(transform("fetchFormData"));
/**
* @todo {https://github.com/r17x/fetch.macro/issues/22}
* - [ ] fetchArrayBuffer
*/
(fetchArrayBuffer || []).forEach(transform("fetchArrayBuffer"));
},
);
16 changes: 16 additions & 0 deletions tests/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ const testCases = [
fetch(\`/api/v1/user/\${id}/project/\${projectId}/\${others}\`, opts).then((r) => r.formData());
`,
},
// fetchArrayBuffer
{
title: "fetchArrayBuffer with url params",
name: "fetchArrayBuffer",
description:
"It will be produce a code for fetch function with URL by input and return [**response arrayBuffer**](https://fetch.spec.whatwg.org/#dom-body-arraybuffer).",
category: "API",
code: `
import {fetchArrayBuffer} from '../src/fetch.macro'
const fetchProject = fetchArrayBuffer\`/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.arrayBuffer());
`,
},
];

module.exports = { testCases };

0 comments on commit 1136e05

Please sign in to comment.