@bool-ts/core 1.7.12 → 1.7.14

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.
@@ -26,8 +26,12 @@ export type TBoolFactoryOptions = Required<{
26
26
  }> & Partial<{
27
27
  headers: Record<string, string>;
28
28
  }>;
29
- allowOrigins: string | Array<string>;
30
- allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
29
+ cors: Partial<{
30
+ credentials: boolean;
31
+ origins: string | Array<string>;
32
+ methods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
33
+ headers: Array<string>;
34
+ }>;
31
35
  }>;
32
36
  export declare const responseConverter: (response: Response) => Response;
33
37
  export declare const controllerCreator: (controllerConstructor: new (...args: any[]) => unknown, group: RouterGroup, injector: Injector, prefix?: string) => RouterGroup;
@@ -568,17 +568,19 @@ const fetcher = async (bun, bool) => {
568
568
  export const BoolFactory = async (modules, options) => {
569
569
  try {
570
570
  const modulesConverted = !Array.isArray(modules) ? [modules] : modules;
571
- const { allowLogsMethods, staticOption, allowOrigins, allowMethods } = Object.freeze({
571
+ const { allowLogsMethods, staticOption, allowOrigins, allowMethods, allowCredentials, allowHeaders } = Object.freeze({
572
572
  allowLogsMethods: options?.log?.methods,
573
573
  staticOption: options.static,
574
- allowOrigins: !options.allowOrigins
574
+ allowOrigins: !options.cors?.origins
575
575
  ? ["*"]
576
- : typeof options.allowOrigins !== "string"
577
- ? options.allowOrigins.includes("*") || options.allowOrigins.length < 1
576
+ : typeof options.cors.origins !== "string"
577
+ ? options.cors.origins.includes("*") || options.cors.origins.length < 1
578
578
  ? ["*"]
579
- : options.allowOrigins
580
- : [options.allowOrigins !== "*" ? options.allowOrigins : "*"],
581
- allowMethods: options.allowMethods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
579
+ : options.cors.origins
580
+ : [options.cors.origins !== "*" ? options.cors.origins : "*"],
581
+ allowMethods: options.cors?.methods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
582
+ allowCredentials: !options.cors?.credentials ? false : true,
583
+ allowHeaders: !options.cors?.headers || options.cors.headers.includes("*") ? ["*"] : options.cors.headers
582
584
  });
583
585
  const moduleResolutions = await Promise.all(modulesConverted.map((moduleConverted) => moduleResolution(moduleConverted, options)));
584
586
  const availableModuleResolutions = moduleResolutions.filter((moduleResolution) => typeof moduleResolution !== "undefined");
@@ -597,19 +599,21 @@ export const BoolFactory = async (modules, options) => {
597
599
  const origin = request.headers.get("origin") || "*";
598
600
  const responseHeaders = new Headers();
599
601
  try {
602
+ allowCredentials && responseHeaders.set("Access-Control-Allow-Credentials", "true");
603
+ responseHeaders.set("Access-Control-Allow-Methods", allowMethods.join(", "));
604
+ responseHeaders.set("Access-Control-Allow-Headers", allowHeaders.join(", "));
605
+ responseHeaders.set("Access-Control-Allow-Origin", allowOrigins.includes("*") ? "*" : !allowOrigins.includes(origin) ? allowOrigins[0] : origin);
600
606
  if (request.method.toUpperCase() === "OPTIONS") {
601
- return responseConverter(!allowOrigins.includes(origin)
607
+ return responseConverter(allowOrigins.includes("*") || allowOrigins.includes(origin)
602
608
  ? new Response(undefined, {
603
- status: 417,
604
- statusText: "Origin Disallowed."
605
- })
606
- : new Response(undefined, {
607
609
  status: 204,
608
610
  statusText: "No Content.",
609
- headers: {
610
- "Access-Control-Allow-Origin": "*",
611
- "Access-Control-Allow-Methods": allowMethods.join(", ")
612
- }
611
+ headers: responseHeaders
612
+ })
613
+ : new Response(undefined, {
614
+ status: 417,
615
+ statusText: "Origin Disallowed.",
616
+ headers: responseHeaders
613
617
  }));
614
618
  }
615
619
  if (staticOption) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bool-ts/core",
3
- "version": "1.7.12",
3
+ "version": "1.7.14",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -58,8 +58,12 @@ export type TBoolFactoryOptions = Required<{
58
58
  Partial<{
59
59
  headers: Record<string, string>;
60
60
  }>;
61
- allowOrigins: string | Array<string>;
62
- allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
61
+ cors: Partial<{
62
+ credentials: boolean;
63
+ origins: string | Array<string>;
64
+ methods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
65
+ headers: Array<string>;
66
+ }>;
63
67
  }>;
64
68
 
65
69
  export const responseConverter = (response: Response) => {
@@ -926,17 +930,19 @@ export const BoolFactory = async (
926
930
  ) => {
927
931
  try {
928
932
  const modulesConverted = !Array.isArray(modules) ? [modules] : modules;
929
- const { allowLogsMethods, staticOption, allowOrigins, allowMethods } = Object.freeze({
933
+ const { allowLogsMethods, staticOption, allowOrigins, allowMethods, allowCredentials, allowHeaders } = Object.freeze({
930
934
  allowLogsMethods: options?.log?.methods,
931
935
  staticOption: options.static,
932
- allowOrigins: !options.allowOrigins
936
+ allowOrigins: !options.cors?.origins
933
937
  ? ["*"]
934
- : typeof options.allowOrigins !== "string"
935
- ? options.allowOrigins.includes("*") || options.allowOrigins.length < 1
938
+ : typeof options.cors.origins !== "string"
939
+ ? options.cors.origins.includes("*") || options.cors.origins.length < 1
936
940
  ? ["*"]
937
- : options.allowOrigins
938
- : [options.allowOrigins !== "*" ? options.allowOrigins : "*"],
939
- allowMethods: options.allowMethods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
941
+ : options.cors.origins
942
+ : [options.cors.origins !== "*" ? options.cors.origins : "*"],
943
+ allowMethods: options.cors?.methods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
944
+ allowCredentials: !options.cors?.credentials ? false : true,
945
+ allowHeaders: !options.cors?.headers || options.cors.headers.includes("*") ? ["*"] : options.cors.headers
940
946
  });
941
947
 
942
948
  const moduleResolutions = await Promise.all(
@@ -965,20 +971,27 @@ export const BoolFactory = async (
965
971
  const responseHeaders = new Headers();
966
972
 
967
973
  try {
974
+ allowCredentials && responseHeaders.set("Access-Control-Allow-Credentials", "true");
975
+
976
+ responseHeaders.set("Access-Control-Allow-Methods", allowMethods.join(", "));
977
+ responseHeaders.set("Access-Control-Allow-Headers", allowHeaders.join(", "));
978
+ responseHeaders.set(
979
+ "Access-Control-Allow-Origin",
980
+ allowOrigins.includes("*") ? "*" : !allowOrigins.includes(origin) ? allowOrigins[0] : origin
981
+ );
982
+
968
983
  if (request.method.toUpperCase() === "OPTIONS") {
969
984
  return responseConverter(
970
- !allowOrigins.includes(origin)
985
+ allowOrigins.includes("*") || allowOrigins.includes(origin)
971
986
  ? new Response(undefined, {
972
- status: 417,
973
- statusText: "Origin Disallowed."
974
- })
975
- : new Response(undefined, {
976
987
  status: 204,
977
988
  statusText: "No Content.",
978
- headers: {
979
- "Access-Control-Allow-Origin": "*",
980
- "Access-Control-Allow-Methods": allowMethods.join(", ")
981
- }
989
+ headers: responseHeaders
990
+ })
991
+ : new Response(undefined, {
992
+ status: 417,
993
+ statusText: "Origin Disallowed.",
994
+ headers: responseHeaders
982
995
  })
983
996
  );
984
997
  }