nural 0.1.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/LICENSE +21 -0
- package/README.md +272 -0
- package/dist/index.cjs +1005 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +458 -0
- package/dist/index.d.ts +458 -0
- package/dist/index.js +973 -0
- package/dist/index.js.map +1 -0
- package/package.json +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chetan Joshi
|
|
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,272 @@
|
|
|
1
|
+
# Nural
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<picture>
|
|
5
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/logo-dark-mode.png">
|
|
6
|
+
<source media="(prefers-color-scheme: light)" srcset="assets/logo-light-mode.png">
|
|
7
|
+
<img alt="Nural Logo" src="assets/logo-light-mode.png" width="200">
|
|
8
|
+
</picture>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<strong>The intelligent, schema-first REST framework for Node.js</strong>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="https://www.npmjs.com/package/nural"><img src="https://img.shields.io/npm/v/nural.svg" alt="npm version"></a>
|
|
17
|
+
<a href="https://github.com/ErrorX407/nural/actions/workflows/ci.yml"><img src="https://github.com/ErrorX407/nural/actions/workflows/ci.yml/badge.svg" alt="Build Status"></a>
|
|
18
|
+
<img src="https://img.shields.io/npm/l/nural.svg" alt="license">
|
|
19
|
+
<a href="https://www.npmjs.com/package/nural"><img src="https://img.shields.io/npm/dm/nural.svg" alt="Downloads"></a>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- ⚡ **Type-Safe** – End-to-end TypeScript inference
|
|
27
|
+
- 📝 **Auto Documentation** – OpenAPI spec & Scalar UI out-of-the-box
|
|
28
|
+
- 🛡️ **Validation** – Zod-powered input/output validation
|
|
29
|
+
- 🔄 **Response Mapping** – Auto-strip unlisted fields (no data leaks!)
|
|
30
|
+
- 🔌 **Multi-Framework** – Works with Express & Fastify
|
|
31
|
+
- 🎯 **Middleware Context** – Type-safe context injection
|
|
32
|
+
- 🔒 **Built-in Security** – CORS & Helmet with zero dependencies
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install nural express
|
|
40
|
+
# or
|
|
41
|
+
npm install nural fastify
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { Nural, createRoute, z } from "nural";
|
|
50
|
+
|
|
51
|
+
// Create app
|
|
52
|
+
const app = new Nural({ framework: "express", docs: true });
|
|
53
|
+
|
|
54
|
+
// Define a route
|
|
55
|
+
const helloRoute = createRoute({
|
|
56
|
+
method: "GET",
|
|
57
|
+
path: "/hello",
|
|
58
|
+
summary: "Health check",
|
|
59
|
+
responses: {
|
|
60
|
+
200: z.object({ message: z.string() }),
|
|
61
|
+
},
|
|
62
|
+
handler: async () => {
|
|
63
|
+
return { message: "Hello from Nural!", secret: "HIDDEN" };
|
|
64
|
+
// ↑ 'secret' is auto-stripped from response!
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Register and start
|
|
69
|
+
app.register([helloRoute]);
|
|
70
|
+
app.start(3000);
|
|
71
|
+
// 🚀 Server: http://localhost:3000
|
|
72
|
+
// 📚 Docs: http://localhost:3000/docs
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Validation
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const userRoute = createRoute({
|
|
81
|
+
method: "POST",
|
|
82
|
+
path: "/users",
|
|
83
|
+
request: {
|
|
84
|
+
body: z.object({
|
|
85
|
+
name: z.string().min(2),
|
|
86
|
+
email: z.string().email(),
|
|
87
|
+
}),
|
|
88
|
+
},
|
|
89
|
+
responses: {
|
|
90
|
+
201: z.object({ id: z.string(), name: z.string() }),
|
|
91
|
+
},
|
|
92
|
+
handler: async ({ body }) => {
|
|
93
|
+
// body is typed as { name: string, email: string }
|
|
94
|
+
return { id: "uuid", name: body.name };
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Middleware
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { defineMiddleware } from "nural";
|
|
105
|
+
|
|
106
|
+
const authMiddleware = defineMiddleware(async (req) => {
|
|
107
|
+
const token = req.headers.authorization;
|
|
108
|
+
if (!token) throw new Error("Unauthorized");
|
|
109
|
+
return { user: { id: "123", role: "admin" } };
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const meRoute = createRoute({
|
|
113
|
+
method: "GET",
|
|
114
|
+
path: "/me",
|
|
115
|
+
middleware: [authMiddleware],
|
|
116
|
+
responses: { 200: z.object({ id: z.string(), role: z.string() }) },
|
|
117
|
+
handler: async ({ user }) => {
|
|
118
|
+
// ↑ 'user' is inferred from middleware!
|
|
119
|
+
return user;
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## CORS & Helmet (Built-in Security)
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
// Enable with defaults
|
|
130
|
+
const app = new Nural({
|
|
131
|
+
cors: true,
|
|
132
|
+
helmet: true,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Or with full configuration
|
|
136
|
+
const app = new Nural({
|
|
137
|
+
cors: {
|
|
138
|
+
origin: "https://example.com",
|
|
139
|
+
methods: ["GET", "POST"],
|
|
140
|
+
credentials: true,
|
|
141
|
+
},
|
|
142
|
+
helmet: {
|
|
143
|
+
contentSecurityPolicy: false,
|
|
144
|
+
hsts: { maxAge: 31536000, includeSubDomains: true },
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Unified Exception System
|
|
154
|
+
|
|
155
|
+
Throw standard exceptions anywhere in your code, and Nural automatically formats them into standard JSON error responses.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { NotFoundException, UnauthorizedException } from "nural";
|
|
159
|
+
|
|
160
|
+
// In your service or handler
|
|
161
|
+
if (!user) {
|
|
162
|
+
throw new NotFoundException("User not found");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!token) {
|
|
166
|
+
throw new UnauthorizedException("Missing token");
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Result:**
|
|
171
|
+
|
|
172
|
+
```json
|
|
173
|
+
{
|
|
174
|
+
"statusCode": 404,
|
|
175
|
+
"message": "User not found",
|
|
176
|
+
"error": "Not Found",
|
|
177
|
+
"timestamp": "2026-02-08T12:00:00.000Z"
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
````
|
|
184
|
+
|
|
185
|
+
## Global Error Handler
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
// Enable with defaults (smart error mapping)
|
|
189
|
+
const app = new Nural({ errorHandler: true });
|
|
190
|
+
|
|
191
|
+
// Custom error handler (e.g., Sentry integration)
|
|
192
|
+
const app = new Nural({
|
|
193
|
+
errorHandler: (ctx) => {
|
|
194
|
+
Sentry.captureException(ctx.error);
|
|
195
|
+
return {
|
|
196
|
+
status: 500,
|
|
197
|
+
body: { error: "Something went wrong" },
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Full configuration
|
|
203
|
+
const app = new Nural({
|
|
204
|
+
errorHandler: {
|
|
205
|
+
handler: customHandler,
|
|
206
|
+
includeStack: process.env.NODE_ENV !== "production",
|
|
207
|
+
logErrors: true,
|
|
208
|
+
logger: (err, ctx) => winston.error(err.message, { path: ctx.path }),
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
````
|
|
212
|
+
|
|
213
|
+
**Smart defaults:** Automatically maps `"unauthorized"` → 401, `"not found"` → 404, Zod errors → 400.
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## Configuration
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
const app = new Nural({
|
|
221
|
+
framework: "fastify", // or 'express'
|
|
222
|
+
cors: true, // Enable CORS
|
|
223
|
+
helmet: true, // Enable security headers
|
|
224
|
+
docs: {
|
|
225
|
+
enabled: true,
|
|
226
|
+
title: "My API",
|
|
227
|
+
version: "2.0.0",
|
|
228
|
+
description: "My awesome API",
|
|
229
|
+
path: "/api-docs",
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## API Reference
|
|
237
|
+
|
|
238
|
+
### `createRoute(config)`
|
|
239
|
+
|
|
240
|
+
| Property | Type | Description |
|
|
241
|
+
| ---------------- | --------------------------- | ------------------------------ |
|
|
242
|
+
| `method` | `HttpMethod` | GET, POST, PUT, PATCH, DELETE |
|
|
243
|
+
| `path` | `string` | Route path (supports `:param`) |
|
|
244
|
+
| `summary` | `string?` | Short description for docs |
|
|
245
|
+
| `description` | `string?` | Detailed description |
|
|
246
|
+
| `tags` | `string[]?` | Grouping for docs |
|
|
247
|
+
| `middleware` | `array?` | Middleware functions |
|
|
248
|
+
| `request.params` | `ZodSchema?` | Path params validation |
|
|
249
|
+
| `request.query` | `ZodSchema?` | Query params validation |
|
|
250
|
+
| `request.body` | `ZodSchema?` | Body validation |
|
|
251
|
+
| `responses` | `Record<number, ZodSchema>` | Response schemas |
|
|
252
|
+
| `handler` | `function` | Route handler |
|
|
253
|
+
|
|
254
|
+
### `defineMiddleware(fn)`
|
|
255
|
+
|
|
256
|
+
Creates a type-safe middleware that injects context into handlers.
|
|
257
|
+
|
|
258
|
+
### `new Nural(config)`
|
|
259
|
+
|
|
260
|
+
| Property | Type | Default | Description |
|
|
261
|
+
| -------------- | ------------------------------ | ----------- | ------------------------ |
|
|
262
|
+
| `framework` | `'express' \| 'fastify'` | `'express'` | Server framework |
|
|
263
|
+
| `docs` | `boolean \| DocsConfig` | `true` | Documentation settings |
|
|
264
|
+
| `cors` | `boolean \| CorsConfig` | `false` | CORS middleware settings |
|
|
265
|
+
| `helmet` | `boolean \| HelmetConfig` | `false` | Security headers |
|
|
266
|
+
| `errorHandler` | `boolean \| fn \| ErrorConfig` | `true` | Global error handling |
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
MIT © [Chetan Joshi](https://github.com/ErrorX407)
|