From 08a83036fde769c8e12ec54699963bcf14907b92 Mon Sep 17 00:00:00 2001 From: Ahmad Muwaffaq <46535277+mupinnn@users.noreply.github.com> Date: Tue, 4 Oct 2022 09:41:50 +0700 Subject: [PATCH] feat(fetchFormData): add fetchFormData API for produce fetch for formData response (#31) #22 for `fetchFormData` --- README.md | 18 ++++++++++++++++++ src/fetch.macro.js | 7 +++---- tests/test-cases.js | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e35b977e..4e02f047 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/fetch.macro.js b/src/fetch.macro.js index 3d97af69..f837c498 100644 --- a/src/fetch.macro.js +++ b/src/fetch.macro.js @@ -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); @@ -31,10 +31,10 @@ module.exports = createMacro( fetchText, fetchJson, fetchBlob, + fetchFormData /** * @todo {https://github.com/r17x/fetch.macro/issues/22} * - [ ] fetchArrayBuffer - * - [ ] fetchFormData */ }, }) => { @@ -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 */ }, ); diff --git a/tests/test-cases.js b/tests/test-cases.js index 8e87a14e..1466063e 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -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 };