From bef082a7ba28a1d7222590d48b5491046a3ae909 Mon Sep 17 00:00:00 2001 From: r17x Date: Mon, 3 Oct 2022 01:46:26 +0800 Subject: [PATCH] feat(fetchText): add fetchText API for produce fetch for text response --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++++- src/fetch.macro.js | 33 ++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index adbc3ceb..ad1e14cc 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,27 @@ + + +- [Usage](#usage) + - [Vite](#vite) + - [Example](#example) + - [Basic](#basic) + - [Nested](#nested) +- [API](#api) + - [Default](#default) + - [fetchText](#fetchtext) +- [Contributors](#contributors) +- [License](#license) + + + ## Usage Simply install and configure [`babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros) and then use `fetch.macro`. +> Some project that build with `create-react-app` doesn't need extra setup for `babel-plugin-macros`. + ### Vite To be able to use these macros in your [Vite](https://vitejs.dev/) project, you only need install [`vite-plugin-babel-macros`](https://github.com/itsMapleLeaf/vite-plugin-babel-macros) and add some configuration in `vite.config.js`. And it just work. @@ -47,11 +64,13 @@ export default { #### Basic Run one of the following command inside your project directory to install the package: + ``` $ npm i fetch.macro or -$ yarn add fetch.macro +$ yarn add fetch.macro ``` + Given the following `Input`: ```javascript @@ -90,6 +109,28 @@ const fetchProject = ({ id, projectId, others, ...opts }) => fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts); ``` +## API + +### Default + +It will be produce a code for fetch function with URL by input and return response that need to be manual handle the response. + +``` +import f from 'fetch.macro' + +const fetchByUrl = f("/api/v1/ping"); +``` + +### fetchText + +It will be produce a code for fetch function with URL by input and return [**response text**](https://webidl.spec.whatwg.org/#idl-USVString). + +``` +import { fetchText } from 'fetch.macro' + +const fetchLicense = fetchText("https://raw.githubusercontent.com/r17x/fetch.macro/main/LICENSE"); +``` + ## Contributors diff --git a/src/fetch.macro.js b/src/fetch.macro.js index 9789c902..ebeef582 100644 --- a/src/fetch.macro.js +++ b/src/fetch.macro.js @@ -9,20 +9,23 @@ const getValue = (path) => const isValueHaveArgs = (val) => /:\w+/g.test(val); -module.exports = createMacro( +const memberExpressionTemplate = (ref) => ({ - babel: { types: t, template }, - references: { - default: paths, - // @TODO fetchText - }, - }) => { - paths.forEach(({ parentPath }) => { + [true]: "", + [ref === "default"]: "", + [ref === "fetchText"]: ".then(r => r.text())", + }.true); + +module.exports = createMacro(({ babel: { types: t, template }, references: { default: paths, fetchText } }) => { + const transform = + (reference) => + ({ parentPath }) => { const value = getValue(parentPath); + const memberExpression = memberExpressionTemplate(reference); if (value) { if (isValueHaveArgs(value)) { - const buildFetch = template(`(PARAM) => fetch(URI, opts)`); + const buildFetch = template(`(PARAM) => fetch(URI, opts)`.concat(memberExpression)); parentPath.replaceWithMultiple( buildFetch({ PARAM: t.objectPattern( @@ -48,7 +51,7 @@ module.exports = createMacro( }), ); } else { - const buildFetch = template(`(opts) => fetch(URI, opts)`); + const buildFetch = template(`(opts) => fetch(URI, opts)`.concat(memberExpression)); parentPath.replaceWithMultiple( buildFetch({ URI: t.stringLiteral(value), @@ -58,6 +61,10 @@ module.exports = createMacro( } else { parentPath.parentPath.remove(); } - }); - }, -); + }; + + (paths || []).forEach(transform("default")); + (fetchText || []).forEach(transform("fetchText")); + // @TODO fetchJson + // @TODO fetchBlob +});