@ooneex/http-response 0.17.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 CHANGED
@@ -1,58 +1,40 @@
1
1
  # @ooneex/http-response
2
2
 
3
- A comprehensive TypeScript/JavaScript library for building HTTP responses in web applications. This package provides a fluent API for creating JSON responses, handling exceptions, managing redirects, and working with HTTP headers in a type-safe manner.
3
+ HTTP response builder with fluent API for setting status codes, headers, cookies, and streaming or buffered content delivery.
4
4
 
5
5
  ![Browser](https://img.shields.io/badge/Browser-Compatible-green?style=flat-square&logo=googlechrome)
6
6
  ![Bun](https://img.shields.io/badge/Bun-Compatible-orange?style=flat-square&logo=bun)
7
- ![Deno](https://img.shields.io/badge/Deno-Compatible-blue?style=flat-square&logo=deno)
8
- ![Node.js](https://img.shields.io/badge/Node.js-Compatible-green?style=flat-square&logo=node.js)
9
7
  ![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue?style=flat-square&logo=typescript)
10
8
  ![MIT License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)
11
9
 
12
10
  ## Features
13
11
 
14
- ✅ **Fluent API** - Chain methods for easy response building
12
+ ✅ **Fluent API** - Chain methods like json(), exception(), notFound(), and redirect() for easy response building
15
13
 
16
- ✅ **Type-Safe** - Full TypeScript support with generic types
14
+ ✅ **Type-Safe** - Full TypeScript support with generic data types
17
15
 
18
- ✅ **JSON Responses** - Easy creation of JSON API responses
16
+ ✅ **JSON Responses** - Create structured JSON responses with success/error flags, status, and metadata
19
17
 
20
- ✅ **Exception Handling** - Built-in error response formatting
18
+ ✅ **Exception Handling** - Built-in error response formatting with configurable status codes and data
21
19
 
22
- ✅ **HTTP Redirects** - Support for all redirect status codes
20
+ ✅ **Not Found Responses** - Dedicated notFound() method for 404 responses
23
21
 
24
- ✅ **Header Management** - Integrated HTTP header handling
22
+ ✅ **HTTP Redirects** - Support for all redirect status codes with Location header management
25
23
 
26
- ✅ **Web Standards** - Returns native Web API Response objects
24
+ ✅ **Header Management** - Integrated HTTP header handling via @ooneex/http-header
27
25
 
28
- ✅ **Method Chaining** - All methods return the instance for chaining
26
+ ✅ **Web Standards** - Returns native Web API Response objects
29
27
 
30
- ✅ **State Management** - Proper state transitions between response types
28
+ ✅ **Environment-Aware** - Includes application environment info in response data
31
29
 
32
- ✅ **Cross-Platform** - Works in Browser, Node.js, Bun, and Deno
30
+ ✅ **Socket Support** - Built-in done flag for WebSocket response completion tracking
33
31
 
34
32
  ## Installation
35
33
 
36
- ### Bun
37
34
  ```bash
38
35
  bun add @ooneex/http-response
39
36
  ```
40
37
 
41
- ### pnpm
42
- ```bash
43
- pnpm add @ooneex/http-response
44
- ```
45
-
46
- ### Yarn
47
- ```bash
48
- yarn add @ooneex/http-response
49
- ```
50
-
51
- ### npm
52
- ```bash
53
- npm install @ooneex/http-response
54
- ```
55
-
56
38
  ## Usage
57
39
 
58
40
  ### Basic JSON Responses
package/dist/index.d.ts CHANGED
@@ -19,6 +19,7 @@ interface IResponse<DataType extends Record<string, unknown> = Record<string, un
19
19
  redirect: (url: string | URL, status?: StatusCodeType) => IResponse<DataType>;
20
20
  get: (env?: Environment) => Response;
21
21
  getData: () => DataType | null;
22
+ getStatus: () => StatusCodeType;
22
23
  }
23
24
  type ResponseDataType<Data extends Record<string, unknown>> = {
24
25
  data: Data;
@@ -54,6 +55,7 @@ declare class HttpResponse<Data extends Record<string, unknown> = Record<string,
54
55
  }): IResponse<Data>;
55
56
  redirect(url: string | URL, status?: StatusCodeType2): IResponse<Data>;
56
57
  getData(): Data | null;
58
+ getStatus(): StatusCodeType2;
57
59
  get(env?: Environment2): Response;
58
60
  }
59
61
  export { ResponseDataType, IResponse, HttpResponse };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import{Environment as r}from"@ooneex/app-env";import{Header as o}from"@ooneex/http-header";import{HttpStatus as s}from"@ooneex/http-status";class n{header;data=null;status=s.Code.OK;redirectUrl=null;message=null;done=!1;constructor(t){this.header=t||new o}json(t,e=s.Code.OK){return this.data=t,this.status=e,this.header.setJson(),this.header.remove("Location"),this.redirectUrl=null,this.message=null,this}exception(t,e){return this.message=t,this.status=e?.status??s.Code.InternalServerError,this.data=e?.data||null,this.redirectUrl=null,this.header.setJson(),this.header.remove("Location"),this}notFound(t,e){return this.message=t,this.status=e?.status||s.Code.NotFound,this.data=e?.data||null,this.redirectUrl=null,this.header.setJson(),this.header.remove("Location"),this}redirect(t,e=s.Code.Found){return this.redirectUrl=t,this.status=e,this.header.setLocation(t.toString()),this.data=null,this.message=null,this}getData(){return this.data}get(t){if(this.redirectUrl)return new Response(null,{status:this.status,headers:this.header.native});let e=new s,a={data:this.data||{},message:this.message,success:e.isSuccessful(this.status),done:this.done,status:this.status,isClientError:e.isClientError(this.status),isServerError:e.isServerError(this.status),isNotFound:!1,isUnauthorized:!1,isForbidden:!1,app:{env:t||r.PRODUCTION}};return new Response(JSON.stringify(a),{status:a.status,headers:this.header.native})}}export{n as HttpResponse};
1
+ import{Environment as r}from"@ooneex/app-env";import{Header as o}from"@ooneex/http-header";import{HttpStatus as s}from"@ooneex/http-status";class n{header;data=null;status=s.Code.OK;redirectUrl=null;message=null;done=!1;constructor(t){this.header=t||new o}json(t,e=s.Code.OK){return this.data=t,this.status=e,this.header.setJson(),this.header.remove("Location"),this.redirectUrl=null,this.message=null,this}exception(t,e){return this.message=t,this.status=e?.status??s.Code.InternalServerError,this.data=e?.data||null,this.redirectUrl=null,this.header.setJson(),this.header.remove("Location"),this}notFound(t,e){return this.message=t,this.status=e?.status||s.Code.NotFound,this.data=e?.data||null,this.redirectUrl=null,this.header.setJson(),this.header.remove("Location"),this}redirect(t,e=s.Code.Found){return this.redirectUrl=t,this.status=e,this.header.setLocation(t.toString()),this.data=null,this.message=null,this}getData(){return this.data}getStatus(){return this.status}get(t){if(this.redirectUrl)return new Response(null,{status:this.status,headers:this.header.native});let e=new s,a={data:this.data||{},message:this.message,success:e.isSuccessful(this.status),done:this.done,status:this.status,isClientError:e.isClientError(this.status),isServerError:e.isServerError(this.status),isNotFound:!1,isUnauthorized:!1,isForbidden:!1,app:{env:t||r.PRODUCTION}};return new Response(JSON.stringify(a),{status:a.status,headers:this.header.native})}}export{n as HttpResponse};
2
2
 
3
- //# debugId=A1CEA356DBFBE4D664756E2164756E21
3
+ //# debugId=9EA7148894EF116764756E2164756E21
package/dist/index.js.map CHANGED
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["src/HttpResponse.ts"],
4
4
  "sourcesContent": [
5
- "import { Environment } from \"@ooneex/app-env\";\nimport { Header, type IHeader } from \"@ooneex/http-header\";\nimport { HttpStatus, type StatusCodeType } from \"@ooneex/http-status\";\nimport type { IResponse, ResponseDataType } from \"./types\";\n\nexport class HttpResponse<Data extends Record<string, unknown> = Record<string, unknown>> implements IResponse<Data> {\n public readonly header: IHeader;\n private data: Data | null = null;\n private status: StatusCodeType = HttpStatus.Code.OK;\n private redirectUrl: string | URL | null = null;\n private message: string | null = null;\n public done = false; // For socket\n\n constructor(header?: IHeader) {\n this.header = header || new Header();\n }\n\n public json(data: Data, status: StatusCodeType = HttpStatus.Code.OK): IResponse<Data> {\n this.data = data;\n this.status = status;\n this.header.setJson();\n this.header.remove(\"Location\");\n this.redirectUrl = null;\n this.message = null;\n\n return this;\n }\n\n public exception(\n message: string,\n config?: {\n data?: Data;\n status?: StatusCodeType;\n },\n ): IResponse<Data> {\n this.message = message;\n this.status = config?.status ?? HttpStatus.Code.InternalServerError;\n this.data = config?.data || null;\n this.redirectUrl = null;\n this.header.setJson();\n this.header.remove(\"Location\");\n\n return this;\n }\n\n public notFound(\n message: string,\n config?: {\n data?: Data;\n status?: StatusCodeType;\n },\n ): IResponse<Data> {\n this.message = message;\n this.status = config?.status || HttpStatus.Code.NotFound;\n this.data = config?.data || null;\n this.redirectUrl = null;\n this.header.setJson();\n this.header.remove(\"Location\");\n\n return this;\n }\n\n public redirect(url: string | URL, status: StatusCodeType = HttpStatus.Code.Found): IResponse<Data> {\n this.redirectUrl = url;\n this.status = status;\n this.header.setLocation(url.toString());\n this.data = null;\n this.message = null;\n\n return this;\n }\n\n public getData(): Data | null {\n return this.data;\n }\n\n public get(env?: Environment): Response {\n if (this.redirectUrl) {\n return new Response(null, {\n status: this.status,\n headers: this.header.native,\n });\n }\n\n const status = new HttpStatus();\n\n const responseData: ResponseDataType<Data> = {\n data: this.data || ({} as Data),\n message: this.message,\n success: status.isSuccessful(this.status),\n done: this.done,\n status: this.status,\n isClientError: status.isClientError(this.status),\n isServerError: status.isServerError(this.status),\n isNotFound: false,\n isUnauthorized: false,\n isForbidden: false,\n app: {\n env: env || Environment.PRODUCTION,\n },\n };\n\n return new Response(JSON.stringify(responseData), {\n status: responseData.status,\n headers: this.header.native,\n });\n }\n}\n"
5
+ "import { Environment } from \"@ooneex/app-env\";\nimport { Header, type IHeader } from \"@ooneex/http-header\";\nimport { HttpStatus, type StatusCodeType } from \"@ooneex/http-status\";\nimport type { IResponse, ResponseDataType } from \"./types\";\n\nexport class HttpResponse<Data extends Record<string, unknown> = Record<string, unknown>> implements IResponse<Data> {\n public readonly header: IHeader;\n private data: Data | null = null;\n private status: StatusCodeType = HttpStatus.Code.OK;\n private redirectUrl: string | URL | null = null;\n private message: string | null = null;\n public done = false; // For socket\n\n constructor(header?: IHeader) {\n this.header = header || new Header();\n }\n\n public json(data: Data, status: StatusCodeType = HttpStatus.Code.OK): IResponse<Data> {\n this.data = data;\n this.status = status;\n this.header.setJson();\n this.header.remove(\"Location\");\n this.redirectUrl = null;\n this.message = null;\n\n return this;\n }\n\n public exception(\n message: string,\n config?: {\n data?: Data;\n status?: StatusCodeType;\n },\n ): IResponse<Data> {\n this.message = message;\n this.status = config?.status ?? HttpStatus.Code.InternalServerError;\n this.data = config?.data || null;\n this.redirectUrl = null;\n this.header.setJson();\n this.header.remove(\"Location\");\n\n return this;\n }\n\n public notFound(\n message: string,\n config?: {\n data?: Data;\n status?: StatusCodeType;\n },\n ): IResponse<Data> {\n this.message = message;\n this.status = config?.status || HttpStatus.Code.NotFound;\n this.data = config?.data || null;\n this.redirectUrl = null;\n this.header.setJson();\n this.header.remove(\"Location\");\n\n return this;\n }\n\n public redirect(url: string | URL, status: StatusCodeType = HttpStatus.Code.Found): IResponse<Data> {\n this.redirectUrl = url;\n this.status = status;\n this.header.setLocation(url.toString());\n this.data = null;\n this.message = null;\n\n return this;\n }\n\n public getData(): Data | null {\n return this.data;\n }\n\n public getStatus(): StatusCodeType {\n return this.status;\n }\n\n public get(env?: Environment): Response {\n if (this.redirectUrl) {\n return new Response(null, {\n status: this.status,\n headers: this.header.native,\n });\n }\n\n const status = new HttpStatus();\n\n const responseData: ResponseDataType<Data> = {\n data: this.data || ({} as Data),\n message: this.message,\n success: status.isSuccessful(this.status),\n done: this.done,\n status: this.status,\n isClientError: status.isClientError(this.status),\n isServerError: status.isServerError(this.status),\n isNotFound: false,\n isUnauthorized: false,\n isForbidden: false,\n app: {\n env: env || Environment.PRODUCTION,\n },\n };\n\n return new Response(JSON.stringify(responseData), {\n status: responseData.status,\n headers: this.header.native,\n });\n }\n}\n"
6
6
  ],
7
- "mappings": "AAAA,sBAAS,wBACT,iBAAS,4BACT,qBAAS,4BAGF,MAAM,CAAwG,CACnG,OACR,KAAoB,KACpB,OAAyB,EAAW,KAAK,GACzC,YAAmC,KACnC,QAAyB,KAC1B,KAAO,GAEd,WAAW,CAAC,EAAkB,CAC5B,KAAK,OAAS,GAAU,IAAI,EAGvB,IAAI,CAAC,EAAY,EAAyB,EAAW,KAAK,GAAqB,CAQpF,OAPA,KAAK,KAAO,EACZ,KAAK,OAAS,EACd,KAAK,OAAO,QAAQ,EACpB,KAAK,OAAO,OAAO,UAAU,EAC7B,KAAK,YAAc,KACnB,KAAK,QAAU,KAER,KAGF,SAAS,CACd,EACA,EAIiB,CAQjB,OAPA,KAAK,QAAU,EACf,KAAK,OAAS,GAAQ,QAAU,EAAW,KAAK,oBAChD,KAAK,KAAO,GAAQ,M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AK,OACb,cAAe,EAAO,cAAc,KAAK,MAAM,EAC/C,cAAe,EAAO,cAAc,KAAK,MAAM,EAC/C,WAAY,GACZ,eAAgB,GAChB,YAAa,GACb,IAAK,CACH,IAAK,GAAO,EAAY,UAC1B,CACF,EAEA,OAAO,IAAI,SAAS,KAAK,UAAU,CAAY,EAAG,CAChD,OAAQ,EAAa,OACrB,QAAS,KAAK,OAAO,MACvB,CAAC,EAEL",
8
+ "debugId": "9EA7148894EF116764756E2164756E21",
9
9
  "names": []
10
10
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ooneex/http-response",
3
- "description": "HTTP response builder with status codes, headers, cookies, and content formatting support",
4
- "version": "0.17.0",
3
+ "description": "HTTP response builder with fluent API for setting status codes, headers, cookies, and streaming or buffered content delivery",
4
+ "version": "1.0.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
@@ -29,9 +29,9 @@
29
29
  "test": "bun test tests"
30
30
  },
31
31
  "dependencies": {
32
- "@ooneex/app-env": "0.0.17",
33
- "@ooneex/http-header": "0.16.0",
34
- "@ooneex/http-status": "0.0.17"
32
+ "@ooneex/app-env": "0.0.19",
33
+ "@ooneex/http-header": "0.17.0",
34
+ "@ooneex/http-status": "0.0.18"
35
35
  },
36
36
  "devDependencies": {},
37
37
  "keywords": [