Quick start
Install dependencies
bash
npm install @duplojs/http@0 @duplojs/utils@1bash
yarn add @duplojs/http@0 @duplojs/utils@1bash
pnpm add @duplojs/http@0 @duplojs/utils@1Development dependencies
bash
npm install typescript@>=5.9 tsx@>=4.21 --save-devbash
yarn add typescript@>=5.9 tsx@>=4.21 --devbash
pnpm add typescript@>=5.9 tsx@>=4.21 --devConfigure package.json
json
{
...,
"type": "module", // Strongly recommended
...,
}TypeScript configuration
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"],
}Create 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);
},
);Create 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 !");Start the HTTP server
bash
npx tsx --watch src/main.tsUse the 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 }) => {
},
);