system: Linux mars.sprixweb.com 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
const { globalApiKey, rateLimitMax, rateLimitWindowMs } = require('./config')
const { sendErrorResponse } = require('./utils')
const { validateSession } = require('./sessions')
const rateLimiting = require('express-rate-limit')
const apikey = async (req, res, next) => {
/*
#swagger.security = [{
"apiKeyAuth": []
}]
*/
/* #swagger.responses[403] = {
description: "Forbidden.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/ForbiddenResponse" }
}
}
}
*/
if (globalApiKey) {
const apiKey = req.headers['x-api-key']
if (!apiKey || apiKey !== globalApiKey) {
return sendErrorResponse(res, 403, 'Invalid API key')
}
}
next()
}
const sessionNameValidation = async (req, res, next) => {
/*
#swagger.parameters['sessionId'] = {
in: 'path',
description: 'Unique identifier for the session (alphanumeric and - allowed)',
required: true,
type: 'string',
example: 'f8377d8d-a589-4242-9ba6-9486a04ef80c'
}
*/
if ((!/^[\w-]+$/.test(req.params.sessionId))) {
/* #swagger.responses[422] = {
description: "Unprocessable Entity.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/ErrorResponse" }
}
}
}
*/
return sendErrorResponse(res, 422, 'Session should be alphanumerical or -')
}
next()
}
const sessionValidation = async (req, res, next) => {
const validation = await validateSession(req.params.sessionId)
if (validation.success !== true) {
/* #swagger.responses[404] = {
description: "Not Found.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/NotFoundResponse" }
}
}
}
*/
return sendErrorResponse(res, 404, validation.message)
}
next()
}
const rateLimiter = rateLimiting({
max: rateLimitMax,
windowMS: rateLimitWindowMs,
message: "You can't make any more requests at the moment. Try again later"
})
const sessionSwagger = async (req, res, next) => {
/*
#swagger.tags = ['Session']
*/
next()
}
const clientSwagger = async (req, res, next) => {
/*
#swagger.tags = ['Client']
*/
next()
}
const contactSwagger = async (req, res, next) => {
/*
#swagger.tags = ['Contact']
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
contactId: {
type: 'string',
description: 'Unique whatsApp identifier for the contact',
example: '6281288888888@c.us'
}
}
}
}
*/
next()
}
const messageSwagger = async (req, res, next) => {
/*
#swagger.tags = ['Message']
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'The Chat id which contains the message',
example: '6281288888888@c.us'
},
messageId: {
type: 'string',
description: 'Unique whatsApp identifier for the message',
example: 'ABCDEF999999999'
}
}
}
}
*/
next()
}
const chatSwagger = async (req, res, next) => {
/*
#swagger.tags = ['Chat']
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'Unique whatsApp identifier for the given Chat (either group or personnal)',
example: '6281288888888@c.us'
}
}
}
}
*/
next()
}
const groupChatSwagger = async (req, res, next) => {
/*
#swagger.tags = ['Group Chat']
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'Unique whatsApp identifier for the given Chat (either group or personnal)',
example: '6281288888888@c.us'
}
}
}
}
*/
next()
}
module.exports = {
sessionValidation,
apikey,
sessionNameValidation,
sessionSwagger,
clientSwagger,
contactSwagger,
messageSwagger,
chatSwagger,
groupChatSwagger,
rateLimiter
}