Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
nestjs-fetch
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). Hurrah!
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
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({
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';
@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({
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';
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 receives a total of 61 weekly downloads. As such, nestjs-fetch popularity was classified as not popular.
We found that nestjs-fetch demonstrated a healthy version release cadence and project activity because the last version was released less than 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.