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

@kristall/http

v1.3.0

Published

HTTP methods for supporting NextJS Server Actions of Kristall

Downloads

36

Readme

@kristall/http

HTTP methods for supporting NextJS Server Actions of Kristall

Features

  • Server Action by default ("use server")
  • Typescript support
  • NextJS tags support for revalidation

Installation

npm install @kristall/http

pnpm add @kristall/http

yarn add @kristall/http

How it works

Where is the request made?

You will define the server that will handle the request in your .env file using the SERVER_API variable.

Example:

SERVER_API="http://localhost:3000"

Cookies

The package uses the next/headers package to get the access token from the cookies and add it to the headers of the request to the server. The cookie must be named session.

The cookie is add to the headers of the request with the following structure:

{
	Authorization: `Bearer ${access_token}`,
	Cookie: `session=${access_token}`,
	"Content-Type": "application/json",
}

Tags

You can add tags to your server actions to revalidate them. The tags are added to the next object of the response. The tags is an array of strings.

Usage

Import the method you need from the package:

import { GET, POST, PATCH, PUT, DELETE } from "@kristall/http";

GET

You can use the GET method to fetch data from the server.

import { GET } from "@kristall/http";

// Your own types
import type { Product } from "@/types/product";

export const getProducts = async (): Promise<Product[]> => {
	const { data } = await GET<Product[]>("/products", {
        // Setting the tags
		tags: ["products"],
	});
	return data;
};

We also check if the tags is not provided sending a warning to the console on development mode and will tell you the path of the untagged server action.

⚠️ No tags provided for GET request: /products

If a response status is not ok like 400 or 500 the response will throw an error with the message of the response and will show on the console on development mode the path of the server action that failed.

❌ Error fetching data at:  /products

POST

You can use the POST method to send data to the server.

import type { Product } from "@/types/product";

const product: Product = {
    id: 1,
    name: "Product 1",
}

const response = await POST<Product, Product[]>("/products", product);

POST can receive two types of parameters:

  • The first one is the body of the request.
  • The second one is the response type.

If you don't need to receive the response type, you can use the POST method without the second parameter and by default it will be unknown.

PATCH / PUT

PATCH and PUT have the same api as POST, but they are used to update data on the server, use as you prefer.

DELETE

You can use the DELETE method to delete data from the server.

const response = await DELETE("/products/1");

DELETE doesn't need a extra parameters. Only need the url of the resource.

Considerations

Response

By default ALL methods are typed to return a ServerResponse object with the following structure BUT WE DON'T CHECK IT so it may cause errors with the response of your server:

interface ServerResponse<T> {
	message: string;
	data: T;
	status: number;
}

Errors

If a response status is not ok like 400 or 500 the response will throw an error with the message of the response.