UNPKG

@nestlab/google-recaptcha

Version:
152 lines (151 loc) 6.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleRecaptchaModule = void 0; const common_1 = require("@nestjs/common"); const google_recaptcha_guard_1 = require("./guards/google-recaptcha.guard"); const google_recaptcha_validator_1 = require("./services/validators/google-recaptcha.validator"); const google_recaptcha_enterprise_validator_1 = require("./services/validators/google-recaptcha-enterprise.validator"); const provider_declarations_1 = require("./provider.declarations"); const recaptcha_request_resolver_1 = require("./services/recaptcha-request.resolver"); const core_1 = require("@nestjs/core"); const axios_1 = require("axios"); const https_1 = require("https"); const recaptcha_validator_resolver_1 = require("./services/recaptcha-validator.resolver"); const enterprise_reason_transformer_1 = require("./services/enterprise-reason.transformer"); const xor_1 = require("./helpers/xor"); const recaptcha_config_ref_1 = require("./models/recaptcha-config-ref"); class GoogleRecaptchaModule { static forRoot(options) { const providers = [ core_1.Reflector, google_recaptcha_guard_1.GoogleRecaptchaGuard, google_recaptcha_validator_1.GoogleRecaptchaValidator, google_recaptcha_enterprise_validator_1.GoogleRecaptchaEnterpriseValidator, recaptcha_request_resolver_1.RecaptchaRequestResolver, recaptcha_validator_resolver_1.RecaptchaValidatorResolver, enterprise_reason_transformer_1.EnterpriseReasonTransformer, { provide: provider_declarations_1.RECAPTCHA_OPTIONS, useValue: options, }, { provide: provider_declarations_1.RECAPTCHA_LOGGER, useFactory: () => options.logger || new common_1.Logger(), }, { provide: recaptcha_config_ref_1.RecaptchaConfigRef, useFactory: () => new recaptcha_config_ref_1.RecaptchaConfigRef(options), }, ]; this.validateOptions(options); const internalProviders = [ { provide: provider_declarations_1.RECAPTCHA_AXIOS_INSTANCE, useFactory: () => axios_1.default.create(this.transformAxiosConfig(Object.assign(Object.assign(Object.assign({}, this.axiosDefaultConfig), options.axiosConfig), { headers: null }))), }, ]; return { global: options.global != null ? options.global : true, module: GoogleRecaptchaModule, providers: providers.concat(internalProviders), exports: providers, }; } static forRootAsync(options) { const providers = [ core_1.Reflector, { provide: provider_declarations_1.RECAPTCHA_LOGGER, useFactory: (options) => options.logger || new common_1.Logger(), inject: [provider_declarations_1.RECAPTCHA_OPTIONS], }, { provide: recaptcha_config_ref_1.RecaptchaConfigRef, useFactory: (opts) => new recaptcha_config_ref_1.RecaptchaConfigRef(opts), inject: [provider_declarations_1.RECAPTCHA_OPTIONS], }, google_recaptcha_guard_1.GoogleRecaptchaGuard, google_recaptcha_validator_1.GoogleRecaptchaValidator, google_recaptcha_enterprise_validator_1.GoogleRecaptchaEnterpriseValidator, recaptcha_request_resolver_1.RecaptchaRequestResolver, recaptcha_validator_resolver_1.RecaptchaValidatorResolver, enterprise_reason_transformer_1.EnterpriseReasonTransformer, ...this.createAsyncProviders(options), ]; const internalProviders = [ { provide: provider_declarations_1.RECAPTCHA_AXIOS_INSTANCE, useFactory: (options) => { this.validateOptions(options); const transformedAxiosConfig = this.transformAxiosConfig(Object.assign(Object.assign(Object.assign({}, this.axiosDefaultConfig), options.axiosConfig), { headers: null })); return axios_1.default.create(transformedAxiosConfig); }, inject: [provider_declarations_1.RECAPTCHA_OPTIONS], }, ]; return { global: options.global != null ? options.global : true, module: GoogleRecaptchaModule, imports: options.imports, providers: providers.concat(internalProviders), exports: providers, }; } static transformAxiosConfig(axiosConfig) { const config = Object.assign({}, axiosConfig); delete config.baseURL; delete config.url; delete config.responseType; delete config.method; delete config.transformRequest; delete config.transformResponse; delete config.paramsSerializer; delete config.validateStatus; delete config.data; delete config.adapter; return config; } static createAsyncProviders(options) { const providers = [this.createAsyncOptionsProvider(options)]; if (options.useClass) { providers.push({ provide: options.useClass, useClass: options.useClass, }); } return providers; } static createAsyncOptionsProvider(options) { if (options.useFactory) { return { provide: provider_declarations_1.RECAPTCHA_OPTIONS, useFactory: options.useFactory, inject: options.inject, }; } return { provide: provider_declarations_1.RECAPTCHA_OPTIONS, useFactory: async (optionsFactory) => { if (!this.isGoogleRecaptchaFactory(optionsFactory)) { throw new Error("Factory must be implement 'GoogleRecaptchaOptionsFactory' interface."); } return optionsFactory.createGoogleRecaptchaOptions(); }, inject: [options.useExisting || options.useClass], }; } static validateOptions(options) { const hasEnterpriseOptions = !!Object.keys(options.enterprise || {}).length; if (!(0, xor_1.xor)(!!options.secretKey, hasEnterpriseOptions)) { throw new Error('Google recaptcha options must be contains "secretKey" xor "enterprise".'); } } static isGoogleRecaptchaFactory(object) { return !!object && typeof object.createGoogleRecaptchaOptions === 'function'; } } exports.GoogleRecaptchaModule = GoogleRecaptchaModule; GoogleRecaptchaModule.axiosDefaultConfig = { timeout: 60000, httpsAgent: new https_1.Agent({ keepAlive: true }), };