uni-response
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published
Build status npm version npm downloads Package size GitHub stars

Uni Response

A Simple Utility to Create Unified Response Objects for APIs. Allowing for Easy Handling of API Responses Across Your Application.

Features

  • Unified response format for all API calls.
  • Flexible to include additional custom fields.
  • Automatically includes a timestamp for - logging and tracking.
  • Supports both successful and error responses.

Installation

npm install uni-response

Usage

import { unifiedResponse } from 'uni-response';

const response = unifiedResponse(true, 'Request was successful', { id: 1 });
console.log(response);
/*
{
  success: true,
  message: "Request was successful",
  data: { id: 1 },
  timestamp: "..."
}
*/
// Example with CustomResponseFields

interface CustomResponseFields {
  customField1: string;
  customField2: number;
  customField3: boolean;
}

// Creating a response with custom fields
const responseWithCustomFields =
  unifiedResponse <
  CustomResponseFields >
  (true,
  'Request was successful',
  { user: 'Jane Doe' },
  null,
  null,
  {
    customField1: 'Custom Data 1',
    customField2: 42,
    customField3: true,
  });

// Output
console.log(responseWithCustomFields);
/*
{
  success: true,
  message: "Request was successful",
  data: { user: 'Jane Doe' },
  timestamp: "..."
  extraFields: {
    customField1: 'Custom Data 1',
    customField2: 42,
    customField3: true,
  }
}
*/

Response Structure

interface Response<T = Record<string, unknown>> {
  success: boolean;
  message: string;
  data?: object | null;
  error?: object | null;
  metadata?: object | null;
  timestamp: string;
  extraFields?: T; // Generic type for extra fields
}

License

MIT License

Author

Sushant Rahate (sushantrahate15@gmail.com)

Package Sidebar

Install

npm i uni-response

Weekly Downloads

5

Version

1.0.3

License

MIT

Unpacked Size

8.76 kB

Total Files

6

Last publish

Collaborators

  • sushantrahate