Code Monkey home page Code Monkey logo

fetch.macro's Introduction

fetch.macro

Allows you to build fetcher function by URL at compile-time.

f("/api/ping")

↓ ↓ ↓ ↓ ↓ ↓

(opts) => fetch("/api/ping", opts)

UsageAPIContributors


GitHub Workflow Status (branch) Codecov branch npm npm downloads License GitHub contributors (via allcontributors.org)

Usage

[Back to the Table of Contents] ↑

Simply install and configure 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.

SWC

🚧 [Under Development] This is section for using fetch.macro as swc-plugin.

You can also use fetch.macro in a swc-based project (e.g: Next.js) by using the SWC plugins.

Due to how the plugins loaded, you have to pass rootDir option pointing to the root directory of your project (where your node_modules directory lives). Typically it's enough to pass __dirname.

// next.config.js
module.exports = {
  experimental: {
    swcPlugins: [["fetch.macro/swc"]],
  },
};

Vite

To be able to use these macros in your Vite project, you only need install vite-plugin-babel-macros and add some configuration in vite.config.js. And it just work.

$ npm i -D vite-plugin-babel-macros
import MacrosPlugin from "vite-plugin-babel-macros";

export default {
  // ...
  plugins: [
    // ...
    MacrosPlugin(),
  ],
};

Example

Basic

Run one of the following command inside your project directory to install the package:

$ npm i fetch.macro
or
$ yarn add fetch.macro

Given the following Input:

import f from "fetch.macro";
const fetchByUrl = f("/api/v1/ping");

Babel will produce the following Output:

const fetchByUrl = (opts) => fetch("/api/v1/ping", opts);

It also works as a tagged template literal:

import f from "fetch.macro";
const fetchByUrl = f`/api/v1/ping`;

That will produce the same output as the function version.

Nested

Given the following Input:

import f from "fetch.macro";
const fetchProject = f`/api/v1/user/:id/project/:projectId/:others`;

Babel will produce the following Output:

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.

Input Output
import f from "fetch.macro";
const fetchByUrl = f("/api/v1/ping");
const fetchByUrl = (opts) => fetch("/api/v1/ping", opts);

fetchText

It will be produce a code for fetch function with URL by input and return response text.

Input Output
import { fetchText } from "fetch.macro";
const fetchProject = fetchText`/api/v1/user/:id/project/:projectId/:others`;
const fetchProject = ({ id, projectId, others, ...opts }) =>
  fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.text());

fetchJson

It will be produce a code for fetch function with URL by input and return response json.

Input Output
import { fetchJson } from "fetch.macro";
const fetchProject = fetchJson`/api/v1/user/:id/project/:projectId/:others`;
const fetchProject = ({ id, projectId, others, ...opts }) =>
  fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.json());

fetchBlob

It will be produce a code for fetch function with URL by input and return response blob.

Input Output
import { fetchBlob } from "fetch.macro";
const fetchProject = fetchBlob`/api/v1/user/:id/project/:projectId/:others`;
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.

Input Output
import { fetchFormData } from "fetch.macro";
const fetchProject = fetchFormData`/api/v1/user/:id/project/:projectId/:others`;
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.

Input Output
import { fetchArrayBuffer } from "fetch.macro";
const fetchProject = fetchArrayBuffer`/api/v1/user/:id/project/:projectId/:others`;
const fetchProject = ({ id, projectId, others, ...opts }) =>
  fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.arrayBuffer());

fetchClone

It will be produce a code for fetch function with URL by input and return response cloned data.

Input Output
import { fetchClone } from "fetch.macro";
const fetchProject = fetchClone`/api/v1/user/:id/project/:projectId/:others`;
const fetchProject = ({ id, projectId, others, ...opts }) =>
  fetch(`/api/v1/user/${id}/project/${projectId}/${others}`, opts).then((r) => r.clone());

Contributors

[Back to the Table of Contents] ↑

RiN
RiN

🤔 🚇 🔧 💻
Ryan Aunur Rassyid
Ryan Aunur Rassyid

💡
Rivaldi Putra
Rivaldi Putra

💡
Ibrahim Hanif
Ibrahim Hanif

💻 💡
Mudassir
Mudassir

💻 💡
Ahmad Muwaffaq
Ahmad Muwaffaq

💻 💡
Abdullah Ammar
Abdullah Ammar

💻 💡
Afrian Junior
Afrian Junior

💻 💡

License

MIT

fetch.macro's People

Contributors

abdmmar avatar afrianjunior avatar allcontributors[bot] avatar anakmagang avatar dependabot[bot] avatar ibrahim4529 avatar lzyct avatar mupinnn avatar nyancodeid avatar r17x avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fetch.macro's Issues

Fetch Interceptor

Is your feature request related to a problem? Please describe.
I need interceptor feature to intercept request or response.

New Features

New Features

Hacktoberfest has arrived

At this project we want to deliver new feature, the new feature is based on fetch specification at whatwag - See here

image

Example Usage

import { fetchText, fetchJson, fetchBlob, fetchArrayBuffer, fetchFormData } from 'fetch.macro'

const fetchSomeText = fetchText`/api/v1/user/:id/project/:projectId/:others`;

const fetchSomeJson = fetchJson`/api/v1/user/:id/project/:projectId/:others`;

const fetchSomeBlob = fetchBlob`/api/v1/user/:id/project/:projectId/:others`;

const fetchSomeArrayBuffer = fetchArrayBuffer`/api/v1/user/:id/project/:projectId/:others`;

const fetchSomeFormData = fetchFormData`/api/v1/user/:id/project/:projectId/:others`;

Todo

This is lists for new API at fetch.macro modules.

Before start the contrbution, we have been provide code flag for make our easy to write code implementation or test case.

This the line of code related with this issue and step to contributed:

  1. Write Reference name - reference name is import module name (e.g: fetchText) - See Here
  2. Write Member Expression implementation - (e.g: .then(r => r.json)) - See Here
  3. Apply transformation - See here
  4. Write little documentation at README.md - Example: fetchText (Skip this step, go to step 5., write documentation about api in test case)
  5. Write Test Case in fetch.test.js - See here
  • fetchText - #23
  • fetchJson - #24
  • fetchBlob - #28
  • fetchArrayBuffer #34
  • fetchFormData - #31
  • fetchClone - Basically just Response.clone - This is low priority but no problem to include as the new features - @fzn0x - Thanks to @afrianjunior

Thanks for you contribution and happy hacking

setup: using @swc/jest

Is your feature request related to a problem? Please describe.
This is not feature, this is just want to use @swc/jest

Describe the solution you'd like
Since, want to provided fetch.macro in swc as swc-plugin. It's better to integrated with @swc/jest.

Describe alternatives you've considered

Additional context

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.