Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@catalist-nestjs/fetch

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@catalist-nestjs/fetch

NestJS Fetch for Catalist Finance projects. Part of [Catalist NestJS Modules](https://github.com/blockarchivelabs/catalist-nestjs-modules/#readme)

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Fetch

NestJS Fetch for Catalist Finance projects. Part of Catalist NestJS Modules

The module is based on the node-fetch package.

Install

yarn add @catalist-nestjs/fetch

Usage

Basic usage

// Import
import { Module } from '@nestjs/common';
import { FetchModule } from '@catalist-nestjs/fetch';
import { MyService } from './my.service';

@Module({
  imports: [FetchModule.forFeature()],
  providers: [MyService],
  exports: [MyService],
})
export class MyModule {}

// Usage
import { FetchService } from '@catalist-nestjs/fetch';

export class MyService {
  constructor(private fetchService: FetchService) {}

  async myFetch() {
    return await this.fetchService.fetchJson('/url');
  }
}

The fetchService provides 2 methods: fetchJson and fetchText, which are based on a call to the fetch function followed by a call to .json() or .text(). Method arguments are compatible with the fetch.

Global usage

import { Module } from '@nestjs/common';
import { FetchModule } from '@catalist-nestjs/fetch';

@Module({
  imports: [FetchModule.forRoot()],
})
export class MyModule {}

Async usage

import { Module } from '@nestjs/common';
import { FetchModule } from '@catalist-nestjs/fetch';
import { ConfigModule, ConfigService } from './my.service';

@Module({
  imports: [
    ConfigModule,
    FetchModule.forRootAsync({
      async useFactory(configService: ConfigService) {
        return { baseUrls: configService.baseUrls };
      },
      inject: [ConfigService],
    }),
  ],
})
export class MyModule {}

Module options

The forRoot and forFeature methods have the same options:

export interface FetchModuleOptions {
  baseUrls?: string[];
  retryPolicy?: RequestRetryPolicy;
}

export interface RequestRetryPolicy {
  delay?: number;
  attempts?: number;
}
OptionDefaultDesc
baseUrls[]Array of base API URLs
delay1000Number of milliseconds between attempts
attempts0Number of times the query is retried
Example
// Import
import { Module } from '@nestjs/common';
import { FetchModule } from '@catalist-nestjs/fetch';
import { MyService } from './my.service';

@Module({
  imports: [
    FetchModule.forFeature({
      baseUrls: ['https://my-api.com', 'https://my-fallback-api.com'],
      retryPolicy: {
        delay: 2000,
        attempts: 3,
      },
    }),
  ],
  providers: [MyService],
  exports: [MyService],
})
export class MyModule {}

// Usage
import { FetchService } from '@catalist-nestjs/fetch';

export class MyService {
  constructor(private fetchService: FetchService) {}

  async myFetch() {
    return await this.fetchService.fetchJson('/foo');
  }
}

If the provided API services are unavailable, the following happens:

Local options

The retryPolicy for each query can be rewritten:

import { FetchService } from '@catalist-nestjs/fetch';

export class MyService {
  constructor(private fetchService: FetchService) {}

  async myFetch() {
    return await this.fetchService.fetchJson('/foo', {
      retryPolicy: {
        delay: 2000,
        attempts: 3,
      },
    });
  }
}

Keywords

FAQs

Package last updated on 30 Apr 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc