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

@itrocks/new

v0.0.12

Published

Generic action-based new object form in HTML and JSON

Downloads

27

Readme

npm version npm downloads GitHub issues discord

new

Generic action-based new object form in HTML and JSON.

This documentation was written by an artificial intelligence and may contain errors or approximations. It has not yet been fully reviewed by a human. If anything seems unclear or incomplete, please feel free to contact the author of this package.

Installation

npm i @itrocks/new

Usage

@itrocks/new provides a ready‑made New action that builds on top of @itrocks/edit to render a form for creating a fresh instance of a domain object.

It is typically used together with the it.rocks framework and routing stack. You usually:

  1. Declare a domain class (for example User).
  2. Attach a New<User> action to this class in your action pack.
  3. Expose a /new route that serves the HTML form and a JSON API variant for asynchronous front‑ends.

Minimal example

import { New } from '@itrocks/new'
import type { Request } from '@itrocks/action-request'

class User {
	name  = ''
	email = ''
}

// Create an action dedicated to creating new User instances
const newUser = new New<User>()

// HTML form endpoint
async function newUserHtml (request: Request<User>) {
	return newUser.html(request)
}

// JSON endpoint (for SPA / XHR based UI)
async function newUserJson (request: Request<User>) {
	return newUser.json(request)
}

The Request<User> is usually created by @itrocks/action-request from an incoming HTTP request.

API

class New<T extends object = object> extends Edit<T>

New specialises the generic Edit action for the "create new object" use case.

From the implementation you can see that it is decorated with:

  • @Need(NOTHING) – the action does not require an existing object instance; it is responsible for providing an empty object to edit.
  • @Route('/new') – declares a conventional /new route when used with @itrocks/route.

It inherits all the behaviour of Edit<T> (form rendering, value binding, validation glue, etc.) but always targets a new instance instead of loading an existing one.

Type parameter

  • T – The domain object type for which you want a "new" form (for example User).

Methods

Because New<T> extends Edit<T>, it exposes the same public methods as Edit:

html(request: Request<T>): Promise<HtmlResponse>

Builds an HTML form used to create a new instance of T.

Typical usage in a route handler:

fastify.get('/users/new', async (req, reply) => {
	const request = toActionRequest<User>(req)
	const response = await newUser.html(request)
	reply
		.status(response.status)
		.headers(response.headers)
		.type('text/html')
		.send(response.body)
})
json(request: Request<T>): Promise<JsonResponse>

Returns a JSON representation of the same "new" form state. This is useful for client‑side form rendering in SPA or mobile applications.

fastify.get('/api/users/new', async (req, reply) => {
	const request = toActionRequest<User>(req)
	const response = await newUser.json(request)
	reply
		.status(response.status)
		.headers(response.headers)
		.send(response.body)
})

Typical use cases

  • Provide a conventional /new screen for any business entity (user, product, order, etc.).
  • Expose a JSON API for front‑end frameworks that render their own forms while relying on the same action logic as the HTML variant.
  • Share a consistent way to create new objects across several projects by reusing the New<T> action instead of rewriting boilerplate controllers.