Skip to content

Commit

Permalink
updating static plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Oct 30, 2024
1 parent 7b84eef commit b65a660
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v2.x
- run: deno add jsr:@cross/test jsr:@std/assert
deno-version: v2.x
- run: deno add jsr:@cross/test jsr:@std/assert
- run: deno test -A

- name: Verify formatting
Expand Down
1 change: 0 additions & 1 deletion src/components/nativePlugins/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as plugins from "../../exportable/plugin.ts";
import { f as cookieParser } from "../cookies/mainCookies.ts";
import type { Petition } from "../../morphism.ts";


interface InnerElement {
lastUsed: {
get: () => number;
Expand Down
7 changes: 4 additions & 3 deletions src/exportable/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { isUsing as query } from "../components/queries/mainQueries.ts";
import { isUsing as cookie } from "../components/cookies/mainCookies.ts";
import { isUsing as token } from "../components/cookieToToken/mainCookieToToken.ts";
import type { Petition } from "../morphism.ts";
import type { CyclePlugin, FunRouterOptions } from "../options.ts";
import type { CyclePluginMap, FunRouterOptions } from "../options.ts";
import { type CyclePluginType, pluginCTX } from "./plugin.ts";

type Display = {
using?: string[];
Expand Down Expand Up @@ -32,7 +33,7 @@ export const displayPaths = (p: Petition): void => {
});
};

type PluginType = [string, CyclePlugin<any>];
type PluginType = [string, CyclePluginType<any, any>];

// Main display function that composes the logging of context, components, and plugins
export const display =
Expand Down Expand Up @@ -95,7 +96,7 @@ const logPlugins = (
pluginsToDisplay.forEach(([name, plugin]) => {
// This is always the case `pluginsToDisplay` filters the elements base on
// if the have `isUsing`
const value = plugin.isUsing!(options)(p);
const value = plugin.isUsing!(pluginCTX(options)(p));
console.log(
`\x1b[35m${name}\x1b[0m: \x1b[38;2;255;165;0m${value}\x1b[0m`,
);
Expand Down
12 changes: 3 additions & 9 deletions src/exportable/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import type {
StaticFilePlugin,
} from "../morphism.ts";
import checkerTools from "../composer/checkPetition/checkTools.ts";
import {
type FunRouterOptions,
globalOptions,
} from "../options.ts";
import { type FunRouterOptions, globalOptions } from "../options.ts";

const pluginIsUsing = (p: Petition) => (currentName: string) =>
(
Expand Down Expand Up @@ -118,12 +115,9 @@ export default {
fileServer: <MI extends true | false>(
s: fileServerPetition<MI>,
): fileServerPetition<MI> => s,
/**
* @deprecated
*/
/** */
staticFilePlugin: <
TP extends "response" | "request" | undefined,
O extends StaticFilePlugin<TP>,
O extends StaticFilePlugin,
>(config: O) => config,
/**
* @deprecated
Expand Down
81 changes: 68 additions & 13 deletions src/morphism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,64 @@ export const petitions = {
type: 1,
},
}),
/**
*
* Enhances a function with additional typings for handling HTTP requests within the 'vixeny' framework.
* This function binds the provided Morphism to the rules set by `composer`, producing a typed `Petition`.
* The resulting function can be used with `wrap` or can be `composed`, and it's guaranteed to return a `Response` or `Promise<Response>`.
*
* @param {O} [options] - Optional configuration options that may include plugin settings.
* @returns {Function} A function that accepts a Morphism defining an HTTP petition.
*
* @example
* Example usage:
* ```typescript
* import { petitions } from 'vixeny';
*
* const standard = petitions.custom()({
* path: '/yourPath',
* f: ctx => new Response(ctx.query.hello ?? 'queryNotFound')
* });
* ```
*/
add: <
FC extends CyclePluginMap,
O extends FunRouterOptions<FC>,
>(o?: O) =>
<
RM extends ResolveMap<any>,
BM extends BranchMap<any>,
QO extends QueryOptions,
PO extends ParamOptions,
RO extends O,
CO extends CryptoOptions,
AR = any,
R = any,
>(
I: Morphism<
{
type: "add";
hasPath: true;
isAPetition: true;
typeNotNeeded: true;
},
RM,
BM,
QO,
PO,
RO,
CO,
AR,
R
>,
) =>
({
...I,
type: "request",
o,
}) as unknown as Petition,
/**
*
* Enhances a function with additional typings for handling HTTP requests within the 'vixeny' framework.
* This function binds the provided Morphism to the rules set by `composer`, producing a typed `Petition`.
* The resulting function can be used with `wrap` or can be `composed`, and it's guaranteed to return a `Response` or `Promise<Response>`.
Expand Down Expand Up @@ -1113,24 +1170,22 @@ export type CryptoOptions = {
};
} | {};

type StaticFileOptions = {
path: () => string
thisOptions: () => fileServerPetition<any>
globalOptions: () => FunRouterOptions<any>
}
type StaticFilePlugin <> = {
type?: 'add'
export type StaticFileOptions = {
path: string;
thisOptions: () => fileServerPetition<any>;
globalOptions: () => FunRouterOptions<any>;
};
export type StaticFilePlugin<> = {
type?: "add";
async?: boolean;
checker?: (ctx: StaticFileOptions) => boolean;
f: (options: {
checker: (ctx: StaticFileOptions) => boolean;
p: (options: {
root: string;
path: string;
o?: FunRouterOptions<any>;
relativeName: string;
}) => (req: Request) => Response
}


}) => Petition;
};

/**
* Object for raw response static.
Expand Down
33 changes: 23 additions & 10 deletions src/staticFiles/composedPaths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { fileServerPetition, Petition } from "../morphism.ts";
import type {
fileServerPetition,
Petition,
StaticFileOptions,
} from "../morphism.ts";
import type { FunRouterOptions } from "../options.ts";
import staticFileTools from "./staticFileTools.ts";

Expand All @@ -17,11 +21,9 @@ export default (o?: FunRouterOptions<any>) =>
(x) =>
((checks) =>
checks
? staticFileTools.getValidPetitionFromPlugin(o)(checks)(root)(
x,
)(
name,
)
? staticFileTools.getValidPetitionFromPlugin(o)(checks)(
root,
)(x)(name)
: ({
path: root.slice(1, -1) + x.slice(name.length - 1),
...mimeIsTrue(f)("." + x.split(".").at(-1)),
Expand All @@ -34,7 +36,7 @@ export default (o?: FunRouterOptions<any>) =>
),
}))(
f.template!.find((y) =>
y.checker(root.slice(1, -1) + x.slice(name.length - 1))
y.checker(createContext({ root, name, x, o, f }))
),
),
) as unknown as Petition[]
Expand All @@ -61,8 +63,7 @@ export default (o?: FunRouterOptions<any>) =>
((checks) =>
checks
? staticFileTools.getValidPetitionFromPlugin(o)(checks)(root)(x)(
name,
)
name)
: ({
path: root.slice(1, -1) + x.slice(name.length - 1),
type: "base",
Expand All @@ -75,7 +76,7 @@ export default (o?: FunRouterOptions<any>) =>
),
}))(
f.template!.find((y) =>
y.checker(root.slice(1, -1) + x.slice(name.length - 1))
y.checker(createContext({ root, name, x, o, f }))
),
),
) as unknown as Petition[]
Expand All @@ -101,3 +102,15 @@ const mimeIsTrue = (f: fileServerPetition<any>) => (mime: string) =>
},
}
: { headings: undefined };

const createContext = (obj: {
root: string;
name: string;
x: string;
o?: FunRouterOptions<any>;
f: fileServerPetition<any>;
}): StaticFileOptions => ({
path: obj.root.slice(1, -1) + obj.x.slice(obj.name.length - 1),
thisOptions: () => ({ ...obj.f }),
globalOptions: () => ({ ...obj.o }),
});
27 changes: 9 additions & 18 deletions src/staticFiles/staticFileTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
defaultMime,
fileServerPetition,
Petition,
StaticFilePluginExtensions,
StaticFilePlugin,
} from "../morphism.ts";
import type { FunRouterOptions } from "../options.ts";
import mime from "../util/mime.ts";
Expand Down Expand Up @@ -57,24 +57,15 @@ export default {

getValidPetitionFromPlugin:
(o?: FunRouterOptions<any>) =>
(checks: StaticFilePluginExtensions<any>) =>
(checks: StaticFilePlugin) =>
(root: string) =>
(x: string) =>
(name: string) =>
"r" in checks
? checks!.r(
{
root: root,
path: x,
o: o,
relativeName: root.slice(1, -1) + x.slice(name.length - 1),
},
)
: checks!.f(
{
root: root,
path: x,
relativeName: root.slice(1, -1) + x.slice(name.length - 1),
},
),
checks!.p(
{
root: root,
path: x,
relativeName: root.slice(1, -1) + x.slice(name.length - 1),
},
),
};
20 changes: 10 additions & 10 deletions test/staticFile/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assertEquals } from "@std/assert";
import { test } from "@cross/test";
import main from "../../src/staticFiles/staticFileMain.ts";
import { type fileServerPetition } from "../../src/morphism.ts";

import { petitions, plugins } from "../../main.ts";

test(
"static file checking logo",
Expand Down Expand Up @@ -68,15 +68,15 @@ test(
path: "./misc/",
name: "/hello/nested",
mime: false,
template: [{
checker: (s) => s.path().includes(".png"),
type: 'add',
f: (options) =>
() => new Response(options.relativeName.slice(0, -4)),

}],
} as fileServerPetition<false>)
.some((x) => x.path === "/hello/nested/logo"),
template: [plugins.staticFilePlugin({
checker: (s) => s.path.includes(".png"),
type: "add",
p: (options) => petitions.add()({
path: options.relativeName.slice(0, -4),
f:() => new Response('hello')
}),
})],
}).some(p => p.path === "/hello/nested/logo"),
true,
),
);

0 comments on commit b65a660

Please sign in to comment.