npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@asterflow/response

v1.0.11

Published

<div align="center">

Readme

@asterflow/response

license-info stars-info last-commit

bundle-size

Unified HTTP response adapter system for AsterFlow applications.

📦 Installation

npm install @asterflow/response
# or
bun install @asterflow/response

💡 About

@asterflow/response provides a type-safe, unified response system for AsterFlow applications. It standardizes HTTP response handling across different runtimes while maintaining full type safety and providing convenient helper methods for common HTTP status codes.

✨ Features

  • Complete Type Safety: Full TypeScript support with type inference for responses, status codes, and body types
  • Status Code Helpers: Built-in methods for common HTTP status codes (200, 201, 400, 404, etc.)
  • Header Management: Type-safe header manipulation with method chaining
  • Cookie Support: Integrated cookie management system
  • Content Type Detection: Automatic content type detection and JSON serialization
  • Multi-Runtime Support: Compatible with standard Response and Node.js ServerResponse
  • Immutable Design: Response objects are immutable, promoting safer code patterns
  • Method Chaining: Fluent API for building complex responses

🚀 Usage

Basic Response

import { Response } from '@asterflow/response'

// Simple text response
const response = new Response()
  .success({ message: 'Hello World!' })

// With status code
const response = new Response()
  .status(201)
  .send({ id: 1, name: 'User' })

Status Code Helpers

import { Response } from '@asterflow/response'

// Success responses
const success = new Response().success({ data: 'Success!' })
const created = new Response().created({ id: 1 })
const noContent = new Response().noContent()

// Error responses
const badRequest = new Response().badRequest({ error: 'Invalid input' })
const unauthorized = new Response().unauthorized({ error: 'Not authenticated' })
const forbidden = new Response().forbidden({ error: 'Access denied' })
const notFound = new Response().notFound({ error: 'Resource not found' })
const zodError = new Response().zodError({ errors: ['Validation failed'] })

Headers and Cookies

import { Response } from '@asterflow/response'

const response = new Response()
  .success({ message: 'Hello!' })
  .setHeader('X-Custom-Header', 'custom-value')
  .setHeader('Cache-Control', 'no-cache')
  .setCookie('session', 'abc123')
  .setCookie('theme', 'dark')

// Headers and cookies are fully typed
console.log(response.header) // Map with typed entries
console.log(response.cookies) // Map with typed entries

JSON Responses

import { Response } from '@asterflow/response'

// Automatic JSON serialization
const jsonResponse = new Response()
  .json({ 
    users: [
      { id: 1, name: 'John' },
      { id: 2, name: 'Jane' }
    ]
  })

// Content-Type automatically set to application/json

Integration with Different Runtimes

Standard Web API

import { Response } from '@asterflow/response'

const response = new Response()
  .success({ message: 'Hello World!' })

// Convert to standard Response
const webResponse = response.toResponse()

Node.js HTTP Server

import { Response } from '@asterflow/response'
import { createServer } from 'http'

createServer((req, res) => {
  const response = new Response()
    .success({ message: 'Hello from Node.js!' })
  
  // Convert to ServerResponse
  response.toServerResponse(res)
})

Advanced Type Safety

import { Response } from '@asterflow/response'

// Define custom response types
type MyResponders = {
  200: { message: string; data: unknown }
  201: { id: number; message: string }
  400: { error: string; details?: string[] }
  404: { error: string }
}

const response = new Response<MyResponders>()
  .success({ message: 'Success!', data: { id: 1 } }) // Fully typed
  
// TypeScript will enforce the correct shape for each status code

Method Chaining

import { Response } from '@asterflow/response'

const response = new Response()
  .status(201)
  .setHeader('Location', '/users/123')
  .setHeader('X-Request-ID', 'req-123')
  .setCookie('last-action', 'create-user')
  .json({ 
    id: 123, 
    message: 'User created successfully' 
  })

🔧 API Reference

Core Methods

  • status(code) - Set HTTP status code
  • getStatus() - Get current status code
  • send(data) - Send response with data
  • json(data) - Send JSON response with proper Content-Type

Status Code Helpers

  • success(data) - 200 OK
  • created(data) - 201 Created
  • noContent(data) - 204 No Content
  • badRequest(data) - 400 Bad Request
  • unauthorized(data) - 401 Unauthorized
  • forbidden(data) - 403 Forbidden
  • notFound(data) - 404 Not Found
  • zodError(data) - 422 Unprocessable Entity

Header and Cookie Management

  • setHeader(name, value) - Add/update header
  • setCookie(name, value) - Add/update cookie

Runtime Conversion

  • toResponse() - Convert to standard Web API Response
  • toServerResponse(serverRes) - Write to Node.js ServerResponse

🔗 Related Packages

📄 License

MIT - See LICENSE for more details.