Skip to content

Commit

Permalink
add additions to schemas. Add new schemas to messages.js and also add…
Browse files Browse the repository at this point in the history
… response object there. Add response object parsing functionality to api.js
  • Loading branch information
NickOvt committed Oct 4, 2023
1 parent e7a3c72 commit f3fe6cb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
28 changes: 21 additions & 7 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ module.exports = done => {
},
required: true
};

for (const reqBodyKey in spec.requestBody) {
const reqBodyKeyData = spec.requestBody[reqBodyKey];

Expand All @@ -757,8 +758,6 @@ module.exports = done => {
obj.description = paramKeyData._flags.description || '';
obj.required = paramKeyData._flags.presence === 'required';
obj.schema = { type: paramKeyData.type };

// console.log(paramKeyData);
}

for (const paramKey in spec.queryParams) {
Expand All @@ -774,6 +773,14 @@ module.exports = done => {

// 7) add responses
methodObj.responses = {};

for (const resHttpCode in spec.response) {
const restBodyData = spec.response[resHttpCode];

parseJoiObject(resHttpCode, restBodyData, methodObj.responses);
}

console.log(methodObj.responses);
}
})
);
Expand All @@ -789,18 +796,25 @@ module.exports = done => {
binary: 'string'
};

/**
* Parse Joi Objects
*/
function parseJoiObject(path, joiObject, requestBodyProperties) {
// console.log(path, joiObject, requestBody);
if (joiObject.type === 'object') {
// recursion
const fieldsMap = joiObject._ids._byKey;

const data = {
type: joiObject.type,
descrption: joiObject._flags.description || '',
description: joiObject._flags.description || '',
properties: {},
required: []
};

if (joiObject._flags.objectName) {
data.objectName = joiObject._flags.objectName;
}

if (path) {
requestBodyProperties[path] = data;
} else {
Expand Down Expand Up @@ -863,9 +877,9 @@ module.exports = done => {
}
// TODO: if type before and after is string, add additional checks

if (openApiType === 'string') {
console.log(joiObject._rules);
}
// if (openApiType === 'string') {
// console.log(joiObject._rules);
// }

const data = { type: openApiType, description, required: isRequired };
if (format) {
Expand Down
21 changes: 16 additions & 5 deletions lib/api/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const { getMongoDBQuery /*, getElasticSearchQuery*/ } = require('../search-query
//const { getClient } = require('../elasticsearch');

const BimiHandler = require('../bimi-handler');
const { Address, AddressOptionalName, Header, Attachment, ReferenceWithAttachments, Bimi } = require('../schemas/request/messages-schemas');
const { Address, AddressOptionalNameArray, Header, Attachment, ReferenceWithAttachments, Bimi } = require('../schemas/request/messages-schemas');

module.exports = (db, server, messageHandler, userHandler, storageHandler, settingsHandler) => {
let maildrop = new Maildropper({
Expand Down Expand Up @@ -1551,11 +1551,11 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti

replyTo: Address,

to: AddressOptionalName,
to: AddressOptionalNameArray,

cc: AddressOptionalName,
cc: AddressOptionalNameArray,

bcc: AddressOptionalName,
bcc: AddressOptionalNameArray,

headers: Joi.array().items(Header),

Expand Down Expand Up @@ -1588,7 +1588,18 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
ip: sessIPSchema
},
queryParams: {},
tags: ['Messages']
tags: ['Messages'],
response: {
200: Joi.object({
success: Joi.boolean(),
message: Joi.object({
id: Joi.number(),
malbox: Joi.string(),
size: Joi.number()
}),
previousDeleted: Joi.boolean()
})
}
},
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');
Expand Down
24 changes: 12 additions & 12 deletions lib/schemas/request/messages-schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ const { booleanSchema } = require('../../schemas');
const Address = Joi.object({
name: Joi.string().empty('').max(255),
address: Joi.string().email({ tlds: false }).required()
});
}).$_setFlag('objectName', 'Address');

const AddressOptionalName = Joi.array().items(
Joi.object({
name: Joi.string().empty('').max(255),
address: Joi.string().email({ tlds: false }).required()
})
);
const AddressOptionalName = Joi.object({
name: Joi.string().empty('').max(255),
address: Joi.string().email({ tlds: false }).required()
}).$_setFlag('objectName', 'AddressOptionalName');

const AddressOptionalNameArray = Joi.array().items(AddressOptionalName);

const Header = Joi.object({
key: Joi.string().empty('').max(255),
value: Joi.string()
.empty('')
.max(100 * 1024)
});
}).$_setFlag('objectName', 'Header');

const Attachment = Joi.object({
filename: Joi.string().empty('').max(255),
Expand All @@ -29,7 +29,7 @@ const Attachment = Joi.object({
contentTransferEncoding: Joi.string().empty(''),
content: Joi.string().required(),
cid: Joi.string().empty('').max(255)
});
}).$_setFlag('objectName', 'Attachment');

const ReferenceWithAttachments = Joi.object({
mailbox: Joi.string().hex().lowercase().length(24).required(),
Expand All @@ -43,16 +43,16 @@ const ReferenceWithAttachments = Joi.object({
.uppercase()
)
)
});
}).$_setFlag('objectName', 'ReferenceWithAttachments');

const Bimi = Joi.object({
domain: Joi.string().domain().required(),
selector: Joi.string().empty('').max(255)
});
}).$_setFlag('objectName', 'Bimi');

module.exports = {
Address,
AddressOptionalName,
AddressOptionalNameArray,
Header,
Attachment,
ReferenceWithAttachments,
Expand Down

0 comments on commit f3fe6cb

Please sign in to comment.