@bool-ts/core 1.7.10 → 1.7.11

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.
@@ -23,8 +23,10 @@ export type TBoolFactoryOptions = Required<{
23
23
  queryParser: Parameters<typeof Qs.parse>[1];
24
24
  static: Required<{
25
25
  path: string;
26
- }> & Partial<{}>;
27
- allowOrigins: Array<string>;
26
+ }> & Partial<{
27
+ headers: Record<string, string>;
28
+ }>;
29
+ allowOrigins: string | Array<string>;
28
30
  allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
29
31
  }>;
30
32
  export declare const responseConverter: (response: Response) => Response;
@@ -201,11 +201,11 @@ export const moduleResolution = async (module, options) => {
201
201
  });
202
202
  };
203
203
  const fetcher = async (bun, bool) => {
204
- const { query, route: { parameters, model }, moduleResolution: { startMiddlewareGroup, endMiddlewareGroup, guardGroup, openDispatcherGroup, closeDispatcherGroup } } = bool;
204
+ const { query, responseHeaders, route: { parameters, model }, moduleResolution: { startMiddlewareGroup, endMiddlewareGroup, guardGroup, openDispatcherGroup, closeDispatcherGroup } } = bool;
205
205
  const { request, server: _server } = bun;
206
206
  const context = {
207
207
  [requestHeadersArgsKey]: request.headers,
208
- [responseHeadersArgsKey]: new Headers(),
208
+ [responseHeadersArgsKey]: responseHeaders,
209
209
  [queryArgsKey]: query,
210
210
  [paramsArgsKey]: parameters,
211
211
  [routeModelArgsKey]: model
@@ -571,7 +571,13 @@ export const BoolFactory = async (modules, options) => {
571
571
  const { allowLogsMethods, staticOption, allowOrigins, allowMethods } = Object.freeze({
572
572
  allowLogsMethods: options?.log?.methods,
573
573
  staticOption: options.static,
574
- allowOrigins: options.allowOrigins,
574
+ allowOrigins: !options.allowOrigins
575
+ ? ["*"]
576
+ : typeof options.allowOrigins !== "string"
577
+ ? options.allowOrigins.includes("*") || options.allowOrigins.length < 1
578
+ ? ["*"]
579
+ : options.allowOrigins
580
+ : [options.allowOrigins !== "*" ? options.allowOrigins : "*"],
575
581
  allowMethods: options.allowMethods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
576
582
  });
577
583
  const moduleResolutions = await Promise.all(modulesConverted.map((moduleConverted) => moduleResolution(moduleConverted, options)));
@@ -588,57 +594,34 @@ export const BoolFactory = async (modules, options) => {
588
594
  const start = performance.now();
589
595
  const url = new URL(request.url);
590
596
  const query = Qs.parse(url.searchParams.toString(), options.queryParser);
591
- const origin = request.headers.get("origin");
597
+ const origin = request.headers.get("origin") || "*";
598
+ const responseHeaders = new Headers();
592
599
  try {
593
600
  if (request.method.toUpperCase() === "OPTIONS") {
594
- if (!origin) {
595
- return !allowOrigins
596
- ? new Response(undefined, {
597
- status: 204,
598
- statusText: "No Content.",
599
- headers: {
600
- "Content-Type": "text/plain",
601
- "Access-Control-Allow-Origin": "*",
602
- "Access-Control-Allow-Credentials": "true",
603
- "Access-Control-Allow-Headers": "*",
604
- "Access-Control-Allow-Methods": allowMethods.join(", ")
605
- }
606
- })
607
- : new Response(undefined, {
608
- status: 417,
609
- statusText: "Origin Disallowed."
610
- });
611
- }
612
- else {
613
- return allowOrigins && !allowOrigins.includes(origin)
614
- ? new Response(undefined, {
615
- status: 417,
616
- statusText: "Origin Disallowed."
617
- })
618
- : new Response(undefined, {
619
- status: 204,
620
- statusText: "No Content.",
621
- headers: {
622
- "Content-Type": "text/plain",
623
- "Access-Control-Allow-Origin": origin,
624
- "Access-Control-Allow-Credentials": "true",
625
- "Access-Control-Allow-Headers": "*",
626
- "Access-Control-Allow-Methods": allowMethods.join(", ")
627
- }
628
- });
629
- }
601
+ return responseConverter(!allowOrigins.includes(origin)
602
+ ? new Response(undefined, {
603
+ status: 417,
604
+ statusText: "Origin Disallowed."
605
+ })
606
+ : new Response(undefined, {
607
+ status: 204,
608
+ statusText: "No Content.",
609
+ headers: {
610
+ "Access-Control-Allow-Origin": "*",
611
+ "Access-Control-Allow-Methods": allowMethods.join(", ")
612
+ }
613
+ }));
630
614
  }
631
615
  if (staticOption) {
632
616
  const file = Bun.file(`${staticOption.path}/${url.pathname}`);
633
617
  const isFileExists = await file.exists();
634
618
  if (isFileExists) {
635
- return new Response(await file.arrayBuffer(), {
619
+ responseHeaders.set("Content-Type", file.type);
620
+ return responseConverter(new Response(await file.arrayBuffer(), {
636
621
  status: 200,
637
622
  statusText: "SUCCESS",
638
- headers: {
639
- "Content-Type": file.type
640
- }
641
- });
623
+ headers: responseHeaders
624
+ }));
642
625
  }
643
626
  }
644
627
  let collection;
@@ -664,13 +647,14 @@ export const BoolFactory = async (modules, options) => {
664
647
  server
665
648
  }, {
666
649
  query: query,
650
+ responseHeaders: responseHeaders,
667
651
  route: collection.route,
668
652
  moduleResolution: collection.resolution
669
653
  });
670
654
  }
671
655
  catch (error) {
672
656
  options.debug && console.error(error);
673
- return responseConverter(jsonErrorInfer(error));
657
+ return responseConverter(jsonErrorInfer(error, responseHeaders));
674
658
  }
675
659
  finally {
676
660
  if (allowLogsMethods) {
@@ -9,6 +9,6 @@ export type THttpMethods = {
9
9
  TRACE: "TRACE";
10
10
  PATCH: "PATCH";
11
11
  };
12
- export declare const jsonErrorInfer: (data: any) => Response;
12
+ export declare const jsonErrorInfer: (data: any, headers?: Headers) => Response;
13
13
  export * from "./clientError";
14
14
  export * from "./serverError";
@@ -1,7 +1,6 @@
1
1
  import { HttpClientError } from "./clientError";
2
2
  import { HttpServerError } from "./serverError";
3
- export const jsonErrorInfer = (data) => {
4
- const headers = new Headers();
3
+ export const jsonErrorInfer = (data, headers = new Headers()) => {
5
4
  headers.set("Content-Type", "application/json");
6
5
  if (data instanceof HttpClientError || data instanceof HttpServerError) {
7
6
  return new Response(JSON.stringify(data), {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bool-ts/core",
3
- "version": "1.7.10",
3
+ "version": "1.7.11",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -55,8 +55,10 @@ export type TBoolFactoryOptions = Required<{
55
55
  static: Required<{
56
56
  path: string;
57
57
  }> &
58
- Partial<{}>;
59
- allowOrigins: Array<string>;
58
+ Partial<{
59
+ headers: Record<string, string>;
60
+ }>;
61
+ allowOrigins: string | Array<string>;
60
62
  allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
61
63
  }>;
62
64
 
@@ -325,6 +327,7 @@ const fetcher = async (
325
327
  server: Server;
326
328
  }>,
327
329
  bool: Required<{
330
+ responseHeaders: Headers;
328
331
  query: Record<string, unknown>;
329
332
  route: NonNullable<ReturnType<RouterGroup["find"]>>;
330
333
  moduleResolution: NonNullable<Awaited<ReturnType<typeof moduleResolution>>>;
@@ -332,6 +335,7 @@ const fetcher = async (
332
335
  ) => {
333
336
  const {
334
337
  query,
338
+ responseHeaders,
335
339
  route: { parameters, model },
336
340
  moduleResolution: { startMiddlewareGroup, endMiddlewareGroup, guardGroup, openDispatcherGroup, closeDispatcherGroup }
337
341
  } = bool;
@@ -339,7 +343,7 @@ const fetcher = async (
339
343
 
340
344
  const context: Record<symbol, any> = {
341
345
  [requestHeadersArgsKey]: request.headers,
342
- [responseHeadersArgsKey]: new Headers(),
346
+ [responseHeadersArgsKey]: responseHeaders,
343
347
  [queryArgsKey]: query,
344
348
  [paramsArgsKey]: parameters,
345
349
  [routeModelArgsKey]: model
@@ -925,7 +929,13 @@ export const BoolFactory = async (
925
929
  const { allowLogsMethods, staticOption, allowOrigins, allowMethods } = Object.freeze({
926
930
  allowLogsMethods: options?.log?.methods,
927
931
  staticOption: options.static,
928
- allowOrigins: options.allowOrigins,
932
+ allowOrigins: !options.allowOrigins
933
+ ? ["*"]
934
+ : typeof options.allowOrigins !== "string"
935
+ ? options.allowOrigins.includes("*") || options.allowOrigins.length < 1
936
+ ? ["*"]
937
+ : options.allowOrigins
938
+ : [options.allowOrigins !== "*" ? options.allowOrigins : "*"],
929
939
  allowMethods: options.allowMethods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
930
940
  });
931
941
 
@@ -951,29 +961,13 @@ export const BoolFactory = async (
951
961
  const start = performance.now();
952
962
  const url = new URL(request.url);
953
963
  const query = Qs.parse(url.searchParams.toString(), options.queryParser);
954
- const origin = request.headers.get("origin");
964
+ const origin = request.headers.get("origin") || "*";
965
+ const responseHeaders = new Headers();
955
966
 
956
967
  try {
957
968
  if (request.method.toUpperCase() === "OPTIONS") {
958
- if (!origin) {
959
- return !allowOrigins
960
- ? new Response(undefined, {
961
- status: 204,
962
- statusText: "No Content.",
963
- headers: {
964
- "Content-Type": "text/plain",
965
- "Access-Control-Allow-Origin": "*",
966
- "Access-Control-Allow-Credentials": "true",
967
- "Access-Control-Allow-Headers": "*",
968
- "Access-Control-Allow-Methods": allowMethods.join(", ")
969
- }
970
- })
971
- : new Response(undefined, {
972
- status: 417,
973
- statusText: "Origin Disallowed."
974
- });
975
- } else {
976
- return allowOrigins && !allowOrigins.includes(origin)
969
+ return responseConverter(
970
+ !allowOrigins.includes(origin)
977
971
  ? new Response(undefined, {
978
972
  status: 417,
979
973
  statusText: "Origin Disallowed."
@@ -982,14 +976,11 @@ export const BoolFactory = async (
982
976
  status: 204,
983
977
  statusText: "No Content.",
984
978
  headers: {
985
- "Content-Type": "text/plain",
986
- "Access-Control-Allow-Origin": origin,
987
- "Access-Control-Allow-Credentials": "true",
988
- "Access-Control-Allow-Headers": "*",
979
+ "Access-Control-Allow-Origin": "*",
989
980
  "Access-Control-Allow-Methods": allowMethods.join(", ")
990
981
  }
991
- });
992
- }
982
+ })
983
+ );
993
984
  }
994
985
 
995
986
  if (staticOption) {
@@ -997,13 +988,15 @@ export const BoolFactory = async (
997
988
  const isFileExists = await file.exists();
998
989
 
999
990
  if (isFileExists) {
1000
- return new Response(await file.arrayBuffer(), {
1001
- status: 200,
1002
- statusText: "SUCCESS",
1003
- headers: {
1004
- "Content-Type": file.type
1005
- }
1006
- });
991
+ responseHeaders.set("Content-Type", file.type);
992
+
993
+ return responseConverter(
994
+ new Response(await file.arrayBuffer(), {
995
+ status: 200,
996
+ statusText: "SUCCESS",
997
+ headers: responseHeaders
998
+ })
999
+ );
1007
1000
  }
1008
1001
  }
1009
1002
 
@@ -1048,6 +1041,7 @@ export const BoolFactory = async (
1048
1041
  },
1049
1042
  {
1050
1043
  query: query,
1044
+ responseHeaders: responseHeaders,
1051
1045
  route: collection.route,
1052
1046
  moduleResolution: collection.resolution
1053
1047
  }
@@ -1055,7 +1049,7 @@ export const BoolFactory = async (
1055
1049
  } catch (error) {
1056
1050
  options.debug && console.error(error);
1057
1051
 
1058
- return responseConverter(jsonErrorInfer(error));
1052
+ return responseConverter(jsonErrorInfer(error, responseHeaders));
1059
1053
  } finally {
1060
1054
  if (allowLogsMethods) {
1061
1055
  const end = performance.now();
package/src/http/index.ts CHANGED
@@ -13,8 +13,7 @@ export type THttpMethods = {
13
13
  PATCH: "PATCH";
14
14
  };
15
15
 
16
- export const jsonErrorInfer = (data: any) => {
17
- const headers = new Headers();
16
+ export const jsonErrorInfer = (data: any, headers: Headers = new Headers()) => {
18
17
  headers.set("Content-Type", "application/json");
19
18
 
20
19
  if (data instanceof HttpClientError || data instanceof HttpServerError) {