JSPM

  • Created
  • Published
  • Downloads 122
  • Score
    100M100P100Q70243F
  • License ISC

Shared API core library for PowrStack projects

Package Exports

  • powr-sdk-api
  • powr-sdk-api/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (powr-sdk-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

PowrStack API Core Library

A shared library for Express API projects in the PowrStack ecosystem. This library provides common middleware and utilities for error handling, response formatting, logging, and Swagger documentation.

Installation

npm install powr-sdk-api

Features

  • Error handling middleware with APIError class
  • Response formatting middleware
  • Request/Response logging middleware
  • Swagger documentation setup
  • Async handler utility

Usage

Basic Setup

const express = require('express');
const {
  errorHandler,
  responseHandler,
  logger,
  createSwaggerSpec
} = require('powr-sdk-api');

const app = express();

// Apply middleware
app.use(logger);
app.use(responseHandler);

// Setup Swagger
const swaggerSpec = createSwaggerSpec({
  title: 'Your API',
  version: '1.0.0',
  description: 'Your API description',
  serverUrl: 'http://localhost:3000'
});

// Apply error handler last
app.use(errorHandler);

Error Handling

const { APIError, asyncHandler } = require('powr-sdk-api');

// In your route handler
app.get('/users/:id', asyncHandler(async (req, res) => {
  const user = await User.findById(req.params.id);
  if (!user) {
    throw new APIError('User not found', 404);
  }
  res.success(user);
}));

Response Formatting

// Success response
res.success(data, 'Operation successful', 200);

// Error response
res.error('Something went wrong', 500, { details: 'Error details' });

Swagger Documentation

const swaggerUi = require('swagger-ui-express');
const { createSwaggerSpec } = require('powr-sdk-api');

const swaggerSpec = createSwaggerSpec({
  title: 'Your API',
  version: '1.0.0',
  description: 'Your API description',
  serverUrl: 'http://localhost:3000'
});

app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

API Reference

Error Handling

  • APIError: Custom error class for API errors
  • asyncHandler: Wrapper for async route handlers
  • errorHandler: Global error handling middleware

Response Formatting

  • responseHandler: Middleware for standardizing API responses
  • res.success(): Send a success response
  • res.error(): Send an error response

Logging

  • logger: Middleware for logging requests and responses

Swagger

  • createSwaggerSpec: Function to create Swagger specification

License

ISC