npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fetch.macro

v1.9.2

Published

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

Downloads

82

Readme

f("/api/ping")

↓ ↓ ↓ ↓ ↓ ↓

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


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.

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.

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.

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.

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.

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.

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.

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] ↑

License

MIT