@arkyn/server@arkyn/server is a comprehensive server-side utility library designed for building robust HTTP APIs. It provides standardized HTTP responses, validation utilities, form parsing, and debugging tools that integrate seamlessly with modern web frameworks like React Router, Remix, and Hono.@arkyn/server?@arkyn/server solves these challenges by providing a unified set of tools that ensure your server responses are predictable, well-structured, and easy to debug.Request and Response objectsbash
npm install @arkyn/server zod
bash
bun add @arkyn/server zod
BadRequest (400), Unauthorized (401), Forbidden (403), NotFound (404), Conflict (409), UnprocessableEntity (422), ServerError (500), NotImplemented (501), and BadGateway (502).ts
import { BadRequest, NotFound, UnprocessableEntity } from "@arkyn/server";// Throw errors with descriptive messagesthrow new NotFound("User not found");// Form validation errors with field-specific messagesthrow new UnprocessableEntity({message: "Validation failed",fieldErrors: { email: "Invalid email format" },fields: { email: "invalid-email" },});
Success (200), Created (201), NoContent (204), Updated (200), and Found (302).ts
import { Created, Success, NoContent } from "@arkyn/server";// Return created resourcereturn new Created("User created successfully", {id: "123",name: "John",}).toResponse();// Return datareturn new Success("Users retrieved", users).toJson();// No content responsereturn new NoContent("Resource deleted").toResponse();
ts
import { ApiService, DebugService, LogService } from "@arkyn/server";// Configure API clientconst api = new ApiService({baseUrl: "https://api.example.com",baseHeaders: { "Content-Type": "application/json" },enableDebug: true,});const users = await api.get("/users", { urlParams: { page: "1" } });// Configure debug service for accurate caller detectionDebugService.setIgnoreFile("httpAdapter.ts");// Configure log service for centralized loggingLogService.setConfig({trafficSourceId: "my-app",userToken: "auth-token",});
ts
import { SchemaValidator, formParse, getScopedParams } from "@arkyn/server";import { z } from "zod";// Schema validation with multiple strategiesconst schema = z.object({ name: z.string().min(1), email: z.string().email() });const validator = new SchemaValidator(schema);validator.isValid(data); // Boolean checkvalidator.validate(data); // Throws ServerErrorvalidator.formValidate(data); // Throws UnprocessableEntity// Extract scoped query parametersconst filters = getScopedParams(request, "filter");// ?filter:status=active&filter:type=user → { status: "active", type: "user" }
ts
import { validateEmail, validateCpf, validateCnpj } from "@arkyn/server";// Email validation with DNS verificationconst isValidEmail = await validateEmail("user@example.com");// Document validationconst isValidCpf = await validateCpf("123.456.789-09");const isValidCnpj = await validateCnpj("12.345.678/0001-90");
@arkyn/server follows these core principles:toResponse() for standard responses or toJson() for Response.json() alternative