@ooneex/middleware 0.16.0 → 1.0.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 +7 -25
- package/dist/index.d.ts +14 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +4 -3
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,49 +1,31 @@
|
|
|
1
1
|
# @ooneex/middleware
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Middleware pipeline framework with decorator-based registration for processing HTTP requests, responses, and WebSocket events in sequence.
|
|
4
4
|
|
|
5
5
|

|
|
6
|
-

|
|
7
|
-

|
|
8
6
|

|
|
9
7
|

|
|
10
8
|
|
|
11
9
|
## Features
|
|
12
10
|
|
|
13
|
-
✅ **HTTP Middleware** - Intercept and process HTTP requests and responses
|
|
11
|
+
✅ **HTTP Middleware** - Intercept and process HTTP requests and responses in a pipeline
|
|
14
12
|
|
|
15
|
-
✅ **WebSocket Middleware** - Handle WebSocket connection
|
|
13
|
+
✅ **WebSocket Middleware** - Handle WebSocket connection events with dedicated interface
|
|
16
14
|
|
|
17
|
-
✅ **
|
|
15
|
+
✅ **Built-in CORS** - Ready-to-use CorsMiddleware with environment variable configuration
|
|
18
16
|
|
|
19
|
-
✅ **
|
|
17
|
+
✅ **Decorator Registration** - Register middleware with `@decorator.middleware()` for clean code
|
|
20
18
|
|
|
21
|
-
✅ **
|
|
19
|
+
✅ **Context Access** - Full access to request, response, headers, and application context
|
|
22
20
|
|
|
23
|
-
✅ **
|
|
21
|
+
✅ **Container Integration** - Works seamlessly with the Ooneex dependency injection container
|
|
24
22
|
|
|
25
23
|
## Installation
|
|
26
24
|
|
|
27
|
-
### Bun
|
|
28
25
|
```bash
|
|
29
26
|
bun add @ooneex/middleware
|
|
30
27
|
```
|
|
31
28
|
|
|
32
|
-
### pnpm
|
|
33
|
-
```bash
|
|
34
|
-
pnpm add @ooneex/middleware
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
### Yarn
|
|
38
|
-
```bash
|
|
39
|
-
yarn add @ooneex/middleware
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### npm
|
|
43
|
-
```bash
|
|
44
|
-
npm install @ooneex/middleware
|
|
45
|
-
```
|
|
46
|
-
|
|
47
29
|
## Usage
|
|
48
30
|
|
|
49
31
|
### Basic HTTP Middleware
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ContextConfigType as ContextConfigType2, ContextType as ContextType2 } from "@ooneex/controller";
|
|
2
2
|
import { ContextConfigType, ContextType } from "@ooneex/controller";
|
|
3
3
|
import { ContextConfigType as SocketContextConfigType, ContextType as SocketContextType } from "@ooneex/socket";
|
|
4
4
|
type MiddlewareClassType = new (...args: any[]) => IMiddleware;
|
|
@@ -9,7 +9,19 @@ interface IMiddleware<T extends ContextConfigType = ContextConfigType> {
|
|
|
9
9
|
interface ISocketMiddleware<T extends SocketContextConfigType = SocketContextConfigType> {
|
|
10
10
|
handler: (context: SocketContextType<T>) => Promise<SocketContextType<T>> | SocketContextType<T>;
|
|
11
11
|
}
|
|
12
|
+
declare class CorsMiddleware<T extends ContextConfigType2 = ContextConfigType2> implements IMiddleware<T> {
|
|
13
|
+
private readonly origins;
|
|
14
|
+
private readonly methods;
|
|
15
|
+
private readonly headers;
|
|
16
|
+
private readonly exposedHeaders;
|
|
17
|
+
private readonly credentials;
|
|
18
|
+
private readonly maxAge;
|
|
19
|
+
constructor();
|
|
20
|
+
handler: (context: ContextType2<T>) => Promise<ContextType2<T>>;
|
|
21
|
+
private isOriginAllowed;
|
|
22
|
+
}
|
|
23
|
+
import { EContainerScope } from "@ooneex/container";
|
|
12
24
|
declare const decorator: {
|
|
13
25
|
middleware: (scope?: EContainerScope) => (target: MiddlewareClassType | SocketMiddlewareClassType) => void;
|
|
14
26
|
};
|
|
15
|
-
export { decorator, SocketMiddlewareClassType, MiddlewareClassType, ISocketMiddleware, IMiddleware };
|
|
27
|
+
export { decorator, SocketMiddlewareClassType, MiddlewareClassType, ISocketMiddleware, IMiddleware, CorsMiddleware };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
var d=function(e,t,s,o){var i=arguments.length,r=i<3?t:o===null?o=Object.getOwnPropertyDescriptor(t,s):o,n;if(typeof Reflect==="object"&&typeof Reflect.decorate==="function")r=Reflect.decorate(e,t,s,o);else for(var p=e.length-1;p>=0;p--)if(n=e[p])r=(i<3?n(r):i>3?n(t,s,r):n(t,s))||r;return i>3&&r&&Object.defineProperty(t,s,r),r};var l=(e,t)=>{if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(e,t)};import{injectable as h}from"@ooneex/container";var m=["GET","HEAD","PUT","PATCH","POST","DELETE"],C=["Content-Type","Authorization"];class a{origins;methods;headers;exposedHeaders;credentials;maxAge;constructor(){let e=Bun.env.CORS_ORIGINS??"*";this.origins=e==="*"?"*":e.split(",").map((t)=>t.trim()),this.methods=Bun.env.CORS_METHODS?.split(",").map((t)=>t.trim())??m,this.headers=Bun.env.CORS_HEADERS?.split(",").map((t)=>t.trim())??C,this.exposedHeaders=Bun.env.CORS_EXPOSED_HEADERS?.split(",").map((t)=>t.trim())??[],this.credentials=Bun.env.CORS_CREDENTIALS==="true",this.maxAge=Number(Bun.env.CORS_MAX_AGE??86400)}handler=async(e)=>{let t=e.header.get("Origin");if(!t)return e;if(!this.isOriginAllowed(t))return e;let s=this.origins==="*"?"*":t;if(e.response.header.setAccessControlAllowOrigin(s).setAccessControlAllowMethods(this.methods).setAccessControlAllowHeaders(this.headers).setAccessControlAllowCredentials(this.credentials),this.exposedHeaders.length>0)e.response.header.set("Access-Control-Expose-Headers",this.exposedHeaders.join(", "));if(e.method==="OPTIONS")e.response.header.set("Access-Control-Max-Age",String(this.maxAge)),e.response.json({},204);return e};isOriginAllowed(e){if(this.origins==="*")return!0;return this.origins.includes(e)}}a=d([h(),l("design:paramtypes",[])],a);import{container as y,EContainerScope as g}from"@ooneex/container";var T={middleware:(e=g.Singleton)=>{return(t)=>{y.add(t,e)}}};export{T as decorator,a as CorsMiddleware};
|
|
2
3
|
|
|
3
|
-
//# debugId=
|
|
4
|
+
//# debugId=C2EEC218963D209264756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["src/decorators.ts"],
|
|
3
|
+
"sources": ["src/CorsMiddleware.ts", "src/decorators.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
+
"import { injectable } from \"@ooneex/container\";\nimport type { ContextConfigType, ContextType } from \"@ooneex/controller\";\nimport type { HttpMethodType } from \"@ooneex/types\";\nimport type { IMiddleware } from \"./types\";\n\nconst defaultMethods: HttpMethodType[] = [\"GET\", \"HEAD\", \"PUT\", \"PATCH\", \"POST\", \"DELETE\"];\nconst defaultHeaders: string[] = [\"Content-Type\", \"Authorization\"];\n\n@injectable()\nexport class CorsMiddleware<T extends ContextConfigType = ContextConfigType> implements IMiddleware<T> {\n private readonly origins: string[] | \"*\";\n private readonly methods: HttpMethodType[];\n private readonly headers: string[];\n private readonly exposedHeaders: string[];\n private readonly credentials: boolean;\n private readonly maxAge: number;\n\n constructor() {\n const origins = Bun.env.CORS_ORIGINS ?? \"*\";\n this.origins = origins === \"*\" ? \"*\" : origins.split(\",\").map((o) => o.trim());\n this.methods = (Bun.env.CORS_METHODS?.split(\",\").map((m) => m.trim()) as HttpMethodType[]) ?? defaultMethods;\n this.headers = Bun.env.CORS_HEADERS?.split(\",\").map((h) => h.trim()) ?? defaultHeaders;\n this.exposedHeaders = Bun.env.CORS_EXPOSED_HEADERS?.split(\",\").map((h) => h.trim()) ?? [];\n this.credentials = Bun.env.CORS_CREDENTIALS === \"true\";\n this.maxAge = Number(Bun.env.CORS_MAX_AGE ?? 86400);\n }\n\n public handler = async (context: ContextType<T>): Promise<ContextType<T>> => {\n const origin = context.header.get(\"Origin\");\n\n if (!origin) {\n return context;\n }\n\n if (!this.isOriginAllowed(origin)) {\n return context;\n }\n\n const allowedOrigin = this.origins === \"*\" ? \"*\" : origin;\n\n context.response.header\n .setAccessControlAllowOrigin(allowedOrigin)\n .setAccessControlAllowMethods(this.methods)\n .setAccessControlAllowHeaders(this.headers)\n .setAccessControlAllowCredentials(this.credentials);\n\n if (this.exposedHeaders.length > 0) {\n context.response.header.set(\"Access-Control-Expose-Headers\", this.exposedHeaders.join(\", \"));\n }\n\n if (context.method === \"OPTIONS\") {\n context.response.header.set(\"Access-Control-Max-Age\", String(this.maxAge));\n context.response.json({}, 204);\n }\n\n return context;\n };\n\n private isOriginAllowed(origin: string): boolean {\n if (this.origins === \"*\") {\n return true;\n }\n\n return this.origins.includes(origin);\n }\n}\n",
|
|
5
6
|
"import { container, EContainerScope } from \"@ooneex/container\";\nimport type { MiddlewareClassType, SocketMiddlewareClassType } from \"./types\";\n\nexport const decorator = {\n middleware: (scope: EContainerScope = EContainerScope.Singleton) => {\n return (target: MiddlewareClassType | SocketMiddlewareClassType): void => {\n container.add(target, scope);\n };\n },\n};\n"
|
|
6
7
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
8
|
+
"mappings": ";ybAAA,qBAAS,0BAKT,IAAM,EAAmC,CAAC,MAAO,OAAQ,MAAO,QAAS,OAAQ,QAAQ,EACnF,EAA2B,CAAC,eAAgB,eAAe,EAG1D,MAAM,CAA0F,CACpF,QACA,QACA,QACA,eACA,YACA,OAEjB,WAAW,EAAG,CACZ,IAAM,EAAU,IAAI,IAAI,cAAgB,IACxC,KAAK,QAAU,IAAY,IAAM,IAAM,EAAQ,MAAM,GAAG,EAAE,IAAI,CAAC,IAAM,EAAE,KAAK,CAAC,EAC7E,KAAK,QAAW,IAAI,IAAI,cAAc,MAAM,GAAG,EAAE,IAAI,CAAC,IAAM,EAAE,KAAK,CAAC,GAA0B,E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|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/middleware",
|
|
3
|
-
"description": "Middleware framework with
|
|
4
|
-
"version": "0.
|
|
3
|
+
"description": "Middleware pipeline framework with decorator-based registration for processing HTTP requests, responses, and WebSocket events in sequence",
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -28,11 +28,12 @@
|
|
|
28
28
|
"npm:publish": "bun publish --tolerate-republish --access public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ooneex/container": "0.0.
|
|
31
|
+
"@ooneex/container": "0.0.19"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@ooneex/controller": "0.
|
|
35
|
-
"@ooneex/
|
|
34
|
+
"@ooneex/controller": "0.17.1",
|
|
35
|
+
"@ooneex/types": "0.0.19",
|
|
36
|
+
"@ooneex/socket": "0.17.1"
|
|
36
37
|
},
|
|
37
38
|
"keywords": [
|
|
38
39
|
"bun",
|