Skip to content

Démarrage rapide

Installation des dépendances

bash
npm install @duplojs/http@0 @duplojs/utils@1
bash
yarn add @duplojs/http@0 @duplojs/utils@1
bash
pnpm add @duplojs/http@0 @duplojs/utils@1

Dépendances de développement

bash
npm install typescript@>=5.9 tsx@>=4.21 --save-dev
bash
yarn add typescript@>=5.9 tsx@>=4.21 --dev
bash
pnpm add typescript@>=5.9 tsx@>=4.21 --dev

Configuration du package.json

json
{
	...,
	"type": "module", // Fortement conseillé
	...,
}

Configuration typescript

json
{
	"compilerOptions": {
		"target": "ESNext",
		"lib": ["ESNext"],
		"moduleDetection": "force",
		"module": "ESNext",
		"moduleResolution": "Bundler",
		"noEmit": true,
		"isolatedModules": true,
		"esModuleInterop": true,
		"forceConsistentCasingInFileNames": true,
		"strict": true,
		"noImplicitAny": true,
		"strictNullChecks": true,
		"strictFunctionTypes": true,
		"strictBindCallApply": true,
		"strictPropertyInitialization": true,
		"noImplicitThis": true,
		"useUnknownInCatchVariables": true,
		"alwaysStrict": true,
		"noImplicitReturns": true,
		"noUncheckedIndexedAccess": true,
		"noImplicitOverride": true,
		"skipLibCheck": true,
	},
	"include": ["src/**/*.ts"],
}

Créer le fichier src/routes/helloWorld.ts

ts
import { ResponseContract, 
useRouteBuilder
} from "@duplojs/http";
import {
DPE
} from "@duplojs/utils";
useRouteBuilder
("GET", "/hello-world")
.
extract
({
query
: {
name
:
DPE
.
string
(),
}, }) .
handler
(
ResponseContract.
ok
("helloWorld.send",
DPE
.
string
()),
(
floor
, {
response
}) => {
const
message
= `hello ${
floor
.
name
}!`;
return
response
("helloWorld.send",
message
);
}, );

Créer le fichier src/main.ts

ts
import { 
createHub
,
routeStore
} from "@duplojs/http";
import {
createHttpServer
} from "@duplojs/http/node";
import {
codeGeneratorPlugin
} from "@duplojs/http/codeGenerator";
import {
openApiGeneratorPlugin
} from "@duplojs/http/openApiGenerator";
import "./routes/helloWorld"; const
hub
=
createHub
({
environment
: "DEV" })
.
register
(
routeStore
.
getAll
())
.
plug
(
codeGeneratorPlugin
({
outputFile
: "types.d.ts" }))
.
plug
(
openApiGeneratorPlugin
({
routePath
: "/swagger-ui" }));
await
createHttpServer
(
hub
,
{
host
: "localhost",
port
: 1506,
}, );
console
.
log
("Server is ready !");

Lancer le serveur HTTP

bash
npx tsx --watch src/main.ts

Utiliser le client

ts
import { 
createHttpClient
} from "@duplojs/http/client";
import { type
Routes
} from "./types";
const
client
=
createHttpClient
<
Routes
>({
baseUrl
: "http://localhost:1506",
}); await
client
.
get
(
"/hello-world", {
query
: {
name
: "math" } },
) .
whenInformation
(
"helloWorld.send", ({
body
}) => {
}, ) .
whenInformation
(
"extract-error", ({
code
}) => {
}, );

Diffusé sous licence MIT.