@janindu-pathirana/message-builder
v1.0.2
Published
A simple TypeScript utility for generating reusable success, error, validation, and resource messages.
Maintainers
Readme
@janindu-pathirana/message-builder
A lightweight TypeScript utility for creating consistent API and service messages.
MessageBuilder helps you generate reusable success, error, validation, and resource messages around a resource name such as user, order, product, or invoice.
Installation
npm install @janindu-pathirana/message-builderQuick Start
import MessageBuilder from "@janindu-pathirana/message-builder";
const messages = new MessageBuilder("user");
messages.success("create");
// "Create user was successful."
messages.notFound();
// "The user you are looking for could not be found."
messages.invalid("email", "Use a valid email address.");
// "The email provided for the user is invalid. Hint: Use a valid email address."CommonJS
const MessageBuilder = require("@janindu-pathirana/message-builder").default;
const messages = new MessageBuilder("product");
console.log(messages.notFound());
// "The product you are looking for could not be found."API
new MessageBuilder(resource?)
Creates a new message builder for a resource.
const userMessages = new MessageBuilder("user");
const orderMessages = new MessageBuilder("order");For best results, pass a resource name. If resource is omitted, methods that use the resource name will include undefined in the generated message.
Methods
| Method | Description |
| --- | --- |
| success(action) | Creates a success message and capitalizes the action. |
| error(action, hint?) | Creates a general error message for an action. |
| badRequest(action, hint?) | Creates an invalid request message for an action. |
| notFound() | Creates a resource not found message. |
| unauthorized() | Creates an authorization failure message. |
| alreadyExists() | Creates a duplicate resource message. |
| somethingWentWrong(hint?) | Creates a generic unexpected error message. |
| failedToInsert(field) | Creates an insert failure message for a field. |
| failedToUpdate(field) | Creates an update failure message for a field. |
| failedToDelete(field) | Creates a delete failure message for a field. |
| invalid(field, hint?) | Creates an invalid field message. |
| customMessage(message) | Returns a custom message unchanged. |
Message Examples
const messages = new MessageBuilder("user");
messages.success("create");
// "Create user was successful."
messages.error("update");
// "There was an error while trying to update the user. Please try again later."
messages.error("update", "The user ID must exist.");
// "There was an error while trying to update the user. Please try again later. Hint: The user ID must exist."
messages.badRequest("create");
// "The request to create the user was invalid. Please check the data and try again."
messages.badRequest("create", "Email is required.");
// "The request to create the user was invalid. Please check the data and try again. Hint: Email is required."
messages.notFound();
// "The user you are looking for could not be found."
messages.unauthorized();
// "You are not authorized to perform this action on user."
messages.alreadyExists();
// "The user already exists."
messages.somethingWentWrong();
// "Something went wrong with the request. Please try again later or contact support if the problem persists."
messages.somethingWentWrong("Try again after a few minutes.");
// "Something went wrong with the request. Please try again later or contact support if the problem persists. Hint: Try again after a few minutes."
messages.failedToInsert("email");
// "Failed to insert data into the user for the field 'email'. Please check the data and try again."
messages.failedToUpdate("password");
// "Failed to update the user for the field 'password'. Please check the data and try again."
messages.failedToDelete("id");
// "Failed to delete the user for the field 'id'. Please try again later."
messages.invalid("email");
// "The email provided for the user is invalid."
messages.invalid("email", "Use a valid email address.");
// "The email provided for the user is invalid. Hint: Use a valid email address."
messages.customMessage("Your custom response message.");
// "Your custom response message."Development
Install dependencies:
npm installBuild the package:
npm run buildThe build uses tsup to generate ESM, CommonJS, and TypeScript declaration files in dist.
Package Output
This package ships:
| Format | Path |
| --- | --- |
| ESM | dist/index.js |
| CommonJS | dist/index.cjs |
| TypeScript declarations | dist/index.d.ts |
License
MIT
