
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
nestjs-fetch-module
Advanced tools
A lightweight NestJS wrapper around the native fetch()
API.
The Fetch API is
awesome, but until recently we have needed a library to use it with Node. As of
Node 18, Fetch is available by default (based on the undici
library). This
library wraps a small API around native fetch()
so it can be used in NestJS
instead of @nestjs/axios
.
Note: This is not a drop-in replacement for @nestjs/axios
or the HttpModule
.
It has a completely different API.
npm install nestjs-fetch-module
Import the FetchModule
in your application module.
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { FetchModule } from 'nestjs-fetch-module';
@Module({
imports: [FetchModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
The FetchService
is now available as a provider in your application.
import { Injectable } from '@nestjs/common';
import { FetchService } from 'nestjs-fetch-module';
@Injectable()
export class AppService {
constructor(private readonly fetch: FetchService) {}
async getExample(): Promise<string> {
const response = await this.fetch.get('http://example.com/');
return response.text();
}
}
The public API presents six helper methods for making HTTP requests. The first
argument to each method is a URL (either a URL
object or a string); the second
argument is an optional list of configuration options.
Each of these helpers returns a Response
object.
This Response
object
is documented over on MDN. You will probably want to await response.json()
or
response.text()
to retrieve the body content.
interface FetchService {
get(url: URL | string, options?: FetchModuleOptions): Promise<Response>;
head(url: URL | string, options?: FetchModuleOptions): Promise<Response>;
post(url: URL | string, options?: FetchModuleOptions): Promise<Response>;
put(url: URL | string, options?: FetchModuleOptions): Promise<Response>;
patch(url: URL | string, options?: FetchModuleOptions): Promise<Response>;
delete(url: URL | string, options?: FetchModuleOptions): Promise<Response>;
}
FetchModuleOptions
includes all the
options you can send to a Request
object,
plus one additional option: baseUrl
. If you supply a relative URL as the
argument to the FetchService
then it will be resolved against the baseUrl
.
Set up as a provider in your app module:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { FetchModule } from 'nestjs-fetch-module';
@Module({
imports: [FetchModule.register({ baseUrl: 'http://example.com' })],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Async set up is also supported.
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { FetchModule } from 'nestjs-fetch-module';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
FetchModule.registerAsync({
inject: [ConfigService],
useFactory(config: ConfigService) {
return { keepalive: config.get('ENABLE_KEEPALIVE') };
},
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
fetch()
; >=17.5
with fetch()
flagged on,
or >=18.0
which has fetch()
enabled by default.This project is MIT licensed.
FAQs
Fetch API as a NestJS provider
The npm package nestjs-fetch-module receives a total of 0 weekly downloads. As such, nestjs-fetch-module popularity was classified as not popular.
We found that nestjs-fetch-module demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.