diff --git a/fun.ts b/fun.ts index 997b6b9..0341193 100644 --- a/fun.ts +++ b/fun.ts @@ -13,11 +13,11 @@ import type { fileServerPetition, Petition } from "./src/morphism.ts"; type vixeny = ( o?: FunRouterOptions, ) => ( - routes: (Petition | fileServerPetition)[], + routes: (Petition | fileServerPetition)[], ) => (r: Request) => Promise | Response; export default ((o?: FunRouterOptions) => -(routes: (Petition | fileServerPetition)[]) => +(routes: (Petition | fileServerPetition)[]) => ((re) => ((map) => ((s) => (r: Request): Promise | Response => map[s(r)](r))( diff --git a/src/composer/mainComposer.ts b/src/composer/mainComposer.ts index 99a39fc..f46b2c6 100644 --- a/src/composer/mainComposer.ts +++ b/src/composer/mainComposer.ts @@ -9,7 +9,7 @@ import type { fileServerPetition, Petition } from "../morphism.ts"; export default ( o?: FunRouterOptions, -): (routes: (Petition | fileServerPetition)[]) => RouteTypes[] => +): (routes: (Petition | fileServerPetition)[]) => RouteTypes[] => (ar) => ar .filter( diff --git a/src/exportable/plugin.ts b/src/exportable/plugin.ts index 757c5e3..0cb2681 100644 --- a/src/exportable/plugin.ts +++ b/src/exportable/plugin.ts @@ -1,6 +1,6 @@ //TODO: add import composerTools from "../composer/composerTools.ts"; -import type { Petition, StaticFilePlugin } from "../morphism.ts"; +import type { Petition, StaticFilePlugin, fileServerPetition } from "../morphism.ts"; import checkerTools from "../composer/checkPetition/checkTools.ts"; import { type CyclePlugin, @@ -110,6 +110,7 @@ export default { * only the relevant context elements are included */ isUsing: composerTools.isUsing, + fileServer: (s:fileServerPetition):fileServerPetition => s, staticFilePlugin: < TP extends "response" | "request" | undefined, O extends StaticFilePlugin, diff --git a/src/morphism.ts b/src/morphism.ts index 345751a..bc297ac 100644 --- a/src/morphism.ts +++ b/src/morphism.ts @@ -1019,24 +1019,15 @@ export type StaticFilePluginExtensions< /** * Object for raw response static. */ -export type fileServerPetition = - & ({ - type: "fileServer"; - name: string; - path: string; - } | { - type: "fileServer"; - name: string; - path: string; - mime?: true; - extra: [string, string][]; - } | { +export type fileServerPetition < + MI extends true | false +> = + { type: "fileServer"; name: string; path: string; - mime: false; - }) - & { + mime?: MI; + extra?: MI extends true ? [string, string][] : never; template?: StaticFilePlugin[]; removeExtensionOf?: defaultMime[]; slashIs?: string; diff --git a/src/staticFiles/composedPaths.ts b/src/staticFiles/composedPaths.ts index 8fba106..0d43a2e 100644 --- a/src/staticFiles/composedPaths.ts +++ b/src/staticFiles/composedPaths.ts @@ -3,7 +3,7 @@ import staticFileTools from "./staticFileTools.ts"; //TODO: make it more readable 🙏 -export default (f: fileServerPetition) => +export default (f: fileServerPetition) => (name: string) => (root: string) => (paths: string[]) => diff --git a/src/staticFiles/staticFileMain.ts b/src/staticFiles/staticFileMain.ts index 01c49fc..9acd75b 100644 --- a/src/staticFiles/staticFileMain.ts +++ b/src/staticFiles/staticFileMain.ts @@ -2,7 +2,7 @@ import type { fileServerPetition } from "../morphism.ts"; import composedPaths from "./composedPaths.ts"; import staticFileTools from "./staticFileTools.ts"; -export default (maybeOfFiles?: string[]) => (f: fileServerPetition) => +export default (maybeOfFiles?: string[]) => (f: fileServerPetition) => staticFileTools.removeExtension(f)( composedPaths(f)(staticFileTools.rectify(f.path))( staticFileTools.rectify(f.name), diff --git a/src/staticFiles/staticFileTools.ts b/src/staticFiles/staticFileTools.ts index 5ec6b6b..9f9de05 100644 --- a/src/staticFiles/staticFileTools.ts +++ b/src/staticFiles/staticFileTools.ts @@ -11,17 +11,17 @@ export default { join: (base: string) => (target: string): string => base.endsWith("/") ? base + target : base + "/" + target, - mimeForm: (f: fileServerPetition) => + mimeForm: (f: fileServerPetition) => "mime" in f && f.mime === false ? [] : "extra" in f - ? mime.concat(f.extra) + ? mime.concat(f.extra as [string,string][]) : mime, getMime: (mimes: [string, string][]) => (ext: string): string => (mimes.find((x) => x[0] === ext) || [".txt", "text/html"])[1], - removeExtension: (f: fileServerPetition) => (petitions: Petition[]) => + removeExtension: (f: fileServerPetition) => (petitions: Petition[]) => f.removeExtensionOf && Array.isArray(f.removeExtensionOf) ? petitions.map( (x) => diff --git a/test/staticFile/main.test.ts b/test/staticFile/main.test.ts index 43211a0..abd1242 100644 --- a/test/staticFile/main.test.ts +++ b/test/staticFile/main.test.ts @@ -1,7 +1,7 @@ import { assertEquals } from "@std/assert"; import { test } from "@cross/test"; import main from "../../src/staticFiles/staticFileMain.ts"; -import { petitions } from "../../src/morphism.ts"; +import { petitions, type fileServerPetition } from "../../src/morphism.ts"; test( "static file checking logo", @@ -76,7 +76,7 @@ test( r: () => new Response(""), }), }], - }) + } as fileServerPetition) .some((x) => x.path === "/hello/nested/logo"), true, ),