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-hapi-adapter

v1.0.8

Published

Adaptador HTTP profesional para usar Hapi.js como servidor en aplicaciones NestJS con soporte completo para CORS, Swagger, vistas (HBS/EJS/Pug) y todas las características de NestJS

Readme

nestjs-hapi-adapter

npm version License: MIT

Adaptador HTTP profesional para usar Hapi.js como servidor en aplicaciones NestJS, con la misma facilidad que los adaptadores oficiales de Express o Fastify.

✨ Características

  • Integración completa con NestJS - Todos los decoradores, pipes, guards, interceptors funcionan perfectamente
  • 🚀 Soporte para Swagger/OpenAPI - Documentación automática con UI incluida y autenticación básica opcional
  • 🌐 CORS avanzado - Configuración flexible con múltiples orígenes y headers personalizados
  • 🎨 Motor de vistas - Soporte para Handlebars (HBS), EJS y Pug
  • 📦 Manejo de archivos - Multipart/form-data y uploads
  • 🔒 Producción ready - Sistema de autenticación básica para Swagger y control por ambientes
  • TypeScript nativo - Tipado completo y soporte para todos los tipos de NestJS

📦 Instalación

npm install nestjs-hapi-adapter @hapi/hapi

Dependencias opcionales

Según las características que necesites:

# Para usar vistas (cualquiera de estos)
npm install @hapi/vision handlebars  # Para Handlebars
npm install @hapi/vision ejs          # Para EJS
npm install @hapi/vision pug          # Para Pug

# Para usar Swagger
npm install @nestjs/swagger

🚀 Uso básico

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HapiAdapter, NestHapiApplication } from 'nestjs-hapi-adapter';

async function bootstrap() {
  const app = await NestFactory.create<NestHapiApplication>(
    AppModule,
    new HapiAdapter()
  );
  
  await app.listen(3000);
  console.log('🚀 Aplicación corriendo en http://localhost:3000');
}
bootstrap();

📚 Ejemplos completos

1. Con CORS

import { NestFactory } from '@nestjs/core';
import { HapiAdapter, NestHapiApplication } from 'nestjs-hapi-adapter';
import { AppModule } from './app.module';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';

async function bootstrap() {
  const app = await NestFactory.create<NestHapiApplication>(
    AppModule,
    new HapiAdapter()
  );
  
  // Configuración de CORS
  const corsOptions: CorsOptions = {
    origin: ['http://localhost:3000', 'http://localhost:4200'],
    methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
    allowedHeaders: [
      'Content-Type',
      'Accept',
      'Authorization',
      'x-api-key',
      'x-request-id'
    ],
    credentials: true,
    optionsSuccessStatus: 204
  };

  app.enableCors(corsOptions);
  
  await app.listen(3000);
}
bootstrap();

2. Con Swagger

import { NestFactory } from '@nestjs/core';
import { HapiAdapter, NestHapiApplication } from 'nestjs-hapi-adapter';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestHapiApplication>(
    AppModule,
    new HapiAdapter()
  );
  
  // Configuración de Swagger
  const config = new DocumentBuilder()
    .setTitle('Mi API')
    .setDescription('Documentación de la API')
    .setVersion('1.0')
    .addTag('users', 'Operaciones de usuarios')
    .build();
  
  const document = SwaggerModule.createDocument(app, config);

  // Registrar Swagger desde el adapter
  (app.getHttpAdapter() as any).registerSwagger(document, {
    mountPath: '/api/docs',           // URL de la UI
    jsonPath: '/api/docs-json',       // URL del JSON
    allowedEnvironments: ['development', 'staging'],
    basicAuth: {
      username: process.env.SWAGGER_USER || 'admin',
      password: process.env.SWAGGER_PASS || 'secret'
    }
  });
  
  await app.listen(3000);
  console.log('📚 Swagger UI: http://localhost:3000/api/docs');
  console.log('📄 Swagger JSON: http://localhost:3000/api/docs-json');
}
bootstrap();

3. Con vistas (Handlebars, EJS o Pug)

import { NestFactory } from '@nestjs/core';
import { HapiAdapter, NestHapiApplication, ViewEngine } from 'nestjs-hapi-adapter';
import { AppModule } from './app.module';
import { join } from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestHapiApplication>(
    AppModule,
    new HapiAdapter()
  );
  
  // Configurar directorio de vistas
  app.getHttpAdapter().setBaseViewsDir(join(__dirname, '..', 'views'));
  
  // Configurar motor de plantillas
  await app.getHttpAdapter().setViewEngine(ViewEngine.HBS);  // Handlebars
  // await app.getHttpAdapter().setViewEngine(ViewEngine.EJS);  // EJS
  // await app.getHttpAdapter().setViewEngine(ViewEngine.PUG);  // Pug
  
  await app.listen(3000);
}
bootstrap();

En tu controlador:

import { Controller, Get, Render } from '@nestjs/common';

@Controller()
export class AppController {
  @Get('home')
  @Render('index')  // Renderiza views/index.hbs
  getHome() {
    return { 
      title: 'Mi aplicación',
      message: 'Hola desde Hapi + NestJS'
    };
  }
}

4. Ejemplo completo (CORS + Swagger + Vistas)

import { NestFactory } from '@nestjs/core';
import { HapiAdapter, NestHapiApplication, ViewEngine } from 'nestjs-hapi-adapter';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import { join } from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestHapiApplication>(
    AppModule,
    new HapiAdapter()
  );
  
  // 1. Configurar vistas
  app.getHttpAdapter().setBaseViewsDir(join(__dirname, '..', 'views'));
  await app.getHttpAdapter().setViewEngine(ViewEngine.HBS);
  
  // 2. Configurar prefijo global
  app.setGlobalPrefix('api/v1');

  // 3. Configuración de CORS
  const corsOptions: CorsOptions = {
    origin: process.env.CORS_ORIGIN?.split(',') || ['http://localhost:3000'],
    methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
    allowedHeaders: [
      'Origin',
      'X-Requested-With',
      'Content-Type',
      'Accept',
      'Authorization',
      'x-api-key',
      'x-request-id',
      'x-correlation-id'
    ],
    credentials: true,
    optionsSuccessStatus: 204
  };

  app.enableCors(corsOptions);
  
  // 4. Configuración de Swagger
  const config = new DocumentBuilder()
    .setTitle('Mi API con Hapi')
    .setDescription('API construida con NestJS + Hapi.js')
    .setVersion('1.0')
    .addTag('users', 'Gestión de usuarios')
    .addTag('views', 'Renderizado de vistas')
    .build();
  
  const document = SwaggerModule.createDocument(app, config);

  (app.getHttpAdapter() as any).registerSwagger(document, {
    mountPath: '/api/docs',
    jsonPath: '/api/docs-json',
    allowedEnvironments: ['development', 'staging'],
    basicAuth: process.env.SWAGGER_USER && process.env.SWAGGER_PASS
      ? {
          username: process.env.SWAGGER_USER,
          password: process.env.SWAGGER_PASS,
        }
      : undefined,
  });
  
  await app.listen(3001);
  console.log('🚀 Aplicación corriendo en http://localhost:3001');
  console.log('📄 Vistas disponibles en http://localhost:3001/views');
  console.log('📋 API disponible en http://localhost:3001/api/v1');
  console.log('📚 Swagger UI en http://localhost:3001/api/docs');
  console.log('📄 Swagger JSON en http://localhost:3001/api/docs-json');
}
bootstrap();

🎨 Motores de vistas disponibles

El adaptador soporta los siguientes motores de plantillas:

import { ViewEngine } from 'nestjs-hapi-adapter';

// Handlebars
await app.getHttpAdapter().setViewEngine(ViewEngine.HBS);

// EJS
await app.getHttpAdapter().setViewEngine(ViewEngine.EJS);

// Pug
await app.getHttpAdapter().setViewEngine(ViewEngine.PUG);

📖 API del adaptador

Tipos exportados

import { 
  HapiAdapter,           // El adaptador principal
  NestHapiApplication,   // Tipo para la aplicación
  ViewEngine,            // Enum de motores de vistas
} from 'nestjs-hapi-adapter';

Métodos del adaptador

// Configurar vistas
app.getHttpAdapter().setBaseViewsDir(path: string)
app.getHttpAdapter().setViewEngine(engine: ViewEngine)

// Registrar Swagger (método custom del adaptador)
app.getHttpAdapter().registerSwagger(document: any, options?: SwaggerOptions)

Opciones de Swagger

interface SwaggerOptions {
  mountPath?: string;              // Ruta de la UI (default: '/api/docs')
  jsonPath?: string;               // Ruta del JSON (default: '/api/docs-json')
  allowedEnvironments?: string[];  // Ambientes permitidos (default: ['development'])
  basicAuth?: {                    // Autenticación básica opcional
    username: string;
    password: string;
  };
}

🔧 Configuración avanzada

Variables de entorno recomendadas

# CORS
CORS_ORIGIN=http://localhost:3000,http://localhost:4200

# Swagger
SWAGGER_USER=admin
SWAGGER_PASS=secret123
NODE_ENV=development

TypeScript

Asegúrate de tener estas opciones en tu tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}

🤝 Compatibilidad

  • NestJS: >= 10.0.0 (compatible con v10 y v11+)
  • Hapi.js: >= 21.0.0
  • Node.js: >= 16.0.0
  • TypeScript: >= 5.0.0

Versiones probadas

Esta librería se prueba automáticamente con:

  • ✅ NestJS: 10.0.0, 10.4.0, 11.0.0, 11.0.10, 11.1.0, 11.1.6
  • ✅ Node.js: 18.x, 20.x

🧪 Testing

Esta librería incluye tests completos de compatibilidad. Ver test/README.md para más información.

📝 Licencia

MIT

🐛 Reportar problemas

Si encuentras algún bug o tienes una sugerencia, por favor crea un issue en: https://github.com/your-username/nestjs-hapi-adapter/issues

🤝 Contribuir

Las contribuciones son bienvenidas! Por favor:

  1. Fork el proyecto
  2. Crea una rama para tu feature (git checkout -b feature/AmazingFeature)
  3. Commit tus cambios (git commit -m 'Add some AmazingFeature')
  4. Push a la rama (git push origin feature/AmazingFeature)
  5. Abre un Pull Request

⭐ ¿Te gusta el proyecto?

Si este proyecto te resulta útil, considera darle una estrella ⭐ en GitHub!


Hecho con ❤️ para la comunidad de NestJS