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

@are/f-fetch

v1.15.5

Published

Functional fetch wrapper

Downloads

11

Readme

f-fetch

f-fetch is a JavaScript wrapper for functional fetch.

Installation

npm i @are1000/f-fetch

Example

import { request } from '@are1000/f-fetch'

import target from '@are1000/f-fetch/operators/target'
import when from '@are1000/f-fetch/operators/when'
import parse from '@are1000/f-fetch/operators/parse'
import map from '@are1000/f-fetch/operators/map'
import error from '@are1000/f-fetch/operators/error'

/*
  You can alternatively import like this:
  
  import { target, when, parse, map, error } from '@are1000/f-fetch/operators'
*/

const req = request(
    target('GET', 'https://example.com', 'api', 'users', 5),
    when(r => r.ok, parse('json'), map(res => res.entities)),
    when(r => r.status === 404, error(NotFoundError)),
    when(r => r.status === 500, error(InternalServerError)),
)

const entities = await req.run()

Usage

To use f-fetch, import the module using npm or yarn.

npm install @are1000/f-fetch

Then, import the main request builder and pass any amount of operators inside.

The operators are not executed until the method run is called, so any custom operators that contain time sentitive logic are safe.

const req = request(...)

const response = await req.run()

You can additionally pass values to the run function. They will become available in certain operators (i.e. json).

const req = request(
    ...,
    json((query) => ({ query }))
)

const response = await req.run('myQuery')

You can also extend existing requests by adding more operators:

const req1 = request(...)

const req2 = req1.extend(...)

Operators

Request operators

target(method: string, ...url: Array<string | number>)

Sets the method and the URL of the request.

After the method, you can pass an arbitrary amount of URL fragments - they will be joined using the / character.

appendUrl(...url: Array<string | number>)

Join current URL with URL fragments using / character. Useful for extending requests.

json(obj: JSON) | json((...Args) => JSON)

Sets the request body to an JSON object (internally uses JSON.stringify). It also correctly sets Content-Type header.

If you pass a function instead, you can access the arguments from request().run.

body(data: string, type: string)

Sets the request body to a string. You must pass the Content-Type as a second argument.

timeout(delay: number)

Timeouts the request after a number of milliseconds passed.

headers(obj: Record<string, string>)

Appends the request headers.

Response operators

when(predicate: (response: Response) => boolean, ...operators: Array<Operator>)

Conditional operator that executes other operators only on success and only if the predicate returns true.

whenError(predicate: (error: Error) => boolean, ...operators: Array<Operator>)

Conditional operator that executes other operators only on failure and only if the predicate returns true.

parse(format: 'json' | 'text' | 'blob' | 'arrayBuffer')

Operator that parses response body into one of available formats.

error<T extends Error>(errorClass: T)

If this operator is invoked, it will throw.

It passes the response object as the first argument to the error constructor.

map(mapper: <T>(res: T) => T)

Maps the response object. Useful for transformations.

project(keys: Array<string>, mapper?: <T>(obj: T) => T)

When the response is an array of objects, it transforms the objects in such a way, that only the keys that are included in the argument go through.

You can optionally pass a second argument that will be used to map through every projected object.

prop(key: string)

When the response is an object, return the value of key. Otherwise returns null.

Meta operators

custom(creator: (...Args) => Operator)

Creates a custom operator. It is used to group common and repeating operators into one.

The creator function has to return an operator instance or an array of operator instances.

hook(hookName: string, handler: (args, { params, data, ctx }))

Hooks into internals of f-fetch. Only use if you know what you are doing!

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT