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

unrested

v1.0.3

Published

Minimal, type-safe REST client using JS proxies

Downloads

45

Readme

unrested

Minimal, type-safe REST client using JS proxies.

ℹ️ If you like this package, upvote this feature to be part of unjs/ofetch Much appreciated.

Features

  • 🌁 Lightweight, only 36 loc
  • 🦾 Strongly typed
  • 📚 Supports chain and bracket syntax
    • api.nested.users(1).get()
    • or api.nested.users["1"].get()
  • 🪵 Use other HTTP methods, like .post()

unrested uses ofetch for data fetching under the hood. Thus, every option available for ofetch is usable with unrested as well!

Installation

Run the following command to add unrested to your project.

# pnpm
pnpm add -D unrested

# npm
npm install -D unrested

# yarn
yarn add -D unrested

Usage

import { createClient } from 'unrested'

// The base URL default is `/`
const api = createClient()

unrested inherits ofetch's options. Refer to the documentation for a complete list of options.

import { createClient } from 'unrested'

// Set a custom base URL as needed
const api = createClient({
  baseURL: 'https://jsonplaceholder.typicode.com',
})

Path Segment Chaining

Chain single path segments or path ids by a dot. You can even type the response of your request!

// GET request to <baseURL>/users
const users = await api.users.get<UserResponse>()

// For GET request you can add search params
// <baseURL>/users?search=john
const users = await api.users.get<UserResponse>({ search: 'john' })

To include dynamic API path segments, you have two options:

// Typed GET request to <baseURL>/users/1
const userId = 1
// … using the chain syntax:
const user = await api.users(userId).get<UserResponse>()
// … or the bracket syntax:
const user = await api.users[`${userId}`].get<UserResponse>()

HTTP Request Methods

Add the appropriate method to the end of your API call. The following methods are supported:

  • get()
  • post()
  • put()
  • delete()
  • patch()

Payload Requests

For HTTP request methods supporting a payload, add it to the method call:

// POST request to <baseURL>/users
const response = await api.users.post({ name: 'foo' })

Default Options For ofetch

import { createClient } from 'unrested'

const api = createClient({
  baseURL: 'https://jsonplaceholder.typicode.com',
  async onRequestError({ request, options, error }) {
    console.log('[fetch request error]', request, error)
  },
  async onResponseError({ request, options, error }) {
    console.log('[fetch response error]', request, error)
  },
})

Override Default Options

You can add/overwrite ofetch options on a method-level:

const response = await api.users.get({
  headers: {
    'Cache-Control': 'no-cache',
  },
})

Credits

License

MIT License © 2022-2023 Johann Schopplich