{"library":"servie","title":"Servie HTTP Interfaces","description":"Servie provides a standard, framework-agnostic set of HTTP interfaces, including `Request`, `Response`, `Body`, `Headers`, and `AbortController`, designed for building interchangeable HTTP clients and servers in both Node.js and browser environments. It aims to offer primitives similar to the Web Fetch API, facilitating universal JavaScript HTTP operations. The current stable version is `4.3.3`, with minor releases frequently addressing bug fixes and introducing small features. Its key differentiators include its dual-environment compatibility without configuration, its focus on raw HTTP interface primitives rather than a full framework, and its modular design which allows it to be used as a foundational layer for various transport mechanisms and middleware libraries.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install servie"],"cli":null},"imports":["import { Request } from 'servie'","import { Response } from 'servie'","import { Body } from 'servie'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Request, Response, Body, Headers, AbortController } from \"servie\";\n\n// A simple Servie-compatible request handler\nasync function handleRequest(request: Request): Promise<Response> {\n  const url = new URL(request.url);\n  console.log(`Received request for: ${url.pathname} with method ${request.method}`);\n\n  if (url.pathname === \"/greet\" && request.method === \"POST\") {\n    const name = await request.text();\n    const responseBody = `Hello, ${name}!`;\n    const headers = new Headers({ \"Content-Type\": \"text/plain\" });\n    return new Response(responseBody, { status: 200, headers });\n  }\n\n  if (url.pathname === \"/json\" && request.method === \"GET\") {\n    const data = { message: \"This is a JSON response\", timestamp: new Date().toISOString() };\n    const headers = new Headers({ \"Content-Type\": \"application/json\" });\n    return new Response(JSON.stringify(data), { status: 200, headers });\n  }\n\n  if (url.pathname === \"/abort-test\") {\n    const controller = new AbortController();\n    const signal = controller.signal;\n    setTimeout(() => controller.abort(), 100); // Abort after 100ms\n    try {\n      await new Promise((resolve, reject) => {\n        signal.addEventListener('abort', () => reject(new Error('Request aborted')));\n        // Simulate a long operation that could be aborted\n        setTimeout(resolve, 500);\n      });\n      return new Response(\"Operation completed before abort.\", { status: 200 });\n    } catch (e: any) {\n      if (e.message === 'Request aborted') {\n        return new Response(\"Operation aborted successfully.\", { status: 400 });\n      }\n      throw e;\n    }\n  }\n\n  return new Response(\"Not Found\", { status: 404 });\n}\n\n// Example usage:\nasync function runExamples() {\n  // 1. Simple GET request\n  const getRequest = new Request(\"http://localhost:3000/json\", { method: \"GET\" });\n  const getResponse = await handleRequest(getRequest);\n  console.log(`GET /json Status: ${getResponse.status}, Body: ${await getResponse.json().then(data => JSON.stringify(data))}`);\n\n  // 2. POST request with a text body\n  const postRequest = new Request(\"http://localhost:3000/greet\", {\n    method: \"POST\",\n    body: \"World\",\n    headers: { \"Content-Type\": \"text/plain\" }\n  });\n  const postResponse = await handleRequest(postRequest);\n  console.log(`POST /greet Status: ${postResponse.status}, Body: ${await postResponse.text()}`);\n\n  // 3. Aborted request demonstration\n  const abortRequest = new Request(\"http://localhost:3000/abort-test\", { method: \"GET\" });\n  const abortResponse = await handleRequest(abortRequest);\n  console.log(`GET /abort-test Status: ${abortResponse.status}, Body: ${await abortResponse.text()}`);\n}\n\nrunExamples().catch(console.error);","lang":"typescript","description":"This quickstart demonstrates how to create `Request` and `Response` objects, handle various body types (text, JSON), manage headers, and utilize `AbortController` for request cancellation within a simple Servie-compatible request handler.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}