diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml index 6c0331a..24e77af 100644 --- a/.github/workflows/deno.yml +++ b/.github/workflows/deno.yml @@ -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 diff --git a/src/components/nativePlugins/session.ts b/src/components/nativePlugins/session.ts index dd88683..533b197 100644 --- a/src/components/nativePlugins/session.ts +++ b/src/components/nativePlugins/session.ts @@ -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; diff --git a/src/exportable/display.ts b/src/exportable/display.ts index 0be8b2a..e3871f0 100644 --- a/src/exportable/display.ts +++ b/src/exportable/display.ts @@ -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[]; @@ -32,7 +33,7 @@ export const displayPaths = (p: Petition): void => { }); }; -type PluginType = [string, CyclePlugin]; +type PluginType = [string, CyclePluginType]; // Main display function that composes the logging of context, components, and plugins export const display = @@ -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`, ); diff --git a/src/exportable/plugin.ts b/src/exportable/plugin.ts index be18501..7604621 100644 --- a/src/exportable/plugin.ts +++ b/src/exportable/plugin.ts @@ -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) => ( @@ -118,12 +115,9 @@ export default { fileServer: ( s: fileServerPetition, ): fileServerPetition => s, - /** - * @deprecated - */ + /** */ staticFilePlugin: < - TP extends "response" | "request" | undefined, - O extends StaticFilePlugin, + O extends StaticFilePlugin, >(config: O) => config, /** * @deprecated diff --git a/src/morphism.ts b/src/morphism.ts index 915cd7b..ec3bcc3 100644 --- a/src/morphism.ts +++ b/src/morphism.ts @@ -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`. + * + * @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, + >(o?: O) => + < + RM extends ResolveMap, + BM extends BranchMap, + 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`. @@ -1113,24 +1170,22 @@ export type CryptoOptions = { }; } | {}; -type StaticFileOptions = { - path: () => string - thisOptions: () => fileServerPetition - globalOptions: () => FunRouterOptions -} -type StaticFilePlugin <> = { - type?: 'add' +export type StaticFileOptions = { + path: string; + thisOptions: () => fileServerPetition; + globalOptions: () => FunRouterOptions; +}; +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; relativeName: string; - }) => (req: Request) => Response -} - - + }) => Petition; +}; /** * Object for raw response static. diff --git a/src/staticFiles/composedPaths.ts b/src/staticFiles/composedPaths.ts index 2e6d8fe..9ad1ae0 100644 --- a/src/staticFiles/composedPaths.ts +++ b/src/staticFiles/composedPaths.ts @@ -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"; @@ -17,11 +21,9 @@ export default (o?: FunRouterOptions) => (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)), @@ -34,7 +36,7 @@ export default (o?: FunRouterOptions) => ), }))( 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[] @@ -61,8 +63,7 @@ export default (o?: FunRouterOptions) => ((checks) => checks ? staticFileTools.getValidPetitionFromPlugin(o)(checks)(root)(x)( - name, - ) + name) : ({ path: root.slice(1, -1) + x.slice(name.length - 1), type: "base", @@ -75,7 +76,7 @@ export default (o?: FunRouterOptions) => ), }))( 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[] @@ -101,3 +102,15 @@ const mimeIsTrue = (f: fileServerPetition) => (mime: string) => }, } : { headings: undefined }; + +const createContext = (obj: { + root: string; + name: string; + x: string; + o?: FunRouterOptions; + f: fileServerPetition; +}): StaticFileOptions => ({ + path: obj.root.slice(1, -1) + obj.x.slice(obj.name.length - 1), + thisOptions: () => ({ ...obj.f }), + globalOptions: () => ({ ...obj.o }), +}); diff --git a/src/staticFiles/staticFileTools.ts b/src/staticFiles/staticFileTools.ts index dc4bc1c..c76ea56 100644 --- a/src/staticFiles/staticFileTools.ts +++ b/src/staticFiles/staticFileTools.ts @@ -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"; @@ -57,24 +57,15 @@ export default { getValidPetitionFromPlugin: (o?: FunRouterOptions) => - (checks: StaticFilePluginExtensions) => + (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), + }, + ), }; diff --git a/test/staticFile/main.test.ts b/test/staticFile/main.test.ts index 15688c3..b871f99 100644 --- a/test/staticFile/main.test.ts +++ b/test/staticFile/main.test.ts @@ -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", @@ -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) - .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, ), );