@ttoss/http-server 0.1.16 → 0.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ttoss/http-server
2
2
 
3
- HTTP server for Node.js for ttoss ecosystem.
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
- ## Usage
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 route = new Router();
21
+ const router = new Router();
23
22
 
24
- route.get('/health', (ctx) => {
25
- ctx.body = 'OK';
23
+ router.get('/health', (ctx) => {
24
+ ctx.body = { status: 'ok' };
26
25
  });
27
26
 
28
- app.use(route.routes());
29
-
27
+ app.use(router.routes());
30
28
  app.use(router.allowedMethods());
31
29
 
32
30
  app.listen(3000, () => {
33
- console.log('Server is running on port 3000');
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
- Some methods are re-exports from other libraries:
105
+ All exports are re-exported from established Koa ecosystem packages:
40
106
 
41
- - `App` - [Koa application](https://github.com/koajs/koa)
42
- - `Router` - [Koa router](https://github.com/koajs/router)
43
- - `bodyParser` - [Koa body parser](https://github.com/koajs/bodyparser)
44
- - `cors` - [Koa CORS](https://github.com/koajs/cors)
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
- export { App, Router, bodyParser, cors, multer };
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 };
@@ -0,0 +1,41 @@
1
+ export { bodyParser } from '@koa/bodyparser';
2
+ export { default as cors } from '@koa/cors';
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/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/dist/index.js ADDED
@@ -0,0 +1,82 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ "use strict";
3
+
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __name = (target, value) => __defProp(target, "name", {
11
+ value,
12
+ configurable: true
13
+ });
14
+ var __export = (target, all) => {
15
+ for (var name in all) __defProp(target, name, {
16
+ get: all[name],
17
+ enumerable: true
18
+ });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
23
+ get: () => from[key],
24
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
25
+ });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
35
+ value: mod,
36
+ enumerable: true
37
+ }) : target, mod));
38
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
39
+ value: true
40
+ }), mod);
41
+
42
+ // src/index.ts
43
+ var index_exports = {};
44
+ __export(index_exports, {
45
+ App: () => import_koa.default,
46
+ Router: () => import_router2.default,
47
+ addHealthCheck: () => addHealthCheck,
48
+ bodyParser: () => import_bodyparser.bodyParser,
49
+ cors: () => import_cors.default,
50
+ multer: () => import_multer.default
51
+ });
52
+ module.exports = __toCommonJS(index_exports);
53
+ var import_bodyparser = require("@koa/bodyparser");
54
+ var import_cors = __toESM(require("@koa/cors"), 1);
55
+ var import_multer = __toESM(require("@koa/multer"), 1);
56
+ var import_router2 = __toESM(require("@koa/router"), 1);
57
+ var import_koa = __toESM(require("koa"), 1);
58
+
59
+ // src/addHealthCheck.ts
60
+ var import_router = __toESM(require("@koa/router"), 1);
61
+ var addHealthCheck = /* @__PURE__ */__name(({
62
+ app,
63
+ path = "/health"
64
+ }) => {
65
+ const router = new import_router.default();
66
+ router.get(path, ctx => {
67
+ ctx.body = {
68
+ status: "ok"
69
+ };
70
+ });
71
+ app.use(router.routes());
72
+ app.use(router.allowedMethods());
73
+ }, "addHealthCheck");
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ App,
77
+ Router,
78
+ addHealthCheck,
79
+ bodyParser,
80
+ cors,
81
+ multer
82
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/http-server",
3
- "version": "0.1.16",
3
+ "version": "0.3.1",
4
4
  "description": "HTTP Server for ttoss environment",
5
5
  "license": "MIT",
6
6
  "author": "ttoss",
@@ -16,6 +16,7 @@
16
16
  "exports": {
17
17
  ".": {
18
18
  "import": "./dist/esm/index.js",
19
+ "require": "./dist/index.js",
19
20
  "types": "./dist/index.d.ts"
20
21
  }
21
22
  },
@@ -24,20 +25,20 @@
24
25
  ],
25
26
  "sideEffects": false,
26
27
  "dependencies": {
27
- "@koa/bodyparser": "^5.1.1",
28
+ "@koa/bodyparser": "^6.0.0",
28
29
  "@koa/cors": "^5.0.0",
29
- "@koa/multer": "^3.0.2",
30
- "@koa/router": "^13.1.0",
31
- "koa": "^2.15.3",
32
- "multer": "1.4.5-lts.1"
30
+ "@koa/multer": "^4.0.0",
31
+ "@koa/router": "^15.1.1",
32
+ "koa": "^3.1.1",
33
+ "multer": "2.0.2"
33
34
  },
34
35
  "devDependencies": {
35
- "@types/koa": "^2.15.0",
36
- "@types/koa__cors": "^5.0.0",
37
- "@types/koa__multer": "^2.0.7",
38
- "@types/koa__router": "^12.0.4",
36
+ "@types/koa": "^3.0.1",
37
+ "@types/koa__cors": "^5.0.1",
38
+ "@types/koa__multer": "^2.0.8",
39
+ "@types/koa__router": "^12.0.5",
39
40
  "jest": "^30.2.0",
40
- "supertest": "^7.0.0",
41
+ "supertest": "^7.1.4",
41
42
  "tsup": "^8.5.1",
42
43
  "@ttoss/config": "^1.35.12"
43
44
  },