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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ukitgroup/nestjs-http

v2.0.4

Published

Full featured Http adapter for nestjs based on GOT

Downloads

31

Readme

nestjs-http

Travis Coverage Status node npm

GitHub top language GitHub code size in bytes David David

license GitHub last commit semantic-release

Description

Rich featured HttpClient for nestjs applications

  • Got integration for nestjs;
  • Retries and all Got functions out of the box
  • Transparent Got usage (you will work with Got interface)
  • Accept external Tracing Service via DI for attaching specific http-headers across your microservice architecture;
  • Modularity - create instances for your modules with different configurations;
  • Default keep-alive http/https agent.

Requirements

  1. @nestjs/common ^9.1.4
  2. @nestjs/core ^9.1.4

Installation

npm install --save @ukitgroup/nestjs-http

or

yarn add @ukitgroup/nestjs-http

Short example

cat.service.ts

@Injectable()
export class CatService {
  constructor(@Inject(HttpClientService) private readonly httpClient: Got) {}

  meow(): string {
    // httpClient is an instance of Got
    // configuration from AppModule and CatModule
    // set built-in headers
    this.httpClient.post('/meow');
    return `meows..`;
  }
}

cat.module.ts

@Module({
  imports: [
    HttpClient.forInstance({
      imports: [YourConfigModule],
      providers: [
        {
          // override got configuration
          provide: HTTP_CLIENT_INSTANCE_GOT_OPTS,
          inject: [YourConfig],
          useFactory: config => {
            return {
              retry: config.retry,
            };
          },
        },
      ],
    }),
  ],
  providers: [CatService],
})
export class CatModule {}

app.module.ts

@Module({
  imports: [HttpClient.forRoot({}), CatModule],
})
export class AppModule {}

API

Define root configuration for Got in AppModule with:

HttpClient.forRoot(options: HttpClientForRootType)
HttpClientForRootType: {
  imports?: [],
  providers?: [],
}

Provide configuration with tokens:

  • HTTP_SERVICE_CONFIG - ServiceConfigType for HttpClientService
  • TRACE_DATA_SERVICE - should implements TraceDataServiceInterface
  • GOT_CONFIG - got configuration

Provided configuration will be merged: defaultConfig -> forRoot() -> forInstance() -> execution

default config for httpService:

const defaultConfig = {
  enableTraceService: false,
  headersMap: {
    traceId: 'x-trace-id',
    ip: 'x-real-ip',
    userAgent: 'user-agent',
    referrer: 'referrer',
  },
  excludeHeaders: [],
};

default config for got extends default config from got documentation

const defaultConfig = {
  agent: {
    http: new http.Agent({ keepAlive: true }),
    https: new https.Agent({ keepAlive: true }),
  },
};

Define instance configuration, trace service and http client service config in your module with:

HttpClient.forInstance(options: HttpClientForRootType)

Trace service injection

Usually you pass some headers across your microservices such as: trace-id, ip, user-agent etc. To make it convenient you can inject traceService via dependency injection and HTTPClient will pass this data with headers

You just have to implement TraceServiceInterface:

export interface TraceDataServiceInterface {
  getRequestData(): TraceDataType;
}

// and TraceDataType
export type TraceDataType = {
  [key: string]: string;
};

Headers mapping to values:

const headersMap = {
  traceId: 'x-trace-id',
  ip: 'x-real-ip',
  userAgent: 'user-agent',
  referrer: 'referrer',
};

Sou you can just define your service which should return these fields. For example, you can use cls-hooked for retrieving request data

export class TraceService implements TraceDataServiceInterface {
  getRequestData() {
    return {
      traceId: 'unique-id',
      ip: '127.0.0.1',
    };
  }
}

And then just define configuration:

@Module({
  imports: [
    HttpClient.forInstance({
      providers: [
        {
          provide: HTTP_SERVICE_CONFIG,
          useValue: {
            enableTraceService: true,
          },
        },
        { provide: TRACE_DATA_SERVICE, useClass: TraceService },
      ],
    }),
  ],
})
class AwesomeModule {}

TODO

  • Injection of MetricService for exports requests metrics such as: statuses, errors, retries, timing
  • Client balancing for servers with several ips or endpoints in different AZ which don't support server's LB.
  • ... Feature/Pull requests are welcome!😅

License

@ukitgroup/nestjs-http is MIT licensed.