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

nestjs-restcall

v1.0.0

Published

Generic, extendible REST template for NestJS projects.

Readme

nestjs-restcall

Reusable REST client utilities for NestJS applications. nestjs-restcall provides an extendible AbstractRestTemplate class plus a default RestTemplateService, making it simple to wrap upstream HTTP APIs with consistent error handling, auth hooks, and reusable helpers.

  • 🔁 Shared request pipeline across verbs (GET, POST, etc.) with typed overloads
  • 🛡️ Centralized error translation via mapAndThrowError
  • 🔐 Pluggable auth/token handling using RequestContext
  • 🧰 Helper utilities for query strings and validation

Installation

npm install nestjs-restcall

If you are using Yarn or pnpm, install via yarn add nestjs-restcall or pnpm add nestjs-restcall.

Quick Start

Register the provided module, then inject the ready-to-use RestTemplateService anywhere you need to make outbound HTTP calls.

// src/app.module.ts
import { Module } from '@nestjs/common';
import { RestTemplateModule } from 'nestjs-restcall';

@Module({
  imports: [RestTemplateModule],
})
export class AppModule {}
// Some service or controller
import { Injectable } from '@nestjs/common';
import { RestTemplateService } from 'nestjs-restcall';

@Injectable()
export class PaymentsService {
  constructor(private readonly rest: RestTemplateService) {}

  async fetchPayment(id: string) {
    return this.rest.get<Payment>(`https://payments.internal/api/payments/${id}`);
  }
}

Passing headers, params, and context

await this.rest.get<Payment[]>('https://payments.internal/api/payments', {
  params: { limit: 50 },
  headers: { 'X-Trace-Id': ctx.traceId },
  context: { token: userToken, requestId: ctx.traceId },
});

Extending the template

Most projects will subclass AbstractRestTemplate to customize auth, headers, or error handling.

import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
  AbstractRestTemplate,
  RequestContext,
  UpstreamErrorBody,
} from 'nestjs-restcall';

@Injectable()
export class PaymentsRestTemplate extends AbstractRestTemplate {
  protected getDefaultHeaders() {
    return {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    } as const;
  }

  protected async applyAuth(config, context?: RequestContext) {
    if (!context?.token) return config;
    return {
      ...config,
      headers: {
        ...(config.headers ?? {}),
        Authorization: `Bearer ${context.token}`,
      },
    };
  }

  protected mapAndThrowError(error: unknown): never {
    const upstream = error as { response?: { status?: number; data?: UpstreamErrorBody } };
    const status = upstream.response?.status ?? HttpStatus.BAD_GATEWAY;
    throw new HttpException(
      {
        message: upstream.response?.data?.message ?? 'Upstream request failed',
        code: upstream.response?.data?.code ?? 'UPSTREAM_ERROR',
        details: upstream.response?.data?.details,
      },
      status,
    );
  }
}

Provide your subclass via Nest DI as needed (e.g., register it in a feature module, use it instead of the default RestTemplateService, or wrap it with domain-specific methods).

Helpers and types

The package re-exports utility helpers and types to keep integrations strongly typed:

  • buildUrl(url, params) – append primitive query params
  • assertNonEmpty(value, label) – guard string arguments
  • RequestContext and RestRequestOption interfaces – consistent context typing
  • TokenProvider interface – handshake around caching and refreshing auth tokens

Import them directly from the package root:

import { buildUrl, RequestContext, TokenProvider } from 'nestjs-restcall';

Development

Local scripts assume Node.js 18+:

# Install deps
npm install

# Check formatting & lint
npm run lint

# Run tests
npm run test

# Compile to dist/
npm run build

The test script disables Watchman to stay CI-friendly. Unit coverage lives in src/rest-template/services/rest-template.service.spec.ts; expand the suite as you add features.

CI & Publishing

  • .github/workflows/test.yml runs linting and tests on every pull request and pushes to main/master.
  • .github/workflows/publish.yml executes lint, tests, build, and npm publish when a release is published or a version tag is pushed. Configure the NPM_TOKEN secret in your repository settings before triggering a publish.

License

MIT © 2025 rest-call contributors