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

@nestcfork/http

v0.7.5

Published

NestCloud is a Node.js micro-service solution, writing by Typescript language and Nest.js.

Downloads

1

Readme

NestCloud - Http

Description

This is a Nest module for writing nestjs http clients easier.

Installation

$ npm i --save @nestcfork/http

Quick Start

Import Module

import { Module } from '@nestjs/common';
import { HttpModule } from '@nestcfork/http';

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

Configurations

http:
  axios:
    timeout: 1000

Usage

import { Injectable } from "@nestjs/common";
import { Get, Query, Post, Body, Param, Put, Delete } from "@nestcfork/http";

@Injectable()
export class UserClient {
    @Get('http://test.com/users')
    getUsers(@Query('role') role: string) {
    }
    
    @Post('http://test.com/users')
    createUser(@Body('user') user: any) {
    }
    
    @Put('http://test.com/users/:userId')
    updateUser(@Param('userId') userId: string, @Body('user') user: any) {
    }
    
    @Delete('http://test.com/users/:userId')
    deleteUser(@Param('userId') userId: string) {
    }
}

Loadbalance

import { Injectable } from "@nestjs/common";
import { Loadbalanced, Get, Query } from "@nestcfork/http";

@Injectable()
// enable loadbalance supports, need import @nestcfork/loadbalance module at first.
@Loadbalanced('user-service')
export class UserClient {
    @Get('/users')
    getUsers(@Query('role') role: string) {
    }
    
    @Get('http://test.com/users')
    // disable loadbalance supports.
    @Loadbalanced(false)
    getRemoteUsers() {
    }
}

Interceptor

import { Injectable } from '@nestjs/common';
import { Interceptor } from "@nestcfork/http";
import { AxiosResponse, AxiosRequestConfig } from 'axios';

@Injectable()
export class AddHeaderInterceptor implements Interceptor {
    onRequest(request: AxiosRequestConfig): AxiosRequestConfig {
        request.headers['x-service'] = 'service-name';
        return request;
    }
    
    onResponse(response: AxiosResponse): AxiosResponse {
        return response;
    }
    
    onRequestError(error: any): any {
        return Promise.reject(error);
    }
    
    onResponseError(error: any): any {
        return Promise.reject(error);
    }
}
import { Injectable } from "@nestjs/common";
import { Get, UseInterceptors } from "@nestcfork/http";
import { AddHeaderInterceptor } from "./middlewares/AddHeaderInterceptor";

@Injectable()
@UseInterceptors(AddHeaderInterceptor)
export class ArticleClient {
    @Get('https://api.apiopen.top/recommendPoetry')
    getArticles() {
    }
}

examples:

@UseInterceptors(Interceptor1)
@UseInterceptors(Interceptor2)
export class Client {

    @UseInterceptors(Interceptor3)
    @UseInterceptors(Interceptor4)
    getArticles() {
    }
}

result:

interceptor1 request
interceptor2 request
interceptor3 request
interceptor4 request
interceptor4 response
interceptor3 response
interceptor2 response
interceptor1 response

Brakes

Import @nestcfork/brakes module at first.

import { Module } from '@nestjs/common';
import { BrakesModule } from '@nestcfork/brakes';

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

Write a fallback class.

import { Fallback } from '@nestcfork/brakes';
import { BadRequestException, Injectable } from '@nestjs/common';

@Injectable()
export class UserFallback implements Fallback {
  config() {
    return {};
  }

  fallback(...params) {
    throw new BadRequestException('fallback invoke');
  }

  healthCheck() {
  }
}

Use fallback.

import { Injectable } from '@nestjs/common';
import { Get } from '@nestcfork/http';
import { UseFallback } from '@nestcfork/brakes';
import { UserFallback } from './UserFallback';

@Injectable()
@UseFallback(UserFallback)
export class UserClient {
  @Get('/users')
  getUsers(): any {
  }
}

API

Get|Post|Put|Delete|Options|Head|Patch|Trace(uri: string, options?: AxiosRequestConfig): MethodDecorator

Route decorator.

| field | type | description | | :------ | :----- | :-------------------------------------------------------- | | uri | string | the url | | options | object | axios config,see axios |

Param|Body|Query|Header(field?: string): ParameterDecorator

Parameter decorator.

| field | type | description | | :---- | :----- | :------------- | | field | string | the field name |

SetHeader|SetQuery|SetParam|SetBody(field: string, value: any): MethodDecorator

constant parameter decorator

| field | type | description | | :---- | :----- | :-------------- | | field | string | the field name | | value | any | the field value |

Response(): MethodDecorator

If set this decorator, it will return full http response.

ResponseHeader(): MethodDecorator

If set this decorator, it will return response.headers.

ResponseBody(): MethodDecorator

It's a default decorator, it will return response.data.

ResponseType(type: string): MethodDecorator

set response data type, eg: 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream', default 'json'

ResponseEncode(type: string): MethodDecorator

Set response data encode, default 'utf8'

Loadbalanced(service: string | boolean): ClassDecorator | MethodDecorator

Open or close lb support.

UseInterceptors<T extends IInterceptor>(...interceptors: Function[])

Use interceptor, supports dynamic import and inject.

Stay in touch

License

NestCloud is MIT licensed.