@bool-ts/core 1.5.2 → 1.5.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.
@@ -1,11 +1,7 @@
1
1
  import type { IModule } from "../interfaces/module";
2
2
  type TInstances = Array<new (...args: any[]) => any>;
3
3
  export type TModuleOptions = Partial<{
4
- options: Partial<{
5
- prefix: string;
6
- allowOrigins: string | Array<string>;
7
- allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
8
- }>;
4
+ prefix: string;
9
5
  dependencies: TInstances;
10
6
  controllers: TInstances;
11
7
  middlewares: TInstances;
@@ -14,11 +10,7 @@ export type TModuleOptions = Partial<{
14
10
  afterDispatchers: TInstances;
15
11
  }> | undefined;
16
12
  export type TModuleMetadata = Partial<{
17
- options: Partial<{
18
- prefix: string;
19
- allowOrigins: string | Array<string>;
20
- allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
21
- }>;
13
+ prefix: string;
22
14
  controllers: TInstances;
23
15
  dependencies: TInstances;
24
16
  middlewares: TInstances;
@@ -90,7 +90,7 @@ export const BoolFactory = (target, options) => {
90
90
  fetch: () => new Response()
91
91
  });
92
92
  }
93
- const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, options: moduleOptions } = moduleMetadata;
93
+ const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, prefix: modulePrefix } = moduleMetadata;
94
94
  // Middleware(s)
95
95
  const middlewareGroup = !middlewares
96
96
  ? []
@@ -127,15 +127,7 @@ export const BoolFactory = (target, options) => {
127
127
  // Controller(s)
128
128
  const routerGroup = new RouterGroup();
129
129
  controllers &&
130
- controllers.map((controllerConstructor) => controllerCreator(controllerConstructor, routerGroup, `${options.prefix || ""}/${moduleOptions?.prefix || ""}`));
131
- const allowOrigins = !moduleOptions?.allowOrigins
132
- ? ["*"]
133
- : typeof moduleOptions.allowOrigins !== "string"
134
- ? moduleOptions.allowOrigins
135
- : [moduleOptions.allowOrigins];
136
- const allowMethods = !moduleOptions?.allowMethods
137
- ? ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
138
- : moduleOptions.allowMethods;
130
+ controllers.map((controllerConstructor) => controllerCreator(controllerConstructor, routerGroup, `${options.prefix || ""}/${modulePrefix || ""}`));
139
131
  const { allowLogsMethods } = Object.freeze({
140
132
  allowLogsMethods: options?.log?.methods
141
133
  });
@@ -155,64 +147,9 @@ export const BoolFactory = (target, options) => {
155
147
  fetch: async (request) => {
156
148
  const start = performance.now();
157
149
  const url = new URL(request.url);
150
+ const reqHeaders = request.headers;
151
+ const resHeaders = new Headers();
158
152
  try {
159
- const reqHeaders = request.headers;
160
- const origin = reqHeaders.get("origin");
161
- const resHeaders = new Headers({
162
- "Access-Control-Allow-Origin": origin || "*",
163
- "Access-Control-Allow-Headers": "*",
164
- "Access-Control-Allow-Credentials": "true",
165
- "Access-Control-Allow-Methods": allowMethods.join(", "),
166
- "Content-Type": "application/json"
167
- });
168
- if (!allowOrigins.includes("*")) {
169
- if (!origin) {
170
- return new Response(JSON.stringify({
171
- httpCode: 403,
172
- message: "Origin not found.",
173
- data: {
174
- origin: {
175
- code: "origin:invalid:0x00001",
176
- message: "Origin not found."
177
- }
178
- }
179
- }), {
180
- status: 403,
181
- statusText: "Origin not found.",
182
- headers: resHeaders
183
- });
184
- }
185
- if (!allowOrigins.includes(origin)) {
186
- return new Response(JSON.stringify({
187
- httpCode: 400,
188
- message: "Origin not found.",
189
- data: {
190
- origin: {
191
- code: "origin:invalid:0x00002",
192
- message: "Invalid origin."
193
- }
194
- }
195
- }), {
196
- status: 400,
197
- statusText: "Invalid origin.",
198
- headers: resHeaders
199
- });
200
- }
201
- }
202
- if (request.method.toUpperCase() === "OPTIONS") {
203
- return new Response(undefined, {
204
- status: 204,
205
- statusText: "SUCCESS",
206
- headers: resHeaders
207
- });
208
- }
209
- if (!allowMethods.includes(request.method.toUpperCase())) {
210
- throw new HttpClientError({
211
- httpCode: 405,
212
- message: "Method Not Allowed.",
213
- data: undefined
214
- });
215
- }
216
153
  // Execute middleware(s)
217
154
  for (let i = 0; i < middlewareGroup.length; i++) {
218
155
  const middlewareArguments = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bool-ts/core",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -9,11 +9,7 @@ type TInstances = Array<new (...args: any[]) => any>;
9
9
 
10
10
  export type TModuleOptions =
11
11
  | Partial<{
12
- options: Partial<{
13
- prefix: string;
14
- allowOrigins: string | Array<string>;
15
- allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
16
- }>;
12
+ prefix: string;
17
13
  dependencies: TInstances;
18
14
  controllers: TInstances;
19
15
  middlewares: TInstances;
@@ -25,11 +21,7 @@ export type TModuleOptions =
25
21
 
26
22
  export type TModuleMetadata =
27
23
  | Partial<{
28
- options: Partial<{
29
- prefix: string;
30
- allowOrigins: string | Array<string>;
31
- allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
32
- }>;
24
+ prefix: string;
33
25
  controllers: TInstances;
34
26
  dependencies: TInstances;
35
27
  middlewares: TInstances;
@@ -132,7 +132,7 @@ export const BoolFactory = (target: new (...args: any[]) => unknown, options: TB
132
132
  });
133
133
  }
134
134
 
135
- const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, options: moduleOptions } = moduleMetadata;
135
+ const { middlewares, guards, beforeDispatchers, controllers, afterDispatchers, prefix: modulePrefix } = moduleMetadata;
136
136
 
137
137
  // Middleware(s)
138
138
  const middlewareGroup = !middlewares
@@ -178,17 +178,9 @@ export const BoolFactory = (target: new (...args: any[]) => unknown, options: TB
178
178
 
179
179
  controllers &&
180
180
  controllers.map((controllerConstructor) =>
181
- controllerCreator(controllerConstructor, routerGroup, `${options.prefix || ""}/${moduleOptions?.prefix || ""}`)
181
+ controllerCreator(controllerConstructor, routerGroup, `${options.prefix || ""}/${modulePrefix || ""}`)
182
182
  );
183
183
 
184
- const allowOrigins = !moduleOptions?.allowOrigins
185
- ? ["*"]
186
- : typeof moduleOptions.allowOrigins !== "string"
187
- ? moduleOptions.allowOrigins
188
- : [moduleOptions.allowOrigins];
189
- const allowMethods = !moduleOptions?.allowMethods
190
- ? ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
191
- : moduleOptions.allowMethods;
192
184
  const { allowLogsMethods } = Object.freeze({
193
185
  allowLogsMethods: options?.log?.methods
194
186
  });
@@ -211,76 +203,10 @@ export const BoolFactory = (target: new (...args: any[]) => unknown, options: TB
211
203
  fetch: async (request) => {
212
204
  const start = performance.now();
213
205
  const url = new URL(request.url);
206
+ const reqHeaders = request.headers;
207
+ const resHeaders = new Headers();
214
208
 
215
209
  try {
216
- const reqHeaders = request.headers;
217
- const origin = reqHeaders.get("origin");
218
- const resHeaders = new Headers({
219
- "Access-Control-Allow-Origin": origin || "*",
220
- "Access-Control-Allow-Headers": "*",
221
- "Access-Control-Allow-Credentials": "true",
222
- "Access-Control-Allow-Methods": allowMethods.join(", "),
223
- "Content-Type": "application/json"
224
- });
225
-
226
- if (!allowOrigins.includes("*")) {
227
- if (!origin) {
228
- return new Response(
229
- JSON.stringify({
230
- httpCode: 403,
231
- message: "Origin not found.",
232
- data: {
233
- origin: {
234
- code: "origin:invalid:0x00001",
235
- message: "Origin not found."
236
- }
237
- }
238
- }),
239
- {
240
- status: 403,
241
- statusText: "Origin not found.",
242
- headers: resHeaders
243
- }
244
- );
245
- }
246
-
247
- if (!allowOrigins.includes(origin)) {
248
- return new Response(
249
- JSON.stringify({
250
- httpCode: 400,
251
- message: "Origin not found.",
252
- data: {
253
- origin: {
254
- code: "origin:invalid:0x00002",
255
- message: "Invalid origin."
256
- }
257
- }
258
- }),
259
- {
260
- status: 400,
261
- statusText: "Invalid origin.",
262
- headers: resHeaders
263
- }
264
- );
265
- }
266
- }
267
-
268
- if (request.method.toUpperCase() === "OPTIONS") {
269
- return new Response(undefined, {
270
- status: 204,
271
- statusText: "SUCCESS",
272
- headers: resHeaders
273
- });
274
- }
275
-
276
- if (!allowMethods.includes(request.method.toUpperCase())) {
277
- throw new HttpClientError({
278
- httpCode: 405,
279
- message: "Method Not Allowed.",
280
- data: undefined
281
- });
282
- }
283
-
284
210
  // Execute middleware(s)
285
211
  for (let i = 0; i < middlewareGroup.length; i++) {
286
212
  const middlewareArguments = [];