@ttoss/http-server 0.1.16 → 0.3.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/README.md +84 -15
- package/dist/esm/index.js +24 -3
- package/dist/index.d.ts +39 -3
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ttoss/http-server
|
|
2
2
|
|
|
3
|
-
HTTP server
|
|
3
|
+
Lightweight HTTP server built on [Koa](https://koajs.com/) for the ttoss ecosystem.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -8,7 +8,7 @@ HTTP server for Node.js for ttoss ecosystem.
|
|
|
8
8
|
pnpm add @ttoss/http-server
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { App, Router, bodyParser, cors } from '@ttoss/http-server';
|
|
@@ -16,29 +16,98 @@ import { App, Router, bodyParser, cors } from '@ttoss/http-server';
|
|
|
16
16
|
const app = new App();
|
|
17
17
|
|
|
18
18
|
app.use(cors());
|
|
19
|
-
|
|
20
19
|
app.use(bodyParser());
|
|
21
20
|
|
|
22
|
-
const
|
|
21
|
+
const router = new Router();
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
ctx.body = '
|
|
23
|
+
router.get('/health', (ctx) => {
|
|
24
|
+
ctx.body = { status: 'ok' };
|
|
26
25
|
});
|
|
27
26
|
|
|
28
|
-
app.use(
|
|
29
|
-
|
|
27
|
+
app.use(router.routes());
|
|
30
28
|
app.use(router.allowedMethods());
|
|
31
29
|
|
|
32
30
|
app.listen(3000, () => {
|
|
33
|
-
console.log('Server
|
|
31
|
+
console.log('Server running on http://localhost:3000');
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Health Check Endpoint
|
|
36
|
+
|
|
37
|
+
Add a health check endpoint with a single line:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { App, addHealthCheck } from '@ttoss/http-server';
|
|
41
|
+
|
|
42
|
+
const app = new App();
|
|
43
|
+
|
|
44
|
+
addHealthCheck({ app });
|
|
45
|
+
// or with custom path: addHealthCheck({ app, path: '/healthz' });
|
|
46
|
+
|
|
47
|
+
app.listen(3000);
|
|
48
|
+
// GET /health returns { status: 'ok' }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Core Features
|
|
52
|
+
|
|
53
|
+
### Route Parameters
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
router.get('/users/:id', (ctx) => {
|
|
57
|
+
const { id } = ctx.params;
|
|
58
|
+
ctx.body = { userId: id };
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Request Body Parsing
|
|
63
|
+
|
|
64
|
+
JSON and form-urlencoded data are automatically parsed when using `bodyParser()`:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
router.post('/users', (ctx) => {
|
|
68
|
+
const userData = ctx.request.body;
|
|
69
|
+
ctx.body = { created: userData };
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### File Uploads
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { multer } from '@ttoss/http-server';
|
|
77
|
+
import type { MulterFile } from '@ttoss/http-server';
|
|
78
|
+
|
|
79
|
+
const upload = multer();
|
|
80
|
+
|
|
81
|
+
router.post('/upload', upload.single('file'), (ctx) => {
|
|
82
|
+
const file = ctx.file as MulterFile | undefined;
|
|
83
|
+
ctx.body = {
|
|
84
|
+
filename: file?.originalname,
|
|
85
|
+
size: file?.size,
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Error Handling
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
app.use(async (ctx, next) => {
|
|
94
|
+
try {
|
|
95
|
+
await next();
|
|
96
|
+
} catch (error) {
|
|
97
|
+
ctx.status = error.status || 500;
|
|
98
|
+
ctx.body = { error: error.message };
|
|
99
|
+
}
|
|
34
100
|
});
|
|
35
101
|
```
|
|
36
102
|
|
|
37
|
-
## API
|
|
103
|
+
## API Reference
|
|
38
104
|
|
|
39
|
-
|
|
105
|
+
All exports are re-exported from established Koa ecosystem packages:
|
|
40
106
|
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
107
|
+
- **`App`** - [Koa application](https://github.com/koajs/koa)
|
|
108
|
+
- **`Router`** - [Koa router](https://github.com/koajs/router) for routing
|
|
109
|
+
- **`bodyParser`** - [Koa body parser](https://github.com/koajs/bodyparser) for JSON/form parsing
|
|
110
|
+
- **`cors`** - [Koa CORS](https://github.com/koajs/cors) for cross-origin requests
|
|
111
|
+
- **`multer`** - [Koa multer](https://github.com/koajs/multer) for file uploads
|
|
112
|
+
- **`addHealthCheck({ app, path? })`** - Adds a health endpoint (defaults to `/health`) returning `{ status: 'ok' }`
|
|
113
|
+
- **`MulterFile`** (type) - File type for uploaded files
|
package/dist/esm/index.js
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
1
|
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", {
|
|
4
|
+
value,
|
|
5
|
+
configurable: true
|
|
6
|
+
});
|
|
2
7
|
|
|
3
8
|
// src/index.ts
|
|
4
9
|
import { bodyParser } from "@koa/bodyparser";
|
|
5
|
-
import App from "koa";
|
|
6
|
-
import Router from "@koa/router";
|
|
7
10
|
import cors from "@koa/cors";
|
|
8
11
|
import multer from "@koa/multer";
|
|
9
|
-
|
|
12
|
+
import Router2 from "@koa/router";
|
|
13
|
+
import App from "koa";
|
|
14
|
+
|
|
15
|
+
// src/addHealthCheck.ts
|
|
16
|
+
import Router from "@koa/router";
|
|
17
|
+
var addHealthCheck = /* @__PURE__ */__name(({
|
|
18
|
+
app,
|
|
19
|
+
path = "/health"
|
|
20
|
+
}) => {
|
|
21
|
+
const router = new Router();
|
|
22
|
+
router.get(path, ctx => {
|
|
23
|
+
ctx.body = {
|
|
24
|
+
status: "ok"
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
app.use(router.routes());
|
|
28
|
+
app.use(router.allowedMethods());
|
|
29
|
+
}, "addHealthCheck");
|
|
30
|
+
export { App, Router2 as Router, addHealthCheck, bodyParser, cors, multer };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
export { bodyParser } from '@koa/bodyparser';
|
|
2
|
-
export { default as App } from 'koa';
|
|
3
|
-
export { default as Router } from '@koa/router';
|
|
4
2
|
export { default as cors } from '@koa/cors';
|
|
5
|
-
export { default as multer } from '@koa/multer';
|
|
3
|
+
export { File as MulterFile, default as multer } from '@koa/multer';
|
|
4
|
+
export { default as Router } from '@koa/router';
|
|
5
|
+
import App from 'koa';
|
|
6
|
+
export { default as App } from 'koa';
|
|
7
|
+
|
|
8
|
+
interface AddHealthCheckOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The Koa application instance to attach the health check to.
|
|
11
|
+
*/
|
|
12
|
+
app: App;
|
|
13
|
+
/**
|
|
14
|
+
* The path for the health endpoint.
|
|
15
|
+
* @default '/health'
|
|
16
|
+
*/
|
|
17
|
+
path?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Adds a health check endpoint to the Koa application.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { App, addHealthCheck } from '@ttoss/http-server';
|
|
25
|
+
*
|
|
26
|
+
* const app = new App();
|
|
27
|
+
* addHealthCheck({ app });
|
|
28
|
+
*
|
|
29
|
+
* app.listen(3000);
|
|
30
|
+
* // GET /health returns { status: 'ok' }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Custom path
|
|
36
|
+
* addHealthCheck({ app, path: '/health' });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare const addHealthCheck: ({ app, path, }: AddHealthCheckOptions) => void;
|
|
40
|
+
|
|
41
|
+
export { type AddHealthCheckOptions, addHealthCheck };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/http-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "HTTP Server for ttoss environment",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ttoss",
|
|
@@ -24,20 +24,20 @@
|
|
|
24
24
|
],
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@koa/bodyparser": "^
|
|
27
|
+
"@koa/bodyparser": "^6.0.0",
|
|
28
28
|
"@koa/cors": "^5.0.0",
|
|
29
|
-
"@koa/multer": "^
|
|
30
|
-
"@koa/router": "^
|
|
31
|
-
"koa": "^
|
|
32
|
-
"multer": "
|
|
29
|
+
"@koa/multer": "^4.0.0",
|
|
30
|
+
"@koa/router": "^15.1.1",
|
|
31
|
+
"koa": "^3.1.1",
|
|
32
|
+
"multer": "2.0.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@types/koa": "^
|
|
36
|
-
"@types/koa__cors": "^5.0.
|
|
37
|
-
"@types/koa__multer": "^2.0.
|
|
38
|
-
"@types/koa__router": "^12.0.
|
|
35
|
+
"@types/koa": "^3.0.1",
|
|
36
|
+
"@types/koa__cors": "^5.0.1",
|
|
37
|
+
"@types/koa__multer": "^2.0.8",
|
|
38
|
+
"@types/koa__router": "^12.0.5",
|
|
39
39
|
"jest": "^30.2.0",
|
|
40
|
-
"supertest": "^7.
|
|
40
|
+
"supertest": "^7.1.4",
|
|
41
41
|
"tsup": "^8.5.1",
|
|
42
42
|
"@ttoss/config": "^1.35.12"
|
|
43
43
|
},
|