tspace-spear 1.2.1 → 1.2.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.
package/README.md CHANGED
@@ -3,8 +3,10 @@
3
3
  [![NPM version](https://img.shields.io/npm/v/tspace-spear.svg)](https://www.npmjs.com)
4
4
  [![NPM downloads](https://img.shields.io/npm/dm/tspace-spear.svg)](https://www.npmjs.com)
5
5
 
6
- tspace-spear is a lightweight API framework for Node.js that is fast and highly focused on providing the best developer experience.
7
- It utilizes the native HTTP server.
6
+ tspace-spear is a lightweight and high-performance API framework for Node.js,
7
+ built on the native HTTP server with optional support for uWebSockets.js (C++) to achieve maximum speed and efficiency.
8
+
9
+ It is designed with a strong focus on delivering an excellent developer experience.
8
10
 
9
11
  ## Install
10
12
 
@@ -16,6 +18,7 @@ npm install tspace-spear --save
16
18
  ```
17
19
  ## Basic Usage
18
20
  - [Start Server](#start-server)
21
+ - [Adapter](#adapter)
19
22
  - [Cluster](#cluster)
20
23
  - [Global Prefix](#global-prefix)
21
24
  - [Logger](#logger)
@@ -31,7 +34,7 @@ npm install tspace-spear --save
31
34
  - [Controller](#controller)
32
35
  - [Router](#router)
33
36
  - [Swagger](#swagger)
34
- - [WebSocket](#web-socket)
37
+ - [WebSocket](#websocket)
35
38
  - [Example CRUD](#example-crud)
36
39
 
37
40
  ## Start Server
@@ -49,6 +52,35 @@ new Spear()
49
52
 
50
53
  ```
51
54
 
55
+ ## Adapter
56
+ tspace-spear supports multiple server adapters,
57
+ including the native Node.js HTTP server and uWebSockets.js for high performance.
58
+
59
+ ⚠️ Requirements for uWebSockets.js
60
+ Node.js 18 or higher is required
61
+ Installation is done via GitHub (no official npm release)
62
+
63
+ ```js
64
+ import { Spear } from "tspace-spear";
65
+ import uWS from "uWebSockets.js";
66
+
67
+ // Install via package.json
68
+ // "dependencies": {
69
+ // "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.45.0"
70
+ // }
71
+
72
+ new Spear({ adapter: uWS })
73
+ .get("/", () => "Hello world!")
74
+ .get("/json", () => {
75
+ return {
76
+ message: "Hello world!",
77
+ };
78
+ })
79
+ .listen(3000, () =>
80
+ console.log("uWS server is running at http://localhost:3000")
81
+ );
82
+ ```
83
+
52
84
  ## Cluster
53
85
  ```js
54
86
  import { Spear } from "tspace-spear";
@@ -619,7 +651,7 @@ class CatController {
619
651
  file : {
620
652
  type : 'array',
621
653
  items: {
622
- type:"file",
654
+ type :"string",
623
655
  format:"binary"
624
656
  }
625
657
  },
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Extract specific fields from `ctx.body`.
3
+ *
4
+ * This decorator filters the request body and keeps only the
5
+ * specified keys. The filtered result replaces `ctx.body`
6
+ * before the controller method is executed.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * @Body('email', 'password')
11
+ * async login(ctx: T.Context) {
12
+ * // ctx.body = { email: "...", password: "..." }
13
+ * }
14
+ * ```
15
+ *
16
+ * @param {...string} bodyParms - Body field names to extract.
17
+ * @returns {MethodDecorator}
18
+ */
19
+ export declare const Body: (...bodyParms: string[]) => Function;
20
+ /**
21
+ * Extract specific uploaded files from `ctx.files`.
22
+ *
23
+ * Filters uploaded files and keeps only the specified
24
+ * file field names before executing the controller method.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * \@Files('avatar', 'resume')
29
+ * async upload(ctx: T.Context) {
30
+ * // ctx.files = { avatar: File, resume: File }
31
+ * }
32
+ * ```
33
+ *
34
+ * @param {...string} filesParms - File field names to extract.
35
+ * @returns {MethodDecorator}
36
+ */
37
+ export declare const Files: (...filesParms: string[]) => Function;
38
+ /**
39
+ * Extract specific route parameters from `ctx.params`.
40
+ *
41
+ * Filters route parameters and keeps only the specified
42
+ * parameter names.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * \@Params('id')
47
+ * async getUser(ctx: T.Context) {
48
+ * // ctx.params = { id: "123" }
49
+ * }
50
+ * ```
51
+ *
52
+ * @param {...string} paramsData - Route parameter names to extract.
53
+ * @returns {MethodDecorator}
54
+ */
55
+ export declare const Params: (...paramsData: string[]) => Function;
56
+ /**
57
+ * Extract specific query parameters from `ctx.query`.
58
+ *
59
+ * Filters query parameters and keeps only the specified
60
+ * query keys.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * \@Query('page', 'limit')
65
+ * async listUsers(ctx: T.Context) {
66
+ * // ctx.query = { page: "1", limit: "10" }
67
+ * }
68
+ * ```
69
+ *
70
+ * @param {...string} queryParms - Query parameter names to extract.
71
+ * @returns {MethodDecorator}
72
+ */
73
+ export declare const Query: (...queryParms: string[]) => Function;
74
+ /**
75
+ * Extract specific cookies from `ctx.cookies`.
76
+ *
77
+ * Filters cookies and keeps only the specified cookie keys
78
+ * before executing the controller method.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * \@Cookies('token')
83
+ * async profile(ctx: T.Context) {
84
+ * // ctx.cookies = { token: "..." }
85
+ * }
86
+ * ```
87
+ *
88
+ * @param {...string} cookiesParms - Cookie names to extract.
89
+ * @returns {MethodDecorator}
90
+ */
91
+ export declare const Cookies: (...cookiesParms: string[]) => Function;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Declares a class as a controller and assigns a base route path.
3
+ *
4
+ * The provided path will be used as the **base URL prefix**
5
+ * for all routes defined inside the controller. The path must
6
+ * start with `/`.
7
+ *
8
+ * This decorator stores the controller path using
9
+ * `Reflect.defineMetadata` under the key `"controllers"`.
10
+ * The framework can later read this metadata to register
11
+ * routes automatically.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * \@Controller('/users')
16
+ * class UserController {
17
+ *
18
+ * async list(ctx: T.Context) {
19
+ * return [{ id: 1, name: "John" }];
20
+ * }
21
+ *
22
+ * }
23
+ * ```
24
+ *
25
+ * If a route handler defines `/profile`, the final route becomes:
26
+ *
27
+ * ```
28
+ * /users/profile
29
+ * ```
30
+ *
31
+ * @param {`/${string}`} path - Base route path for the controller.
32
+ * @returns {ClassDecorator}
33
+ */
34
+ export declare const Controller: (path: `/${string}`) => ClassDecorator;
@@ -0,0 +1,31 @@
1
+ import { OutgoingHttpHeaders } from 'http';
2
+ /**
3
+ * Sets the HTTP response headers and status code before the controller method executes.
4
+ *
5
+ * This decorator calls `res.writeHead()` on the underlying Node.js response object,
6
+ * allowing you to define the response **status code** and **headers** in advance.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * class UserController {
11
+ *
12
+ * \@WriteHeader(200, { "Content-Type": "application/json" })
13
+ * async profile(ctx: T.Context) {
14
+ * return { id: 1, name: "John" };
15
+ * }
16
+ *
17
+ * }
18
+ * ```
19
+ *
20
+ * Example response:
21
+ *
22
+ * ```
23
+ * HTTP/1.1 200 OK
24
+ * Content-Type: application/json
25
+ * ```
26
+ *
27
+ * @param {number} statusCode - HTTP status code to send with the response.
28
+ * @param {OutgoingHttpHeaders} contentType - Response headers to set.
29
+ * @returns {Function}
30
+ */
31
+ export declare const WriteHeader: (statusCode: number, contentType: OutgoingHttpHeaders) => Function;
@@ -0,0 +1,9 @@
1
+ import 'reflect-metadata';
2
+ export * from './methods';
3
+ export * from './headers';
4
+ export * from './middleware';
5
+ export * from './controller';
6
+ export * from './headers';
7
+ export * from './statusCode';
8
+ export * from './context';
9
+ export * from './swagger';
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Maps a controller method to an HTTP GET route.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * \@Get('/users')
7
+ * async list(ctx: T.Context) {
8
+ * return [{ id: 1, name: "John" }];
9
+ * }
10
+ * ```
11
+ */
12
+ export declare const Get: (path: `/${string}`) => (target: any, propertyKey: any) => void;
13
+ /**
14
+ * Maps a controller method to an HTTP POST route.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * \@Post('/users')
19
+ * async create(ctx: T.Context) {
20
+ * return { success: true };
21
+ * }
22
+ * ```
23
+ */
24
+ export declare const Post: (path: `/${string}`) => (target: any, propertyKey: any) => void;
25
+ /**
26
+ * Maps a controller method to an HTTP PUT route.
27
+ *
28
+ * Used for replacing a resource.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * \@Put('/users/:id')
33
+ * async update(ctx: T.Context) {}
34
+ * ```
35
+ */
36
+ export declare const Put: (path: `/${string}`) => (target: any, propertyKey: any) => void;
37
+ /**
38
+ * Maps a controller method to an HTTP PATCH route.
39
+ *
40
+ * Used for partially updating a resource.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * \@Patch('/users/:id')
45
+ * async patch(ctx: T.Context) {}
46
+ * ```
47
+ */
48
+ export declare const Patch: (path: `/${string}`) => (target: any, propertyKey: any) => void;
49
+ /**
50
+ * Maps a controller method to an HTTP DELETE route.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * \@Delete('/users/:id')
55
+ * async remove(ctx: T.Context) {}
56
+ * ```
57
+ */
58
+ export declare const Delete: (path: `/${string}`) => (target: any, propertyKey: any) => void;
59
+ /**
60
+ * Maps a controller method to an HTTP HEAD route.
61
+ *
62
+ * HEAD responses contain only headers and no response body.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * \@Head('/health')
67
+ * async health(ctx: T.Context) {}
68
+ * ```
69
+ */
70
+ export declare const Head: (path: `/${string}`) => (target: any, propertyKey: any) => void;
71
+ /**
72
+ * Maps a controller method to an HTTP OPTIONS route.
73
+ *
74
+ * Typically used for CORS preflight requests.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * \@Options('/users')
79
+ * async options(ctx: T.Context) {}
80
+ * ```
81
+ */
82
+ export declare const Options: (path: `/${string}`) => (target: any, propertyKey: any) => void;
@@ -0,0 +1,39 @@
1
+ import { T } from '../types';
2
+ /**
3
+ * Attaches a middleware function to a controller method.
4
+ *
5
+ * The middleware will be executed **before the route handler**.
6
+ * If the middleware calls `next(err)`, the error will be forwarded
7
+ * to the framework's error handler. Otherwise, the original
8
+ * controller method will be executed.
9
+ *
10
+ * This decorator also stores middleware metadata using `Reflect.defineMetadata`
11
+ * so the framework can discover and execute it during the request lifecycle.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * class UserController {
16
+ *
17
+ * \@Middleware(authMiddleware)
18
+ * async profile(ctx: T.Context) {
19
+ * return { user: ctx.user };
20
+ * }
21
+ *
22
+ * }
23
+ * ```
24
+ *
25
+ * Example middleware:
26
+ *
27
+ * ```ts
28
+ * const authMiddleware: T.RequestFunction = (ctx, next) => {
29
+ * if (!ctx.user) {
30
+ * return next(new Error("Unauthorized"));
31
+ * }
32
+ * next();
33
+ * };
34
+ * ```
35
+ *
36
+ * @param {T.RequestFunction} middleware - Middleware function to execute before the route handler.
37
+ * @returns {MethodDecorator}
38
+ */
39
+ export declare const Middleware: (middleware: T.RequestFunction) => Function;
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/middleware.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACI,MAAM,UAAU,GAAG,CAAC,UAA6B,EAAY,EAAE;IAEpE,OAAO,CAAC,MAAW,EAAE,GAAW,EAAE,UAA8B,EAAE,EAAE;QAClE,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAExC,UAAU,CAAC,KAAK,GAAG,UAAU,GAAc,EAAE,IAAoB;YAC/D,IAAI;gBAEF,OAAO,CAAC,cAAc,CAAC,aAAa,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;gBAE1D,OAAO,UAAU,CAAC,GAAG,EAAE,CAAC,GAAS,EAAE,EAAE;oBACnC,IAAI,GAAG,IAAI,IAAI,EAAE;wBACf,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;qBAClB;oBAED,OAAO,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;gBAC9C,CAAC,CAAC,CAAC;aAEJ;YAAC,OAAO,KAAU,EAAE;gBACnB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;aACpB;QACH,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AAvBW,QAAA,UAAU,cAuBrB"}
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../../src/lib/core/decorators/middleware.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACI,MAAM,UAAU,GAAG,CAAC,UAA6B,EAAY,EAAE;IAEpE,OAAO,CAAC,MAAW,EAAE,GAAW,EAAE,UAA8B,EAAE,EAAE;QAClE,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAExC,UAAU,CAAC,KAAK,GAAG,UAAU,GAAc,EAAE,IAAoB;YAC/D,IAAI,CAAC;gBAEH,OAAO,CAAC,cAAc,CAAC,aAAa,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;gBAE1D,OAAO,UAAU,CAAC,GAAG,EAAE,CAAC,GAAS,EAAE,EAAE;oBACnC,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;wBAChB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;oBACnB,CAAC;oBAED,OAAO,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;gBAC9C,CAAC,CAAC,CAAC;YAEL,CAAC;YAAC,OAAO,KAAU,EAAE,CAAC;gBACpB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC;QACH,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AAvBW,QAAA,UAAU,cAuBrB"}
@@ -0,0 +1,26 @@
1
+ import { type T } from '../types';
2
+ /**
3
+ * Sets the HTTP response status code before executing the controller method.
4
+ *
5
+ * The provided status code will be automatically clamped between **100** and **599**
6
+ * to ensure a valid HTTP status range. It also sets the default
7
+ * `Content-Type` header to `application/json`.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * class UserController {
12
+ * \@StatusCode(201)
13
+ * async create(ctx: T.Context) {
14
+ * return { success: true };
15
+ * }
16
+ * }
17
+ * ```
18
+ *
19
+ * In this example the response will be sent with:
20
+ * - Status: **201 Created**
21
+ * - Header: `Content-Type: application/json`
22
+ *
23
+ * @param {T.StatusCode} statusCode - HTTP status code to send with the response.
24
+ * @returns {MethodDecorator}
25
+ */
26
+ export declare const StatusCode: (statusCode: T.StatusCode) => Function;
@@ -0,0 +1,36 @@
1
+ import { type T } from "../types";
2
+ /**
3
+ * Attaches Swagger/OpenAPI specification metadata to a controller method.
4
+ *
5
+ * This decorator stores route documentation using `Reflect.defineMetadata`
6
+ * so the framework can later collect it and generate **Swagger / OpenAPI**
7
+ * documentation automatically.
8
+ *
9
+ * The metadata is stored on the controller constructor under the key `"swaggers"`.
10
+ * Each decorated method contributes a Swagger specification object.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * class UserController {
15
+ *
16
+ * \@Swagger({
17
+ * summary: "Get user profile",
18
+ * description: "Returns the authenticated user's profile",
19
+ * tags: ["Users"],
20
+ * responses: {
21
+ * 200: {
22
+ * description: "Successful response"
23
+ * }
24
+ * }
25
+ * })
26
+ * async profile(ctx: T.Context) {
27
+ * return { id: 1, name: "John" };
28
+ * }
29
+ *
30
+ * }
31
+ * ```
32
+ *
33
+ * @param {T.Swagger.Spec} data - Swagger/OpenAPI specification for the route.
34
+ * @returns {MethodDecorator}
35
+ */
36
+ export declare const Swagger: (data: T.Swagger.Spec) => (target: any, propertyKey: any) => void;