@bool-ts/core 1.7.7 → 1.7.9

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.
@@ -21,6 +21,11 @@ export type TBoolFactoryOptions = Required<{
21
21
  methods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
22
22
  }>;
23
23
  queryParser: Parameters<typeof Qs.parse>[1];
24
+ static: Required<{
25
+ path: string;
26
+ }> & Partial<{}>;
27
+ allowOrigins: Array<string>;
28
+ allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
24
29
  }>;
25
30
  export declare const responseConverter: (response: Response) => Response;
26
31
  export declare const controllerCreator: (controllerConstructor: new (...args: any[]) => unknown, group: RouterGroup, injector: Injector, prefix?: string) => RouterGroup;
@@ -568,8 +568,11 @@ 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 } = Object.freeze({
572
- allowLogsMethods: options?.log?.methods
571
+ const { allowLogsMethods, staticOption, allowOrigins, allowMethods } = Object.freeze({
572
+ allowLogsMethods: options?.log?.methods,
573
+ staticOption: options.static,
574
+ allowOrigins: options.allowOrigins,
575
+ allowMethods: options.allowMethods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
573
576
  });
574
577
  const moduleResolutions = await Promise.all(modulesConverted.map((moduleConverted) => moduleResolution(moduleConverted, options)));
575
578
  const availableModuleResolutions = moduleResolutions.filter((moduleResolution) => typeof moduleResolution !== "undefined");
@@ -585,7 +588,59 @@ export const BoolFactory = async (modules, options) => {
585
588
  const start = performance.now();
586
589
  const url = new URL(request.url);
587
590
  const query = Qs.parse(url.searchParams.toString(), options.queryParser);
591
+ const origin = request.headers.get("origin");
588
592
  try {
593
+ 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
+ }
630
+ }
631
+ if (staticOption) {
632
+ const file = Bun.file(`${staticOption.path}/${url.pathname}`);
633
+ const isFileExists = await file.exists();
634
+ if (isFileExists) {
635
+ return new Response(await file.arrayBuffer(), {
636
+ status: 200,
637
+ statusText: "SUCCESS",
638
+ headers: {
639
+ "Content-Type": file.type
640
+ }
641
+ });
642
+ }
643
+ }
589
644
  let collection;
590
645
  for (let i = 0; i < availableModuleResolutions.length; i++) {
591
646
  const routeResult = availableModuleResolutions[i].routerGroup.find(url.pathname, request.method);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bool-ts/core",
3
- "version": "1.7.7",
3
+ "version": "1.7.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -56,6 +56,8 @@ export type TBoolFactoryOptions = Required<{
56
56
  path: string;
57
57
  }> &
58
58
  Partial<{}>;
59
+ allowOrigins: Array<string>;
60
+ allowMethods: Array<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
59
61
  }>;
60
62
 
61
63
  export const responseConverter = (response: Response) => {
@@ -920,9 +922,11 @@ export const BoolFactory = async (
920
922
  ) => {
921
923
  try {
922
924
  const modulesConverted = !Array.isArray(modules) ? [modules] : modules;
923
- const { allowLogsMethods, staticOption } = Object.freeze({
925
+ const { allowLogsMethods, staticOption, allowOrigins, allowMethods } = Object.freeze({
924
926
  allowLogsMethods: options?.log?.methods,
925
- staticOption: options.static
927
+ staticOption: options.static,
928
+ allowOrigins: options.allowOrigins,
929
+ allowMethods: options.allowMethods || ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
926
930
  });
927
931
 
928
932
  const moduleResolutions = await Promise.all(
@@ -947,8 +951,47 @@ export const BoolFactory = async (
947
951
  const start = performance.now();
948
952
  const url = new URL(request.url);
949
953
  const query = Qs.parse(url.searchParams.toString(), options.queryParser);
954
+ const origin = request.headers.get("origin");
950
955
 
951
956
  try {
957
+ 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)
977
+ ? new Response(undefined, {
978
+ status: 417,
979
+ statusText: "Origin Disallowed."
980
+ })
981
+ : new Response(undefined, {
982
+ status: 204,
983
+ statusText: "No Content.",
984
+ headers: {
985
+ "Content-Type": "text/plain",
986
+ "Access-Control-Allow-Origin": origin,
987
+ "Access-Control-Allow-Credentials": "true",
988
+ "Access-Control-Allow-Headers": "*",
989
+ "Access-Control-Allow-Methods": allowMethods.join(", ")
990
+ }
991
+ });
992
+ }
993
+ }
994
+
952
995
  if (staticOption) {
953
996
  const file = Bun.file(`${staticOption.path}/${url.pathname}`);
954
997
  const isFileExists = await file.exists();