@dveloxsoft/dvs-client
v0.0.4
Published
Framework-agnostic Node.js HTTP client for DVS-Auth, DVS-User and DVS-Notification microservices
Maintainers
Readme
@dveloxsoft/dvs-client
Framework-agnostic Node.js HTTP client for the DVS-Auth, DVS-User and DVS-Notification microservices.
The package no longer depends on NestJS for its core API. NestJS support is available through an optional adapter.
Installation
npm install @dveloxsoft/dvs-clientOptional Axios adapter:
npm install axiosOptional NestJS adapter:
npm install @nestjs/common @nestjs/coreRuntime requirements
- Node.js
>=18.17 - Global
fetchis used by the default HTTP client - CJS and ESM entrypoints are both published
Environment variables
| Variable | Description | Required | Default |
| --- | --- | --- | --- |
| DVELOXSOFT_MS_AUTH_TOKEN | Gateway token used as x-ms-authorization-token | Yes, unless a custom httpClient is supplied | none |
| DVELOXSOFT_MS_BASE_URL | Gateway base URL | No | https://ms.dveloxsoft.com |
| DVELOXSOFT_MS_ORIGIN | Default Origin header | No | none |
| DVELOXSOFT_MS_REFERER | Default Referer header | No | none |
| HTTP_REQUEST_TIMEOUT | Default timeout in milliseconds | No | 30000 |
Plain Node.js usage
ESM
import { createDVSClient } from '@dveloxsoft/dvs-client';
const dvs = createDVSClient({
authToken: process.env.DVELOXSOFT_MS_AUTH_TOKEN,
baseUrl: process.env.DVELOXSOFT_MS_BASE_URL,
defaultOrigin: process.env.DVELOXSOFT_MS_ORIGIN,
defaultReferer: process.env.DVELOXSOFT_MS_REFERER,
});
const auth = await dvs.auth.login({ email, password });
const users = await dvs.user.findAll({ page: 1, limit: 20 }, auth.access_token);
const emailResult = await dvs.notification.sendEmail(emailDto, auth.access_token);CommonJS
const { createDVSClient } = require('@dveloxsoft/dvs-client');
const dvs = createDVSClient({
authToken: process.env.DVELOXSOFT_MS_AUTH_TOKEN,
baseUrl: process.env.DVELOXSOFT_MS_BASE_URL,
defaultOrigin: process.env.DVELOXSOFT_MS_ORIGIN,
defaultReferer: process.env.DVELOXSOFT_MS_REFERER,
});
const auth = await dvs.auth.login({ email, password });
const users = await dvs.user.findAll({ page: 1, limit: 20 }, auth.access_token);Optional access-token provider
Protected methods still accept an explicit accessToken parameter. For non-Nest apps that already have a request/session context, you can also provide an async token provider:
const dvs = createDVSClient({
authToken: process.env.DVELOXSOFT_MS_AUTH_TOKEN,
accessTokenProvider: () => getCurrentUserAccessToken(),
});
await dvs.user.findOne(userId);Explicit accessToken arguments take precedence over accessTokenProvider.
Custom HTTP client
The default client uses Node's built-in fetch. You can replace it with any implementation that satisfies HttpClient.
import { createDVSClient, type HttpClient, type HttpRequestConfig, type HttpResponse } from '@dveloxsoft/dvs-client';
class MyHttpClient implements HttpClient {
async get<T = any>(url: string, config?: HttpRequestConfig): Promise<HttpResponse<T>> {
return { data: {} as T, status: 200, statusText: 'OK', headers: {} };
}
async post<T = any>(url: string, data?: any, config?: HttpRequestConfig): Promise<HttpResponse<T>> {
return { data: {} as T, status: 200, statusText: 'OK', headers: {} };
}
async put<T = any>(url: string, data?: any, config?: HttpRequestConfig): Promise<HttpResponse<T>> {
return { data: {} as T, status: 200, statusText: 'OK', headers: {} };
}
async delete<T = any>(url: string, config?: HttpRequestConfig): Promise<HttpResponse<T>> {
return { data: {} as T, status: 200, statusText: 'OK', headers: {} };
}
async request<T = any>(config: HttpRequestConfig & { method: any; url: string }): Promise<HttpResponse<T>> {
return { data: {} as T, status: 200, statusText: 'OK', headers: {} };
}
}
const dvs = createDVSClient({
authToken: process.env.DVELOXSOFT_MS_AUTH_TOKEN,
httpClient: new MyHttpClient(),
});Optional Axios adapter
import { createDVSClient } from '@dveloxsoft/dvs-client';
import { createAxiosHttpClient } from '@dveloxsoft/dvs-client/axios';
const dvs = createDVSClient({
authToken: process.env.DVELOXSOFT_MS_AUTH_TOKEN,
httpClientFactory: createAxiosHttpClient,
});NestJS adapter
The core package has no NestJS dependency. NestJS apps can use the optional adapter:
import { Module } from '@nestjs/common';
import { DVSClientModule } from '@dveloxsoft/dvs-client/nestjs';
import { DVSAuthClient, DVSUserClient } from '@dveloxsoft/dvs-client';
@Module({
imports: [
DVSClientModule.forRoot({
authToken: process.env.DVELOXSOFT_MS_AUTH_TOKEN,
baseUrl: process.env.DVELOXSOFT_MS_BASE_URL,
defaultOrigin: process.env.DVELOXSOFT_MS_ORIGIN,
defaultReferer: process.env.DVELOXSOFT_MS_REFERER,
}),
],
})
export class AppModule {}Inject individual clients:
import { Injectable } from '@nestjs/common';
import { DVSAuthClient, DVSUserClient } from '@dveloxsoft/dvs-client';
@Injectable()
export class AuthService {
constructor(
private readonly authClient: DVSAuthClient,
private readonly userClient: DVSUserClient,
) {}
}Or inject the container:
import { Inject, Injectable } from '@nestjs/common';
import { DVS_CLIENT_CONTAINER, DVSClientContainer } from '@dveloxsoft/dvs-client/nestjs';
@Injectable()
export class MyService {
constructor(@Inject(DVS_CLIENT_CONTAINER) private readonly dvs: DVSClientContainer) {}
}Migration from the Nest-only package
Old:
import { DVSClientModule } from '@dveloxsoft/dvs-client-nestjs';
@Module({
imports: [DVSClientModule.forRoot(config)],
})
export class AppModule {}New:
import { DVSClientModule } from '@dveloxsoft/dvs-client/nestjs';
@Module({
imports: [DVSClientModule.forRoot(config)],
})
export class AppModule {}For non-Nest apps, use:
import { createDVSClient } from '@dveloxsoft/dvs-client';
const dvs = createDVSClient(config);Public API
DVSAuthClient
login(dto: LoginDto)register(dto: RegisterDto)refreshToken(dto: RefreshTokenDto, accessToken?: string)requestPasswordReset(dto: PasswordResetRequestDto, accessToken?: string)validateResetToken(token: string)resetPassword(dto: ResetPasswordDto, accessToken?: string)activateAccount(code: string, pageId: string, accessToken?: string)resendConfirmationToken(email: string, accessToken?: string)verifyTwoFactor(dto: TwoFactorDto, accessToken?: string)setupTwoFactor(userId: string, accessToken?: string)confirmTwoFactorSetup(dto: TwoFactorDto, accessToken?: string)disableTwoFactor(userId: string, accessToken?: string)logout(refreshToken?: string, accessToken?: string)
DVSUserClient
create(dto: CreateUserDto)search(email: string, companyId?: string, accessToken?: string)getFullDetail(email: string, pages: string[], accessToken?: string)findAll(options: PaginationOptions, accessToken: string)findOne(id: string | number, accessToken: string)findByEmail(email: string, accessToken: string, includePassword?: boolean)update(id: string | number, dto: UpdateUserDto, accessToken: string)delete(id: string | number, accessToken: string)deleteWithData(id: string | number, accessToken: string)findPartner(email: string, accessToken: string)updateLastLogin(userId: string | number, accessToken: string)resetPassword(email: string, accessToken: string, password: string, pageId?: string)verifyTwoFactor(userId: string, code: string, accessToken: string)setupTwoFactor(dto: SetupTwoFactorDto, accessToken: string)confirmTwoFactorSetup(userId: string, code: string, accessToken: string)enableTwoFactor(userId: string | number, accessToken: string)disableTwoFactor(userId: string | number, accessToken: string)
DVSNotificationClient
sendEmail(dto: SendEmailDto, accessToken: string)sendEmailByTrigger(dto: SendEmailTriggerDto, accessToken: string)createTemplate(dto: CreateEmailTemplateDto, accessToken?: string)getAllTemplates(options: PaginationOptions, accessToken?: string)getTemplatesByCompany(companyId: string, options: PaginationOptions, accessToken?: string)getTemplate(id: string, accessToken?: string)updateTemplate(id: string, dto: UpdateEmailTemplateDto, accessToken?: string)deleteTemplate(id: string, accessToken?: string)testTemplate(template: string, accessToken: string)testEmailConfig(config: TestEmailDto, accessToken: string)deleteAllTemplates(ids: string[], accessToken?: string)deleteTemplatesByCompany(companyId: string, accessToken?: string)
Build and test
npm run build
npm testThe test suite builds both CJS and ESM outputs, runs unit tests with Node's built-in test runner, and executes CJS/ESM smoke imports.
