@bool-ts/core 1.4.0 → 1.4.2

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.
@@ -3,7 +3,9 @@ export declare enum EArgumentTypes {
3
3
  headers = "HEADERS",
4
4
  body = "BODY",
5
5
  params = "PARAMS",
6
- query = "QUERY"
6
+ param = "PARAM",
7
+ query = "QUERY",
8
+ request = "REQUEST"
7
9
  }
8
10
  export type TMetadata = {
9
11
  index: number;
@@ -17,15 +19,25 @@ export type TMetadata = {
17
19
  } | {
18
20
  index: number;
19
21
  type: EArgumentTypes.params;
20
- key?: string;
22
+ zodSchema?: Zod.Schema;
23
+ } | {
24
+ index: number;
25
+ type: EArgumentTypes.param;
26
+ key: string;
21
27
  zodSchema?: Zod.Schema;
22
28
  } | {
23
29
  index: number;
24
30
  type: EArgumentTypes.query;
25
31
  zodSchema?: Zod.Schema;
32
+ } | {
33
+ index: number;
34
+ type: EArgumentTypes.request;
35
+ zodSchema?: Zod.Schema;
26
36
  };
27
37
  export declare const controllerActionArgumentsKey: unique symbol;
28
38
  export declare const Headers: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
29
39
  export declare const Body: (zodSchema?: Zod.Schema, parser?: "arrayBuffer" | "blob" | "formData" | "json" | "text") => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
30
- export declare const Params: (key?: string, zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
40
+ export declare const Params: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
41
+ export declare const Param: (key: string, zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
31
42
  export declare const Query: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
43
+ export declare const Request: (zodSchema?: Zod.Schema) => (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => void;
@@ -4,7 +4,9 @@ export var EArgumentTypes;
4
4
  EArgumentTypes["headers"] = "HEADERS";
5
5
  EArgumentTypes["body"] = "BODY";
6
6
  EArgumentTypes["params"] = "PARAMS";
7
+ EArgumentTypes["param"] = "PARAM";
7
8
  EArgumentTypes["query"] = "QUERY";
9
+ EArgumentTypes["request"] = "REQUEST";
8
10
  })(EArgumentTypes || (EArgumentTypes = {}));
9
11
  export const controllerActionArgumentsKey = Symbol.for("__bool:controller.action::arguments__");
10
12
  export const Headers = (zodSchema) => {
@@ -36,7 +38,7 @@ export const Body = (zodSchema, parser) => {
36
38
  Reflect.defineMetadata(controllerActionArgumentsKey, bodyMetadata, target.constructor, methodName);
37
39
  };
38
40
  };
39
- export const Params = (key, zodSchema) => {
41
+ export const Params = (zodSchema) => {
40
42
  return (target, methodName, parameterIndex) => {
41
43
  if (!methodName) {
42
44
  return;
@@ -45,6 +47,20 @@ export const Params = (key, zodSchema) => {
45
47
  paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
46
48
  index: parameterIndex,
47
49
  type: EArgumentTypes.params,
50
+ zodSchema: zodSchema
51
+ };
52
+ Reflect.defineMetadata(controllerActionArgumentsKey, paramsMetadata, target.constructor, methodName);
53
+ };
54
+ };
55
+ export const Param = (key, zodSchema) => {
56
+ return (target, methodName, parameterIndex) => {
57
+ if (!methodName) {
58
+ return;
59
+ }
60
+ const paramsMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
61
+ paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
62
+ index: parameterIndex,
63
+ type: EArgumentTypes.param,
48
64
  key: key,
49
65
  zodSchema: zodSchema
50
66
  };
@@ -65,3 +81,17 @@ export const Query = (zodSchema) => {
65
81
  Reflect.defineMetadata(controllerActionArgumentsKey, queryMetadata, target.constructor, methodName);
66
82
  };
67
83
  };
84
+ export const Request = (zodSchema) => {
85
+ return (target, methodName, parameterIndex) => {
86
+ if (!methodName) {
87
+ return;
88
+ }
89
+ const queryMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
90
+ queryMetadata[`argumentIndexes.${parameterIndex}`] = {
91
+ index: parameterIndex,
92
+ type: EArgumentTypes.request,
93
+ zodSchema: zodSchema
94
+ };
95
+ Reflect.defineMetadata(controllerActionArgumentsKey, queryMetadata, target.constructor, methodName);
96
+ };
97
+ };
@@ -1,4 +1,4 @@
1
- export { Headers, Body, Params, Query, EArgumentTypes } from "./arguments";
1
+ export { Headers, Body, Params, Param, Query, EArgumentTypes } from "./arguments";
2
2
  export { Controller, controllerKey } from "./controller";
3
3
  export { Inject, injectKey } from "./inject";
4
4
  export { Injectable, injectableKey } from "./injectable";
@@ -1,4 +1,4 @@
1
- export { Headers, Body, Params, Query, EArgumentTypes } from "./arguments";
1
+ export { Headers, Body, Params, Param, Query, EArgumentTypes } from "./arguments";
2
2
  export { Controller, controllerKey } from "./controller";
3
3
  export { Inject, injectKey } from "./inject";
4
4
  export { Injectable, injectableKey } from "./injectable";
@@ -103,9 +103,8 @@ export const BoolFactory = (target, options) => {
103
103
  const start = performance.now();
104
104
  const url = new URL(request.url);
105
105
  try {
106
- const headers = request.headers;
107
- const origin = headers.get("origin");
108
- const response = new Response();
106
+ const reqHeaders = request.headers;
107
+ const origin = reqHeaders.get("origin");
109
108
  if (!allowOrigins.includes("*")) {
110
109
  if (!origin) {
111
110
  throw new HttpClientError({
@@ -132,10 +131,6 @@ export const BoolFactory = (target, options) => {
132
131
  });
133
132
  }
134
133
  }
135
- response.headers.set("Access-Control-Allow-Origin", origin || "*");
136
- response.headers.set("Access-Control-Allow-Headers", "*");
137
- response.headers.set("Access-Control-Allow-Credentials", "true");
138
- response.headers.set("Access-Control-Allow-Methods", allowMethods.join(", "));
139
134
  if (!allowMethods.includes(request.method.toUpperCase())) {
140
135
  throw new HttpClientError({
141
136
  httpCode: 405,
@@ -153,6 +148,7 @@ export const BoolFactory = (target, options) => {
153
148
  }
154
149
  const params = result.params;
155
150
  const query = Qs.parse(url.search, options.queryParser);
151
+ let responseBody = undefined;
156
152
  for (let i = 0; i < result.handlers.length; i++) {
157
153
  const handler = result.handlers[i];
158
154
  const handlerMetadata = (Reflect.getOwnMetadata(controllerActionArgumentsKey, handler.constructor, handler.funcName) || {});
@@ -162,8 +158,8 @@ export const BoolFactory = (target, options) => {
162
158
  switch (argsMetadata.type) {
163
159
  case EArgumentTypes.headers:
164
160
  controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
165
- ? headers
166
- : await controllerActionArgumentsResolution(headers, argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
161
+ ? reqHeaders
162
+ : await controllerActionArgumentsResolution(reqHeaders, argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
167
163
  break;
168
164
  case EArgumentTypes.body:
169
165
  controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
@@ -180,14 +176,41 @@ export const BoolFactory = (target, options) => {
180
176
  ? query
181
177
  : await controllerActionArgumentsResolution(query, argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
182
178
  break;
179
+ case EArgumentTypes.param:
180
+ controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
181
+ ? !(argsMetadata.key in params)
182
+ ? undefined
183
+ : params[argsMetadata.key]
184
+ : await controllerActionArgumentsResolution(query, argsMetadata.zodSchema, argsMetadata.index, handler.funcName);
185
+ break;
186
+ case EArgumentTypes.request:
187
+ controllerActionArguments[argsMetadata.index] = request;
188
+ break;
183
189
  }
184
190
  }
185
191
  }
186
- const response = await handler.func(...controllerActionArguments);
187
- if (response instanceof Response) {
188
- return response;
192
+ const responseData = await handler.func(...controllerActionArguments);
193
+ if (responseData instanceof Response) {
194
+ return responseData;
189
195
  }
196
+ responseBody = responseData;
190
197
  }
198
+ const resHeaders = new Headers({
199
+ "Access-Control-Allow-Origin": origin || "*",
200
+ "Access-Control-Allow-Headers": "*",
201
+ "Access-Control-Allow-Credentials": "true",
202
+ "Access-Control-Allow-Methods": allowMethods.join(", "),
203
+ "Content-Type": "application/json"
204
+ });
205
+ const response = new Response(JSON.stringify({
206
+ httpCode: 200,
207
+ message: "Success",
208
+ data: responseBody
209
+ }), {
210
+ status: 200,
211
+ statusText: "Success",
212
+ headers: resHeaders
213
+ });
191
214
  return response;
192
215
  }
193
216
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bool-ts/core",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -4,7 +4,9 @@ export enum EArgumentTypes {
4
4
  headers = "HEADERS",
5
5
  body = "BODY",
6
6
  params = "PARAMS",
7
- query = "QUERY"
7
+ param = "PARAM",
8
+ query = "QUERY",
9
+ request = "REQUEST"
8
10
  }
9
11
 
10
12
  export type TMetadata =
@@ -22,13 +24,23 @@ export type TMetadata =
22
24
  | {
23
25
  index: number;
24
26
  type: EArgumentTypes.params;
25
- key?: string;
27
+ zodSchema?: Zod.Schema;
28
+ }
29
+ | {
30
+ index: number;
31
+ type: EArgumentTypes.param;
32
+ key: string;
26
33
  zodSchema?: Zod.Schema;
27
34
  }
28
35
  | {
29
36
  index: number;
30
37
  type: EArgumentTypes.query;
31
38
  zodSchema?: Zod.Schema;
39
+ }
40
+ | {
41
+ index: number;
42
+ type: EArgumentTypes.request;
43
+ zodSchema?: Zod.Schema;
32
44
  };
33
45
 
34
46
  export const controllerActionArgumentsKey = Symbol.for("__bool:controller.action::arguments__");
@@ -80,7 +92,7 @@ export const Body = (zodSchema?: Zod.Schema, parser?: "arrayBuffer" | "blob" | "
80
92
  };
81
93
  };
82
94
 
83
- export const Params = (key?: string, zodSchema?: Zod.Schema) => {
95
+ export const Params = (zodSchema?: Zod.Schema) => {
84
96
  return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
85
97
  if (!methodName) {
86
98
  return;
@@ -91,7 +103,6 @@ export const Params = (key?: string, zodSchema?: Zod.Schema) => {
91
103
  paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
92
104
  index: parameterIndex,
93
105
  type: EArgumentTypes.params,
94
- key: key,
95
106
  zodSchema: zodSchema
96
107
  } satisfies Extract<
97
108
  TMetadata,
@@ -104,6 +115,30 @@ export const Params = (key?: string, zodSchema?: Zod.Schema) => {
104
115
  };
105
116
  };
106
117
 
118
+ export const Param = (key: string, zodSchema?: Zod.Schema) => {
119
+ return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
120
+ if (!methodName) {
121
+ return;
122
+ }
123
+
124
+ const paramsMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
125
+
126
+ paramsMetadata[`argumentIndexes.${parameterIndex}`] = {
127
+ index: parameterIndex,
128
+ type: EArgumentTypes.param,
129
+ key: key,
130
+ zodSchema: zodSchema
131
+ } satisfies Extract<
132
+ TMetadata,
133
+ {
134
+ type: EArgumentTypes.param;
135
+ }
136
+ >;
137
+
138
+ Reflect.defineMetadata(controllerActionArgumentsKey, paramsMetadata, target.constructor, methodName);
139
+ };
140
+ };
141
+
107
142
  export const Query = (zodSchema?: Zod.Schema) => {
108
143
  return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
109
144
  if (!methodName) {
@@ -126,3 +161,26 @@ export const Query = (zodSchema?: Zod.Schema) => {
126
161
  Reflect.defineMetadata(controllerActionArgumentsKey, queryMetadata, target.constructor, methodName);
127
162
  };
128
163
  };
164
+
165
+ export const Request = (zodSchema?: Zod.Schema) => {
166
+ return (target: Object, methodName: string | symbol | undefined, parameterIndex: number) => {
167
+ if (!methodName) {
168
+ return;
169
+ }
170
+
171
+ const queryMetadata = Reflect.getOwnMetadata(controllerActionArgumentsKey, target.constructor, methodName) || {};
172
+
173
+ queryMetadata[`argumentIndexes.${parameterIndex}`] = {
174
+ index: parameterIndex,
175
+ type: EArgumentTypes.request,
176
+ zodSchema: zodSchema
177
+ } satisfies Extract<
178
+ TMetadata,
179
+ {
180
+ type: EArgumentTypes.request;
181
+ }
182
+ >;
183
+
184
+ Reflect.defineMetadata(controllerActionArgumentsKey, queryMetadata, target.constructor, methodName);
185
+ };
186
+ };
@@ -1,4 +1,4 @@
1
- export { Headers, Body, Params, Query, EArgumentTypes } from "./arguments";
1
+ export { Headers, Body, Params, Param, Query, EArgumentTypes } from "./arguments";
2
2
  export { Controller, controllerKey } from "./controller";
3
3
  export { Inject, injectKey } from "./inject";
4
4
  export { Injectable, injectableKey } from "./injectable";
@@ -139,43 +139,58 @@ export const BoolFactory = (target: new (...args: any[]) => unknown, options: TB
139
139
  const url = new URL(request.url);
140
140
 
141
141
  try {
142
- const headers = request.headers;
143
- const origin = headers.get("origin");
144
- const response = new Response();
142
+ const reqHeaders = request.headers;
143
+ const origin = reqHeaders.get("origin");
144
+ const resHeaders = new Headers({
145
+ "Access-Control-Allow-Origin": origin || "*",
146
+ "Access-Control-Allow-Headers": "*",
147
+ "Access-Control-Allow-Credentials": "true",
148
+ "Access-Control-Allow-Methods": allowMethods.join(", "),
149
+ "Content-Type": "application/json"
150
+ });
145
151
 
146
152
  if (!allowOrigins.includes("*")) {
147
153
  if (!origin) {
148
- throw new HttpClientError({
149
- httpCode: 403,
150
- message: "Origin not found.",
151
- data: {
152
- origin: {
153
- code: "origin:invalid:0x00001",
154
- message: "Origin not found."
154
+ return new Response(
155
+ JSON.stringify({
156
+ httpCode: 403,
157
+ message: "Origin not found.",
158
+ data: {
159
+ origin: {
160
+ code: "origin:invalid:0x00001",
161
+ message: "Origin not found."
162
+ }
155
163
  }
164
+ }),
165
+ {
166
+ status: 403,
167
+ statusText: "Origin not found.",
168
+ headers: resHeaders
156
169
  }
157
- });
170
+ );
158
171
  }
159
172
 
160
173
  if (!allowOrigins.includes(origin)) {
161
- throw new HttpClientError({
162
- httpCode: 403,
163
- message: "Invalid origin.",
164
- data: {
165
- origin: {
166
- code: "origin:invalid:0x00002",
167
- message: "Invalid origin."
174
+ return new Response(
175
+ JSON.stringify({
176
+ httpCode: 403,
177
+ message: "Origin not found.",
178
+ data: {
179
+ origin: {
180
+ code: "origin:invalid:0x00002",
181
+ message: "Invalid origin."
182
+ }
168
183
  }
184
+ }),
185
+ {
186
+ status: 403,
187
+ statusText: "Invalid origin.",
188
+ headers: resHeaders
169
189
  }
170
- });
190
+ );
171
191
  }
172
192
  }
173
193
 
174
- response.headers.set("Access-Control-Allow-Origin", origin || "*");
175
- response.headers.set("Access-Control-Allow-Headers", "*");
176
- response.headers.set("Access-Control-Allow-Credentials", "true");
177
- response.headers.set("Access-Control-Allow-Methods", allowMethods.join(", "));
178
-
179
194
  if (!allowMethods.includes(request.method.toUpperCase())) {
180
195
  throw new HttpClientError({
181
196
  httpCode: 405,
@@ -197,6 +212,8 @@ export const BoolFactory = (target: new (...args: any[]) => unknown, options: TB
197
212
  const params = result.params;
198
213
  const query = Qs.parse(url.search, options.queryParser);
199
214
 
215
+ let responseBody = undefined;
216
+
200
217
  for (let i = 0; i < result.handlers.length; i++) {
201
218
  const handler = result.handlers[i];
202
219
  const handlerMetadata = (Reflect.getOwnMetadata(
@@ -212,9 +229,9 @@ export const BoolFactory = (target: new (...args: any[]) => unknown, options: TB
212
229
  switch (argsMetadata.type) {
213
230
  case EArgumentTypes.headers:
214
231
  controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
215
- ? headers
232
+ ? reqHeaders
216
233
  : await controllerActionArgumentsResolution(
217
- headers,
234
+ reqHeaders,
218
235
  argsMetadata.zodSchema,
219
236
  argsMetadata.index,
220
237
  handler.funcName
@@ -250,17 +267,47 @@ export const BoolFactory = (target: new (...args: any[]) => unknown, options: TB
250
267
  handler.funcName
251
268
  );
252
269
  break;
270
+ case EArgumentTypes.param:
271
+ controllerActionArguments[argsMetadata.index] = !argsMetadata.zodSchema
272
+ ? !(argsMetadata.key in params)
273
+ ? undefined
274
+ : params[argsMetadata.key]
275
+ : await controllerActionArgumentsResolution(
276
+ query,
277
+ argsMetadata.zodSchema,
278
+ argsMetadata.index,
279
+ handler.funcName
280
+ );
281
+ break;
282
+ case EArgumentTypes.request:
283
+ controllerActionArguments[argsMetadata.index] = request;
284
+ break;
253
285
  }
254
286
  }
255
287
  }
256
288
 
257
- const response = await handler.func(...controllerActionArguments);
289
+ const responseData = await handler.func(...controllerActionArguments);
258
290
 
259
- if (response instanceof Response) {
260
- return response;
291
+ if (responseData instanceof Response) {
292
+ return responseData;
261
293
  }
294
+
295
+ responseBody = responseData;
262
296
  }
263
297
 
298
+ const response = new Response(
299
+ JSON.stringify({
300
+ httpCode: 200,
301
+ message: "Success",
302
+ data: responseBody
303
+ }),
304
+ {
305
+ status: 200,
306
+ statusText: "Success",
307
+ headers: resHeaders
308
+ }
309
+ );
310
+
264
311
  return response;
265
312
  } catch (error) {
266
313
  return jsonErrorInfer(error);