Skip to content

Commit

Permalink
feat(fetchFormData): add fetchFormData API for produce fetch for form…
Browse files Browse the repository at this point in the history
…Data response (#31)

#22 for `fetchFormData`
  • Loading branch information
mupinnn authored Oct 4, 2022
1 parent aae1f9f commit 08a8303
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ const fetchProject = ({ id, projectId, others, ...opts }) =>
fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.blob());
```

### fetchFormData

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

#### Input

```javascript
import { fetchFormData } from "../src/fetch.macro";
const fetchProject = fetchFormData`/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.formData());
```

## Contributors

[\[Back to the Table of Contents\]](#toc)
Expand Down
7 changes: 3 additions & 4 deletions src/fetch.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const memberExpressionTemplate = (ref) =>
[ref === "fetchText"]: ".then(r => r.text())",
[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
* - [ ] fetchFormData
*/
}.true);

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

module.exports = { testCases };

0 comments on commit 08a8303

Please sign in to comment.