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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nuxx/torn-fetch

v1.1.3

Published

provides a wrapper around openapi-fetch that throws an error when Torn's API returns an error

Readme

Torn Fetch

A TypeScript wrapper around openapi-fetch that provides a better developer experience when working with the Torn API. This library automatically handles authentication and error checking, throwing JavaScript errors when the API returns error responses.

Features

  • Type-safe API calls: Full TypeScript support with generated types from Torn's OpenAPI schema
  • Automatic error handling: Throws JavaScript errors when the API returns error responses
  • Higher-order function pattern: Create specialized fetchers for specific API endpoints
  • Clean API: Simple and intuitive interface for making API calls

Installation

npm install @nuxx/torn-fetch

Migration from v0.x

If you're upgrading from v0.x, the main export has been renamed from useTornFetch to tornFetch. The old name still works in v1.x with a deprecation warning, but will be removed in v2.0.0.

Migration:

// Old (v0.x)
import { useTornFetch } from '@nuxx/torn-fetch'
const data = await useTornFetch(apiKey, path)

// New (v1.0+)
import { tornFetch } from '@nuxx/torn-fetch'
const data = await tornFetch(apiKey, path)

The function signature and behavior are identical.

Usage

Basic Usage

import { tornFetch } from '@nuxx/torn-fetch'

try {
  const userAttacks = await tornFetch( 
    'your-api-key',
    '/user/attacks'
  )
  console.log(userAttacks)
} catch (error) {
  console.error('API Error:', error.message)
}
import { tornFetch } from '@nuxx/torn-fetch'

// Use with path parameters
const attacks = await tornFetch(
  'your-api-key',
  '/faction/{id}/chain',
  {
    path: {
      id: 33458
    }
  }
)
import { tornFetch } from '@nuxx/torn-fetch'

// Use with query parameters
const attacks = await tornFetch(
  'your-api-key',
  '/user/attacks',
  {
    query: {
      limit: 25,
      from: 1753037683
    }
  }
)

Error Handling

The library automatically throws JavaScript errors when the Torn API returns error responses:

import { tornFetch } from '@nuxx/torn-fetch'

try {
  const userAttacks = await tornFetch( 
    'invalid-key-abc123',
    '/user/attacks'
  )
  console.log(userAttacks)
} catch (error) {
  console.error('API Error:', error.message)
}

API Reference

tornFetch<TPath>(apiKey: string, path: TPath, options?: TParams<TPath>): Promise<TResponse<TPath>>

Makes a type-safe call to the Torn API with automatic error handling.

Parameters:

  • apiKey: Your Torn API key
  • path: The API endpoint path (e.g., /user, /faction/{id}/chain)
  • options (optional): An object conforming to the values expected by the Torn API, including:
    • query: Query parameters to include in the request
    • path: Path parameters to replace in the endpoint path

Returns: Promise resolving to the API response data

Throws: JavaScript error if the API returns an error response

Development

Prerequisites

  • Node.js 18+
  • Bun

Setup

# Install dependencies
bun install

# Get the latest Torn API schema and build
bun run build

Scripts

  • bun run get-schema - Downloads the latest Torn API OpenAPI schema and generates TypeScript types
  • bun run build - Builds the project using tsup
  • bun run lint - Runs ESLint on the codebase
  • bun run type-check - Runs TypeScript type checking
  • bun test - Runs the test suite
  • bun run test:watch - Runs tests in watch mode
  • bun run test:coverage - Runs tests with coverage reporting
  • bun run ci - Runs the complete CI pipeline (schema, lint, type-check, test, build)

Testing

This package includes comprehensive tests with 100% code coverage. The test suite covers:

  • Core functionality: API calls, parameter handling, authentication
  • Error handling: Torn API error detection and JavaScript error throwing
  • Edge cases: Null values, undefined responses, non-object data
  • Type safety: Proper TypeScript type checking

Run tests with:

bun run test              # Run tests once
bun run test:watch        # Run tests in watch mode
bun run test:coverage     # Run tests with coverage report

See TESTING.md for detailed testing documentation.

Project Structure

torn-fetch/
├── coverage/                 # Test coverage reports
├── dist/                     # Compiled output
├── src/
│   ├── __tests__/           # Test files
│   │   ├── index.test.ts    # Main functionality tests
│   │   ├── error-detection.test.ts # Error detection logic tests
│   │   └── error-handling.test.ts  # Error handling tests
│   ├── index.ts             # Main export (useTornFetch)
│   ├── openapi.json         # Torn API OpenAPI schema
│   └── torn-api.ts          # Generated TypeScript types
├── eslint.config.js         # ESLint configuration
├── package.json
├── tsconfig.json            # TypeScript configuration
├── tsup.config.ts           # Build configuration
├── bun.lock                 # Bun lock file
├── TESTING.md               # Testing documentation
└── README.md

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run the full CI pipeline: bun run ci
  5. Submit a pull request

All contributions should include appropriate tests and maintain 100% code coverage.

License

Unlicense

Author

nuxx [3054747] [email protected]