Skip to content

Get data

ts
import { ResponseContract, 
useRouteBuilder
} from "@duplojs/http";
import {
DPE
} from "@duplojs/utils";
useRouteBuilder
("POST", "/users/{userId}")
.
extract
({
params
: {
userId
:
DPE
.
coerce
.
number
(),
},
query
: {
someValue
:
DPE
.
string
().
optional
(),
superQuery
:
DPE
.
coerce
.
number
(),
}, }) .
extract
({
headers
: {
token
:
DPE
.
string
(),
},
body
:
DPE
.
object
({
username
:
DPE
.
string
(),
age
:
DPE
.
string
(),
}), }) .
handler
(
ResponseContract.
noContent
("someInformation"),
(
floor
, {
response
}) => {
const {
someValue
,
superQuery
,
token
,
userId
,
body
: {
username
,
age
,
}, } =
floor
;
return
response
("someInformation");
}, );

To retrieve data from a request, call the extract method on the route builder. This adds a data extraction step to your route. This extraction step always uses a dataParser from the @duplojs/utils library. This validates the data before you use it.

The data is then available in the Floor, an object that contains the accumulated data for the route. Data can come from extraction steps or other steps like checkers, processes, cuts, etc.

Extraction depth

ts
import { 
useRouteBuilder
} from "@duplojs/http";
import {
DPE
} from "@duplojs/utils";
useRouteBuilder
("POST", "/users/{userId}")
.
extract
({
headers
: {
token
:
DPE
.
string
(),
},
body
:
DPE
.
object
({
username
:
DPE
.
string
(),
age
:
DPE
.
string
(),
}), });

Data extraction can be done at two levels. The storage key will be the dataParser key.

Receive FormData

ts
import { 
controlBodyAsFormData
, ResponseContract,
useRouteBuilder
} from "@duplojs/http";
import {
SDPE
} from "@duplojs/server-utils";
import {
asserts
,
DPE
,
E
,
O
, Path } from "@duplojs/utils";
useRouteBuilder
("POST", "/documents", {
bodyController
:
controlBodyAsFormData
({
maxFileQuantity
: 5 }),
}) .
extract
({
body
: {
userId
:
DPE
.
coerce
.
number
(),
files
:
SDPE
.
file
().
array
().
max
(3),
}, }) .
handler
(
ResponseContract.
noContent
("files.receive"),
async({
files
,
userId
}, {
response
}) => {
for (const [
index
,
file
] of
O
.
entries
(
files
)) {
asserts
(
await
file
.
move
(
Path.
resolveRelative
([
"new/path/of/file", `${
userId
}-${
index
}${
file
.
getExtension
() ?? ""}`,
]), ),
E
.
isRight
,
); } return
response
("files.receive");
}, );

To receive FormData, you just need to configure a bodyController on your route. This tells the route to prepare for a FormData stream during a request. The parameters defined for this bodyController are applied while streaming the body, unlike the dataParser, which is applied after the body is received.

INFO

Extraction schemas can be as complex as JSON schemas, because the FormData serializer used supports deeper nesting than the default FormData.

File extraction is done with the file dataParser (SDPE.file()) from @duplojs/server-utils.

Released under the MIT License.