Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Byndyusoft/nest-http-client
axios for NestJS
https://github.com/Byndyusoft/nest-http-client
axios http-client nestjs nodejs typescript
Last synced: about 2 months ago
JSON representation
axios for NestJS
- Host: GitHub
- URL: https://github.com/Byndyusoft/nest-http-client
- Owner: Byndyusoft
- License: apache-2.0
- Created: 2022-06-02T10:49:54.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T02:56:33.000Z (3 months ago)
- Last Synced: 2024-10-26T07:32:01.014Z (3 months ago)
- Topics: axios, http-client, nestjs, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 271 KB
- Stars: 3
- Watchers: 9
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nest-http-client
[![npm@latest](https://img.shields.io/npm/v/@byndyusoft/nest-http-client/latest.svg)](https://www.npmjs.com/package/@byndyusoft/nest-http-client)
[![test](https://github.com/Byndyusoft/nest-http-client/actions/workflows/test.yaml/badge.svg?branch=master)](https://github.com/Byndyusoft/nest-http-client/actions/workflows/test.yaml)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)axios for NestJS
## Comparison with `@nestjs/axios`
- [Promises instead of Observables](https://github.com/nestjs/axios/issues/271)
- 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
```bash
yarn add @byndyusoft/nest-http-client @nestjs/common axios
```## Usage
1. Create module
```typescript
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,
): 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. Module Initialization
```typescript
import { Module } from "@nestjs/common";
import axios from "axios";import { UsersClientModule } from "./clients/users";
import { ConfigModule } from "./configModule";
import { ConfigDto } from "./dtos";
import { SomeService } from "./some.service";
import { SomeController } from "./someController";const axiosInstance = axios.create();
// You can configure axios here, e.g. interceptors
@Module({
imports: [
ConfigModule.forRoot(),
UsersClientModule.registerAsync({
inject: [ConfigDto],
useFactory: async (config: ConfigDto) => ({
axios: axiosInstance,
config: config.usersApiClient,
}),
}),
],
controllers: [SomeController],
providers: [SomeService],
})
export class AppModule {}
```3. Create client (using new endpoint method)
```typescript
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 {
return this.httpClient.endpoint("POST /users", request);
}public deleteUser(
request: ParamsWithUserIdDto & QueryWithUserVersionDto,
): Promise {
return this.httpClient.endpoint(
"DELETE /users/{userId}{?userVersion}",
request,
);
}public getUserById(request: ParamsWithUserIdDto): Promise {
return this.httpClient.endpoint("GET /users/{userId}", request);
}public listUsers(
request?: Partial,
): Promise {
return this.httpClient.endpoint("GET /users", request);
}public updateUser(
request: ParamsWithUserIdDto & QueryWithUserVersionDto & UpdateUserDto,
): Promise {
return this.httpClient.endpoint(
"PATCH /users/{userId}{?userVersion}",
request,
);
}
}
```3.1. Create client (using standard methods)
```typescript
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 {
return this.httpClient.post("/users", request);
}public deleteUser(
request: ParamsWithUserIdDto & QueryWithUserVersionDto,
): Promise {
return this.httpClient.delete(
`/users/${encodeURIComponent(request.userId)}`,
{
params: _.omit(request, "userId"),
},
);
}public getUserById(request: ParamsWithUserIdDto): Promise {
return this.httpClient.get(`/users/${encodeURIComponent(request.userId)}`);
}public listUsers(
request?: Partial,
): Promise {
return this.httpClient.get("/users", {
params: request,
});
}public updateUser(
request: ParamsWithUserIdDto & QueryWithUserVersionDto & UpdateUserDto,
): Promise {
return this.httpClient.patch(
`/users/${encodeURIComponent(request.userId)}`,
_.omit(request, "userId", "userVersion"),
{
params: _.pick(request, "userVersion"),
},
);
}
}
```## Maintainers
- [@Byndyusoft/owners](https://github.com/orgs/Byndyusoft/teams/owners) <>
- [@Byndyusoft/team](https://github.com/orgs/Byndyusoft/teams/team)
- [@KillWolfVlad](https://github.com/KillWolfVlad)## License
This repository is released under version 2.0 of the
[Apache License](https://www.apache.org/licenses/LICENSE-2.0).