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

vzemi

v0.5.1

Published

Fetching REST APIs

Downloads

3

Readme

vzemi

vzemi provides a wrapper for the global fetch method.

It exposes a Proxy object with two instances of Map: endpoints and origins.

Quick usage

import vzemi from "vzemi"

vzemi.endpoints.set("books", { uri: "/jumbo/books" })

await vzemi.get("books", { index: 0, size: 100 })
await vzemi.put("books", { index: 0, size: 100 })
await vzemi.post("books", { index: 0, size: 100 })
await vzemi.patch("books", { index: 0, size: 100 })
await vzemi.delete("books", { index: 0, size: 100 })
await vzemi.fetch("books", { index: 0, size: 100 })

// FetchResponse: {
//   MOCK?: boolean
//   data: unknown | null,
//   error?: 1,
//   problems?: string[],
// }

A word on the vzemi's structure

Both origins and endpoints extend the regular old Map and can be managed via the set, setMany, delete, and clear methods.

Endpoints

An EndPoint is just an object helper for HTTP requests. Its contents are used and/or merged when fetching data.

interface EndPoint {
  // This can be any full or relative path
  uri: string

  // Optional. vzemi.fetch defaults to this value. vzemi.get/post/etc. override this value
  method?: string

  // Optional. Predefined props that you might have for this endpoint
  // Merged with request-specific params
  props?: {}

  // Optional. The actual fetch RequestInit object that includes headers, mode, etc
  // Merged with origin-specific options
  options?: {}

  // Optional. Merged with origin-specific headers.
  // Can be overwritten while making this request via '$headers'
  headers?: {}

  // Optional. Whatever you put here will be your `data` property
  mock?: {some: [1, 22, 333], more: 'stuff', aaa: 111, bbb: 222, ccc: {ama: 'zing'}},
  // OR... it can also be a function that recevies what `fetch` receives
  mock?: (uri: string, options: {}, endpointName: string) => ()
}

Set one

vzemi.endpoints.set("books", { uri: "/jumbo/books" })

Set multiple

zemi.endpoints.setMany({
  endpoint1: { uri: "https://example.com/endpoint1" },
  endpoint2: { uri: "https://example.com/endpoint2", method: "POST" },
})

Origins

Before a request is being made, vzemi matches an EndPoint's uri to an existing key in origins.

This way, we can easily scope different header, credentials, or any other options, to an origin.

Set one

vzemi.origins.set("/", { cache: "force-cache" })

Set multiple

zemi.origins.setMany({
  "/": {
    mode: "cors",
    headers: { LEVEL_0_H: "0_hehehe" },
  },
  "http://localhost:1001": {
    mode: "no-cors",
    cache: "force-cache",
    signal: null,
    boo2: "222_shakalaka",
    headers: { auth: "DIESEL_123" },
  },
})

Usage and special props - everything is optional

const mai_data = await fff.fetch('employee', {
  // Your props are transformed to either CGI in the URL or body payload
  a: 1,
  b: 2,
  c: 3,
  
  // Special props ARE OPTIONAL

  // When used, its value will replace your regular non-special props
  // which are usually used as your body payload
  $body: {},

  // Merged into a collection's url
  $path: '/aaa/bbb/ccc',

  // Merged with the fetch options object.
  // Will overwrite any matching props provided by
  // the global options and the collection itself
  $options: {...},

  // Merged with headers from the global setup object
  // AND the headers in the collection itself
  $headers: {x: 1, y: 2, z: 3},

  // Causes to transform a non-GET request's body/payload to FormData
  $formData: true,
})

Aborting requests

The simplest way to abort a request is like this

const ac = new AbortController()

vzemi.get("someCollection", {
  $options: { signal: ac.signal },
})

ac.abort()

Dispatching global event on any fetch, successful and failed

As vzemi is a Proxy, the setter function only accepts one possible key: emitAlways The only acceptable value is string

vzemi.emitAlways = 'custom-event-name'