curry-request
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

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.

Package Sidebar

Install

npm i curry-request

Weekly Downloads

0

Version

2.0.1

License

ISC

Unpacked Size

8.76 kB

Total Files

6

Last publish

Collaborators

  • friki