@nilejs/nile 0.0.1 → 0.0.2

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 +351 -0
  2. package/package.json +3 -1
package/README.md CHANGED
@@ -1,3 +1,354 @@
1
1
  # 🌊 Nile
2
2
 
3
+ [![NPM Version](https://img.shields.io/npm/v/@nilejs/nile.svg)](https://www.npmjs.com/package/@nilejs/nile)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](./CONTRIBUTING.md)
6
+
7
+ ![TypeScript](https://img.shields.io/badge/TypeScript-3178C6?logo=typescript&logoColor=white)
8
+ ![Hono](https://img.shields.io/badge/Hono-E36002?logo=hono&logoColor=white)
9
+ ![Zod](https://img.shields.io/badge/Zod-3E67B1?logo=zod&logoColor=white)
10
+ ![Drizzle](https://img.shields.io/badge/Drizzle-C5F74F?logo=drizzle&logoColor=black)
11
+
3
12
  TypeScript-first, service and actions oriented backend framework for building modern, fast, safe and AI-ready backends with simplest developer experience possible.
13
+
14
+ You define actions, group them into services, and get a predictable API with validation, error handling, and schema export, no route definitions, no controllers, no middleware chains and rest api conventions to care about, just your business logic. And it's all AI agent-ready out of the box, progressively discoverable and tool calling ready with validation.
15
+
16
+ ## Install
17
+
18
+ > Or View Full Docs -> [nile-js.github.io/nile](https://nile-js.github.io/nile)
19
+
20
+ ```bash
21
+ bun add @nilejs/nile zod slang-ts
22
+ ```
23
+
24
+ ```bash
25
+ npm install @nilejs/nile zod slang-ts
26
+ ```
27
+
28
+ If using the database layer (`createModel`, `getZodSchema`):
29
+
30
+ ```bash
31
+ bun add drizzle-orm drizzle-zod
32
+ ```
33
+
34
+ ## Quick Start
35
+
36
+ ### 1. Define an action
37
+
38
+ ```typescript
39
+ // services/tasks/create.ts
40
+ import { Ok } from "slang-ts";
41
+ import z from "zod";
42
+ import { createAction, type Action } from "@nilejs/nile";
43
+
44
+ const createTaskSchema = z.object({
45
+ title: z.string().min(1, "Title is required"),
46
+ status: z.enum(["pending", "in-progress", "done"]).default("pending"),
47
+ });
48
+
49
+ const createTaskHandler = (data: Record<string, unknown>) => {
50
+ const task = {
51
+ id: crypto.randomUUID(),
52
+ title: data.title as string,
53
+ status: (data.status as string) ?? "pending",
54
+ };
55
+ return Ok({ task });
56
+ };
57
+
58
+ export const createTaskAction: Action = createAction({
59
+ name: "create",
60
+ description: "Create a new task",
61
+ validation: createTaskSchema,
62
+ handler: createTaskHandler,
63
+ });
64
+ ```
65
+
66
+ ### 2. Group actions into a service
67
+
68
+ ```typescript
69
+ // services/config.ts
70
+ import { type Services } from "@nilejs/nile";
71
+ import { createTaskAction } from "./tasks/create";
72
+ import { listTaskAction } from "./tasks/list";
73
+
74
+ export const services: Services = [
75
+ {
76
+ name: "tasks",
77
+ description: "Task management",
78
+ actions: [createTaskAction, listTaskAction],
79
+ },
80
+ ];
81
+ ```
82
+
83
+ ### 3. Start the server
84
+
85
+ ```typescript
86
+ // server.ts
87
+ import { createNileServer } from "@nilejs/nile";
88
+ import { services } from "./services/config";
89
+
90
+ const server = createNileServer({
91
+ serverName: "my-app",
92
+ services,
93
+ rest: {
94
+ baseUrl: "/api",
95
+ port: 8000,
96
+ },
97
+ });
98
+
99
+ if (server.rest) {
100
+ const { fetch } = server.rest.app;
101
+ Bun.serve({ fetch, port: 8000 });
102
+ console.log("Server running at http://localhost:8000");
103
+ }
104
+ ```
105
+
106
+ ### 4. Call it
107
+
108
+ ```bash
109
+ curl -X POST http://localhost:8000/api/services \
110
+ -H "Content-Type: application/json" \
111
+ -d '{
112
+ "intent": "execute",
113
+ "service": "tasks",
114
+ "action": "create",
115
+ "payload": { "title": "Ship it", "status": "pending" }
116
+ }'
117
+ ```
118
+
119
+ ```json
120
+ {
121
+ "status": true,
122
+ "message": "Action 'tasks.create' executed",
123
+ "data": {
124
+ "task": {
125
+ "id": "a1b2c3d4-...",
126
+ "title": "Ship it",
127
+ "status": "pending"
128
+ }
129
+ }
130
+ }
131
+ ```
132
+
133
+ ## Why Nile
134
+
135
+ **You write business logic. Nile handles the rest.**
136
+
137
+ Most backend frameworks make you think about HTTP verbs, route trees, middleware ordering, and error serialization before you write a single line of domain logic. Nile removes that ceremony. You define actions, plain functions that take data and return results, and they become callable over a single POST endpoint or other protocols such as web sockets or rpc within your codebase.
138
+
139
+ **Nothing crashes silently.** Every action handler returns `Ok(data)` or `Err(message)` using the Result pattern from [Slang Ts](github.com/Hussseinkizz/slang) Functional programming utilities library. So no unhandled exceptions, no try-catch spaghetti, no mystery 500s. Your control flow is predictable by design and safe.
140
+
141
+ **AI agents can call your API without adapters.** Every action with a Zod validation schema automatically exports its parameters as JSON Schema. An LLM can discover your services, read the schemas, and make tool calls, no custom integration code required.
142
+
143
+ **Your database, your choice.** Nile doesn't own your data layer. When you want structured DB access, nile works with drizzle orm and any databases it supports, postgres, pglite or sqlite and more, but also provides utilities like `createModel` for simplifying type-safe CRUD operations for any Drizzle table with auto-validation, error handling, and pagination built in to reduce boilerplate. You can also use any other database library or raw queries in your action handlers, it's all up to you.
144
+
145
+ **There's more to Nile** than just the core server, you get service and action based architecture, powerful hook system, structured logging and enforced error handling, rate limiting, CORS control, uploads, single context for dependency injection or sharing, and more. And you don't need a Phd to understand how to use any of them.
146
+
147
+ ## Core Concepts
148
+
149
+ Nile uses a single POST endpoint for everything. Instead of mapping HTTP verbs to routes, you send a JSON body with an **intent** that tells the server what you want to do.
150
+
151
+ Every request has the same shape:
152
+
153
+ ```typescript
154
+ {
155
+ intent: "explore" | "execute" | "schema",
156
+ service: string, // service name, or "*" for all
157
+ action: string, // action name, or "*" for all
158
+ payload: object // data for the action (use {} when not needed)
159
+ }
160
+ ```
161
+
162
+ Every response has the same shape:
163
+
164
+ ```typescript
165
+ {
166
+ status: boolean, // true = success, false = error
167
+ message: string, // human-readable description
168
+ data: object // result payload, or {} on error
169
+ }
170
+ ```
171
+
172
+ ### Explore, discover what's available
173
+
174
+ List all services:
175
+
176
+ ```bash
177
+ curl -X POST http://localhost:8000/api/services \
178
+ -H "Content-Type: application/json" \
179
+ -d '{ "intent": "explore", "service": "*", "action": "*", "payload": {} }'
180
+ ```
181
+
182
+ ```json
183
+ {
184
+ "status": true,
185
+ "message": "Available services",
186
+ "data": {
187
+ "result": [
188
+ {
189
+ "name": "tasks",
190
+ "description": "Task management",
191
+ "actions": ["create", "list"]
192
+ }
193
+ ]
194
+ }
195
+ }
196
+ ```
197
+
198
+ Drill into a service to see its actions:
199
+
200
+ ```bash
201
+ curl -X POST http://localhost:8000/api/services \
202
+ -H "Content-Type: application/json" \
203
+ -d '{ "intent": "explore", "service": "tasks", "action": "*", "payload": {} }'
204
+ ```
205
+
206
+ ```json
207
+ {
208
+ "status": true,
209
+ "message": "Actions for 'tasks'",
210
+ "data": {
211
+ "result": [
212
+ {
213
+ "name": "create",
214
+ "description": "Create a new task",
215
+ "isProtected": false,
216
+ "validation": true
217
+ }
218
+ ]
219
+ }
220
+ }
221
+ ```
222
+
223
+ ### Execute, call an action
224
+
225
+ This is the same call shown in Quick Start. Send `"intent": "execute"` with the service, action, and payload. The action's Zod schema validates the payload before the handler runs. If validation fails, you get a clear error:
226
+
227
+ ```json
228
+ {
229
+ "status": false,
230
+ "message": "Validation failed: title - Required",
231
+ "data": {}
232
+ }
233
+ ```
234
+
235
+ ### Schema, get JSON Schema for actions
236
+
237
+ Fetch the validation schema for any action as JSON Schema. This is what makes Nile AI-ready, an agent can read these schemas to know exactly what parameters an action accepts.
238
+
239
+ ```bash
240
+ curl -X POST http://localhost:8000/api/services \
241
+ -H "Content-Type: application/json" \
242
+ -d '{ "intent": "schema", "service": "tasks", "action": "create", "payload": {} }'
243
+ ```
244
+
245
+ ```json
246
+ {
247
+ "status": true,
248
+ "message": "Schema for 'tasks.create'",
249
+ "data": {
250
+ "create": {
251
+ "type": "object",
252
+ "properties": {
253
+ "title": { "type": "string", "minLength": 1 },
254
+ "status": { "type": "string", "enum": ["pending", "in-progress", "done"], "default": "pending" }
255
+ },
256
+ "required": ["title"]
257
+ }
258
+ }
259
+ }
260
+ ```
261
+
262
+ Use `"service": "*", "action": "*"` to get schemas for every action across all services in one call.
263
+
264
+ ### Hooks, intercept and transform
265
+
266
+ Hooks let you run logic before or after an action executes. They work at two levels:
267
+
268
+ **Per-action hooks** point to other registered actions. A hook definition is just a reference, `{ service, action, isCritical }`, so any action can serve as a hook for any other action.
269
+
270
+ ```typescript
271
+ export const createTaskAction: Action = createAction({
272
+ name: "create",
273
+ description: "Create a new task",
274
+ validation: createTaskSchema,
275
+ handler: createTaskHandler,
276
+ hooks: {
277
+ before: [
278
+ { service: "audit", action: "logAccess", isCritical: false }
279
+ ],
280
+ after: [
281
+ { service: "notifications", action: "notify", isCritical: false }
282
+ ]
283
+ },
284
+ });
285
+ ```
286
+
287
+ Before hooks run sequentially and chain, each hook's output becomes the next hook's input. After hooks work the same way, receiving the handler's result.
288
+
289
+ When `isCritical` is `true`, a hook failure stops the pipeline. When `false`, failures are logged and skipped.
290
+
291
+ **Global hooks** run on every action. Define them in your server config:
292
+
293
+ ```typescript
294
+ const server = createNileServer({
295
+ serverName: "my-app",
296
+ services,
297
+ onBeforeActionHandler: ({ nileContext, action, payload }) => {
298
+ // runs before every action, auth checks, logging, etc.
299
+ return Ok(payload);
300
+ },
301
+ onAfterActionHandler: ({ nileContext, action, payload, result }) => {
302
+ // runs after every action, transforms, auditing, etc.
303
+ return result;
304
+ },
305
+ });
306
+ ```
307
+
308
+ The full execution pipeline runs in this order:
309
+
310
+ ```txt
311
+ Global Before Hook
312
+ -> Per-Action Before Hooks (sequential)
313
+ -> Validation (Zod)
314
+ -> Handler
315
+ -> Per-Action After Hooks (sequential)
316
+ -> Global After Hook
317
+ -> Response
318
+ ```
319
+
320
+ Any step returning `Err` short-circuits the pipeline.
321
+
322
+ ## Project Structure
323
+
324
+ ```txt
325
+ my-api/
326
+ server.ts
327
+ services/
328
+ config.ts
329
+ tasks/
330
+ create.ts
331
+ list.ts
332
+ get.ts
333
+ db/
334
+ schema.ts
335
+ client.ts
336
+ models/
337
+ tasks.ts
338
+ ```
339
+
340
+ ## Contributing
341
+
342
+ > First developed by Hussein Kizz at [Nile Squad Labz](https://nilesquad.com) to power our own B2B saas products and services, and now open-sourced for the community. Over 1 year in the making, to now powering Agentic backends and open for community contributions.
343
+
344
+ Contributions are welcome.
345
+
346
+ 1. Fork the repository
347
+ 2. Create your feature branch (`git checkout -b feature/your-feature`)
348
+ 3. Commit your changes (`git commit -m 'Add your feature'`)
349
+ 4. Push to the branch (`git push origin feature/your-feature`)
350
+ 5. Open a Pull Request
351
+
352
+ ## License
353
+
354
+ MIT
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@nilejs/nile",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
+ "description": "TypeScript-first, service and actions oriented backend framework",
8
9
  "exports": {
9
10
  ".": {
10
11
  "types": "./dist/index.d.ts",
@@ -22,6 +23,7 @@
22
23
  "framework",
23
24
  "backend",
24
25
  "api",
26
+ "hono",
25
27
  "server",
26
28
  "typescript",
27
29
  "service",