@ooneex/http-response 0.13.0 → 0.14.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/dist/index.js +2 -86
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,87 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import { Environment } from "@ooneex/app-env";
|
|
3
|
-
import { Header } from "@ooneex/http-header";
|
|
4
|
-
import { HttpStatus } from "@ooneex/http-status";
|
|
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};
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
header;
|
|
8
|
-
data = null;
|
|
9
|
-
status = HttpStatus.Code.OK;
|
|
10
|
-
redirectUrl = null;
|
|
11
|
-
message = null;
|
|
12
|
-
done = false;
|
|
13
|
-
constructor(header) {
|
|
14
|
-
this.header = header || new Header;
|
|
15
|
-
}
|
|
16
|
-
json(data, status = HttpStatus.Code.OK) {
|
|
17
|
-
this.data = data;
|
|
18
|
-
this.status = status;
|
|
19
|
-
this.header.setJson();
|
|
20
|
-
this.header.remove("Location");
|
|
21
|
-
this.redirectUrl = null;
|
|
22
|
-
this.message = null;
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
exception(message, config) {
|
|
26
|
-
this.message = message;
|
|
27
|
-
this.status = config?.status ?? HttpStatus.Code.InternalServerError;
|
|
28
|
-
this.data = config?.data || null;
|
|
29
|
-
this.redirectUrl = null;
|
|
30
|
-
this.header.setJson();
|
|
31
|
-
this.header.remove("Location");
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
notFound(message, config) {
|
|
35
|
-
this.message = message;
|
|
36
|
-
this.status = config?.status || HttpStatus.Code.NotFound;
|
|
37
|
-
this.data = config?.data || null;
|
|
38
|
-
this.redirectUrl = null;
|
|
39
|
-
this.header.setJson();
|
|
40
|
-
this.header.remove("Location");
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
redirect(url, status = HttpStatus.Code.Found) {
|
|
44
|
-
this.redirectUrl = url;
|
|
45
|
-
this.status = status;
|
|
46
|
-
this.header.setLocation(url.toString());
|
|
47
|
-
this.data = null;
|
|
48
|
-
this.message = null;
|
|
49
|
-
return this;
|
|
50
|
-
}
|
|
51
|
-
getData() {
|
|
52
|
-
return this.data;
|
|
53
|
-
}
|
|
54
|
-
get(env) {
|
|
55
|
-
if (this.redirectUrl) {
|
|
56
|
-
return new Response(null, {
|
|
57
|
-
status: this.status,
|
|
58
|
-
headers: this.header.native
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
const status = new HttpStatus;
|
|
62
|
-
const responseData = {
|
|
63
|
-
data: this.data || {},
|
|
64
|
-
message: this.message,
|
|
65
|
-
success: status.isSuccessful(this.status),
|
|
66
|
-
done: this.done,
|
|
67
|
-
status: this.status,
|
|
68
|
-
isClientError: status.isClientError(this.status),
|
|
69
|
-
isServerError: status.isServerError(this.status),
|
|
70
|
-
isNotFound: false,
|
|
71
|
-
isUnauthorized: false,
|
|
72
|
-
isForbidden: false,
|
|
73
|
-
app: {
|
|
74
|
-
env: env || Environment.PRODUCTION
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
return new Response(JSON.stringify(responseData), {
|
|
78
|
-
status: responseData.status,
|
|
79
|
-
headers: this.header.native
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
export {
|
|
84
|
-
HttpResponse
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
//# debugId=509062B587B588D864756E2164756E21
|
|
3
|
+
//# debugId=A1CEA356DBFBE4D664756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
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"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
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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