@nexo-alpha/hapi 0.1.0 → 0.1.1
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/README.md +67 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# @nexo-alpha/hapi
|
|
2
|
+
|
|
3
|
+
Turns a [`@nexo-alpha/core`](https://www.npmjs.com/package/@nexo-alpha/core) `NexoApplication`'s declared APIs into a real, running [Hapi.js](https://hapi.dev) HTTP server. This is the framework's first package with a real external runtime dependency (`@hapi/hapi`) — everything before it stays dependency-free by design.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @nexo-alpha/hapi @nexo-alpha/core @hapi/hapi
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Why
|
|
12
|
+
|
|
13
|
+
Nexo's application model is otherwise purely declarative — `NexoApi` describes an endpoint's method, path, and metadata, but nothing runs it. `@nexo-alpha/hapi` is the adapter that turns a `handler`-bearing `NexoApi` into an actual route, keeping `@nexo-alpha/core` itself Hapi-agnostic.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createApplication } from "@nexo-alpha/core";
|
|
19
|
+
import { startHapiServer } from "@nexo-alpha/hapi";
|
|
20
|
+
|
|
21
|
+
const app = createApplication({ name: "shop" });
|
|
22
|
+
|
|
23
|
+
app.module({
|
|
24
|
+
name: "hello",
|
|
25
|
+
apis: [
|
|
26
|
+
{
|
|
27
|
+
name: "sayHello",
|
|
28
|
+
method: "GET",
|
|
29
|
+
path: "/hello",
|
|
30
|
+
handler: async () => ({ message: "Hello from Nexo" })
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const server = await startHapiServer(app, { port: 3000 });
|
|
36
|
+
console.log(`Listening on ${server.info.uri}`);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
curl http://localhost:3000/hello
|
|
41
|
+
# {"message":"Hello from Nexo"}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## What's here
|
|
45
|
+
|
|
46
|
+
- **`createHapiServer(app, options?)`** — builds a `Hapi.server(...)` and registers a route for every API that has a `handler`. Path params use Express-style `:id` in `NexoApi.path` (matching the rest of Nexo's examples) and are converted to Hapi's `{id}` syntax automatically.
|
|
47
|
+
- **`startHapiServer(app, options?)`** — `createHapiServer` plus `server.start()`.
|
|
48
|
+
- **`toHapiPath(path)`** — the `:id` → `{id}` path converter, exported directly if you need it.
|
|
49
|
+
- A handler receives a plain `NexoRequestContext` (`params`, `query`, `payload`, `headers`) — no Hapi types leak into `@nexo-alpha/core`. Return a value to send it as the response (objects are serialized to JSON automatically); return `undefined` for a `204`.
|
|
50
|
+
|
|
51
|
+
## Design notes
|
|
52
|
+
|
|
53
|
+
- **APIs without a `handler` get no route.** They stay descriptive-only, exactly as they appear in `@nexo-alpha/context`'s manifest and `@nexo-alpha/cli`'s output.
|
|
54
|
+
- **`HEAD` is not registered as an explicit route** — Hapi generates `HEAD` responses from `GET` routes automatically and rejects `HEAD` as an explicit method.
|
|
55
|
+
- **No request validation or auth yet.** Every handler-backed API is wired with no input validation and no authentication layer — that's Phase 5 ("Safe Development Operations") territory, not this package.
|
|
56
|
+
|
|
57
|
+
## Related packages
|
|
58
|
+
|
|
59
|
+
- [`@nexo-alpha/core`](https://www.npmjs.com/package/@nexo-alpha/core) — the application/module model, including `NexoRequestContext` and `NexoApiHandler`
|
|
60
|
+
|
|
61
|
+
## Status
|
|
62
|
+
|
|
63
|
+
**v0.1-alpha.** No request validation, no authentication, no lifecycle wiring to `NexoApplication.start()`/`stop()` yet — creating and starting the Hapi server is a separate step from the application's own lifecycle.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT
|