Skip to content

Commit

Permalink
feat(fetchText): add fetchText API for produce fetch for text response
Browse files Browse the repository at this point in the history
  • Loading branch information
r17x committed Oct 2, 2022
1 parent 1a82c51 commit bef082a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@

</div>

<!-- vim-markdown-toc GFM -->

- [Usage](#usage)
- [Vite](#vite)
- [Example](#example)
- [Basic](#basic)
- [Nested](#nested)
- [API](#api)
- [Default](#default)
- [fetchText](#fetchtext)
- [Contributors](#contributors)
- [License](#license)

<!-- vim-markdown-toc -->

## 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.
Expand All @@ -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
Expand Down Expand Up @@ -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

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
Expand Down
33 changes: 20 additions & 13 deletions src/fetch.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand All @@ -58,6 +61,10 @@ module.exports = createMacro(
} else {
parentPath.parentPath.remove();
}
});
},
);
};

(paths || []).forEach(transform("default"));
(fetchText || []).forEach(transform("fetchText"));
// @TODO fetchJson
// @TODO fetchBlob
});

0 comments on commit bef082a

Please sign in to comment.