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

alpinejs-fetch

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alpinejs-fetch

Alpine JS magic methods wrapper for fetch API methods 📨

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
14
increased by7.69%
Maintainers
1
Weekly downloads
 
Created
Source

Alpine JS Fetch

Alpine JS magic methods wrapper for fetch API methods 📨

Install

With a CDN

<script
  defer
  src="https://unpkg.com/alpinejs-fetch@latest/dist/api.min.js"
></script>

<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>

With a Package Manager

yarn add -D alpinejs-fetch

npm install -D alpinejs-fetch
import Alpine from 'alpinejs'
import api from 'alpinejs-fetch'

Alpine.plugin(api)

Alpine.start()

Example

GET

<div x-data="{ productData: {} }">
  <button @click="productData = await $get('https://dummyjson.com/products/1')">
    Get
  </button>

  <pre x-text="JSON.stringify(productData, null, 2)"></pre>
</div>

POST

<div x-data="{ productData: {} }">
  <button
    @click="productData = await $post('https://dummyjson.com/products/add', { title: 'BMW Pencil' })"
  >
    Create
  </button>

  <pre x-text="JSON.stringify(productData, null, 2)"></pre>
</div>

PUT/PATCH

<div x-data="{ productData: {} }">
  <!-- You can also use $patch -->
  <button
    @click="productData = await $put('https://dummyjson.com/products/1', { title: 'iPhone Galaxy +1' })"
  >
    Update
  </button>

  <pre x-text="JSON.stringify(productData, null, 2)"></pre>
</div>

DELETE

<div x-data="{ productData: {} }">
  <button
    @click="productData = await $delete('https://dummyjson.com/products/1')"
  >
    Delete
  </button>

  <pre x-text="JSON.stringify(productData, null, 2)"></pre>
</div>

Filtering the Response

A lot of the times you only need a property from the response object, this is usually data. You'll see/write stuff like this:

Please note, this depends on the API response and not all APIs will share the same response format.

const { data } = fetch('https://dummyjson.com/products/1')

With this package you can do that by adding [data] to the end of the URL.

<div x-data="{ productData: {} }">
  <!-- Single -->
  <button
    @click="productData = await $get('https://dummyjson.com/products/1[data]')"
  >
    Get
  </button>

  <!-- Multiple -->
  <!-- Comma separation is optional -->
  <button
    @click="productData = await $get('https://dummyjson.com/products/1[data, status]')"
  >
    Get
  </button>

  <pre x-text="JSON.stringify(productData, null, 2)"></pre>
</div>

This works for all the requests and isn't limited to the data property. Anything that is part of the response object can be used to filter the response. For example, [status].

Heads up! Syntax is important, the filter property key must be place in square brackets [x].

Nested Filters

Sometimes you don't want the whole object back, in this example data returns a lot of information but we only want title and price back. Here's how you can do that.


  <button
    @click="productData = await $get('https://dummyjson.com/products/1[data.title, data.price]')"
  >
    Get
  </button>

  <pre x-text="JSON.stringify(productData, null, 2)"></pre>
</div>

This will return with the first key, in this case data omitted.

{
  "title": "iPhone 9",
  "price": 549
}

Note! You can still pass other filters in when using nested filters, something like [data.title, status] will work fine!

Stats

Keywords

FAQs

Package last updated on 31 Oct 2024

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