Skip to main content
Home

Built and signed on GitHub Actions

Multiple operations on a single endpoint with hono and zod 🚀

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a year ago (1.2.3)

singlend

Multiple operations on a single endpoint with hono and zod 🚀

When using singlend, headers and cookies are anti-patterns ✖ Everything is managed by the body, so session should be stored in localStorage, etc.
It is more secure 🔓

  • 🔥 Support Hono RPC Client
  • 🌪️ Fastest
  • 🌍️ Support All Runtime
  • 🧩 Highly Typed

How to use

npx jsr add @evex/singlend
bunx jsr add @evex/singlend
deno add @evex/singlend
import { Hono } from "@hono/hono";
import { z } from "zod";
import { Singlend } from "@evex/singlend";
import { hc } from "@hono/hono/client";

const app = new Hono();
const singlend = new Singlend();

const sub = singlend.on(
	"getIcon",
	z.object({}),
	(_query, ok) => {
		return ok({
			iconUrl: "default.png",
		});
	},
);

const singleRoute = singlend
	.group(
		z.object({
			id: z.string(),
		}),
		(query, next, error) => {
			if (!query.id.startsWith("@")) {
				return error({
					message: "Invalid id",
				});
			} else {
				return next({
					id: query.id.slice(1),
				});
			}
		},
		(singlend) =>
			singlend.on(
				"setIcon",
				z.object({
					iconUrl: z.string(),
				}),
				(query, value, ok) =>
					ok({
						message: "Set icon of " + value.id + " to " +
							query.iconUrl,
					}),
			),
	)
	.on(
		"setBackgroundColor",
		z.object({
			backgroundColor: z.string().length(7),
		}),
		(query, ok, error) => {
			if (!query.backgroundColor.startsWith("#")) {
				return error({
					message: "Invalid background color",
				});
			}

			return ok({
				message: "Set background color to " + query.backgroundColor,
			});
		},
	)
	.mount(sub);

const routes = app.post("/api/singlend", singleRoute.handler());

// launch server

// in client
const client = hc<typeof routes>("/");

const response = await client.api.singlend.$post({
	json: {
		type: "setIcon",
		query: {
			id: "@12345",
			iconUrl: "default.png",
		},
	},
});

const data = await response.json();
fetch("/api/singlend", {
	method: "POST",
	body: JSON.stringify({
		type: "setIcon",
		query: {
			id: "@114514",
			iconUrl: "default.png",
		},
	}),
});

More: https://jsr.io/@evex/singlend/doc/~/Singlend

Authors

  • @EdamAme-x
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@evex/singlend

Import symbol

import * as singlend from "@evex/singlend";
or

Import directly with a jsr specifier

import * as singlend from "jsr:@evex/singlend";

Add Package

pnpm i jsr:@evex/singlend
or (using pnpm 10.8 or older)
pnpm dlx jsr add @evex/singlend

Import symbol

import * as singlend from "@evex/singlend";

Add Package

yarn add jsr:@evex/singlend
or (using Yarn 4.8 or older)
yarn dlx jsr add @evex/singlend

Import symbol

import * as singlend from "@evex/singlend";

Add Package

vlt install jsr:@evex/singlend

Import symbol

import * as singlend from "@evex/singlend";

Add Package

npx jsr add @evex/singlend

Import symbol

import * as singlend from "@evex/singlend";

Add Package

bunx jsr add @evex/singlend

Import symbol

import * as singlend from "@evex/singlend";