Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetchArrayBuffer): add fetchArrayBuffer API for produce fetch for arrayBuffer response #34

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -211,11 +232,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 };