Skip to content

hebertcisco/nestjs-techniques-undici

Repository files navigation

Open in Gitpod

Docker Image CI

Basic documentation

Usage example:

// app.module.ts
import { Module } from '@nestjs/common';
import { HttpModule } from 'nestjs-undici';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
    imports: [
        HttpModule.register({
            headers: {
                'my-header': `foo-bar`,
            },
        }),
    ],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule {}
// app.service.ts
import { lastValueFrom } from 'rxjs';

import { Injectable } from '@nestjs/common';
import { HttpService } from 'nestjs-undici';

@Injectable()
export class AppService {
    constructor(private httpService: HttpService) {}
    public fetchExternalInfo = async () => {
        const baseURL = 'https://api.github.com';
        try {
            const result = this.httpService.request(
                `${baseURL}/repos/hebertcisco/undici`,
            );

            const response = await lastValueFrom(result);

            return response.body.json();
        } catch (error) {
            throw error;
        }
    };
}

Runing the application with docker

Run as dev

docker-compose up dev

Run as prod

docker-compose up -d prod

Runing the application with npm scrips

npm install && npm run build
npm run prepare:enviroment

Run as dev

npm run dev

or

npm run dev:test

Run as prod

npm run start

or

npm run start:prod