Skip to content

Commit

Permalink
eslint, prettier, husky added with test cases issues resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmuhtasimfuadfahim committed Jun 1, 2023
1 parent a5d79d0 commit 7d5f1ae
Show file tree
Hide file tree
Showing 26 changed files with 474 additions and 377 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
KAFKA_CLIENT_ID=test-client
KAFKA_BROKER_URL=localhost:9092
KAFKA_GROUP_ID=test-group
KAFKA_GROUP_ID=test-group
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bin
19 changes: 19 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"env": {
"node": true,
"jest": true,
"browser": true,
"es2021": true
},
"extends": ["airbnb-base", "plugin:jest/recommended", "plugin:security/recommended", "plugin:prettier/recommended"],
"plugins": ["jest", "security", "prettier"],
"rules": {
"no-console": "error",
"func-names": "off",
"no-underscore-dangle": "off",
"consistent-return": "off",
"jest/expect-expect": "off",
"semi": ["error", "always"],
"security/detect-object-injection": "off"
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ package-lock.json
.DS_Store

# env file
.env
.env
4 changes: 4 additions & 0 deletions .husky/post-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm install
4 changes: 4 additions & 0 deletions .husky/post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

git status
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm lint-staged
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
4 changes: 4 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"printWidth": 125
}
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
### [1.1.4] - 2023-01-06

#### Added

- Eslint, Prettier code formatter and linter support
- Husky pre-commit, post-commit and post-checkout rules added to commit

#### Fixed

- test validations fixed from previoous release (v1.1.3)

#### Contributors to this release

- <img src="https://avatars.githubusercontent.com/u/69357704?v=4/u/12586868?v&#x3D;4&amp;s&#x3D;18" alt="avatar" width="18"/> [Md. Muhtasim Fuad Fahim](https://github.com/mdmuhtasimfuadfahim)

### [1.1.3] - 2023-29-05

#### Added
Expand Down
80 changes: 41 additions & 39 deletions ConsumeEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,55 @@ const validateHeaders = require('./validation/validateHeaders');
const consumer = kafka.consumer({ groupId: config.kafka_group_id });

/**
* An asynchronous function that consumes events from a specified topic.
* An asynchronous function that consumes events from a specified topic.
* It validates the topic, each message's event and data before returning the message data object.
*
* @param {string} topic - The topic to consume events from.
* @return {Promise<object>} The message data object containing topic, partition, offset, timestamp, key, value, and headers.
*/
const ConsumeEvent = async (topic) => {
await consumer.connect();
await consumer.subscribe({ topics: [topic], fromBeginning: true });
await consumer.connect();
await consumer.subscribe({ topics: [topic], fromBeginning: true });

validateTopic(topic);
validateTopic(topic);

return new Promise(async (resolve, reject) => {
try {
await consumer.run({
eachMessage: async ({ topic, partition, message, heartbeat }) => {
let key = message.key.toString();
validateEvent(message.key.toString());
return new Promise((resolve, reject) => {
try {
consumer.run({
// eslint-disable-next-line no-shadow
eachMessage: async ({ topic, partition, message, heartbeat }) => {
const key = message.key.toString();
validateEvent(message.key.toString());

let value = JSON.parse(message.value);
validateData(value);
const value = JSON.parse(message.value);
validateData(value);

let headers = Object.keys(message.headers).reduce(
(headers, key) => ({
...headers,
[key]: message.headers[key].toString(),
}),
{}
);
validateHeaders(headers);
const data = {
topic,
partition,
offset: message.offset,
timestamp: message.timestamp,
key,
value,
headers,
};
await heartbeat();
resolve(data);
},
})
} catch (error) {
reject(error.message);
}
});
}
const headers = Object.keys(message.headers).reduce(
// eslint-disable-next-line no-shadow
(headers, key) => ({
...headers,
[key]: message.headers[key].toString(),
}),
{}
);
validateHeaders(headers);
const data = {
topic,
partition,
offset: message.offset,
timestamp: message.timestamp,
key,
value,
headers,
};
await heartbeat();
resolve(data);
},
});
} catch (error) {
reject(error.message);
}
});
};

module.exports = ConsumeEvent;
module.exports = ConsumeEvent;
54 changes: 28 additions & 26 deletions ProduceEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const validateData = require('./validation/validateData');
const validateHeaders = require('./validation/validateHeaders');

const producer = kafka.producer({
createPartitioner: Partitioners.DefaultPartitioner
createPartitioner: Partitioners.DefaultPartitioner,
});

/**
Expand All @@ -20,34 +20,36 @@ const producer = kafka.producer({
* @throws {string} an error message if message sending fails.
*/
const ProduceEvent = async (topic, event, data = {}, headers = {}) => {
// basic validations
validateTopic(topic);
validateEvent(event);
validateData(data);
validateHeaders(headers);
// basic validations
validateTopic(topic);
validateEvent(event);
validateData(data);
validateHeaders(headers);

await producer.connect();
await producer.connect();

return new Promise(async (resolve, reject) => {
try {
const message = [{
key: `key-${event}`,
value: JSON.stringify({ data }),
headers,
}];
return new Promise((resolve, reject) => {
try {
const message = [
{
key: `key-${event}`,
value: JSON.stringify({ data }),
headers,
},
];

const response = await producer.send({
topic,
messages: message,
});
const response = producer.send({
topic,
messages: message,
});

await producer.disconnect();
resolve(response);
} catch (error) {
await producer.disconnect();
reject(error.message);
}
});
producer.disconnect();
resolve(response);
} catch (error) {
producer.disconnect();
reject(error.message);
}
});
};

module.exports = ProduceEvent;
module.exports = ProduceEvent;
10 changes: 5 additions & 5 deletions config/kafka.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const config = require('./config');
const WinstonLogCreator = require('./winstonKafkaLogger');

const kafka = new Kafka({
clientId: config.kafka_client_id,
brokers: [config.kafka_broker_url],
logLevel: logLevel.ERROR,
logCreator: WinstonLogCreator,
clientId: config.kafka_client_id,
brokers: [config.kafka_broker_url],
logLevel: logLevel.ERROR,
logCreator: WinstonLogCreator,
});

module.exports = kafka;
module.exports = kafka;
54 changes: 28 additions & 26 deletions config/winstonKafkaLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ const winston = require('winston');
* @return {string} The corresponding Winston log level.
*/
const toWinstonLogLevel = (level) => {
switch (level) {
case logLevel.ERROR:
case logLevel.NOTHING:
return 'error';
case logLevel.WARN:
return 'warn';
case logLevel.INFO:
return 'info';
case logLevel.DEBUG:
return 'debug';
default:
return 'all';
}
switch (level) {
case logLevel.ERROR:
case logLevel.NOTHING:
return 'error';
case logLevel.WARN:
return 'warn';
case logLevel.INFO:
return 'info';
case logLevel.DEBUG:
return 'debug';
default:
return 'all';
}
};

/**
Expand All @@ -34,21 +34,23 @@ const toWinstonLogLevel = (level) => {
* - label: A label for the log.
* - log: An object containing the log data.
*/
// eslint-disable-next-line no-shadow
const WinstonLogCreator = (logLevel) => {
const logger = winston.createLogger({
level: toWinstonLogLevel(logLevel),
transports: [new winston.transports.Console(), new winston.transports.File({ filename: 'kafka.log' })],
});
const logger = winston.createLogger({
level: toWinstonLogLevel(logLevel),
transports: [new winston.transports.Console(), new winston.transports.File({ filename: 'kafka.log' })],
});

return ({ namespace, level, label, log }) => {
const { message, ...extra } = log;
logger.log({
namespace, label,
level: toWinstonLogLevel(level),
message,
extra,
});
};
return ({ namespace, level, label, log }) => {
const { message, ...extra } = log;
logger.log({
namespace,
label,
level: toWinstonLogLevel(level),
message,
extra,
});
};
};

module.exports = WinstonLogCreator;
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js']
}
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
jest.setTimeout(30000)
jest.setTimeout(30000);
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
"winston": "^3.8.2"
},
"devDependencies": {
"eslint": "^8.41.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-security": "^1.7.1",
"husky": "^8.0.3",
"jest": "^29.5.0"
}
}
Loading

0 comments on commit 7d5f1ae

Please sign in to comment.