@itrocks/delete
v0.0.11
Published
Deletion action handling the button, user confirmation, data source deletion, and visual feedback
Downloads
199
Maintainers
Readme
delete
Deletion action handling the button, user confirmation, data source deletion, and visual feedback.
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/deleteAdding this dependency gives you a ready‑to‑use Delete action that fits
into the it.rocks action stack (@itrocks/action, @itrocks/route,
@itrocks/action-request, …).
Usage
@itrocks/delete provides a generic Delete<T> action that:
- asks the user for confirmation before deleting (HTML variant),
- deletes one or more objects from the configured data source,
- returns a small HTML page with a notification and an automatic redirect back to the listing, or a JSON payload describing the deleted objects.
It is typically used together with:
@itrocks/routeto expose a conventional/deleteroute,@itrocks/action-requestto createRequest<T>objects from HTTP requests,@itrocks/storageas the persistence layer,@itrocks/confirmto handle the confirmation flow,@itrocks/auto-redirectfor post‑deletion navigation.
Minimal example
import { Delete } from '@itrocks/delete'
import { Route } from '@itrocks/route'
import type { Request } from '@itrocks/action-request'
class User {
id = 0
name = ''
}
// Conventional configuration: a delete action for User
@Route('/users/:id/delete')
export class DeleteUser extends Delete<User> {}
// Somewhere in your HTTP layer
const deleteUser = new DeleteUser()
async function deleteUserHtml (request: Request<User>) {
// The first call shows a confirmation page.
// Once confirmed, it deletes the object and shows a success message
// with an automatic redirect back to the list.
return deleteUser.html(request)
}Request<User> is usually created by
@itrocks/action-request from the incoming HTTP request.
Complete example: HTML + JSON delete endpoints
The same Delete<T> action can serve both HTML and JSON use cases.
import { Delete } from '@itrocks/delete'
import type { Request } from '@itrocks/action-request'
import type { FastifyInstance } from 'fastify'
import { User } from '../domain/user.js'
// A simple delete action for User
class DeleteUser extends Delete<User> {}
export function registerUserDeleteRoutes (fastify: FastifyInstance) {
const deleteUser = new DeleteUser()
// HTML endpoint: confirmation + redirecting feedback page
fastify.get('/users/:id/delete', async (req, reply) => {
const request = toActionRequest<User>(req)
const response = await deleteUser.html(request)
reply
.status(response.status)
.headers(response.headers)
.type('text/html')
.send(response.body)
})
// JSON endpoint: directly delete and return deleted objects as JSON
fastify.delete('/api/users/:id', async (req, reply) => {
const request = toActionRequest<User>(req)
const response = await deleteUser.json(request)
reply
.status(response.status)
.headers(response.headers)
.send(response.body)
})
}In the HTML flow, the template bundled with the package shows a
notification such as "John Doe deleted." and a link that automatically
redirects the user back to the list of objects.
API
class Delete<T extends object = object> extends Action<T>
Delete<T> is a concrete action that removes objects of type T from
the configured data source.
From the implementation you can see that it is decorated with:
@Need('object')– the action requires an existing object (or objects) to be present in the request.@Route('/delete')– declares a conventional/deleteroute when used with@itrocks/route. You can also add your own route decorators in your project (for example/users/:id/delete).
Typical call sites receive a Request<T> (for example built by
@itrocks/action-request from an HTTP request) and invoke either the
html or json method.
Type parameter
T– The domain object type you want to delete (for exampleUser,Product,Order, …).
Methods
html(request: Request<T>): Promise<HtmlResponse>
HTML‑oriented delete flow:
- Uses
@itrocks/confirmto ask the user for a confirmation (if not yet confirmed). - Once confirmed, loads the objects from the request via
request.getObjects(). - Deletes each object with
dataSource().delete(object). - Returns an HTML response built from the provided
delete.htmltemplate, including a success message and a link that uses@itrocks/auto-redirectto go back to a sensible screen (typically the list of objects).
Use this method for classic web flows where the browser follows redirects and renders HTML.
json(request: Request<T>): Promise<JsonResponse>
JSON‑oriented delete flow:
- Loads the objects from the request via
request.getObjects(). - Deletes each object using the configured data source.
- Returns a JSON response containing the deleted objects.
Use this method from APIs consumed by SPAs, mobile applications, or other back‑end services that want to manage their own UI after the deletion.
Typical use cases
- Back‑office delete action – provide a consistent way to delete entities (users, products, orders, …) from an admin UI, with confirmation and a redirect back to the list.
- REST/JSON delete endpoint – expose
DELETE /api/...routes that delete one or more entities and return the deleted records as JSON. - Reusable domain‑specific CRUD packs – compose
Delete<T>withNew<T>,List<T>andEdit<T>to publish a complete CRUD module for a given business entity. - Safety wrapper around destructive operations – centralise confirmation, error handling and messaging for destructive actions instead of re‑implementing them in each controller.
