The branch
function was created to utilize all the functionalities of Vixeny while being lazy. In contrast, resolve
does the same but is always resolved before the request in context.
You can also pass arguments to these functions.
f
)It gets the type of the value
in the key args
. In the next example, it gets the type of text
as a string.
import { petitions } from "vixeny";
// `args` are only used in `branch`
const returnArgs = petitions.branch()({
// Gets the type of string; `{} as string` is also valid
args: "text",
// Using the args that are passed to this `branch`
f: (ctx) => ctx.args,
});
// Creating a petition at `/`
const hello = petitions.common()({
path: "/",
branch: {
returnArgs,
},
// It returns arguments
f: ({ branch }) => branch.returnArgs("hello world"),
});
You can also coerce the type, in this case to Record<string, string>
:
import { petitions, wrap } from "vixeny";
// `args` are only used in `branch`
const returnArgs = petitions.branch()({
args: {} as Record<string, string>,
// Using the args that are passed to this `branch`
f: (ctx) => ctx.args,
});
Here we are using a petition in wrap
and adding a branch that returns the string.
import { petitions, wrap } from "vixeny";
// `args` are only used in `branch`
const returnArgs = petitions.branch()({
// Gets the type of string; `{} as string` is also valid
args: "string",
// Using the args that are passed to this `branch`
f: (ctx) => ctx.args,
});
const handler = wrap()()
.stdPetition({
path: "/user/:id",
branch: {
// Adding a branch
returnArgs,
},
f: ({ branch, param }) => branch.returnArgs(param.id),
})
.testRequests();
// Logging bubbles
console.log(
await handler(new Request("http://localhost/user/bubbles")).then(
(res) => res.text(),
),
);
In this example, the body will only be parsed if the token is valid.
import { petitions, wrap } from "vixeny";
// Common key
const key = `secret!`;
// Branch
const getBody = petitions.branch()({
args: undefined,
f: async (ctx) => await ctx.req.text(),
});
const handler = wrap()()
// Getting keys
.stdPetition({
path: "/getKey/:name",
// Adding Crypto
crypto: {
globalKey: key,
},
f: ({ sign, param }) => sign(param),
})
.customPetition({
path: "/user/:id",
method: "POST",
// Adding Crypto
crypto: {
globalKey: key,
},
branch: {
// Adding `getBody`
getBody,
},
f: async ({ token, branch }) =>
// If the Token is valid it will parse the body
token.user
? new Response(await branch.getBody())
: new Response(null, { status: 403 }),
})
.testRequests();
// Getting token
const token = await handler(new Request("http://localhost/getKey/bubbles"))
.then((res) => res.text());
// Valid request
const req = new Request("http://localhost/user/bubbles", {
method: "POST",
headers: {
Cookie: "user=" + token,
},
});
// Invalid request 403
console.log(
await handler(new Request("http://localhost/user/bubbles", {
method: "POST",
})
.then((res) => res.status),
);
// Valid request 200
console.log(
await handler(req).then((res) => res.status),
);
Using a resolve as a branch and showing that you need to add undefined
.
import { petitions, wrap } from "vixeny";
// Resolve
const world = petitions.resolve()({
f: () => "world",
});
// Branch
const hello = petitions.branch()({
args: undefined,
f: () => "hello",
});
const handle = wrap()()
.stdPetition({
path: "/",
branch: {
// Adding branches
hello,
world,
},
// Resolves are valid branches, you only need to pass an undefined argument
f: ({ branch }) => `${branch.hello()} ${branch.world(undefined)}`,
})
// Creating a server for testing
.testRequests();
console.log(
// Logging `Hello world`
await handle(new Request("http://localhost/"))
.then((res) => res.text()),
);
If crypto
with at least a globalKey
is present.