0.3.1 • Published 3 years ago

@dashkite/mercury v0.3.1

Weekly downloads
3
License
SEE LICENSE IN LI...
Repository
github
Last release
3 years ago

Mercury

Combinators for making HTTP requests.

Mercury works a lot like SuperAgent, except via function composition instead of chaining.

import * as _ from "@dashkite/joy"
import * as m from "@dashkite/mercury"

PublicAPI =
  search:
    _.flow [
      m.request [
        m.url "https://api.publicapis.org/entries"
        m.query
        m.method "get"
        m.headers accept: "application/json"
        m.expect.status [ 200 ]
      ]
      m.response [ $.json ]
      _.get "json"
    ]

The result is an async function that we can call to make the request:

{entries} = await PublicAPI.search
  title: "cat"
  category: "animals"

Using composition means Mercury is trivially extensible. For example, Mercury Sky comes with functions to support Panda Sky-based APIs to construct the request and check the response.

Since these are just functions, we can easily add new features. For example, we could write a simple function that adapts the http combinators to check the URL against an application cache.

Mercury combinators compose to async functions, which means they can be reused within other compositions. For example, we might create an initialization combinator that we can reuse, to ensure a set of resources is available for subsequent requests. This is harder to do with chaining.

Installation

npm i @dashkite/mercury

Use with your favorite bundler or import directly in the browser.

API

Mercury provides two top-level combinators:

  • request, which takes an array of combinators to specify the request
  • response, which takes an array of combinators for processing the response

Both combinators take a daisho datastructure that may be manipulated using Katana operators.

The response combinator returns a promise for an object describing the request and response.

Request Combinators

Request combinators may take an argument or implicitly read from the daisho stack.

CombinatorArgumentsDescription
baseURLThe base URL, used in conjunction with path to construct the full URL.
pathURL pathThe path of the URL, relative to the base URL.
templateURL templateThe URL template to be expanded to generate the URL.
parametersobjectThe parameters to use to expand the URL template. Not be confused with query.
urlURLThe ultimate URL of the request.
queryobjectThe search parameters to be appended to the URL.
methodtextThe method name (ex: GET, PUT, …) for the request. Will be converted to uppercase for you.
contentanyThe content body of the request. Takes a string or a value, which will be converted into a string using its toString method, except for arrays and objects, which are converted into JSON.
urlencodedobjectThe content body, formatted as a URL encoded form.
headersobjectThe headers of the request.
accepttextThe accept header.
mediatextThe content-type header.
authorizetextThe authorization header.
cachetextThe named CacheStorage object to use in processing the request.
expiresnumber (milliseconds)The expiration for cached responses.
expect.ok-Expect an OK response (200 range).
expect.statusarrayThe status codes to expect (that will not throw).
expect.mediatextThe content-type of the response. Use with accept.

Response Combinators

CombinatorDescription
jsonThe result of parsing the response body as JSON.
textThe response body as plain text.
blobThe response body as raw data.

Combinators from outside of Mercury may use additional properties.

Use Outside The Browser

When using Mercury in Node, you will need to install Fetch and Request globally.

import fetch from "node-fetch"

globalThis.fetch ?= fetch
global.Request ?= fetch.Request

If you want to use the cache combinator, you will also need to install caches globally:

import { caches } from 'cache-polyfill'
globalThis.caches ?= caches

Errors

When a Mercury combinator throws an exception, the error will contain additional information, if applicable. If there’s a response, it will contain response and status properties. If the request does not yet have a corresponding response, these will be undefined.

error.response

Convenience for:

error.context && error.context.response

Undefined if request does not have a corresponding response.

error.status

Convenience for:

error.response && error.response.status

Undefined if request does not have a corresponding response.

0.3.1

3 years ago

0.3.0

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.1.3

3 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago