powr-sdk-api 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/services/logger.js +1 -1
- package/package.json +1 -1
- package/README.md +0 -115
package/dist/services/logger.js
CHANGED
package/package.json
CHANGED
package/README.md
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
# PowrStack API Core Library
|
|
2
|
-
|
|
3
|
-
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.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install powr-sdk-api
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- Error handling middleware with `APIError` class
|
|
14
|
-
- Response formatting middleware
|
|
15
|
-
- Request/Response logging middleware
|
|
16
|
-
- Swagger documentation setup
|
|
17
|
-
- Async handler utility
|
|
18
|
-
|
|
19
|
-
## Usage
|
|
20
|
-
|
|
21
|
-
### Basic Setup
|
|
22
|
-
|
|
23
|
-
```javascript
|
|
24
|
-
const express = require('express');
|
|
25
|
-
const {
|
|
26
|
-
errorHandler,
|
|
27
|
-
responseHandler,
|
|
28
|
-
logger,
|
|
29
|
-
createSwaggerSpec
|
|
30
|
-
} = require('powr-sdk-api');
|
|
31
|
-
|
|
32
|
-
const app = express();
|
|
33
|
-
|
|
34
|
-
// Apply middleware
|
|
35
|
-
app.use(logger);
|
|
36
|
-
app.use(responseHandler);
|
|
37
|
-
|
|
38
|
-
// Setup Swagger
|
|
39
|
-
const swaggerSpec = createSwaggerSpec({
|
|
40
|
-
title: 'Your API',
|
|
41
|
-
version: '1.0.0',
|
|
42
|
-
description: 'Your API description',
|
|
43
|
-
serverUrl: 'http://localhost:3000'
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// Apply error handler last
|
|
47
|
-
app.use(errorHandler);
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Error Handling
|
|
51
|
-
|
|
52
|
-
```javascript
|
|
53
|
-
const { APIError, errorCatcher } = require('powr-sdk-api');
|
|
54
|
-
|
|
55
|
-
// In your route handler
|
|
56
|
-
app.get('/users/:id', errorCatcher(async (req, res) => {
|
|
57
|
-
const user = await User.findById(req.params.id);
|
|
58
|
-
if (!user) {
|
|
59
|
-
throw new APIError('User not found', 404);
|
|
60
|
-
}
|
|
61
|
-
res.success(user);
|
|
62
|
-
}));
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### Response Formatting
|
|
66
|
-
|
|
67
|
-
```javascript
|
|
68
|
-
// Success response
|
|
69
|
-
res.success(data, 'Operation successful', 200);
|
|
70
|
-
|
|
71
|
-
// Error response
|
|
72
|
-
res.error('Something went wrong', 500, { details: 'Error details' });
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Swagger Documentation
|
|
76
|
-
|
|
77
|
-
```javascript
|
|
78
|
-
const swaggerUi = require('swagger-ui-express');
|
|
79
|
-
const { createSwaggerSpec } = require('powr-sdk-api');
|
|
80
|
-
|
|
81
|
-
const swaggerSpec = createSwaggerSpec({
|
|
82
|
-
title: 'Your API',
|
|
83
|
-
version: '1.0.0',
|
|
84
|
-
description: 'Your API description',
|
|
85
|
-
serverUrl: 'http://localhost:3000'
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
## API Reference
|
|
92
|
-
|
|
93
|
-
### Error Handling
|
|
94
|
-
|
|
95
|
-
- `APIError`: Custom error class for API errors
|
|
96
|
-
- `errorCatcher`: Wrapper for async route handlers
|
|
97
|
-
- `errorHandler`: Global error handling middleware
|
|
98
|
-
|
|
99
|
-
### Response Formatting
|
|
100
|
-
|
|
101
|
-
- `responseHandler`: Middleware for standardizing API responses
|
|
102
|
-
- `res.success()`: Send a success response
|
|
103
|
-
- `res.error()`: Send an error response
|
|
104
|
-
|
|
105
|
-
### Logging
|
|
106
|
-
|
|
107
|
-
- `logger`: Middleware for logging requests and responses
|
|
108
|
-
|
|
109
|
-
### Swagger
|
|
110
|
-
|
|
111
|
-
- `createSwaggerSpec`: Function to create Swagger specification
|
|
112
|
-
|
|
113
|
-
## License
|
|
114
|
-
|
|
115
|
-
ISC
|