This is the standard method to retrieve query parameters from a request.
In the Vixeny framework, thereβs a component called the checker
, which actively tries to optimize your functions.
In this example, it understands that we are only using id
.
import { wrap } from "vixeny";
const handler = wrap()()
.stdPetition({
path: "/",
f: ({ query }) => query.id ?? "not found",
})
// `debugLast` shows all components used in the last request
// query: [id?]
.debugLast()
.testRequests();
await handler(new Request("http://localhost/?id=123"))
.then((x) => x.text())
// Logging : 123
.then(console.log);
There are cases where the checker will not be able to infer the elements used, which will trigger a default case. You can check this at any time using debugLast
.
import { wrap } from "vixeny";
const handler = wrap()()
.stdPetition({
path: "/",
f: ({ query }) => JSON.stringify(query) ?? "not found",
})
// `debugLast` shows all components used in the last request
// query: Record<string, string|null> | null
.debugLast()
.testRequests();
await handler(new Request("http://localhost/?id=123&user=Avant"))
.then((x) => x.text())
// Logging : {"id":"123","user":"Avant"}
.then(console.log);
The only
option restricts query parameters to only accept the parameters specified in it.
import { wrap } from "vixeny";
const handler = wrap()()
.stdPetition({
path: "/",
query: {
// Locking this path to use only the specified parameters
only: ["id"],
},
f: ({ query }) => JSON.stringify(query) ?? "not found",
})
// `debugLast` shows all components used in the last request
// query: [id?]
.debugLast()
.testRequests();
await handler(new Request("http://localhost/?id=123&user=Avant"))
.then((x) => x.text())
// Logging : {"id":"123"}
.then(console.log);
Hereβs a more naturally phrased version of your documentation:
This option simplifies handling by replacing the query
object with the value of the specified name
key, if present. This is a targeted optimization to streamline access to important parameters.
import { wrap } from "vixeny";
const handler = wrap()()
.stdPetition({
path: "/",
query: {
// Enabling unique
unique: true,
// Key to target
name: "user",
},
// Directly returning the value of the specified query parameter
f: ({ query }) => query ?? "not found",
})
// `debugLast` visualizes which components were utilized in the last request
// query: [user?]
.debugLast()
.testRequests();
await handler(new Request("http://localhost/?id=123&user=Avant"))
.then((x) => x.text())
// Outputs the value of 'user', i.e., 'Avant'
.then(console.log);
If crypto
with at least a globalKey
is present.