@designofadecade/server 4.0.0
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/CHANGELOG.md +62 -0
- package/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/client/ApiClient.d.ts +121 -0
- package/dist/client/ApiClient.d.ts.map +1 -0
- package/dist/client/ApiClient.js +289 -0
- package/dist/client/ApiClient.js.map +1 -0
- package/dist/context/Context.d.ts +71 -0
- package/dist/context/Context.d.ts.map +1 -0
- package/dist/context/Context.js +81 -0
- package/dist/context/Context.js.map +1 -0
- package/dist/docs/OpenApiGenerator.d.ts +135 -0
- package/dist/docs/OpenApiGenerator.d.ts.map +1 -0
- package/dist/docs/OpenApiGenerator.js +165 -0
- package/dist/docs/OpenApiGenerator.js.map +1 -0
- package/dist/events/Events.d.ts +52 -0
- package/dist/events/Events.d.ts.map +1 -0
- package/dist/events/Events.js +70 -0
- package/dist/events/Events.js.map +1 -0
- package/dist/events/EventsManager.d.ts +46 -0
- package/dist/events/EventsManager.d.ts.map +1 -0
- package/dist/events/EventsManager.js +137 -0
- package/dist/events/EventsManager.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/local/Local.d.ts +83 -0
- package/dist/local/Local.d.ts.map +1 -0
- package/dist/local/Local.js +114 -0
- package/dist/local/Local.js.map +1 -0
- package/dist/logger/Logger.d.ts +365 -0
- package/dist/logger/Logger.d.ts.map +1 -0
- package/dist/logger/Logger.js +582 -0
- package/dist/logger/Logger.js.map +1 -0
- package/dist/middleware/RequestLogger.d.ts +62 -0
- package/dist/middleware/RequestLogger.d.ts.map +1 -0
- package/dist/middleware/RequestLogger.js +71 -0
- package/dist/middleware/RequestLogger.js.map +1 -0
- package/dist/notifications/Slack.d.ts +19 -0
- package/dist/notifications/Slack.d.ts.map +1 -0
- package/dist/notifications/Slack.js +55 -0
- package/dist/notifications/Slack.js.map +1 -0
- package/dist/router/RouteError.d.ts +21 -0
- package/dist/router/RouteError.d.ts.map +1 -0
- package/dist/router/RouteError.js +31 -0
- package/dist/router/RouteError.js.map +1 -0
- package/dist/router/Router.d.ts +66 -0
- package/dist/router/Router.d.ts.map +1 -0
- package/dist/router/Router.js +327 -0
- package/dist/router/Router.js.map +1 -0
- package/dist/router/Routes.d.ts +30 -0
- package/dist/router/Routes.d.ts.map +1 -0
- package/dist/router/Routes.js +52 -0
- package/dist/router/Routes.js.map +1 -0
- package/dist/router/StaticFileHandler.d.ts +44 -0
- package/dist/router/StaticFileHandler.d.ts.map +1 -0
- package/dist/router/StaticFileHandler.js +148 -0
- package/dist/router/StaticFileHandler.js.map +1 -0
- package/dist/sanitizer/HtmlSanitizer.d.ts +306 -0
- package/dist/sanitizer/HtmlSanitizer.d.ts.map +1 -0
- package/dist/sanitizer/HtmlSanitizer.js +808 -0
- package/dist/sanitizer/HtmlSanitizer.js.map +1 -0
- package/dist/server/Server.d.ts +28 -0
- package/dist/server/Server.d.ts.map +1 -0
- package/dist/server/Server.js +95 -0
- package/dist/server/Server.js.map +1 -0
- package/dist/state/AppState.d.ts +64 -0
- package/dist/state/AppState.d.ts.map +1 -0
- package/dist/state/AppState.js +89 -0
- package/dist/state/AppState.js.map +1 -0
- package/dist/utils/HtmlRenderer.d.ts +6 -0
- package/dist/utils/HtmlRenderer.d.ts.map +1 -0
- package/dist/utils/HtmlRenderer.js +128 -0
- package/dist/utils/HtmlRenderer.js.map +1 -0
- package/dist/websocket/WebSocketMessageFormatter.d.ts +40 -0
- package/dist/websocket/WebSocketMessageFormatter.d.ts.map +1 -0
- package/dist/websocket/WebSocketMessageFormatter.js +99 -0
- package/dist/websocket/WebSocketMessageFormatter.js.map +1 -0
- package/dist/websocket/WebSocketServer.d.ts +14 -0
- package/dist/websocket/WebSocketServer.d.ts.map +1 -0
- package/dist/websocket/WebSocketServer.js +138 -0
- package/dist/websocket/WebSocketServer.js.map +1 -0
- package/package.json +97 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [4.0.0] - 2026-02-28
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Updated package to publish to npm.js registry instead of GitHub Packages
|
|
14
|
+
- Improved package.json with enhanced keywords and metadata
|
|
15
|
+
- Enhanced README.md with comprehensive badges, installation instructions, and documentation links
|
|
16
|
+
- Optimized build configuration to exclude test and benchmark files from distribution
|
|
17
|
+
- Updated GitHub Actions workflow for npm publishing with provenance
|
|
18
|
+
- Improved .gitignore with additional patterns
|
|
19
|
+
|
|
20
|
+
### Added
|
|
21
|
+
- .npmignore file to ensure only necessary files are published
|
|
22
|
+
- Security section in README
|
|
23
|
+
- API Reference section in README with links to all documentation
|
|
24
|
+
- Support and Links sections in README
|
|
25
|
+
- Additional badges for npm version, TypeScript, and build status
|
|
26
|
+
|
|
27
|
+
### Removed
|
|
28
|
+
- .npmrc file (no longer needed for public npm registry)
|
|
29
|
+
- Test and benchmark files from compiled output
|
|
30
|
+
|
|
31
|
+
## [3.0.0] - 2026-02-27
|
|
32
|
+
|
|
33
|
+
### Added
|
|
34
|
+
- Core HTTP/HTTPS server implementation
|
|
35
|
+
- WebSocket server with message formatting
|
|
36
|
+
- Flexible routing system with URL pattern matching
|
|
37
|
+
- Static file serving with MIME type detection
|
|
38
|
+
- Middleware support (request logging)
|
|
39
|
+
- Application state management
|
|
40
|
+
- Event system with pub/sub pattern
|
|
41
|
+
- HTML sanitization utilities
|
|
42
|
+
- Slack notifications integration
|
|
43
|
+
- Comprehensive test suite with Vitest
|
|
44
|
+
- TypeScript definitions and ESM support
|
|
45
|
+
- Performance benchmarks using Vitest bench for Router, AppState, EventsManager, and HtmlSanitizer
|
|
46
|
+
- OpenAPI/Swagger documentation generator with TypeScript support
|
|
47
|
+
- Pre-commit hooks using Husky and lint-staged for code quality (Prettier, ESLint, tests)
|
|
48
|
+
- Swagger UI HTML generation for interactive API documentation
|
|
49
|
+
- Benchmark npm scripts (`bench` and `bench:watch`)
|
|
50
|
+
- API client utility (ApiClient)
|
|
51
|
+
- Context object for request handling
|
|
52
|
+
- Route error handling (RouteError)
|
|
53
|
+
- HTML rendering utilities
|
|
54
|
+
|
|
55
|
+
## [1.0.0] - 2026-02-26
|
|
56
|
+
|
|
57
|
+
### Added
|
|
58
|
+
- Initial package setup
|
|
59
|
+
- GitHub Actions workflows for testing and publishing
|
|
60
|
+
- ESLint and Prettier configuration
|
|
61
|
+
- MIT License
|
|
62
|
+
- README and contributing guidelines
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Design of a Decade
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# Design of a Decade Server
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
[](https://www.npmjs.com/package/@designofadecade/server)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://github.com/designofadecade/server/actions)
|
|
8
|
+
|
|
9
|
+
A modern, type-safe Node.js server framework with built-in WebSocket support, routing, static file handling, and middleware capabilities. Built with TypeScript for Node.js 24+.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- ✅ Full TypeScript support with comprehensive type definitions
|
|
14
|
+
- ✅ ESM (ES Modules) compatible
|
|
15
|
+
- ✅ WebSocket server with message formatting and event handling
|
|
16
|
+
- ✅ Flexible routing system with URL pattern matching
|
|
17
|
+
- ✅ Static file serving with MIME type detection
|
|
18
|
+
- ✅ Built-in middleware support (request logging, etc.)
|
|
19
|
+
- ✅ Application state management
|
|
20
|
+
- ✅ Event system with pub/sub pattern
|
|
21
|
+
- ✅ HTML sanitization utilities
|
|
22
|
+
- ✅ Comprehensive test coverage with Vitest
|
|
23
|
+
- ✅ Modern async/await API
|
|
24
|
+
|
|
25
|
+
## 📦 Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @designofadecade/server
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or using other package managers:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
# Yarn
|
|
35
|
+
yarn add @designofadecade/server
|
|
36
|
+
|
|
37
|
+
# pnpm
|
|
38
|
+
pnpm add @designofadecade/server
|
|
39
|
+
|
|
40
|
+
# Bun
|
|
41
|
+
bun add @designofadecade/server
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Requirements
|
|
45
|
+
|
|
46
|
+
- **Node.js** >= 24.0.0
|
|
47
|
+
- **ES Modules** support (package uses `"type": "module"`)
|
|
48
|
+
- TypeScript >= 5.0 (if using TypeScript)
|
|
49
|
+
|
|
50
|
+
## Quick Start
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { Server } from '@designofadecade/server';
|
|
54
|
+
|
|
55
|
+
// Create a new server instance
|
|
56
|
+
const server = new Server({
|
|
57
|
+
port: 3000,
|
|
58
|
+
hostname: 'localhost'
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Start the server
|
|
62
|
+
await server.start();
|
|
63
|
+
console.log('Server running on http://localhost:3000');
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Architecture Overview
|
|
67
|
+
|
|
68
|
+
The server is composed of several modular components:
|
|
69
|
+
|
|
70
|
+
### Server Core
|
|
71
|
+
- **Server**: Main HTTP/HTTPS server with WebSocket upgrade support
|
|
72
|
+
- **Router**: URL pattern-based routing with request handlers
|
|
73
|
+
- **Routes**: Route collection and management
|
|
74
|
+
|
|
75
|
+
### WebSocket Support
|
|
76
|
+
- **WebSocketServer**: WebSocket connection management
|
|
77
|
+
- **WebSocketMessageFormatter**: Message serialization/deserialization
|
|
78
|
+
|
|
79
|
+
### Middleware & Utilities
|
|
80
|
+
- **RequestLogger**: HTTP request logging middleware
|
|
81
|
+
- **StaticFileHandler**: Static file serving with caching
|
|
82
|
+
- **HtmlSanitizer**: XSS protection for HTML content
|
|
83
|
+
- **HtmlRenderer**: Server-side HTML rendering
|
|
84
|
+
|
|
85
|
+
### State & Events
|
|
86
|
+
- **AppState**: Application-wide state management
|
|
87
|
+
- **Events**: Event emitter with type-safe event handling
|
|
88
|
+
- **EventsManager**: Event subscription and lifecycle management
|
|
89
|
+
|
|
90
|
+
### Storage
|
|
91
|
+
- **Local**: Local file system utilities
|
|
92
|
+
|
|
93
|
+
### Integrations
|
|
94
|
+
- **Slack**: Slack notifications integration
|
|
95
|
+
- **ApiClient**: HTTP client for external APIs
|
|
96
|
+
|
|
97
|
+
## Usage Examples
|
|
98
|
+
|
|
99
|
+
### Basic Routing
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { Router } from '@designofadecade/server';
|
|
103
|
+
|
|
104
|
+
const router = new Router();
|
|
105
|
+
|
|
106
|
+
// Add routes
|
|
107
|
+
router.addRoute({
|
|
108
|
+
pattern: '/api/users/:id',
|
|
109
|
+
method: 'GET',
|
|
110
|
+
handler: async (req, res, params) => {
|
|
111
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
112
|
+
res.end(JSON.stringify({ userId: params.id }));
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Handle requests
|
|
117
|
+
await router.route(request, response);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### WebSocket Communication
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { WebSocketServer } from '@designofadecade/server';
|
|
124
|
+
|
|
125
|
+
const wsServer = new WebSocketServer({ port: 8080 });
|
|
126
|
+
|
|
127
|
+
wsServer.on('connection', (ws) => {
|
|
128
|
+
console.log('Client connected');
|
|
129
|
+
|
|
130
|
+
ws.on('message', (data) => {
|
|
131
|
+
console.log('Received:', data);
|
|
132
|
+
ws.send('Echo: ' + data);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Static File Serving
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { StaticFileHandler } from '@designofadecade/server';
|
|
141
|
+
|
|
142
|
+
const staticHandler = new StaticFileHandler({
|
|
143
|
+
rootDir: './public',
|
|
144
|
+
cacheControl: 'public, max-age=3600'
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
await staticHandler.serve(request, response, '/assets/style.css');
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Event System
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { Events, EventsManager } from '@designofadecade/server';
|
|
154
|
+
|
|
155
|
+
const events = new Events();
|
|
156
|
+
const manager = new EventsManager(events);
|
|
157
|
+
|
|
158
|
+
// Subscribe to events
|
|
159
|
+
manager.on('user:login', (data) => {
|
|
160
|
+
console.log('User logged in:', data);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Emit events
|
|
164
|
+
events.emit('user:login', { userId: 123, timestamp: Date.now() });
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Documentation
|
|
168
|
+
|
|
169
|
+
Comprehensive documentation is available in the `/docs` directory:
|
|
170
|
+
|
|
171
|
+
- **[Logger Documentation](docs/logger.md)** - CloudWatch Logger with AWS Lambda integration, sensitive data redaction, and performance tracking
|
|
172
|
+
- **[API Documentation](docs/api-documentation.md)** - OpenAPI/Swagger documentation generation guide
|
|
173
|
+
- **[New Features](FEATURES.md)** - Performance benchmarks, pre-commit hooks, and recent additions
|
|
174
|
+
- **[Changelog](CHANGELOG.md)** - Version history and release notes
|
|
175
|
+
- **[Contributing Guide](CONTRIBUTING.md)** - How to contribute to the project
|
|
176
|
+
|
|
177
|
+
## Development
|
|
178
|
+
|
|
179
|
+
### Install Dependencies
|
|
180
|
+
```bash
|
|
181
|
+
npm install
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Run Tests
|
|
185
|
+
```bash
|
|
186
|
+
npm test # Run tests once
|
|
187
|
+
npm run test:watch # Run tests in watch mode
|
|
188
|
+
npm run test:ui # Open Vitest UI
|
|
189
|
+
npm run test:coverage # Generate coverage report
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Build
|
|
193
|
+
```bash
|
|
194
|
+
npm run build # Build once
|
|
195
|
+
npm run build:watch # Build in watch mode
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Linting & Formatting
|
|
199
|
+
```bash
|
|
200
|
+
npm run lint # Run ESLint
|
|
201
|
+
npm run format # Format code with Prettier
|
|
202
|
+
npm run typecheck # Type-check without emitting
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Project Structure
|
|
206
|
+
|
|
207
|
+
```
|
|
208
|
+
src/
|
|
209
|
+
├── client/ # API client utilities
|
|
210
|
+
├── context/ # Request context handling
|
|
211
|
+
├── docs/ # OpenAPI/Swagger documentation generators
|
|
212
|
+
├── events/ # Event system with pub/sub pattern
|
|
213
|
+
├── local/ # Local storage utilities
|
|
214
|
+
├── logger/ # Logging utilities with CloudWatch support
|
|
215
|
+
├── middleware/ # Request middleware (logging, etc.)
|
|
216
|
+
├── notifications/ # Notification integrations (Slack, etc.)
|
|
217
|
+
├── router/ # Routing system with URL pattern matching
|
|
218
|
+
├── sanitizer/ # HTML sanitization for XSS protection
|
|
219
|
+
├── server/ # Core HTTP/HTTPS server implementation
|
|
220
|
+
├── state/ # Application state management
|
|
221
|
+
├── types/ # TypeScript type definitions
|
|
222
|
+
├── utils/ # Utilities and helpers
|
|
223
|
+
└── websocket/ # WebSocket server implementation
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## API Reference
|
|
227
|
+
|
|
228
|
+
Full API documentation is available in the [docs](docs/) directory:
|
|
229
|
+
|
|
230
|
+
- **[Server](docs/server.md)** - Core server setup and configuration
|
|
231
|
+
- **[Router](docs/router.md)** - Routing and request handling
|
|
232
|
+
- **[WebSocket](docs/websocket.md)** - WebSocket server and messaging
|
|
233
|
+
- **[Logger](docs/logger.md)** - CloudWatch logging and performance tracking
|
|
234
|
+
- **[Events Manager](docs/events-manager.md)** - Event system usage
|
|
235
|
+
- **[API Documentation](docs/api-documentation.md)** - OpenAPI/Swagger setup
|
|
236
|
+
- **[State & Context](docs/context-state.md)** - State management
|
|
237
|
+
- **[Utilities](docs/utilities.md)** - Helper functions and utilities
|
|
238
|
+
|
|
239
|
+
## Security
|
|
240
|
+
|
|
241
|
+
This package includes built-in security features:
|
|
242
|
+
|
|
243
|
+
- **HTML Sanitization** - XSS protection for user-generated content
|
|
244
|
+
- **Type Safety** - Full TypeScript support for compile-time safety
|
|
245
|
+
- **Input Validation** - URL pattern matching and parameter validation
|
|
246
|
+
|
|
247
|
+
If you discover a security vulnerability, please send an email to info@designofadecade.com instead of using the issue tracker.
|
|
248
|
+
|
|
249
|
+
## Versioning
|
|
250
|
+
|
|
251
|
+
This project follows [Semantic Versioning](https://semver.org/). For available versions, see the [tags on this repository](https://github.com/designofadecade/server/tags) or the [npm registry](https://www.npmjs.com/package/@designofadecade/server).
|
|
252
|
+
|
|
253
|
+
See [CHANGELOG.md](CHANGELOG.md) for a history of changes.
|
|
254
|
+
|
|
255
|
+
## Support
|
|
256
|
+
|
|
257
|
+
- **Documentation**: [GitHub Repository](https://github.com/designofadecade/server)
|
|
258
|
+
- **Issues**: [GitHub Issues](https://github.com/designofadecade/server/issues)
|
|
259
|
+
- **Discussions**: [GitHub Discussions](https://github.com/designofadecade/server/discussions)
|
|
260
|
+
|
|
261
|
+
## Contributing
|
|
262
|
+
|
|
263
|
+
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details on:
|
|
264
|
+
|
|
265
|
+
- Code of Conduct
|
|
266
|
+
- Development workflow
|
|
267
|
+
- Pull request process
|
|
268
|
+
- Coding standards
|
|
269
|
+
- Testing requirements
|
|
270
|
+
|
|
271
|
+
## License
|
|
272
|
+
|
|
273
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
274
|
+
|
|
275
|
+
## Author
|
|
276
|
+
|
|
277
|
+
**Design of a Decade**
|
|
278
|
+
Email: info@designofadecade.com
|
|
279
|
+
Website: [designofadecade.com](https://designofadecade.com)
|
|
280
|
+
|
|
281
|
+
## Links
|
|
282
|
+
|
|
283
|
+
- [GitHub Repository](https://github.com/designofadecade/server)
|
|
284
|
+
- [npm Package](https://www.npmjs.com/package/@designofadecade/server)
|
|
285
|
+
- [Issue Tracker](https://github.com/designofadecade/server/issues)
|
|
286
|
+
- [Changelog](CHANGELOG.md)
|
|
287
|
+
|
|
288
|
+
## Acknowledgments
|
|
289
|
+
|
|
290
|
+
Built with:
|
|
291
|
+
- [TypeScript](https://www.typescriptlang.org/) - Type-safe JavaScript
|
|
292
|
+
- [Vitest](https://vitest.dev/) - Unit testing framework
|
|
293
|
+
- [ws](https://github.com/websockets/ws) - WebSocket implementation
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
Made with ❤️ by Design of a Decade
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API Client for making HTTP requests
|
|
3
|
+
* @class ApiClient
|
|
4
|
+
* @example
|
|
5
|
+
* const client = new ApiClient('https://api.example.com');
|
|
6
|
+
* client.setAuthToken('your-token');
|
|
7
|
+
* const result = await client.get('/users', { page: 1, limit: 10 });
|
|
8
|
+
*/
|
|
9
|
+
interface ApiClientOptions {
|
|
10
|
+
timeout?: number;
|
|
11
|
+
retryAttempts?: number;
|
|
12
|
+
retryDelay?: number;
|
|
13
|
+
}
|
|
14
|
+
interface RequestOptions {
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
interface ApiResponse {
|
|
19
|
+
ok: boolean;
|
|
20
|
+
status: number | null;
|
|
21
|
+
statusText: string;
|
|
22
|
+
data: any;
|
|
23
|
+
error: string | null;
|
|
24
|
+
headers: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
type RequestInterceptor = (config: RequestInit & {
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
}) => Promise<RequestInit & {
|
|
29
|
+
signal: AbortSignal;
|
|
30
|
+
}> | RequestInit & {
|
|
31
|
+
signal: AbortSignal;
|
|
32
|
+
};
|
|
33
|
+
type ResponseInterceptor = (response: ApiResponse) => Promise<ApiResponse> | ApiResponse;
|
|
34
|
+
export default class ApiClient {
|
|
35
|
+
#private;
|
|
36
|
+
/**
|
|
37
|
+
* Create an API Client
|
|
38
|
+
* @param {string} baseUrl - Base URL for all requests
|
|
39
|
+
* @param {Object} options - Configuration options
|
|
40
|
+
* @param {number} options.timeout - Request timeout in milliseconds (default: 30000)
|
|
41
|
+
* @param {number} options.retryAttempts - Number of retry attempts for failed requests (default: 0)
|
|
42
|
+
* @param {number} options.retryDelay - Delay between retries in milliseconds (default: 1000)
|
|
43
|
+
*/
|
|
44
|
+
constructor(baseUrl: string, options?: ApiClientOptions);
|
|
45
|
+
/**
|
|
46
|
+
* Set authentication token
|
|
47
|
+
* @param {string} token - Authentication token
|
|
48
|
+
* @param {string} type - Token type (default: 'Bearer')
|
|
49
|
+
*/
|
|
50
|
+
setAuthToken(token: string, type?: string): void;
|
|
51
|
+
/**
|
|
52
|
+
* Clear authentication token
|
|
53
|
+
*/
|
|
54
|
+
clearAuthToken(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Set default headers for all requests
|
|
57
|
+
* @param {Object} headers - Headers object
|
|
58
|
+
*/
|
|
59
|
+
setDefaultHeaders(headers: Record<string, string>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Add a request interceptor
|
|
62
|
+
* @param {Function} interceptor - Function that receives and modifies request config
|
|
63
|
+
*/
|
|
64
|
+
addRequestInterceptor(interceptor: RequestInterceptor): void;
|
|
65
|
+
/**
|
|
66
|
+
* Add a response interceptor
|
|
67
|
+
* @param {Function} interceptor - Function that receives and modifies response
|
|
68
|
+
*/
|
|
69
|
+
addResponseInterceptor(interceptor: ResponseInterceptor): void;
|
|
70
|
+
/**
|
|
71
|
+
* Make GET request
|
|
72
|
+
* @param {string} url - Request URL
|
|
73
|
+
* @param {Object} params - Query parameters (optional)
|
|
74
|
+
* @param {Object} options - Request options
|
|
75
|
+
* @param {Object} options.headers - Custom headers for this request
|
|
76
|
+
* @param {AbortSignal} options.signal - Abort signal for cancellation
|
|
77
|
+
* @returns {Promise<Object>} Response object
|
|
78
|
+
*/
|
|
79
|
+
get(url: string, params?: Record<string, any> | null, options?: RequestOptions): Promise<ApiResponse>;
|
|
80
|
+
/**
|
|
81
|
+
* Make POST request
|
|
82
|
+
* @param {string} url - Request URL
|
|
83
|
+
* @param {Object|FormData} data - Request body
|
|
84
|
+
* @param {Object} options - Request options
|
|
85
|
+
* @param {Object} options.headers - Custom headers for this request
|
|
86
|
+
* @param {AbortSignal} options.signal - Abort signal for cancellation
|
|
87
|
+
* @returns {Promise<Object>} Response object
|
|
88
|
+
*/
|
|
89
|
+
post(url: string, data: any, options?: RequestOptions): Promise<ApiResponse>;
|
|
90
|
+
/**
|
|
91
|
+
* Make PUT request
|
|
92
|
+
* @param {string} url - Request URL
|
|
93
|
+
* @param {Object|FormData} data - Request body
|
|
94
|
+
* @param {Object} options - Request options
|
|
95
|
+
* @param {Object} options.headers - Custom headers for this request
|
|
96
|
+
* @param {AbortSignal} options.signal - Abort signal for cancellation
|
|
97
|
+
* @returns {Promise<Object>} Response object
|
|
98
|
+
*/
|
|
99
|
+
put(url: string, data: any, options?: RequestOptions): Promise<ApiResponse>;
|
|
100
|
+
/**
|
|
101
|
+
* Make DELETE request
|
|
102
|
+
* @param {string} url - Request URL
|
|
103
|
+
* @param {Object} options - Request options
|
|
104
|
+
* @param {Object} options.headers - Custom headers for this request
|
|
105
|
+
* @param {AbortSignal} options.signal - Abort signal for cancellation
|
|
106
|
+
* @returns {Promise<Object>} Response object
|
|
107
|
+
*/
|
|
108
|
+
delete(url: string, options?: RequestOptions): Promise<ApiResponse>;
|
|
109
|
+
/**
|
|
110
|
+
* Make PATCH request
|
|
111
|
+
* @param {string} url - Request URL
|
|
112
|
+
* @param {Object} data - Request body
|
|
113
|
+
* @param {Object} options - Request options
|
|
114
|
+
* @param {Object} options.headers - Custom headers for this request
|
|
115
|
+
* @param {AbortSignal} options.signal - Abort signal for cancellation
|
|
116
|
+
* @returns {Promise<Object>} Response object
|
|
117
|
+
*/
|
|
118
|
+
patch(url: string, data: any, options?: RequestOptions): Promise<ApiResponse>;
|
|
119
|
+
}
|
|
120
|
+
export {};
|
|
121
|
+
//# sourceMappingURL=ApiClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ApiClient.d.ts","sourceRoot":"","sources":["../../src/client/ApiClient.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,UAAU,gBAAgB;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,UAAU,cAAc;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,WAAW,CAAC;CACxB;AAED,UAAU,WAAW;IACjB,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,GAAG,CAAC;IACV,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnC;AAED,KAAK,kBAAkB,GAAG,CAAC,MAAM,EAAE,WAAW,GAAG;IAAE,MAAM,EAAE,WAAW,CAAA;CAAE,KAAK,OAAO,CAAC,WAAW,GAAG;IAAE,MAAM,EAAE,WAAW,CAAA;CAAE,CAAC,GAAG,WAAW,GAAG;IAAE,MAAM,EAAE,WAAW,CAAA;CAAE,CAAC;AACpK,KAAK,mBAAmB,GAAG,CAAC,QAAQ,EAAE,WAAW,KAAK,OAAO,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;AAEzF,MAAM,CAAC,OAAO,OAAO,SAAS;;IAY1B;;;;;;;OAOG;gBACS,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB;IAO3D;;;;OAIG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,MAAiB,GAAG,IAAI;IAK1D;;OAEG;IACH,cAAc,IAAI,IAAI;IAItB;;;OAGG;IACH,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAIxD;;;OAGG;IACH,qBAAqB,CAAC,WAAW,EAAE,kBAAkB,GAAG,IAAI;IAI5D;;;OAGG;IACH,sBAAsB,CAAC,WAAW,EAAE,mBAAmB,GAAG,IAAI;IAwI9D;;;;;;;;OAQG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAW,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,WAAW,CAAC;IASrH;;;;;;;;OAQG;IACG,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,WAAW,CAAC;IAiBtF;;;;;;;;OAQG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,WAAW,CAAC;IAiBrF;;;;;;;OAOG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,WAAW,CAAC;IAU7E;;;;;;;;OAQG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,WAAW,CAAC;CAS1F"}
|