Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/endofunctor/components/http/src/framework/optimizer/types.ts>Ctx

🌍 Vixeny: A runtime-agnostic, performance-centric library championing functional purity for modern web development.
Go to Latest
interface Ctx
import { type Ctx } from "https://deno.land/x/endofunctor@v0.0.945/components/http/src/framework/optimizer/types.ts";

Type Parameters

R extends MorphismMap
B extends AnyMorphismMap
QS extends QueryOptions
PA extends ParamOptions
CR extends CryptoOptions
UNI extends specialElements

Properties

resolve: [V in keyof R]: Awaited<ReturnType<R[V]["f"]>>
branch: [V in keyof B]: { (ctx: any): ReturnType<B[V]["f"]>; }
req: Request

Adds with query to the context


{
 path: "/path",
 f: async ctx => await ctx.req.blob()
}
{
  path: '/path',
  options: {add: ["req"]},
  f: ctx => outOfContext(ctx)
};
query: QS extends { unique: true; } ? (string | null) : { [key: string]: string; }

Gets the Queries from the URL, utilize the ctx.query.


{
  path: '/path',
  f: ctx => ctx.query?.name
};
// If the `ctx` goes out of context
{
  path: '/path',
  options: {add: ["query"]},
  f: ctx => outOfContext(ctx)
};
param: PA extends { unique: true; } ? string : Record<string, string>

Gets the parameters from the URL

{
  path: '/hello/:name',
  f: ctx => ctx.param.name
};

// If the `ctx` goes out of context
{
   path: '/hello/:name',
   options: {add: ["param"]},
   f: ctx => outOfContext(ctx)
};
headers: UNI extends { readonly hasHeaders: true; } ? Record<string, string> : null
date: number

Adds a Date.now() returning the number of milliseconds elapsed since the epoch.

{
   path: "/path"
   f: ctx =>  ctx.date > Date.now()
     ? "unreachable"
     : "date is created before ctx is passed to f"
}

This behavior can be set for testing purpose

{
   path: "/",
   options:{
     setDate: 1694246953189
   },
   f: ctx => ctx.date === 1694246953189
      ? "Date is bind to a state"
      : "unreachable"
}
randomNumber: number

Resolve Behavior

In a petition that includes a resolve, every element within the resolve object is initially unknown. It must be explicitly set within the containing petition to be recognized and processed.

// Example
{
    path: "/sample",
    resolve: { name: "nestedElement", f: () => "Hello World"},
    // 'nestedElement' is set here for the main function to recognize it
    f: context => context.resolve.nestedElement
}

Ensure that every element you want to use from resolve is appropriately defined in your petition.

hash: string

Generates a unique ID using crypto.randomUUID().

{
   path: "/path",
   f: ctx => ctx.hash === "some-random-uuid"
     ? "ID matches expected value"
     : "Generated a unique ID"
}

This behavior can be set for testing purposes:

{
   path: "/",
   options:{
     setHash: "specified-uuid-value"
   },
   f: ctx => ctx.hash === "specified-uuid-value"
      ? "UUID is bind to a state"
      : "unreachable"
}
mutable: { [keys: string]: any; }

Utilizes ctx.mutable for scenarios where mutable state is needed in Vixeny.


{
  path: '/mutable',
  mutable: true,
  //  the function is "example", resolves with name "hello", which mutates "result"
  resolve: {...example_r_$hello_m_$result_string},
  f: ctx => ctx.mutable.result as string
};

The mutable state is global and can be accessed at any depth:

{
  path: '/deepMutable',
  mutable: true,
  //  the function is "example", resolves with name "hello", which mutates "result"
  resolve: {...example_r_$hello_m_$result_string},
  f: ctx => ctx.branch.function("Greetings") as string,
  branch: {
    name: "function",
    f: c => `${c.arguments} ${c.mutable.result}`
  }
};
arguments: unknown

Interacts with the arguments property in ctx.branch to receive input for branch functions.


{
  path: '/path',
  f: ctx => ctx.branch.hello("Greetings!"),
  branch: {
    name: "hello",
    f: c => c.arguments
  }
};

When invoking a branch function, any parameters passed are accessible as arguments within the branch function.


{
  path: '/multipleArgs',
  f: ctx => ctx.branch.greet("Hello", "world!"),
  branch: {
    name: "greet",
    f: c => `${c.arguments[0]} ${c.arguments[1]}`
  }
};

In this example, multiple arguments are passed to the branch function, and they're accessed via index in the branch.