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 🙏

© 2026 – Pkg Stats / Ryan Hefner

typed-rest-routes

v0.2.8

Published

An Astro integration that allows for creating type-safe server endpoints.

Readme

typed-rest-routes

An Astro integration that allows for creating type-safe server endpoints.

Table of Contents

Installation

You can install typed-rest-routes (TRR from here on) from npm. It is recommended to use the Astro CLI for this:

# npm
npx astro add typed-rest-routes

# pnpm
pnpm astro add typed-rest-routes

# yarn
yarn astro add typed-rest-routes

Alternatively, you can manually install the package and add it to your astro.config.mjs file:

# npm
npm install typed-rest-routes

# pnpm
pnpm add typed-rest-routes

# yarn
yarn add typed-rest-routes
// @ts-check
import { defineConfig } from "astro/config";
import typedRestRoutes from 'typed-rest-routes';

// https://astro.build/config
export default defineConfig({
	// ...
	integrations: [
		typedRestRoutes()
	],
});

Usage

TRR aims to keep compatibility with existing Astro server endpoints to provide an experience similar to actions while having full type-safety for normal routes. It is similar to projects like tRPC.

You can use it in any server endpoint to generate a function that you can export like usual. For example, if you want to handle all GET requests to /api/hello, you can define your route handler like this:

// src/pages/api/hello.ts
import { defineRoute } from "typed-rest-routes/server";

export const GET = defineRoute({
	handler: async (context) => {
		return {
			message: "Hello!"
		};
	},
});

When used in conjunction with callRoute, this setup provides you with autocompleted URLs and methods in client-side scripts:

import { callRoute } from "typed-rest-routes/client";

async function main() {
	// You will get full type completions here!
	const result = await callRoute("/api/hello", "GET");
}

main();

Another advantage of using TRR is that you can define schemas for your routes using Zod. This is helpful when creating handlers that take in data (and works with both GET and POST requests):

import { defineRoute } from "typed-rest-routes/server";
import { z } from "astro/zod";

export const GET = defineRoute({
	schema: z.object({
		name: z.string()
	}),
	handler: async (context, data) => {
		// `data` is typed and already verified!
		return `Hello, ${data.name}!`;
	},
});

export const POST = defineRoute({
	schema: z.object({
		name: z.string()
	}),
	handler: async (context, data) => {
		// `data` is typed and already verified!
		return `Hello, ${data.name}!`;
	},
});

When using callRoute, you will then also get access to a third parameter for the data with full type safety:

import { callRoute } from "typed-rest-routes/client";

async function main() {
	// You will get full type completions here!
	const result = await callRoute("/api/hello", "GET", {
		name: "Houston"
	});
	
	// You will get full type completions here!
	const result = await callRoute("/api/hello", "POST", {
		name: "Houston"
	});
}

main();

Advanced Usage

Since TRR is built on normal server endpoints, the handler function you pass to defineRoute always has access to the Astro context as the first parameter. If you define a schema, the second parameter will be the parsed data from the request.

Custom Errors

TRR does not automatically generate errors for your schemas as to not cause confusion when "guessing wrong". Instead, you can use Zod to provide your own error messages. Please check their documentation on how to do so. Please note that the different error messages you can customize depend on the data type. However, in general, you can use the message option to provide a generic error message:


export const POST = defineRoute({
	schema: z.object({
		name: z.string({ message: "The name key must be set to a string!" })
	}, {
		message: "Missing JSON body!"
	}),
	handler: async (context, data) => {
		return `Hello, ${data.name}!`
	},
});

Custom Responses

You can return any data type from the handler, even a Response. TRR will pass all data along to the client and try it's best to give you a parsed output on the client:

import { defineRoute } from "typed-rest-routes/server";
import { z } from "astro/zod";

export const POST = defineRoute({
	schema: z.object({
		name: z.string()
	}),
	handler: async (context, body) => {
		return new Response(`Hello, ${body.name}!`, {
			status: 200,
			headers: {
				"x-my-custom-header": "some-value"
			}
		});
	},
});

But Why?

The advantage TRR gives you over Astro actions is that you can define them at any route you'd like. This is quite advantageous when you are creating a public API that you want to use yourself, since actions get generated at the /_actions path, which may not be what you want. Using TRR, you can define the routes you want the API to be located at yourself.