karos 1.0.0 → 1.0.3

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.
Files changed (2) hide show
  1. package/README.md +127 -63
  2. package/package.json +14 -6
package/README.md CHANGED
@@ -1,103 +1,167 @@
1
- karos
2
- =====
1
+ # Karos
3
2
 
4
3
  **Opinionated, minimal API response & error standardization for Express.**
5
4
 
6
- Karos enforces predictable JSON response shapes and centralized error handling in Node.js APIs — without adding business logic, configuration, or framework lock-in.
5
+ Karos enforces a single, predictable JSON response contract across your entire Node.js API — without adding business logic, configuration, or framework lock-in.
7
6
 
8
- The Problem
9
- -----------
7
+ ---
8
+
9
+ ## 🚫 The Problem
10
10
 
11
11
  In most Express backends:
12
+ - ❌ Every route formats responses differently
13
+ - ❌ Errors are sometimes strings, sometimes objects
14
+ - ❌ Status codes are inconsistent
15
+ - ❌ Frontend logic becomes fragile and conditional-heavy
16
+ - ❌ Teams rewrite the same response boilerplate in every project
17
+
18
+ **There is no enforced backend–frontend contract.**
19
+
20
+ ## ✅ The Solution
21
+
22
+ Karos fixes exactly this problem — nothing more, nothing less. It enforces **one response contract** for your entire API.
23
+
24
+ ### Success Response
25
+ ```json
26
+ {
27
+ "success": true,
28
+ "data": {
29
+ "id": 123,
30
+ "name": "Alice"
31
+ }
32
+ }
33
+ ```
34
+
35
+ ### Error Response
36
+ ```json
37
+ {
38
+ "success": false,
39
+ "error": {
40
+ "code": "NOT_FOUND",
41
+ "message": "User not found"
42
+ }
43
+ }
44
+ ```
12
45
 
13
- * Every route formats responses differently
14
-
15
- * Errors are sometimes strings, sometimes objects
16
-
17
- * Status codes are inconsistent
18
-
19
- * Frontend logic becomes fragile and full of conditionals
20
-
21
- * Teams re-implement the same response boilerplate in every project
22
-
46
+ No exceptions. No special cases.
23
47
 
24
- There is no shared contract between backend and frontend.
48
+ ---
25
49
 
26
- What Karos Enforces
27
- -------------------
50
+ ## 📦 Installation
28
51
 
29
- Karos enforces **one response contract** for your entire API.
52
+ ```bash
53
+ npm install karos
54
+ ```
30
55
 
31
- Success
32
- -------
56
+ ---
33
57
 
34
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` json{ "success": true, "data": {} } `
58
+ ## 🚀 Quick Start (60 seconds)
35
59
 
36
- Error
37
- -----
60
+ Karos replaces manual `try/catch` blocks and inconsistent response formatting.
38
61
 
39
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` json{ "success": false, "error": { "code": "NOT_FOUND", "message": "User not found" } } `
62
+ ### 1. Basic Setup
40
63
 
41
- No exceptions. No special cases.
64
+ ```js
65
+ const express = require('express');
66
+ const { ok, notFoundError, errorHandler } = require('karos');
67
+
68
+ const app = express();
69
+ app.use(express.json());
70
+
71
+ // --- Your Routes ---
72
+
73
+ app.get('/users/:id', async (req, res) => {
74
+ const user = await db.findUser(req.params.id);
75
+
76
+ if (!user) {
77
+ // Throws automatically -> Middleware catches it
78
+ notFoundError('User not found');
79
+ }
80
+
81
+ // Returns standardized 200 OK
82
+ ok(res, user);
83
+ });
84
+
85
+ // --- ONE middleware catches everything ---
86
+ // Must be placed after all routes
87
+ app.use(errorHandler);
42
88
 
43
- Install + Usage (60 seconds)
44
- ----------------------------
89
+ app.listen(3000, () => console.log('Server running on port 3000'));
90
+ ```
45
91
 
46
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` bashnpm install karos `
92
+ **Zero try/catch needed.** If your DB crashes, Karos catches it and returns a clean 500 `INTERNAL_ERROR`.
47
93
 
48
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` const express = require('express'); const { ok, notFoundError, errorHandler } = require('karos'); const app = express(); app.use(express.json()); // Your routes here... app.get('/users/:id', async (req, res) => { const user = await db.findUser(req.params.id); if (!user) { notFoundError('User not found'); // Throws → middleware catches } ok(res, user); }); // ONE middleware catches everything app.use(errorHandler); app.listen(3000); `
94
+ ---
49
95
 
50
- No try/catch. No per-route error formatting. One consistent API surface.
96
+ ## 📚 Core API
51
97
 
52
- Core API
53
- --------
98
+ ### `ok(res, data, message?, meta?)`
54
99
 
55
- **ok(res, data, message?, meta?)**Formats a successful response.
100
+ Formats a successful response.
56
101
 
57
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` ok(res, user); ok(res, user, 'User fetched'); `
102
+ | Param | Type | Required | Description |
103
+ | :--- | :--- | :--- | :--- |
104
+ | `res` | `Response` | **Yes** | Express Response object |
105
+ | `data` | `any` | **Yes** | The payload (object, array, string) |
106
+ | `message` | `string` | No | Optional success message |
107
+ | `meta` | `object` | No | Optional metadata (e.g., pagination info) |
58
108
 
59
- **notFoundError(message?)**Prebuilt helpers (autocomplete-safe).
109
+ **Example:**
110
+ ```js
111
+ ok(res, user);
112
+ ok(res, user, 'User fetched successfully');
113
+ ```
60
114
 
61
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` notFoundError(); // 404 "Not found" validationError('Invalid email'); // 400 unauthorizedError(); // 401 `
115
+ ---
62
116
 
63
- **errorHandler**Express middleware that:
117
+ ### Error Helpers
64
118
 
65
- * Catches all thrown Karos errors
66
-
67
- * Formats unknown errors as INTERNAL\_ERROR
68
-
69
- * Preserves correct HTTP status codes
70
-
119
+ Prebuilt helpers that throw standardized errors. These stop execution immediately, so you don't need `return`.
71
120
 
72
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` app.use(errorHandler); // Last, after all routes `
121
+ | Helper Function | HTTP Status | Error Code |
122
+ | :--- | :--- | :--- |
123
+ | `notFoundError(msg)` | **404** | `NOT_FOUND` |
124
+ | `validationError(msg)` | **400** | `VALIDATION_FAILED` |
125
+ | `unauthorizedError(msg)` | **401** | `UNAUTHORIZED` |
126
+ | `forbiddenError(msg)` | **403** | `FORBIDDEN` |
127
+ | `conflictError(msg)` | **409** | `CONFLICT` |
128
+ | `internalError(msg)` | **500** | `INTERNAL_ERROR` |
73
129
 
74
- Error Codes (Autocomplete)
75
- --------------------------
130
+ **Example:**
131
+ ```js
132
+ if (emailInvalid) validationError('Invalid email format');
133
+ if (!isAdmin) forbiddenError('Admin access required');
134
+ ```
76
135
 
77
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` ErrorCode.NOT_FOUND // 404 ErrorCode.VALIDATION_FAILED // 400 ErrorCode.UNAUTHORIZED // 401 ErrorCode.FORBIDDEN // 403 ErrorCode.CONFLICT // 409 ErrorCode.INTERNAL_ERROR // 500 `
136
+ ---
78
137
 
79
- You control: message, status, structured details.Karos only enforces the response shape.
138
+ ### `errorHandler`
80
139
 
81
- Database Errors (Auto-handled)
82
- ------------------------------
140
+ Express middleware that:
141
+ 1. Catches all thrown Karos errors.
142
+ 2. Catches unexpected crashes (DB down, undefined variables).
143
+ 3. Formats unknown errors as `INTERNAL_ERROR` (500).
83
144
 
84
- Plain textANTLR4BashCC#CSSCoffeeScriptCMakeDartDjangoDockerEJSErlangGitGoGraphQLGroovyHTMLJavaJavaScriptJSONJSXKotlinLaTeXLessLuaMakefileMarkdownMATLABMarkupObjective-CPerlPHPPowerShell.propertiesProtocol BuffersPythonRRubySass (Sass)Sass (Scss)SchemeSQLShellSwiftSVGTSXTypeScriptWebAssemblyYAMLXML` app.get('/orders', async (req, res) => { const orders = await prisma.order.findMany(); // Crashes? → 500 ok(res, orders); }); `
145
+ ```js
146
+ // Add this as the very last middleware in your app
147
+ app.use(errorHandler);
148
+ ```
85
149
 
86
- **Zero try/catch needed.** Middleware formats everything.
150
+ ---
87
151
 
88
- What Karos Does NOT Do (By Design)
89
- ----------------------------------
152
+ ## TypeScript Support
90
153
 
91
- No request validation❌ No authentication helpers❌ No logging❌ No database handling❌ No config files❌ No framework adapters (Fastify, Hono, etc.)
154
+ Karos is written in TypeScript and includes full type definitions out of the box.
92
155
 
93
- **Pure formatting layer only.** Your business logic stays yours.
156
+ - Full type safety
157
+ - ✅ Autocomplete-safe error codes
94
158
 
95
- TypeScript ✅
96
- ------------
159
+ ```ts
160
+ import { ok, ErrorCode } from 'karos';
161
+ ```
97
162
 
98
- Full types + autocomplete for ErrorCode.NOT\_FOUND, response shapes.
163
+ ---
99
164
 
100
- License
101
- -------
165
+ ## License
102
166
 
103
- MIT
167
+ MIT
package/package.json CHANGED
@@ -1,20 +1,28 @@
1
1
  {
2
2
  "name": "karos",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "description": "Opinionated API response and error handling for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
- "files": ["dist"],
7
+ "files": [
8
+ "dist"
9
+ ],
8
10
  "scripts": {
9
11
  "build": "tsup src/index.ts --format cjs --dts",
10
12
  "dev": "tsup src/index.ts --format cjs --watch --dts",
11
13
  "test": "echo \"Error: no test specified\" && exit 1"
12
14
  },
13
- "keywords": ["api", "express", "error-handling", "node", "backend"],
14
- "author": "Krishna Shrivastava",
15
+ "keywords": [
16
+ "api",
17
+ "express",
18
+ "error-handling",
19
+ "node",
20
+ "backend"
21
+ ],
22
+ "author": "Krishna Shrivastava",
15
23
  "license": "MIT",
16
- "repository": "https://github.com/Krishna-Shrivastava-1/Karos",
17
- "bugs": "https://github.com/Krishna-Shrivastava-1/Karos/issues",
24
+ "repository": "https://github.com/Krishna-Shrivastava-1/Karos",
25
+ "bugs": "https://github.com/Krishna-Shrivastava-1/Karos/issues",
18
26
  "type": "commonjs",
19
27
  "devDependencies": {
20
28
  "@types/express": "^5.0.6",