Resolves in Vixeny ensure that all necessary data dependencies are resolved before the main function is executed. This mechanism simplifies asynchronous data handling and composition, making your code more modular and maintainable.
Below are some examples demonstrating the usage of resolves within Vixeny.
import { petitions, wrap } from "vixeny";
// Setting up a resolution
const sayHello = petitions.resolve()({
f: () => "hello",
});
// Creating a petition
const hey = petitions.common()({
path: "/hey",
resolve: {
sayHello,
},
f: ({ resolve }) => `${resolve.sayHello} World!`,
});
const serve = wrap(options)()
.addAnyPetition(hey);
import { petitions, wrap } from "vixeny";
// Defining an asynchronous resolve function
const hello = petitions.resolve()({
f: async () => await Promise.resolve("Hello"),
});
// Using the resolve in a petition
wrap(options)()
.stdPetition({
path: "/helloWorld",
resolve: {
hello,
world: { f: () => "world" },
},
f: (ctx) => `${ctx.resolve.hello} ${ctx.resolve.world}`,
});
import { petitions, wrap } from "vixeny";
// Nested resolve
const nested = petitions.resolve()({
f: () => "hello",
});
// Handler with nested resolve
const handler = wrap()()
.stdPetition({
path: "/",
resolve: {
nested,
},
f: (ctx) => ctx.resolve.nested,
})
.compose();
console.log(
await handler(new Request("http://localhost/"))
.then((res) => res.text()),
);
If crypto
with at least a globalKey
is present.