UNPKG

nestwhats

Version:

A whatsapp-web.js wrapper for NestJS to create WhatsApp bots

120 lines (119 loc) 6.77 kB
"use strict"; 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var NestWhatsModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestWhatsModule = void 0; const common_1 = require("@nestjs/common"); const whatsapp_web_js_1 = require("whatsapp-web.js"); const clients_registry_service_1 = require("./clients-registry.service"); const commands_service_1 = require("./commands/commands.service"); const listener_registry_service_1 = require("./listeners/listener-registry.service"); const nestwhats_client_service_1 = require("./nestwhats-client.service"); const nestwhats_options_interface_1 = require("./nestwhats-options.interface"); const nestwhats_shared_module_1 = require("./nestwhats-shared.module"); const client_token_util_1 = require("./providers/client-token.util"); function toClientOptions(options) { const { name: clientName, prefix: _p, printQR: _q, ignoreSelf: _i } = options, rest = __rest(options, ["name", "prefix", "printQR", "ignoreSelf"]); if (rest.authStrategy instanceof whatsapp_web_js_1.LocalAuth && !rest.authStrategy.clientId) { rest.authStrategy.clientId = clientName; } return rest; } let NestWhatsModule = NestWhatsModule_1 = class NestWhatsModule { static forRoot(options) { const resolved = (0, nestwhats_options_interface_1.resolveClientOptions)(options); const clientToken = (0, client_token_util_1.getClientToken)(resolved.name); const lifecycleToken = Symbol(`NESTWHATS::LIFECYCLE_${resolved.name.toUpperCase()}`); const providers = [ { provide: clientToken, useValue: new whatsapp_web_js_1.Client(toClientOptions(resolved)), }, { provide: lifecycleToken, useFactory: (client, clientsRegistry, commandsService, listenerRegistry) => new nestwhats_client_service_1.NestWhatsClientService(client, resolved, clientsRegistry, commandsService, listenerRegistry), inject: [clientToken, clients_registry_service_1.ClientsRegistryService, commands_service_1.CommandsService, listener_registry_service_1.ListenerRegistryService], }, ]; const exports = [clientToken]; if (!options.name) { providers.push({ provide: whatsapp_web_js_1.Client, useExisting: clientToken }); exports.push(whatsapp_web_js_1.Client); } return { module: NestWhatsModule_1, global: true, imports: [nestwhats_shared_module_1.NestWhatsSharedModule], providers, exports, }; } static forRootAsync(options) { var _a, _b, _c; const displayName = (_a = options.name) !== null && _a !== void 0 ? _a : nestwhats_options_interface_1.NESTWHATS_DEFAULT_NAME; const clientToken = (0, client_token_util_1.getClientToken)(displayName); const optionsToken = Symbol(`NESTWHATS::OPTIONS_${displayName.toUpperCase()}`); const lifecycleToken = Symbol(`NESTWHATS::LIFECYCLE_${displayName.toUpperCase()}`); const providers = [ { provide: optionsToken, useFactory: (...args) => __awaiter(this, void 0, void 0, function* () { return (0, nestwhats_options_interface_1.resolveClientOptions)(Object.assign(Object.assign({}, (yield options.useFactory(...args))), { name: options.name })); }), inject: (_b = options.inject) !== null && _b !== void 0 ? _b : [], }, { provide: clientToken, useFactory: (clientOptions) => new whatsapp_web_js_1.Client(toClientOptions(clientOptions)), inject: [optionsToken], }, { provide: lifecycleToken, useFactory: (client, clientOptions, clientsRegistry, commandsService, listenerRegistry) => new nestwhats_client_service_1.NestWhatsClientService(client, clientOptions, clientsRegistry, commandsService, listenerRegistry), inject: [clientToken, optionsToken, clients_registry_service_1.ClientsRegistryService, commands_service_1.CommandsService, listener_registry_service_1.ListenerRegistryService], }, ]; const exports = [clientToken]; if (!options.name) { providers.push({ provide: whatsapp_web_js_1.Client, useExisting: clientToken }); exports.push(whatsapp_web_js_1.Client); } return { module: NestWhatsModule_1, global: true, imports: [nestwhats_shared_module_1.NestWhatsSharedModule, ...((_c = options.imports) !== null && _c !== void 0 ? _c : [])], providers, exports, }; } }; exports.NestWhatsModule = NestWhatsModule; exports.NestWhatsModule = NestWhatsModule = NestWhatsModule_1 = __decorate([ (0, common_1.Module)({}) // biome-ignore lint/complexity/noStaticOnlyClass: This class is designed to only have static methods for module registration. ], NestWhatsModule);