nestjs-deepl
v1.0.1
Published
DeepL module for Nest framework.
Readme
NestJS DeepL Module
A NestJS module that provides seamless integration with the DeepL translation API. This module wraps the official DeepL Node.js client and makes it easy to use translation services in your NestJS applications.
Features
- 🔧 Easy Integration: Simple module setup with NestJS dependency injection
- 🌍 Translation Support: Full access to DeepL's translation capabilities
- ⚙️ Configurable: Flexible configuration options for different environments
- 🔒 Type Safe: Built with TypeScript for better development experience
- 📦 Lightweight: Minimal overhead with clean architecture
Installation
npm install nestjs-deepl deepl-node
# or
yarn add nestjs-deepl deepl-node
# or
pnpm add nestjs-deepl deepl-nodeNote: This module requires deepl-node as a peer dependency. Make sure to install it alongside nestjs-deepl.
Prerequisites
- Node.js (v18 or higher)
- NestJS framework
- DeepL API key (Get one here)
Quick Start
1. Import the Module
import { Module } from '@nestjs/common';
import { DeepLModule } from 'nestjs-deepl';
@Module({
imports: [
DeepLModule.register({
authKey: 'your-deepl-api-key',
}),
],
})
export class AppModule {}2. Use the DeepL Client
import { Injectable } from '@nestjs/common';
import { DeepLClient } from 'deepl-node';
@Injectable()
export class TranslationService {
constructor(private readonly deepLClient: DeepLClient) {}
async translateText(text: string, targetLang: string) {
try {
const result = await this.deepLClient.translateText(
text,
null,
targetLang,
);
return result.text;
} catch (error) {
throw new Error(`Translation failed: ${error.message}`);
}
}
}Configuration Options
The module accepts all configuration options from the official DeepL Node.js client:
DeepLModule.register({
authKey: 'your-deepl-api-key',
serverUrl: 'https://api-free.deepl.com', // Optional: for DeepL Free API
// Other DeepL client options...
});Global Module
To make the DeepL client available globally across your application:
DeepLModule.register({
authKey: 'your-deepl-api-key',
isGlobal: true,
});Async Configuration
For dynamic configuration (e.g., from environment variables):
DeepLModule.registerAsync({
useFactory: (configService: ConfigService) => ({
authKey: configService.get('DEEPL_API_KEY'),
}),
inject: [ConfigService],
});API Reference
DeepLModule
The main module class that provides the DeepL client.
Static Methods
register(options: DeepLModuleOptions): Configure the module with static optionsregisterAsync(options: DeepLModuleAsyncOptions): Configure the module with async options
DeepLModuleOptions
Extends the official DeepL client options:
interface DeepLModuleOptions extends DeepLClientOptions {
authKey: string;
}Examples
Basic Translation
@Injectable()
export class TranslationService {
constructor(private readonly deepLClient: DeepLClient) {}
async translateToGerman(text: string) {
const result = await this.deepLClient.translateText(text, null, 'DE');
return result.text;
}
}Document Translation
@Injectable()
export class DocumentService {
constructor(private readonly deepLClient: DeepLClient) {}
async translateDocument(filePath: string, targetLang: string) {
const result = await this.deepLClient.translateDocument(
filePath,
targetLang,
);
return result;
}
}Usage Statistics
@Injectable()
export class UsageService {
constructor(private readonly deepLClient: DeepLClient) {}
async getUsage() {
const usage = await this.deepLClient.getUsage();
return usage;
}
}Environment Variables
It's recommended to use environment variables for your DeepL API key:
# .env
DEEPL_API_KEY=your-deepl-api-key// app.module.ts
DeepLModule.register({
authKey: process.env.DEEPL_API_KEY,
});Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
- Clone the repository
- Install dependencies:
pnpm install - Run tests:
pnpm test - Run linting:
pnpm lint - Build the project:
pnpm build
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Xudong Huang
- Email: [email protected]
- Website: https://www.huangxudong.com
- GitHub: @xudongcc
Related Links
Support
If you encounter any issues or have questions, please:
- Check the DeepL API documentation
- Search existing issues
- Create a new issue with detailed information
Made with ❤️ for the NestJS community
