Skip to content

Commit

Permalink
Adding fileServer to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Jun 20, 2024
1 parent ad95380 commit d90c21f
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 26 deletions.
4 changes: 2 additions & 2 deletions fun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import type { fileServerPetition, Petition } from "./src/morphism.ts";
type vixeny = (
o?: FunRouterOptions<any>,
) => (
routes: (Petition | fileServerPetition)[],
routes: (Petition | fileServerPetition<any>)[],
) => (r: Request) => Promise<Response> | Response;

export default ((o?: FunRouterOptions<any>) =>
(routes: (Petition | fileServerPetition)[]) =>
(routes: (Petition | fileServerPetition<any>)[]) =>
((re) =>
((map) =>
((s) => (r: Request): Promise<Response> | Response => map[s(r)](r))(
Expand Down
2 changes: 1 addition & 1 deletion src/composer/mainComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { fileServerPetition, Petition } from "../morphism.ts";

export default (
o?: FunRouterOptions<any>,
): (routes: (Petition | fileServerPetition)[]) => RouteTypes[] =>
): (routes: (Petition | fileServerPetition<any>)[]) => RouteTypes[] =>
(ar) =>
ar
.filter(
Expand Down
3 changes: 2 additions & 1 deletion src/exportable/plugin.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -110,6 +110,7 @@ export default {
* only the relevant context elements are included
*/
isUsing: composerTools.isUsing,
fileServer: <MI extends true | false > (s:fileServerPetition<MI>):fileServerPetition<MI> => s,
staticFilePlugin: <
TP extends "response" | "request" | undefined,
O extends StaticFilePlugin<TP>,
Expand Down
21 changes: 6 additions & 15 deletions src/morphism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>[];
removeExtensionOf?: defaultMime[];
slashIs?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/staticFiles/composedPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import staticFileTools from "./staticFileTools.ts";

//TODO: make it more readable 🙏

export default (f: fileServerPetition) =>
export default (f: fileServerPetition<any>) =>
(name: string) =>
(root: string) =>
(paths: string[]) =>
Expand Down
2 changes: 1 addition & 1 deletion src/staticFiles/staticFileMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) =>
staticFileTools.removeExtension(f)(
composedPaths(f)(staticFileTools.rectify(f.path))(
staticFileTools.rectify(f.name),
Expand Down
6 changes: 3 additions & 3 deletions src/staticFiles/staticFileTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ export default {
join: (base: string) => (target: string): string =>
base.endsWith("/") ? base + target : base + "/" + target,

mimeForm: (f: fileServerPetition) =>
mimeForm: (f: fileServerPetition<any>) =>
"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<any>) => (petitions: Petition[]) =>
f.removeExtensionOf && Array.isArray(f.removeExtensionOf)
? petitions.map(
(x) =>
Expand Down
4 changes: 2 additions & 2 deletions test/staticFile/main.test.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -76,7 +76,7 @@ test(
r: () => new Response(""),
}),
}],
})
} as fileServerPetition<false>)
.some((x) => x.path === "/hello/nested/logo"),
true,
),
Expand Down

0 comments on commit d90c21f

Please sign in to comment.