@byndyusoft/nest-http-client
    TypeScript icon, indicating that this package has built-in type declarations

    1.2.0 • Public • Published

    nest-http-client

    npm@latest test code style: prettier semantic-release

    axios for NestJS

    Comparison with @nestjs/axios

    • Promises instead of Observables
    • axios dependency is not pinning, you must provide it by yourself (e.g. get it from HttpService)
    • Allowing you use global axios with interceptors and different configs for various clients
    • endpoint requests like @octokit/endpoint

    Requirements

    • Node.js v14 LTS or later
    • Yarn

    Install

    yarn add @byndyusoft/nest-http-client @nestjs/common axios

    Usage

    1. Create module
    import { TRegisterAsyncOptions } from "@byndyusoft/nest-dynamic-module";
    import {
      HttpClientModule,
      IHttpClientOptions,
    } from "@byndyusoft/nest-http-client";
    import { DynamicModule, Global, Module } from "@nestjs/common";
    import urlJoin from "proper-url-join";
    import qs from "qs";
    
    import { UsersClient } from "./usersClient";
    
    @Global()
    @Module({
      providers: [UsersClient],
      exports: [UsersClient],
    })
    export class ClientModule {
      public static registerAsync(
        options?: TRegisterAsyncOptions<IHttpClientOptions>,
      ): DynamicModule {
        return HttpClientModule.registerClientModule(
          { module: ClientModule },
          options,
          (config) => ({
            ...config,
            baseURL: urlJoin(config?.baseURL as string, "/api/v1"),
            paramsSerializer: (params) =>
              qs.stringify(params, {
                skipNulls: true,
                arrayFormat: "repeat",
              }),
          }),
        );
      }
    }
    2. Create client (using new endpoint method)
    import { HttpClient } from "@byndyusoft/nest-http-client";
    import { Injectable } from "@nestjs/common";
    
    import {
      CreateUserDto,
      ListUsersQueryDto,
      ListUsersResponseDto,
      ParamsWithUserIdDto,
      QueryWithUserVersionDto,
      UpdateUserDto,
      UserDto,
    } from "ᐸDtosᐳ";
    
    @Injectable()
    export class UsersClient {
      public constructor(private readonly httpClient: HttpClient) {}
    
      public createUser(request: CreateUserDto): Promise<UserDto> {
        return this.httpClient.endpoint("POST /users", request);
      }
    
      public deleteUser(
        request: ParamsWithUserIdDto & QueryWithUserVersionDto,
      ): Promise<UserDto> {
        return this.httpClient.endpoint(
          "DELETE /users/{userId}{?userVersion}",
          request,
        );
      }
    
      public getUserById(request: ParamsWithUserIdDto): Promise<UserDto> {
        return this.httpClient.endpoint("GET /users/{userId}", request);
      }
    
      public listUsers(
        request?: Partial<ListUsersQueryDto>,
      ): Promise<ListUsersResponseDto> {
        return this.httpClient.endpoint("GET /users", request);
      }
    
      public updateUser(
        request: ParamsWithUserIdDto & QueryWithUserVersionDto & UpdateUserDto,
      ): Promise<UserDto> {
        return this.httpClient.endpoint(
          "PATCH /users/{userId}{?userVersion}",
          request,
        );
      }
    }
    2.1. Create client (using standard methods)
    import { HttpClient } from "@byndyusoft/nest-http-client";
    import { Injectable } from "@nestjs/common";
    import _ from "lodash";
    
    import {
      CreateUserDto,
      ListUsersQueryDto,
      ListUsersResponseDto,
      ParamsWithUserIdDto,
      QueryWithUserVersionDto,
      UpdateUserDto,
      UserDto,
    } from "ᐸDtosᐳ";
    
    @Injectable()
    export class UsersClient {
      public constructor(private readonly httpClient: HttpClient) {}
    
      public createUser(request: CreateUserDto): Promise<UserDto> {
        return this.httpClient.post("/users", request);
      }
    
      public deleteUser(
        request: ParamsWithUserIdDto & QueryWithUserVersionDto,
      ): Promise<UserDto> {
        return this.httpClient.delete(
          `/users/${encodeURIComponent(request.userId)}`,
          {
            params: _.omit(request, "userId"),
          },
        );
      }
    
      public getUserById(request: ParamsWithUserIdDto): Promise<UserDto> {
        return this.httpClient.get(`/users/${encodeURIComponent(request.userId)}`);
      }
    
      public listUsers(
        request?: Partial<ListUsersQueryDto>,
      ): Promise<ListUsersResponseDto> {
        return this.httpClient.get("/users", {
          params: request,
        });
      }
    
      public updateUser(
        request: ParamsWithUserIdDto & QueryWithUserVersionDto & UpdateUserDto,
      ): Promise<UserDto> {
        return this.httpClient.patch(
          `/users/${encodeURIComponent(request.userId)}`,
          _.omit(request, "userId", "userVersion"),
          {
            params: _.pick(request, "userVersion"),
          },
        );
      }
    }

    Maintainers

    License

    This repository is released under version 2.0 of the Apache License.

    Keywords

    none

    Install

    npm i @byndyusoft/nest-http-client

    DownloadsWeekly Downloads

    271

    Version

    1.2.0

    License

    Apache-2.0

    Unpacked Size

    62.2 kB

    Total Files

    43

    Last publish

    Collaborators

    • alexanderbyndyu
    • sadcitizen
    • dmitriy.litichevskiy
    • razonrus