@hanivanrizky/nestjs-html-parser
Version:
A powerful NestJS HTML parsing service with XPath and CSS selector support, proxy configuration, random user agents, and rich response metadata including headers and status codes
189 lines • 6.46 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var HtmlParserModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HtmlParserModule = void 0;
const common_1 = require("@nestjs/common");
const html_parser_config_1 = require("./html-parser.config");
const html_parser_service_1 = require("./html-parser.service");
/**
* HTML Parser Module with default configuration
*
* When imported directly, provides HtmlParserService with default settings:
* - loggerLevel: 'log'
*
* For custom configuration, use forRoot() or forRootAsync()
*/
let HtmlParserModule = HtmlParserModule_1 = class HtmlParserModule {
/**
* Configure the HTML Parser Module with custom options
*
* @param config - Configuration options for the HTML Parser
* @returns DynamicModule with configured providers
*
* @example
* Basic configuration
* ```typescript
* @Module({
* imports: [
* HtmlParserModule.forRoot({
* loggerLevel: 'debug'
* })
* ],
* })
* export class AppModule {}
* ```
*
* @example
* Using different logger levels
* ```typescript
* // For production - minimal logging
* HtmlParserModule.forRoot({ loggerLevel: 'error' })
*
* // For development - detailed logging
* HtmlParserModule.forRoot({ loggerLevel: 'debug' })
*
* // For testing - verbose logging
* HtmlParserModule.forRoot({ loggerLevel: 'verbose' })
* ```
*/
static forRoot(config = {}) {
const configProvider = {
provide: html_parser_config_1.HTML_PARSER_LOGGER_LEVEL,
useValue: config.loggerLevel || ['log', 'error', 'debug'],
};
return {
module: HtmlParserModule_1,
providers: [html_parser_service_1.HtmlParserService, configProvider],
exports: [html_parser_service_1.HtmlParserService],
};
}
/**
* Configure the HTML Parser Module asynchronously
*
* Useful when configuration depends on other modules or services that need to be initialized first.
*
* @param options - Async configuration options
* @returns DynamicModule with async configured providers
*
* @example
* Using with ConfigService
* ```typescript
* @Module({
* imports: [
* ConfigModule.forRoot(),
* HtmlParserModule.forRootAsync({
* imports: [ConfigModule],
* useFactory: (configService: ConfigService) => ({
* loggerLevel: configService.get('HTML_PARSER_LOGGER_LEVEL', 'warn')
* }),
* inject: [ConfigService],
* }),
* ],
* })
* export class AppModule {}
* ```
*
* @example
* Using with custom configuration factory
* ```typescript
* @Injectable()
* class HtmlParserConfigService implements HtmlParserConfigFactory {
* createHtmlParserConfig(): HtmlParserConfig {
* return {
* loggerLevel: process.env.NODE_ENV === 'production' ? 'error' : 'debug'
* };
* }
* }
*
* @Module({
* imports: [
* HtmlParserModule.forRootAsync({
* useClass: HtmlParserConfigService,
* }),
* ],
* })
* export class AppModule {}
* ```
*
* @example
* Using with existing provider
* ```typescript
* @Module({
* imports: [
* HtmlParserModule.forRootAsync({
* useExisting: MyExistingConfigService,
* }),
* ],
* })
* export class AppModule {}
* ```
*/
static forRootAsync(options) {
const asyncProviders = this.createAsyncProviders(options);
return {
module: HtmlParserModule_1,
imports: options.imports || [],
providers: [html_parser_service_1.HtmlParserService, ...asyncProviders],
exports: [html_parser_service_1.HtmlParserService],
};
}
/**
* Create async providers for the module configuration
*
* @param options - Async configuration options
* @returns Array of providers for async configuration
* @private
*/
static createAsyncProviders(options) {
if (options.useFactory) {
return [
{
provide: html_parser_config_1.HTML_PARSER_LOGGER_LEVEL,
useFactory: async (...args) => {
const config = await options.useFactory(...args);
return (config.loggerLevel || ['log', 'error', 'debug']);
},
inject: options.inject || [],
},
];
}
// useClass or useExisting
const inject = [
(options.useExisting ||
options.useClass),
];
return [
{
provide: html_parser_config_1.HTML_PARSER_LOGGER_LEVEL,
useFactory: async (factory) => {
const config = await factory.createHtmlParserConfig();
return (config.loggerLevel || ['log', 'error', 'debug']);
},
inject,
},
...(options.useClass
? [{ provide: options.useClass, useClass: options.useClass }]
: []),
];
}
};
exports.HtmlParserModule = HtmlParserModule;
exports.HtmlParserModule = HtmlParserModule = HtmlParserModule_1 = __decorate([
(0, common_1.Module)({
providers: [
html_parser_service_1.HtmlParserService,
{
provide: html_parser_config_1.HTML_PARSER_LOGGER_LEVEL,
useValue: ['log', 'error', 'debug'],
},
],
exports: [html_parser_service_1.HtmlParserService],
})
], HtmlParserModule);
//# sourceMappingURL=html-parser.module.js.map
;