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

alpinejs-requests

v1.0.4

Published

Inline HTTP requests made simple for Alpine.js

Downloads

91

Readme

🌲✉️ Alpine.js Requests

✉️ Inline HTTP requests made simple for Alpine.js 🌲

https://www.npmjs.com/package/alpinejs-requests

What is Alpine.js Requests?

Alpine.js Requests aims to make HTTP requests easy within Alpine. The main use case for this package is for simple requests actions that do not require advance responses. For example, you could use this package to easily send a POST request when a user presses the like button.

Install

NPM

Install the package with the following command:

npm i alpinejs-requests

Import and register the package:

import Alpine from 'alpinejs'
import requests from 'alpinejs-requests'

Alpine.plugin(requests)

Alpine.start()

Script Tag

<html>
    <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/plugin.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
</html>

Documentation

x-post

x-post allows you to easily create a POST request on the click event of a DOM element.

In its simplest form, you can pass a string that will be used as the URL to send the POST request to.

<button type="button" x-post="/api/videos/4/like">Like</button>

If required, additional data can be passed through the x-post directive.

<button
    type="button"
    x-post="{route: '/api/videos/4/like', body: {stars: 3}, headers: {}}"
>Like</button>

If you want to use the response from the POST request, you can access this through the @post event. This event is called once the request has resolved. You can access the response object with $event.detail.response. You can also access a "state" variable with $event.detail.state. This will be true/false depending on the success of the request.

<button
    type="button"
    x-post="{route: '/api/videos/4/like', body: {stars: 3}}"
    @post="liked = true;"
    x-text="liked ? 'Liked' : 'Like'"
></button>

A magic method of $post is also provided. This is useful for making quick requests inside other events.

<div x-init="$post('/api/videos/4/view');"></div>
<button
    type="button"
    @click="$post({route: '/api/videos/4/like', body: {stars: 3}})"
>Like</button>

x-get

x-get works essentially identically to x-post. Rather obviously, the only difference is that it makes a GET request instead of a POST request. The functionality is identical in what can be passed to an x-get, and how the system resolves it. Something to note is that the event for a GET request lands in the @get event.

<button
    type="button"
    x-get="/api/videos/4/refresh"
    @get="views = $event.detail.response.body"
>
    <span x-text="views"></span> Views
</button>

There is also a $get magic method.

<div
    x-init="$get('/api/videos/4/info');"
    @get="views = $event.detail.response.json().views"
></div>
<p>
    <span x-text="views"></span> Views
</p>

x-request

x-request is the Alpine.js Request library in its freest form. It functions the same as x-post and x-get, but allows for the additional definition of a method. This means you can define which method to use, including things like DELETE, PATH ect.

<button
    type="button"
    x-request="{route: '/api/videos/4', method: 'DELETE'}"
>Delete</button>

You can also use the magic method $request.