resply
v0.1.1
Published
Standardized API response structure
Maintainers
Readme
Resply
A lightweight TypeScript/JavaScript package for standardizing API responses with automatic case style transformation.
Installation
npm install resplyFeatures
- ✨ Standardized success and error response formats
- 🔄 Automatic case transformation (camelCase, snake_case, kebab-case)
- 📦 TypeScript support with full type definitions
- 🪶 Zero dependencies
- 🎯 Simple and intuitive API
Quick Start
import { initResply, SuccessReply, ErrorReply } from "resply";
// Initialize with your preferred case style (default: camelCase)
initResply({ caseStyle: "camelCase" });
// Create a success response
const response = SuccessReply(
{ user_id: 123, user_name: "Alice" },
"User fetched"
);
// Output: { success: true, message: 'User fetched', data: { userId: 123, userName: 'Alice' } }
// Create an error response
const error = ErrorReply({ field: "email" }, "Invalid email", 400);
// Output: { success: false, error: { message: 'Invalid email', details: { field: 'email' }, statusCode: 400 } }API Reference
initResply(options)
Initialize the package with your preferred configuration.
Parameters:
options.caseStyle(optional):'camelCase'|'snake_case'|'kebab-case'(default:'camelCase')
Example:
import { initResply } from "resply";
// Use snake_case for all responses
initResply({ caseStyle: "snake_case" });
// Use kebab-case
initResply({ caseStyle: "kebab-case" });
// Use camelCase (default)
initResply({ caseStyle: "camelCase" });SuccessReply<T>(data, message?)
Create a standardized success response.
Parameters:
data: The response data (will be transformed to the configured case style)message(optional): Success message (default:"Success")
Returns:
{
success: true,
message: string,
data: T
}Example:
const user = {
user_id: 123,
first_name: "Alice",
last_name: "Johnson",
};
const response = SuccessReply(user, "User created successfully");
// With camelCase:
// {
// success: true,
// message: 'User created successfully',
// data: { userId: 123, firstName: 'Alice', lastName: 'Johnson' }
// }ErrorReply<T>(data?, message?, statusCode?)
Create a standardized error response.
Parameters:
data(optional): Additional error data (will be transformed to the configured case style)message(optional): Error message (default:"Error")statusCode(optional): HTTP status code (default:500)
Returns:
{
success: false,
error: {
message: string,
details?: T,
statusCode: number
}
}Example:
const validationErrors = {
email_field: "Invalid format",
phone_number: "Required",
};
const response = ErrorReply(validationErrors, "Validation failed", 400);
// With camelCase:
// {
// success: false,
// error: {
// message: 'Validation failed',
// data: { emailField: 'Invalid format', phoneNumber: 'Required' },
// statusCode: 400
// }
// }Case Style Examples
camelCase (default)
initResply({ caseStyle: "camelCase" });
const data = { user_id: 1, first_name: "Alice", "home-address": "123 Main St" };
SuccessReply(data);
// Result: { userId: 1, firstName: 'Alice', homeAddress: '123 Main St' }snake_case
initResply({ caseStyle: "snake_case" });
const data = { userId: 1, firstName: "Alice", "home-address": "123 Main St" };
SuccessReply(data);
// Result: { user_id: 1, first_name: 'Alice', home_address: '123 Main St' }kebab-case
initResply({ caseStyle: "kebab-case" });
const data = { user_id: 1, firstName: "Alice" };
SuccessReply(data);
// Result: { user-id: 1, first-name: 'Alice' }Usage with Express
import express from "express";
import { initResply, SuccessReply, ErrorReply } from "resply";
const app = express();
// Initialize with snake_case for API responses
initResply({ caseStyle: "snake_case" });
app.get("/api/users/:id", async (req, res) => {
try {
const user = await getUserById(req.params.id);
if (!user) {
return res.status(404).json(ErrorReply(null, "User not found", 404));
}
res.json(SuccessReply(user, "User fetched successfully"));
} catch (error) {
res
.status(500)
.json(ErrorReply({ error: error.message }, "Internal server error", 500));
}
});Nested Objects and Arrays
Resply automatically transforms nested objects and arrays:
initResply({ caseStyle: "camelCase" });
const data = {
user_id: 1,
user_profile: {
first_name: "Alice",
contact_info: {
email_address: "[email protected]",
},
},
user_orders: [
{ order_id: 101, order_total: 99.99 },
{ order_id: 102, order_total: 149.99 },
],
};
SuccessReply(data);
// All keys transformed to camelCase recursivelyTypeScript Support
Resply is written in TypeScript and provides full type definitions:
import {
SuccessResponse,
ErrorResponse,
SuccessReply,
ErrorReply,
} from "resply";
interface User {
id: number;
name: string;
}
const response: SuccessResponse<User> = SuccessReply({ id: 1, name: "Alice" });
const error: ErrorResponse = ErrorReply(null, "Not found", 404);License
MIT
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
