Skip to content

Run a check

You can standardize your checks within a route very easily with a checker.

ts
import { 
useCheckerBuilder
} from "@duplojs/http";
import {
findOneUser
} from "./findOneUser";
export const
userExist
=
useCheckerBuilder
({
options
: {
someOption
: "" } })
.
handler
(
async(
input
: number, {
output
,
options
: {
someOption
} }) => {
const
user
= await
findOneUser
(
input
);
if (
user
) {
return
output
("user.find",
user
);
} else { return
output
("user.notfound", null);
} }, );

To create a checker, simply import the useCheckerBuilder function from @duplojs/http and call the builder’s handler method. What matters is defining your checker’s input, and correctly defining its outputs.

INFO

Outputs are accompanied by an information value, which makes it easy to discriminate which output you got.

Implementing a checker in a route

ts
import { ResponseContract, 
useRouteBuilder
} from "@duplojs/http";
import {
DPE
} from "@duplojs/utils";
import {
userExist
} from "./checker";
import {
userSchema
} from "./schema";
useRouteBuilder
("GET", "/users/{userId}")
.
extract
({
params
: {
userId
:
DPE
.
coerce
.
number
(),
}, }) .
check
(
userExist
,
{
input
: ({
userId
}) =>
userId
,
result
: "user.find",
otherwise
: ResponseContract.
notFound
("user.notfound"),
indexing
: "user",
}, ) .
handler
(
ResponseContract.
ok
("user.find",
userSchema
),
({
user
}, {
response
}) =>
response
("user.find",
user
),
);

To use a checker, call the check method on the route builder. Pass the checker as the first argument and configure its implementation:

  • input: passes the input value to the checker from the Floor.
  • result: defines the expected information to move to the next step.
  • otherwise: defines the response returned if the information received from the checker does not match the expected result.
  • indexing: defines the key where the data associated with the defined result will be indexed.

Go faster

You can go much faster by creating a presetChecker.

ts
import { 
createPresetChecker
, ResponseContract } from "@duplojs/http";
import {
userExist
} from "./checker";
export const
iWantUserExist
=
createPresetChecker
(
userExist
,
{
result
: "user.find",
otherwise
: ResponseContract.
notFound
("user.notfound"),
}, );

Simply import the createPresetChecker function from @duplojs/http and define its default implementation options.

ts
import { ResponseContract, 
useRouteBuilder
} from "@duplojs/http";
import {
DPE
} from "@duplojs/utils";
import {
userSchema
} from "./schema";
import {
iWantUserExist
} from "./presetChecker";
useRouteBuilder
("GET", "/users/{userId}")
.
extract
({
params
: {
userId
:
DPE
.
coerce
.
number
(),
}, }) .
presetCheck
(
iWantUserExist
.
indexing
("user"),
({
userId
}) =>
userId
,
) .
handler
(
ResponseContract.
ok
("user.find",
userSchema
),
({
user
}, {
response
}) =>
response
("user.find",
user
),
);

Use the presetCheck method on the route builder and easily implement your checker, remembering an input function to send the data you want.

Special case

Sometimes you’re in a situation where creating a checker to factor code isn’t useful. You just want to call a function and continue or stop the route execution. That’s what cut steps are for.

ts
import { ResponseContract, 
useRouteBuilder
} from "@duplojs/http";
import {
DPE
} from "@duplojs/utils";
import {
userSchema
} from "./schema";
import {
findOneUser
} from "./findOneUser";
useRouteBuilder
("GET", "/users/{userId}")
.
extract
({
params
: {
userId
:
DPE
.
coerce
.
number
(),
}, }) .
cut
(
[ ResponseContract.
notFound
("user.notfound"),
ResponseContract.
forbidden
("user.inaccessible"),
], async({
userId
}, {
response
,
output
}) => {
if (
userId
=== 0) {
return
response
("user.inaccessible");
} const
user
= await
findOneUser
(
userId
);
if (!
user
) {
return
response
("user.notfound");
} return
output
({
user
});
}, ) .
handler
(
ResponseContract.
ok
("user.find",
userSchema
),
({
user
}, {
response
}) =>
response
("user.find",
user
),
);

Use the cut method on the route builder to create your custom step. It’s similar to the handler method, except it isn’t the last step of your route. In a cut step you can respond or move to the next step, with the option to add data to the route.

WARNING

The examples above do not encourage writing business logic in a checker or a cut. They are only there to illustrate the different functions and tools available so you can evaluate the potential and become autonomous as quickly as possible.

Released under the MIT License.