Skip to main content
Home
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
76%
Published
a year ago (1.0.0)

Fetchling - syntactic sugar for fetch

Fetchling provides helpful syntactic sugar for the built-in browser fetch API. Available in modern browsers, Deno, and Node.js 18+, fetch has largely eliminated the need for HTTP client libraries in JavaScript applications.

That being said, fetch does require a non-trivial amount of boilerplate. Fetchling provides a tiny layer abstraction that helps remove some of that boilerplate, especially when making similar requests to the same collection of REST API endpoints.

How it works

Let's see how Fetchling works by writing some code that interacts with the extremely useful D&D Fifth Edition API.

The following example sets up a reusable api object (a Fetchling instance) and uses it to make a GET request to a resource within the API.

import f from "https://deno.land/x/fetchling/mod.ts";

const api = f("https://www.dnd5eapi.co/api", { json: true });
const { data } = await api("monsters/adult-black-dragon").get();

console.log("Name:", data.name);

Already, this is a bit nicer than fetch because of the { json: true } configuration option. That option will set the Accept header for us, and automatically JSON parse the response body.

Where this style of interface really becomes useful is when you have to make multiple requests to the same API. Rather than having to manually construct every input to fetch, you can create subresources to interact with specific endpoints, and share the same configuration.

import f from "https://deno.land/x/fetchling/mod.ts";

const api = f("https://www.dnd5eapi.co/api", { json: true });
const monsters = api("monsters");

const abolethData = (await monsters("aboleth").get()).data;
const dragonData = (await monsters("adult-black-dragon").get()).data;

console.log("Aboleth alignment:", abolethData.alignment);
console.log("Black Dragon alignment:", dragonData.alignment);

There's lots more to document and discover, but that will remain TODO for now :)

License

MIT

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@kwhinnery/fetchling

Import symbol

import * as fetchling from "@kwhinnery/fetchling";
or

Import directly with a jsr specifier

import * as fetchling from "jsr:@kwhinnery/fetchling";

Add Package

pnpm i jsr:@kwhinnery/fetchling
or (using pnpm 10.8 or older)
pnpm dlx jsr add @kwhinnery/fetchling

Import symbol

import * as fetchling from "@kwhinnery/fetchling";

Add Package

yarn add jsr:@kwhinnery/fetchling
or (using Yarn 4.8 or older)
yarn dlx jsr add @kwhinnery/fetchling

Import symbol

import * as fetchling from "@kwhinnery/fetchling";

Add Package

vlt install jsr:@kwhinnery/fetchling

Import symbol

import * as fetchling from "@kwhinnery/fetchling";

Add Package

npx jsr add @kwhinnery/fetchling

Import symbol

import * as fetchling from "@kwhinnery/fetchling";

Add Package

bunx jsr add @kwhinnery/fetchling

Import symbol

import * as fetchling from "@kwhinnery/fetchling";