svelte-http-store
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

svelte-http-store

A Svelte store backed by the fetch API.

Installation

pnpm install -D svelte-http-store

Example

// in src/lib/stores/cart.js
import { httpStore } from 'svelte-http-store'

// performs a `GET /cart` request
export const cart = httpStore('/cart')

// store.refresh() will request `GET /cart` again
cart.refresh()

// call store.fetch() to mutate:
cart.fetch('/cart', { method: 'PATCH' })

Feel free to wrap the store.fetch() with functions that fit your domain:

// example: function to add items to the cart
cart.add = ({ product, quantity }) => {
  const url = `/cart/${product.id}`
  const body = JSON.stringify({ quantity })

  cart.fetch(url, { method: 'POST', body })
}

// example: function to clear the cart
cart.clear = () => {
  cart.fetch('/cart', { method: 'DELETE' })
}

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i svelte-http-store

Weekly Downloads

1

Version

0.0.2

License

none

Unpacked Size

5.26 kB

Total Files

6

Last publish

Collaborators

  • joshnuss