@bool-ts/core 1.7.11 → 1.7.13
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.
- package/dist/hooks/factory.d.ts +6 -2
- package/dist/hooks/factory.js +21 -12
- package/package.json +1 -1
- package/src/hooks/factory.ts +33 -14
package/dist/hooks/factory.d.ts
CHANGED
|
@@ -26,8 +26,12 @@ export type TBoolFactoryOptions = Required<{
|
|
|
26
26
|
}> & Partial<{
|
|
27
27
|
headers: Record<string, string>;
|
|
28
28
|
}>;
|
|
29
|
-
|
|
30
|
-
|
|
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;
|
package/dist/hooks/factory.js
CHANGED
|
@@ -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.
|
|
574
|
+
allowOrigins: !options.cors?.origins
|
|
575
575
|
? ["*"]
|
|
576
|
-
: typeof options.
|
|
577
|
-
? options.
|
|
576
|
+
: typeof options.cors.origins !== "string"
|
|
577
|
+
? options.cors.origins.includes("*") || options.cors.origins.length < 1
|
|
578
578
|
? ["*"]
|
|
579
|
-
: options.
|
|
580
|
-
: [options.
|
|
581
|
-
allowMethods: 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,25 +599,32 @@ 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(origin) ? allowOrigins[0] : origin);
|
|
600
606
|
if (request.method.toUpperCase() === "OPTIONS") {
|
|
601
607
|
return responseConverter(!allowOrigins.includes(origin)
|
|
602
608
|
? new Response(undefined, {
|
|
603
609
|
status: 417,
|
|
604
|
-
statusText: "Origin Disallowed."
|
|
610
|
+
statusText: "Origin Disallowed.",
|
|
611
|
+
headers: responseHeaders
|
|
605
612
|
})
|
|
606
613
|
: new Response(undefined, {
|
|
607
614
|
status: 204,
|
|
608
615
|
statusText: "No Content.",
|
|
609
|
-
headers:
|
|
610
|
-
"Access-Control-Allow-Origin": "*",
|
|
611
|
-
"Access-Control-Allow-Methods": allowMethods.join(", ")
|
|
612
|
-
}
|
|
616
|
+
headers: responseHeaders
|
|
613
617
|
}));
|
|
614
618
|
}
|
|
615
619
|
if (staticOption) {
|
|
616
620
|
const file = Bun.file(`${staticOption.path}/${url.pathname}`);
|
|
617
621
|
const isFileExists = await file.exists();
|
|
618
622
|
if (isFileExists) {
|
|
623
|
+
if (staticOption.headers) {
|
|
624
|
+
for (const [key, value] of Object.entries(staticOption.headers)) {
|
|
625
|
+
responseHeaders.set(key, value);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
619
628
|
responseHeaders.set("Content-Type", file.type);
|
|
620
629
|
return responseConverter(new Response(await file.arrayBuffer(), {
|
|
621
630
|
status: 200,
|
package/package.json
CHANGED
package/src/hooks/factory.ts
CHANGED
|
@@ -58,8 +58,12 @@ export type TBoolFactoryOptions = Required<{
|
|
|
58
58
|
Partial<{
|
|
59
59
|
headers: Record<string, string>;
|
|
60
60
|
}>;
|
|
61
|
-
|
|
62
|
-
|
|
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.
|
|
936
|
+
allowOrigins: !options.cors?.origins
|
|
933
937
|
? ["*"]
|
|
934
|
-
: typeof options.
|
|
935
|
-
? options.
|
|
938
|
+
: typeof options.cors.origins !== "string"
|
|
939
|
+
? options.cors.origins.includes("*") || options.cors.origins.length < 1
|
|
936
940
|
? ["*"]
|
|
937
|
-
: options.
|
|
938
|
-
: [options.
|
|
939
|
-
allowMethods: 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(origin) ? allowOrigins[0] : origin
|
|
981
|
+
);
|
|
982
|
+
|
|
968
983
|
if (request.method.toUpperCase() === "OPTIONS") {
|
|
969
984
|
return responseConverter(
|
|
970
985
|
!allowOrigins.includes(origin)
|
|
971
986
|
? new Response(undefined, {
|
|
972
987
|
status: 417,
|
|
973
|
-
statusText: "Origin Disallowed."
|
|
988
|
+
statusText: "Origin Disallowed.",
|
|
989
|
+
headers: responseHeaders
|
|
974
990
|
})
|
|
975
991
|
: new Response(undefined, {
|
|
976
992
|
status: 204,
|
|
977
993
|
statusText: "No Content.",
|
|
978
|
-
headers:
|
|
979
|
-
"Access-Control-Allow-Origin": "*",
|
|
980
|
-
"Access-Control-Allow-Methods": allowMethods.join(", ")
|
|
981
|
-
}
|
|
994
|
+
headers: responseHeaders
|
|
982
995
|
})
|
|
983
996
|
);
|
|
984
997
|
}
|
|
@@ -988,6 +1001,12 @@ export const BoolFactory = async (
|
|
|
988
1001
|
const isFileExists = await file.exists();
|
|
989
1002
|
|
|
990
1003
|
if (isFileExists) {
|
|
1004
|
+
if (staticOption.headers) {
|
|
1005
|
+
for (const [key, value] of Object.entries(staticOption.headers)) {
|
|
1006
|
+
responseHeaders.set(key, value);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
|
|
991
1010
|
responseHeaders.set("Content-Type", file.type);
|
|
992
1011
|
|
|
993
1012
|
return responseConverter(
|