Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fetchmap

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetchmap

Non-throwing fetch wrapper

  • 0.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

fetchmap

npm build publish codecov Type Coverage Libraries.io dependency status for latest release Bundlephobia npm

Non-throwing fetch wrapper

Getting started

npm i fetchmap

Description

This is a simple wrapper for standard fetch function that catches all possible exceptions and returns a 'success or failure' wrapped value. It takes an object to map response.status to (response: Response) => unknown transform, standard fetch arguments and a fetch function itself. The last argument can be curried.

Example

// server code
import express from 'express'

express()
  .get('/json', (_, res) => {
    res.json({ some: 'data' })
  })
  .listen(5005)

// client code
import { fetchmap } from 'fetchmap'

const json_success = await fetchmap({ ok: 'json' }, 'https://localhost:5005/json')(fetch)
expect(json_success).toEqual({ tag: 'success', success: { some: 'data' } })

const json_success_only_200 = await fetchmap({ 200: 'json' }, 'https://localhost:5005/json')(fetch)
expect(json_success_only_200).toEqual({ tag: 'success', success: { some: 'data' } })

const invalid_url_failure_non_curried_form = await fetchmap({}, '1234', undefined, fetch)
expect(invalid_url_failure_non_curried_form).toEqual({
  tag: 'failure',
  failure: { clientError: new TypeError('Only absolute URLs are supported') }
})

const not_found_error_with_request_init = await fetchmap(
  { notOk: ({ status }: Response) => status },
  'https://localhost:5005/invalid',
  { method: 'POST', credentials: 'include' }
)(fetch)
expect(not_found_error_with_request_init).toEqual({ tag: 'failure', failure: { serverError: 404 } })

Usage

See test or playground

Misc

ts-railway - compatible result library

Keywords

FAQs

Package last updated on 09 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc