Skip to content

Commit

Permalink
new plugin api implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiMonads committed Oct 28, 2024
1 parent 21aa849 commit 3d12ddf
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 141 deletions.
5 changes: 3 additions & 2 deletions src/components/nativePlugins/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ const session = <
return plugins.default.type({
name: sym,
type: {} as unknown,
f: () => (p) => {
return mapper<T, R>(map)()(opt?.tolerance ?? 300000)(p);
isFunction: false,
f: (ctx) => {
return mapper<T, R>(map)()(opt?.tolerance ?? 300000)(ctx.getPetition());
},
}) as unknown as CyclePlugin<{ isFunction: false; return: SessionType<T> }>;
};
Expand Down
5 changes: 3 additions & 2 deletions src/composer/nativeComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { Petition } from "../morphism.ts";

import tools from "./composerTools.ts";
import mime from "../util/mime.ts";
import { pluginCTX } from "../exportable/plugin.ts";

type NativeMaps = {
name: string;
Expand Down Expand Up @@ -95,8 +96,8 @@ export default (o?: FunRouterOptions<any>) =>
.concat(
Object.keys(o?.cyclePlugin || {}).map((x) => ({
condition: ((name) => (x: NativeMaps) => x.name === name)(x),
//@ts-ignore
action: () => o!.cyclePlugin![x]!["f"](o)(p as CommonRequestMorphism),

action: () => o!.cyclePlugin![x]!["f"](pluginCTX(o)(p)),
})),
));

Expand Down
2 changes: 1 addition & 1 deletion src/composer/nativeMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default (o?: FunRouterOptions<any>) =>
: undefined,
//@ts-ignore
value: (o && o.cyclePlugin && o.cyclePlugin[x])
? "isFunction" in o.cyclePlugin[x] || false
? o.cyclePlugin[x].isFunction
//@ts-ignore
? x
//@ts-ignore
Expand Down
133 changes: 38 additions & 95 deletions src/exportable/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
globalOptions,
} from "../options.ts";


const pluginIsUsing = (p: Petition) => (currentName: string) =>
(
(
Expand All @@ -37,13 +36,11 @@ const pluginIsUsing = (p: Petition) => (currentName: string) =>
)(
checkerTools.getArgsname(p.f),
)
) as string[] | null

) as string[] | null;

export default {
globalOptions,
/**
* @deprecated
* Types a plugin's configuration to ensure it meets Vixeny's requirements.
* This function validates and possibly transforms the plugin configuration using TypeScript types.
*
Expand All @@ -65,43 +62,11 @@ export default {
* ```
*/
type: <
FC extends boolean,
O extends CyclePlugin<{
isFunction: FC;
return: unknown;
}>,
>(config: O) => config,

/**
*
* @deprecated
* Retrieves specific plugin options using the current name of the plugin.
* This function is essential for dynamic plugin management where plugins might
* be identified differently over time or across different user options.
*
* @param {unknown} userOptions - The user options from which to retrieve plugin settings.
* @returns {Function} A function that accepts the current plugin name and returns the associated plugin options.
*
* @example
* Example usage:
* ```typescript
* import { plugins } from 'vixeny';
*
* // Define your plugin using a unique symbol
* export const myPlugin = ((mySym) => ({
* name: mySym,
* isFunction: true,
* type: {} as YourType, // Specify the type here
* f: (userOptions) => (currentPetition) => {
* // Retrieving the current name of the plugin
* const currentName = getName(userOptions)(mySym);
* // Getting the options for the current plugin based on its name
* const options = getOptions(currentPetition)(currentName) as YourType;
* // Additional code leveraging the options...
* },
* }))(Symbol("myPlugin"));
* ```
*/
isFunction extends boolean,
isASync extends boolean,
T = any,
R = any,
>(g: CyclePluginType<isFunction, isASync, T, R>) => g,
getOptions: <T extends any>(userOptions: unknown) =>
(
currentName: string,
Expand Down Expand Up @@ -156,83 +121,61 @@ export default {
): fileServerPetition<MI> => s,
/**
* @deprecated
*
*/
staticFilePlugin: <
TP extends "response" | "request" | undefined,
O extends StaticFilePlugin<TP>,
>(config: O) => config,
/**
* @deprecated
*
* @param p
* @returns
*
* @param p
* @returns
*/
pluginIsUsing
pluginIsUsing,
};



const pluginCTX = (o?: FunRouterOptions<any>) => (p: Petition) => ({
getPetition: () => ({...p}),
getGlobalOptions: () => ({...o}),
/**
* Plguin helper
*
* @param o
* @returns
*/
export const pluginCTX = (o?: FunRouterOptions<any>) => (p: Petition) => ({
getPetition: () => ({ ...p }),
getGlobalOptions: () => ({ ...o }),
isUsing: () => composerTools.isUsing(o)(p),
pluginIsUsing: (currentName:string)=> pluginIsUsing(p)(currentName),
pluginIsUsing: (currentName: string) => pluginIsUsing(p)(currentName),
currentName: (sym: symbol) =>
Object
.keys(o?.cyclePlugin ?? [])
// @ts-ignore
.find((name) => o?.cyclePlugin[name]?.name === sym) as string,
getOptionsFromPetition: <T extends any>(p: Petition) =>
((
currentName: string,
): T | null => (p && typeof p === "object" &&
!Array.isArray(p) && "plugins" in p &&
p.plugins
//@ts-ignore
? p.plugins[currentName] as T
: null)),
})
getOptionsFromPetition: <T extends any>(p: Petition) => ((
currentName: string,
): T | null => (p && typeof p === "object" &&
!Array.isArray(p) && "plugins" in p &&
p.plugins
//@ts-ignore
? p.plugins[currentName] as T
: null)),
});

export type PluginCTX = ReturnType<ReturnType<typeof pluginCTX>>
export type PluginCTX = ReturnType<ReturnType<typeof pluginCTX>>;

export type CyclePluginType<
isFunction extends boolean = false,
isASync extends boolean = false,
T = any
isFunction extends boolean,
isASync extends boolean,
T = any,
R = any,
> = {
readonly name: symbol;
// Do not use false
readonly isFunction?: isFunction;
readonly isFunction: isFunction;
readonly isAsync?: isASync;
readonly f: isFunction extends true
? (ctx: PluginCTX) => T
: (ctx: PluginCTX) => (r: Request) => T;
readonly f: isFunction extends true ? (ctx: PluginCTX) => T
: (ctx: PluginCTX) => (r: Request) => R;
readonly type: unknown;
readonly isUsing?: (ctx: PluginCTX ) => string;
readonly isUsing?: (ctx: PluginCTX) => string;
readonly options?: { [k: string]: any };
};

export type CyclePluginTypes<CPM extends CyclePluginType> = {
[K in keyof CPM]: CPM[K] extends
{ isFunction: true; f: (...args: any) => any }
? ReturnType<CPM[K]["f"]>
: CPM[K] extends { f: (...args: any) => any }
? Awaited<ReturnType<ReturnType<CPM[K]["f"]>>>
: never;
};

const apply = <
isFunction extends boolean = false,
isASync extends boolean = false,
T = any
>(g: CyclePluginType<isFunction,isASync,T>)=> g

const a = apply({
name: Symbol.for('hi'),
isFunction: true,
type: 1,
f: (ctx) => () => 1
})


8 changes: 2 additions & 6 deletions src/morphism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,12 +765,8 @@ export type WithPlugins<
// : {}

type CyclePluginFunctions<CPM extends CyclePluginMap> = {
[K in keyof CPM]: CPM[K] extends
{ isFunction: boolean; f: (...args: any) => any }
? ReturnType<ReturnType<CPM[K]["f"]>> // Direct function case
: CPM[K] extends { f: (...args: any) => any }
? Awaited<ReturnType<ReturnType<ReturnType<CPM[K]["f"]>>>> // Nested function case
: never; // Handle cases that do not match expected structure
[K in keyof CPM]: CPM[K]["isFunction"] extends true ? ReturnType<CPM[K]["f"]>
: Awaited<ReturnType<ReturnType<CPM[K]["f"]>>>;
};

type SignerAndVarifier = {
Expand Down
27 changes: 2 additions & 25 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CORSOptions } from "./components/cors/types.ts";
import type { CyclePluginType } from "./exportable/plugin.ts";
import type { Petition } from "./morphism.ts";
/**
* Options for the router, it is `optional`
Expand Down Expand Up @@ -116,31 +117,7 @@ export type FunRouterOptions<
};

export type CyclePluginMap = {
readonly [key: string]: CyclePlugin<any>;
};

type CyclePluginOptions = {
isFunction?: true | false;
return?: unknown;
};

export type CyclePlugin<
T extends CyclePluginOptions,
> = {
readonly name: symbol;
// Do not use false
readonly isFunction?: T["isFunction"];
readonly isAsync?: boolean;
readonly f: T["isFunction"] extends { isFunction: true }
? (o?: FunRouterOptions<any>) => (p: Petition) => T["return"]
: (
o?: FunRouterOptions<any>,
) => (
p: Petition,
) => (r: Request) => T["return"];
readonly type: unknown;
readonly isUsing?: (o?: FunRouterOptions<any>) => (p: Petition) => string;
readonly options?: { [k: string]: any };
readonly [key: string]: CyclePluginType<any, any, any, any>;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion test/composer/checker/checker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const pluginHello = {
const pluginMethod = {
name: Symbol.for("method"),
type: "string",
f: () => () => () => "plugin",
isFunction: false,
f: () => () => "plugin",
};

const opt = {
Expand Down
14 changes: 8 additions & 6 deletions test/exportable/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { assertEquals } from "@std/assert";
import { petitions } from "../../main.ts";
import { test } from "@cross/test";
const dummyRequest = new Request("http://heyINeedTOGoToSleep.com/");

const hello = plugins.type({
name: Symbol.for("hello"),
isFunction: true,
type: undefined,
f: () => () => "hello",
});
const opt = plugins.globalOptions({
cyclePlugin: {
hello: {
name: Symbol.for("hello"),
isFunction: true,
type: undefined,
f: () => () => () => "hello",
},
hello,
},
});

Expand Down
7 changes: 4 additions & 3 deletions test/exportable/wrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ const pluginHello = plugins.type({
name: Symbol.for("hello"),
isFunction: true,
type: {} as string,
f: () => () => () => "function",
f: () => () => "function",
});

const pluginMethod = plugins.type({
name: Symbol.for("method"),
isFunction: false,
type: {} as string,
f: () => () => () => " inCycle",
isUsing: () => () => "sup",
f: () => (r) => " inCycle",
isUsing: () => "sup",
});

const opt = plugins.globalOptions({
Expand Down

0 comments on commit 3d12ddf

Please sign in to comment.