Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
refactor: improving the mapFormData function
Browse files Browse the repository at this point in the history
  • Loading branch information
devNoiseConsulting committed Jul 25, 2021
1 parent 0a0f849 commit a85eef6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
36 changes: 15 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,28 @@ function parseBody (body = {}, method) {
return { requestBody: { content } }
}

/* Accumulator function for form data values */
function mapFormData () {
return (data, { key, value, description, type }) => {
data[key] = { type: inferType(value) }
if (type === 'file') {
data[key].type = 'string'
data[key].format = 'base64'
}
if (description && description !== '') {
data[key].description = description
}
if (value && value !== '') {
data[key].example = value
};
if (/\[required\]/gi.test(description)) {
data[key].require = true
return (obj, { key, type, description, value }) => {
const required = /\[required\]/gi.test(description)
obj[key] = {
type: inferType(value),
...(description ? { description: description.replace(/ ?\[required\] ?/gi, '') } : {}),
...(value ? { example: value } : {}),
...(type === 'file' ? { format: 'binary' } : {}),
...(required ? { required } : {})
}
return data
return obj
}
}

/* Accumulator function for form data value encodings */
function mapFormDataEencoding () {
return (data, { key, contentType }) => {
if (contentType) {
data[key] = { contentType: contentType }
} else {
data[key] = { contentType: 'text/plain' }
return (obj, { key, contentType }) => {
obj[key] = {
...(contentType ? { contentType: contentType } : { contentType: 'text/plain' })
}
return data
return obj
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/resources/output/FormData.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ paths:
properties:
name:
type: string
description: full name of the user (accepts spaces) [required]
description: full name of the user (accepts spaces)
example: New User
require: true
required: true
email:
type: string
description: email of the user (for notifications and login) [required]
description: email of the user (for notifications and login)
example: newuser@example.com
require: true
required: true
password:
type: string
example: pasword123
profileImage:
type: string
format: base64
description: User avatar
format: binary
encoding:
name:
contentType: text/plain
Expand Down
12 changes: 6 additions & 6 deletions test/resources/output/WwwFormUrlencoded.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ paths:
properties:
name:
type: string
description: full name of the user (accepts spaces) [required]
description: full name of the user (accepts spaces)
example: New User
require: true
required: true
email:
type: string
description: email of the user (for notifications and login) [required]
description: email of the user (for notifications and login)
example: newuser@example.com
require: true
required: true
password:
type: string
description: password (to be used for logging in) [required]
description: password (to be used for logging in)
example: pasword123
require: true
required: true
responses:
'200':
description: Successful response
Expand Down

0 comments on commit a85eef6

Please sign in to comment.