Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 27, 2025. It is now read-only.

🚥 Minimal, type-safe REST client using JS proxies (36 loc) – please migrate to apiful

License

NotificationsYou must be signed in to change notification settings

johannschopplich/unrested

Repository files navigation

Tip

This is now part ofapiful.

You can still use this package, but it will no longer be actively maintained. Please consider migrating toapiful. It is the spiritual successor ofunrested and comes with many more features.

unrested

Minimal, type-safe REST client using JS proxies.

Features

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

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

Installation

Run the following command to addunrested to your project.

# npmnpm install unrested# pnpmpnpm add unrested# yarnyarn add unrested

Usage

import{createClient}from'unrested'// The base URL default is `/`constapi=createClient()

unrested inheritsofetch's options. Refer to thedocumentation for a complete list of options.

import{createClient}from'unrested'// Set a custom base URL as neededconstapi=createClient({baseURL:'https://jsonplaceholder.typicode.com',})

Path Segment Chaining

Chain single path segments or path IDs by a dot. You can type the response by passing a generic type to the method:

// GET request to <baseURL>/usersconstresponse=awaitapi.users.get<UserResponse>()

ForGET request, the first parameter is used as query parameters:

// <baseURL>/users?search=johnconstresponse=awaitapi.users.get<UserResponse>({search:'john'})

For HTTP request methods supporting a payload, the first parameter is used as payload:

// POST request to <baseURL>/usersconstresponse=awaitapi.users.post({name:'foo'})

To include dynamic API path segments, you can choose between the chain syntax or the bracket syntax:

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

HTTP Request Methods

The following methods are supported as the last method in the chain:

  • get(<query>, <fetchOptions>)
  • post(<payload>, <fetchOptions>)
  • put(<payload>, <fetchOptions>)
  • delete(<payload>, <fetchOptions>)
  • patch(<payload>, <fetchOptions>)

Default Options forofetch

import{createClient}from'unrested'constapi=createClient({baseURL:'https://jsonplaceholder.typicode.com',asynconRequestError({ request, options, error}){console.log('[fetch request error]',request,error)},asynconResponseError({ request, options, error}){console.log('[fetch response error]',request,error)}})

Override Default Options

Any fetch options on a method-level will override the default options:

constresponse=awaitapi.users.get({headers:{'Cache-Control':'no-cache'}})

License

MIT License © 2022-PRESENTJohann Schopplich

About

🚥 Minimal, type-safe REST client using JS proxies (36 loc) – please migrate to apiful

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp