UNPKG

lesgo

Version:

Core framework for lesgo node.js serverless framework.

29 lines (28 loc) 959 B
import { SQSClient } from '@aws-sdk/client-sqs'; import { logger, isEmpty, validateFields } from '../../utils'; import { sqs as sqsConfig } from '../../config'; const FILE = 'lesgo.services.SQSService.getClient'; const singleton = {}; const getClient = (clientOpts = {}) => { const options = validateFields(clientOpts, [ { key: 'region', type: 'string', required: false }, { key: 'singletonConn', type: 'string', required: false }, ]); const region = options.region || sqsConfig.region; const singletonConn = options.singletonConn || 'default'; if (!isEmpty(singleton[singletonConn])) { logger.debug(`${FILE}::REUSE_CLIENT_SINGLETON`, { client: singleton[singletonConn], region, }); return singleton[singletonConn]; } const client = new SQSClient({ region }); logger.debug(`${FILE}::NEW_CLIENT`, { client, region, }); singleton[singletonConn] = client; return client; }; export default getClient;