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

curry-request

v2.0.1

Published

Http request client built with one curried function

Downloads

18

Readme

Curry Request NPM Version Downloads Per Week example workflow License: ISC codecov

Composable/extendable http client built with one curried function.

This module was born while dealing with REST resources in front-end applications, de-duplicating the configuration of fetch has proven, in our experience, to be beneficial in the maintainability of the App's Api calls.

This is implemented in a functional fashion extending the configuration definition for the fetch function, the original definition is:

  curryRequest :
    baseUrl ->
    baseHeaders ->
    method ->
    route ->
    payload ->
    token -> Fetch.Response

Although the module is already evolving to include additional/alternative parameters, it is guaranteed to stay compatible with its original form.

Installation

npm install -S curry-request

This package is written in typescript, types are included.

Usage

Currying the main function you should be able to easily map REST APIs:

import cr from "curry-request"
// we place the web api base url & the base headers
const apiRequest = cr("https://jsonplaceholder.typicode.com")({
  "Content-Type": "application/json",
  Accept: "application/json"
})

const apiGet = apiRequest("GET")
const apiPost = apiRequest("POST")

// get example
const getTodosById = (id) => apiGet(`/todos/${id}`)()()
getTodosById("1")
  .then((res) => res.json())
  .then((res) => console.log(res))

// post example
const postTodo = (payload) => apiPost("/todos")(payload)()
postTodo({ title: "BuyMilk", completed: false })
  .then((res) => res.json())
  .then((res) => console.log(res))

Parameters explained

  • baseUrl: string the webApi base url, root path for all routes belonging to a specific backend.
  • baseHeaders?: object the base headers included in every call (the token can be added later)
  • method: string any http verb ('GET', 'POST', 'PUT', 'PATCH', 'DELETE', etc.)
  • route?: string the defining part of the route (and eventually the query strings)
  • payload?: object | string the body section of the request (if an object is passed it will also be serialized)
  • token?: string this will be used as defined in JWT specs, Authorization: Bearer ${token}

Compatibility

Although it was born in browser context the default fetch implementation (cross-fetch) is compatible also with nodejs. If you're not happy with that the fetch implementation can actually be swapped.

Extendability

One of the motivations for centralizing all Api requests is having the ability to manipulate them in one place. Therefore we provide additional parameters that can be used in order to expand the function, following are a list of options that can be accessed through optional parameters, if you use Typescript in your project you should be able to identify these with an autocompleting editor, following is a complete description of all options:

curryRequest
  (baseUrl: string, fetchImplementation?: (req: Request => Promise<Response>)) =>
  (baseHeaders?: {[headerName: string]: string}) =>
  (method: string) =>
  (route?: string) =>
  (payload?: string | object) =>
  (token?: string)

Playground

for more info and examples have a look at the playground.